3

Is there any way that I can, over a period of say a week, see how much CPU each app uses? The only way that I can think of is to repeatedly parse top output but that doesn't seem like the most efficient way to do it. Is there a better way?

To add some context, basically the traceview functionality of the SDK on the phone itself: http://developer.android.com/guide/developing/debugging/debugging-tracing.html

Gaurav
  • 1,466
  • 3
  • 17
  • 24

3 Answers3

0

We did it parsing top output, couldn't think of a better way. We needed to monitor all applications, and trace is only good if you need information about your own as you have to add trace code to your application in order to use it.

The app consumes 5% of CPU when running once a sec, aggregating values, writing values to log file, and displaying results on screen.

Unfortunately code is not opened (yet).

Andras Balázs Lajtha
  • 2,576
  • 25
  • 32
0

One of the problems you'd face monitoring processes over that period of time is that they'll almost certainly be destroyed and created MANY times in that period - this makes it hard to gain useful figures.

If you're not a "reinventing the wheel" type - the free version of the popular SystemPanel App shows how much CPU time each process has used (and over what period) - the full version apparently offers a detailed history of both CPU and memory usage.

  • I tried this app but it doesn't show CPU history in the free version. I'm curious to learn how they do the detailed history in the paid version. Any ideas? – Gaurav Oct 24 '11 at 10:02
-1

You can use the Debug class to monitor the processes


or use this function to calculate CPU usage

Use /proc/stat/ for the total CPU usage (for all CPU's) . For per process usage

private float readCpuUsage(int pid) {
    try {
        RandomAccessFile reader = new RandomAccessFile("/proc/" + pid + "/stat", "r");
        String load = reader.readLine();

        String[] toks = load.split(" ");

        long idle1 = Long.parseLong(toks[5]);
        long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
              + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        try {
            Thread.sleep(360);
        } catch (Exception e) {}

        reader.seek(0);
        load = reader.readLine();
        reader.close();

        toks = load.split(" ");

        long idle2 = Long.parseLong(toks[5]);
        long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
            + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return 0;
} 

source


To get the list of running processes

ActivityManager mgr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> allTasks = mgr.getRunningTasks(showLimit);
/* Loop through all tasks returned. */
for (ActivityManager.RunningTaskInfo aTask : allTasks) 
{                  
    Log.i("MyApp", "Task: " + aTask.baseActivity.getClassName()); 
}
Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
  • (2486 (ndroid.settings) S 1375 1375 0 0 -1 4194624 24726 0 359 0 1225 112 0 0 20 0 9 0 237985 115519488 4242 4294967295...) toks[2] is always "S"? – thecr0w Jun 20 '12 at 14:28
  • -1. Your indices into the `toks` array are incorrect, those indices are valid for the `/proc/stat` file, not for the individual process `stat` files. – Artyom Jul 06 '12 at 09:53