Descrip "top" command in Android
Asked Answered
F

4

10

I'm making a small Android application to show current total CPU usage like tab Performance in Windows Task Manager. I use "top -m 1 -n 1 -d 1" to get CPU usage, but i do not really understand the result of "top".

The result like:

User 5%, system 15%, IOW 0%, IRQ 0%
User 5 + Nice 0 + Sys 14 + Idle 73 + IOW 0 + IRQ 0 + SIRQ 0 = 92
PID     CPU%  S   #THR    VSS    RSS    UID     Name
213     11%   R    1      900K   340K   app_16   top

CPU usage = ??? How can i calculated total CPU usage?

Fischer answered 26/5, 2011 at 5:44 Comment(0)
A
10

The accepted answer for this question is incorrect. The second line of the output is the number of threads/processes that are launched in that grouping. Your CPU usage is 20% in the above. 5% of that is from user apps and 15% from system apps. You have 73 idle threads, 14 system threads, and 5 user threads (according to the second line).

for instance, here is a current top snapshot for my Droid.

User 6%, System 5%, IOW 0%, IRQ 0%
User 21 + Nice 0 + Sys 16 + Idle 270 + IOW 0 + IRQ 3 + SIRQ 0 = 310

  PID CPU% S  #THR     VSS     RSS PCY UID      Name
30994   4% S    19 134912K  24140K  bg app_24   edu.vu.isis.ammo.spotreport
 1021   3% S    57 217400K  58504K  fg system   system_server
20911   2% R     1    880K    400K  fg shell    top
 1053   0% S     1      0K      0K  fg root     tiwlan_wq
  995   0% S     2   1272K    128K  fg compass  /system/bin/akmd2

According to the accepted answer, I would have 310% CPU usage, when this is actually just the number of threads. I am pretty sure I am actually using only 11% of the CPU, where the top 3 processes are using 9% of that total.

Anastaciaanastas answered 11/8, 2011 at 17:31 Comment(4)
what is the interpretation if cpu has multi core ?Belda
not correct, see correct answer below (https://mcmap.net/q/1102258/-descrip-quot-top-quot-command-in-android)Percuss
incorrect, check this source code: android.googlesource.com/platform/system/core.git/+/refs/tags/…Crinose
And since Android 8.0, top is provided by toybox toolset: android.googlesource.com/platform/system/core.git/+/refs/heads/…Crinose
P
6

The actual answer to the question is straight-forward - can be calculated from the first line of android top's output

User 5%, system 15%, IOW 0%, IRQ 0%

Total = sum of all the percentages = 5+15 = 20%

The other answer about second line is so wrong. The second line actually gives scheduler time spent in that particular state - user/sys/idle/iow in jiffies (normally 10ms) between one output of top and the next output of top - in OP's case top -d 1 - which prints usage every second - the total number of jiffies per core would be ~100 (assuming 10ms per jiffy) - which would be similar to percentage values.

User 5 + Nice 0 + Sys 14 + Idle 73 + IOW 0 + IRQ 0 + SIRQ 0 = 92

Whereas, the values would be more if the -d is higher or if there are more than one cores

User 21 + Nice 0 + Sys 16 + Idle 270 + IOW 0 + IRQ 3 + SIRQ 0 = 310

This is probably without any -d option, so top takes default delay of 3 seconds = 300 jiffies.

Refer AOSP top sourcecode to get the complete logic of how each value is calculated.

Planospore answered 19/3, 2013 at 10:31 Comment(1)
This should be the accepted answer. The second line gives the time spent, not threads.Already
D
-5

The CPU usage percentage is given as (100-idle_percentage), more or less. In your snippet, the Idle percentage is 73, which makes your CPU usage 27%.

As for the per-process CPU usage, that will be your second column of the last 2 lines.

Deputize answered 26/5, 2011 at 5:48 Comment(1)
As mentioned by @jedmodson is his answer, from the posted example 73 is the number if idle threads, not the idle percentage.Zingg
S
-5

top command is a linux command. Have a look here

Thanks Deepak

Simar answered 26/5, 2011 at 5:50 Comment(1)
I read this manual but don't understand how to calculated CPU usage.Fischer

© 2022 - 2024 — McMap. All rights reserved.