2

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?

Community
  • 1
  • 1
T-D
  • 373
  • 8
  • 21
  • The utime/stime values give you an approximation of the CPU usage of the process (you will need to sum them up over all threads) over the course of its lifetime. Are you trying to get the *recent* percent CPU usage, e.g. what the `top` command reports? – fadden Jul 04 '15 at 20:50
  • Actually i am trying to get the CPU usage of an application from when it is started until i purposely kill it. What do you mean by sum them up over "all threads" ? do you mean over all CPU's on the device? cpu0-->cpu7 (in case the device has 8 cpu's)? – T-D Jul 04 '15 at 21:52
  • @T-D If you list out the specific values you get from the respective files which you use in your calculations, we may be able to track down any errors in your arithmetic and process. – Vilhelm Gray Jul 06 '15 at 19:46
  • @VilhelmGray Alright i edited my question. – T-D Jul 06 '15 at 21:39
  • @T-D What is the value returned by `sysconf(_SC_CLK_TCK)`? – Vilhelm Gray Jul 07 '15 at 15:31

2 Answers2

6

Explanation of error

I suspect that you used the value in /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq as your hertz value. This is incorrect since that file gives you the CPU hardware clock frequency, but you must use the Linux kernel clock frequency as your hertz value.

The CPU hardware clock and Linux kernel clock are different. The Linux kernel -- which Android runs -- has its own timer (clock) which it updates at a certain frequency; the frequency at which this timer updates is the kernel hertz (HZ) value.

For historical reasons, the clock tick values listed in Linux proc and sys files are scaled from the kernel HZ frequency to a common frequency via the Linux kernel USER_HZ constant. It is this USER_HZ constant which we must use as the hertz value in our calculations.

Acquisition of data

  • uptime: 226.06 seconds
  • utime: 38 clock ticks
  • stime: 32 clock ticks
  • starttime: 13137 clock ticks
  • Hertz: 100 (Linux kernel USER_HZ constant)

Computation

total_time = utime + stime = 38 + 32 = 70

seconds = uptime - (starttime / Hertz) = 226.06 - (13137 / 100) = 94.69

cpu_usage = 100 * ((total_time / Hertz) / seconds) = 100 * ((70 / 100) / 94.69) = 0.7392...

Solution

The total CPU usage of your process is about 0.739%. If this seems small, remember that your process shares the CPU with all the other processes on the system: the majority of normal processes are idle for most of their life, so any one process will typically average a low total CPU usage.

Community
  • 1
  • 1
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
  • Thanks for the explanation! – T-D Jul 08 '15 at 17:23
  • How does frequency scaling figure into all this? Are the cpu and process times (e.g. utime, stime) normalized to Fmax? If they are not, wouldn't a process running at the lowest frequency and a process running for the same duration at the max frequency show the same CPU utilization? – Dmitry B. Sep 01 '17 at 03:44
0

While it is true that on most Android devices the value will be 100, on some devices it could be a different value. That is because this value depends on the CPU's architecture. ARM CPUs would have 100 for the USER_HZ constant value but x86 CPUs would have 1000 as you can see here.

Pafoid
  • 435
  • 4
  • 8