mn0102
mn0102

Reputation: 849

How to get the accurate CPU Usage per process and process Idle CPU Time

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

Upvotes: 3

Views: 7867

Answers (2)

Huỳnh Ngọc Bang
Huỳnh Ngọc Bang

Reputation: 1722

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();
    }

Upvotes: 1

mn0102
mn0102

Reputation: 849

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

Upvotes: 2

Related Questions