3

I'hv been struggling to get the accurate CPU Usage per process and process Idle CPU Time.. I tried " top " command proc/[pid]/stat folder but still trying my luck..

TOP command gives CPU usage for all the processes running in fg(foreground) and bg(background) but it is not as accurate i feel.. because it shows %CPU 0, even if the process is running in the background.. Any other way? please help

mn0102
  • 839
  • 1
  • 12
  • 25
  • Dumpsys cpuinfo works absolutely fine on 2.2 and 2.3.6 but when i tried on 4.0, it gives me an error message for output like "Permission Denial: can't dump Battery service from pid..." Also, when the application is launched there is an item in the log tagged with "PackageManager" statingNot granting permission android.permissionDUMP to package.... (protectionLevel = 3 ...)" – mn0102 Apr 24 '13 at 12:16
  • DUMP permission will not work after Android 4.0 or 4.1, you have to use your app as a root user or put it into /system/app/yourapp.apk. – VISHAL VIRADIA Apr 30 '13 at 12:16
  • @VISHAL /system/app/yourapp.apk. requires either a rooted ROM or a rooted device which is not what i need. – mn0102 May 05 '13 at 18:03

2 Answers2

2

I have found a way to do it myself.. I am using

  1. proc/[pid]/stat -> for per process CPU Usage 14th and 15th parameter
  2. proc/stat -> for All processes CPU consumption at the same time
mn0102
  • 839
  • 1
  • 12
  • 25
1

You can get CPU usage with:

ArrayList<String> list = new ArrayList<String>();
    try {
        // -m 10, how many entries you want, -d 1, delay by how much, -n 1,
        // number of iterations
        Process p = Runtime.getRuntime().exec("top -m 15 -d 1 -n 1");

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        int i = 0;
        String line = reader.readLine();
        while (line != null) {
            Log.e("Output "   i, line);
            list.add(line);
            line = reader.readLine();
            i  ;
        }

        p.waitFor();

        Toast.makeText(getBaseContext(), "Got update",Toast.LENGTH_SHORT)
                .show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Caught", Toast.LENGTH_SHORT)
                .show();
    }
Huỳnh Ngọc Bang
  • 1,572
  • 1
  • 19
  • 22