Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I use TOP command, I could get the following info:

shell@android:/ $ top -n 1                                                     

User 31%, System 10%, IOW 0%, IRQ 0%
User 346 + Nice 10 + Sys 120 + Idle 637 + IOW 6 + IRQ 0 + SIRQ 2 = 1121

  PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
  481  1  26% S    89 762832K  81688K  fg system   system_server
 1699  0   5% S    27 676472K  39092K  fg u0_a72   wm.cs.systemmonitor
11243  0   3% S    28 673140K  29796K  bg u0_a111  com.weather.Weather
13327  2   1% S    23 680472K  35844K  bg u0_a83   com.rhmsoft.fm
  659  0   1% S    17 663044K  33136K  bg u0_a13   android.process.media
20260  1   0% R     1   1208K    508K     shell    top

We can see the CPU% is round to integer, is there any way I could get a process's CPU% with higher precision?

-- Clarifications on the bounty -- Alex

The question refers to Android system, and preferably to a non-rooted device. While Android provides advanced profiling techniques for Java applications, tools for native code (C++) are limited. top command on Android allows to show the statistics for all threads running in the system, both Java threads and C++ threads. I am looking for an answer that will help with the following quest:

My app uses 2% CPU when it is inactive in background, while it should be below 0.1%. If I run top -t, I get 0% for all 15 threads that belong to my process (some threads are Java threads, e.g. the Main, UI thread; others are pthreads that never attach to JVM). How can I guess which thread eats the battery?

I would be glad to get even more details about this unexpected activity, and Android provides great helpers like TraceView for Java threads. Any insight regarding tools for native code will be highly appreciated.

like image 773
JackWM Avatar asked Mar 01 '13 00:03

JackWM


2 Answers

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.

like image 164
Gene Avatar answered Sep 28 '22 22:09

Gene


Got this information from another thread:

3) Getting CPU info

~$ adb shell dumpsys cpuinfo

Output:

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

EDIT:

You can also try this command: echo $(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')

Also:

using top : This will show you the cpu stats top -b -n 1 |grep ^Cpu

using ps: This will show you the % cpu usage for each process. ps -eo pcpu,pid,user,args | sort -r -k1 | less

EDIT2:

In realtion to your comments and the bounty description (How can I guess which thread eats the battery?) I found an interesting page:

http://ziyang.eecs.umich.edu/projects/powertutor/

As stated there:

You can use PowerTutor to monitor the power consumption of any application.

Try this for an instance and see if it meets your requirements.

FINAL EDIT:

Check out the Systrace documentation on the developer.android.com site:

http://developer.android.com/tools/debugging/systrace.html http://developer.android.com/tools/help/systrace.html

I'm sorry if you already tried that, but that's one concrete method to measure the performance.

like image 26
g00dy Avatar answered Sep 28 '22 22:09

g00dy