I am trying to calculate the CPU usage of a process in Android as follows, however i am not sure if its right due to the output produced.
To convert from jiffie to seconds: jiffie / hertz
1st step: get the uptime using the 1st parameter of /proc/uptime
file.
2nd step: get the number of clock ticks per second from /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
.
3rd step: get the totaltime spent by the process (utime(14) +stime(15))
parameters from /proc/[pid]/stat
4th step: get the starttime(22) of the process from /proc/[pid]/stat
the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)) after Linux 2.6.
5th step: get the total elapsed time of the process since it started (uptime - (starttime / hertz)
(since uptime is in seconds and starttime is in clock ticks).
6th step: get the CPU usage percentage ((totaltime / hertz) / elapsedTime) * 100
.
The output after the calculation is something like 5.702244483458246E-6 which is approximately equal to ~0.000005702244483
EDIT
Output
Step 1: 226.06 1211.19
Step 2: 1000000
Step 3: 9347 (example.com) S 3573 3573 0 0 -1 1077952832 8971 0 1 0 38 32 0 0 20 0 25 0 13137 983830528 14330 4294967295 1 1 0 0 0 0 4612 0 38136 4294967295 0 0 17 5 0 0 0 0 0 0 0 0 0 0 0 0 0
Reference: How do I get the total CPU usage of an application from /proc/pid/stat?