T-D
T-D

Reputation: 373

Calculating CPU Usage of a Process in Android

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?

Upvotes: 2

Views: 5170

Answers (2)

Pafoid
Pafoid

Reputation: 445

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.

Upvotes: 0

William Breathitt Gray
William Breathitt Gray

Reputation: 12006

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.

Upvotes: 6

Related Questions