How to Get Higher Precision of "Cpu%" Than That from Top Command

How to get higher precision of CPU% than that from TOP command?

You didn't mention it in your post, but in the comment you said that you really need CPU utilization per thread, not per process.

If you can't find a tool that's accurate enough, you can look directly in /proc/[pid]/task/[ThreadName] as described in the man page for /proc. This gives total CPU time consumed in "clock ticks" since execution began. Getting better resolution than this is probably difficult or impossible.

Edit

From the OP's comment, a command that lists the relevant information is:

adb shell cat /proc/${pid}/task/*/stat | awk -F\ '{print $1, $14}' 

This just cats the correct /proc files to the debugging host, which runs a tiny awk program to print the columns for pid and user time. You could also easily use cut -d " " -f1,14 or something similar in perl to get the columns if awk isn't available.

TOP command vs dumpsys cpuinfo: Which one is more accurate?

I suggest going with dumpsys. In my experience, TOP gets its information from a variety of sources, some direct and some estimates. The estimates were necessary in earlier architectures because there weren't any means to directly measure certain statistics. Now days, many of these measurements are available from the lowest level. The main problem with TOP is figuring out if it has been updated to obtaining these measurements directly or is it still estimating them.

In contrast, dumpsys gets its info from /proc. These measurements are at the lowest level or very nearly so. As such, as the architecture changes, /proc is more likely to be updated.

The second part of your question brings up one of /proc's deficiencies. /proc is generally very poorly documented, including sampling rates, etc. I haven't seen a case where a sampling rate for /proc (and so dumpsys) could be set, but that doesn't mean such capability doesn't exist. (Often, the sampling rate is actually dumped along with the data.)

How do you use top to extract only the CPU usage and the process names (command) columns?

From the command line or from within top?

If you're already in top, press f and toggle the columns you want to see.

Alternatively, you can use ps:

ps -eo %cpu,pid --sort -%cpu


Related Topics



Leave a reply



Submit