1

I am trying to write a small code that would dump out the CPU utilization (% of cpu used in last 10 secs and dump that every 10 seconds) of my android app.

Now assuming there are 2 or 4 cores in the phone. How to dump effectively the % cpu utilized by the app. I have googled some samples of top command but I am not sure how to use the figures of top command to display cpu utilization across multicore and in last 10 secs.

I have added the following code: but I am always getting null in the buf. exec returns valid result with ls command but not with top. Do i need to get some permission in mainfest to execute top command...

    String str = new String();
    str = "top -l1";
    Process p;
    p = Runtime.getRuntime().exec(str);
    BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    /* Skip 6 lines */
    int count = 0;
    char[] buf= new char[10240];
     bri.read(buf);
     Log.e("TEST", new String(buf));

2 Answers2

0

I'm not very familiar with top, but this question seems to suggest that there is a way to collect data from individual cores.

In any case, I would suggest you use the Dumpsys tool instead, which is much more interesting, since it can provide you with CPU utilization regardless of the number of cores. For example, the command:

adb shell dumpsys cpuinfo

will give output of the form:

Load: 0.08 / 0.4 / 0.64
CPU usage from 42816ms to 34683ms ago:
system_server: 1% = 1% user + 0% kernel / faults: 16 minor
kdebuglog.sh: 0% = 0% user + 0% kernel / faults: 160 minor
tiwlan_wq: 0% = 0% user + 0% kernel
usb_mass_storag: 0% = 0% user + 0% kernel
pvr_workqueue: 0% = 0% user + 0% kernel
+sleep: 0% = 0% user + 0% kernel
+sleep: 0% = 0% user + 0% kernel
TOTAL: 6% = 1% user + 3% kernel + 0% irq

Check out this SO answer for more details on how to use Dumpsys: What's the Android ADB shell "dumpsys" tool and what are its benefits?

Community
  • 1
  • 1
verybadalloc
  • 5,768
  • 2
  • 33
  • 49
  • I need per process basis and all at one time. I am writing this diagnostic tool to dump out how my process behaves over time. –  Jun 09 '13 at 11:46
  • apparantly the permission android.permission.DUMP is required which is not allowed as per the project requirement I am working with –  Jun 11 '13 at 05:26
0

For anyone who reached this question to solve this problem, I am posting my solution on how to get the value of cpu usage from "top" command

private String getCpuUsage()
    {
        ArrayList<String> list = new ArrayList<String>();
        String cpuUsageValue = "";

        Process p = null;
        try {

            p = Runtime.getRuntime().exec(new String[] { "sh", "-c", "top -n 1 | grep " + appProcessName }); //appProcessName is com.example.app

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        int i =0;
        String line = "";
        try {

            line = reader.readLine();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //while(line != null)
        if(line != null)
        {

            //11203  0   0% S    13 901560K  43528K  fg u0_a141  com.example.app

            String lineOuput[] = line.split(" ");
            cpuUsageValue = lineOuput[5].trim();
            //[19472, , 0, , , 0%, S, , , , 15, 904664K, , 44148K, , fg, u0_a141, , com.example.app]

            Log.i(TAG, "cpu usage value : " + cpuUsageValue);

            list.add(line);
            try {
                line = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            i++;
        }
        else
        {
            Log.i(TAG, "command line is null");

        }

        return cpuUsageValue;


    }
Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41