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