What Is Exactly a "Clock Tick" in the Context of Android CPU Usage

What is exactly a Clock Tick in the context of Android CPU Usage?

100 is the default value set on Linux. And it appears to be unchanged in Android as shown in the sysconf.c source file - goo.gl/C5yubg

If you see line 167, this is what sysconf(_SC_CLK_TCK) will retrun:

case _SC_CLK_TCK: return SYSTEM_CLK_TCK;

The default value for SYSTEM_CLK_TCK is defined as 100

Calculating CPU Usage of a Process in Android


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)

    • This is under the assumption of an unmodified Android system.

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.

How to convert process cpu usage in clock ticks to percentage?

Further inspection of collectd's code revealed that the cpu usage is converted to microseconds.

Also, turns out a similar question was already asked and answered here.

Clock Ticks in android phone


How can we get access to the hardware's clock ticks.

You don't.

We want the ticks to be counted even while the device is on stand by/sleep/switched off.

Your users do not. Your users want their devices to behave normally, rather than having some software keeping the CPU powered on and tying up RAM, just watching the clock tick, because that will drain the battery fairly quickly.

Perhaps things are different in "remote areas and villages" near you. In most of the known portions of the universe, time elapses even if your app is not running.

Hence, you do not need to "count ticks" to determine if time elapses. Instead, when you "get the time from the Master server" and "store it", you also store the current value of SystemClock.elapsedRealtime(), which is the number of milliseconds since the phone was last rebooted. Then, when you need to determine the time relative to "the Master server", you compute the difference between the stored elapsedRealtime() value and the current elapsedRealtime() value, and add that number of milliseconds to the "time from the Master server" to get the current time as seen by "the master Server".

How do I get the total CPU usage of an application from /proc/pid/stat?


Preparation

To calculate CPU usage for a specific process you'll need the following:

  1. /proc/uptime
    • #1 uptime of the system (seconds)
  2. /proc/[PID]/stat
    • #14 utime - CPU time spent in user code, measured in clock ticks
    • #15 stime - CPU time spent in kernel code, measured in clock ticks
    • #16 cutime - Waited-for children's CPU time spent in user code (in clock ticks)
    • #17 cstime - Waited-for children's CPU time spent in kernel code (in clock ticks)
    • #22 starttime - Time when the process started, measured in clock ticks
  3. Hertz (number of clock ticks per second) of your system.
    • In most cases, getconf CLK_TCK can be used to return the number of clock ticks.
    • The sysconf(_SC_CLK_TCK) C function call may also be used to return the hertz value.


Calculation

First we determine the total time spent for the process:

total_time = utime + stime

We also have to decide whether we want to include the time from children processes. If we do, then we add those values to total_time:

total_time = total_time + cutime + cstime

Next we get the total elapsed time in seconds since the process started:

seconds = uptime - (starttime / Hertz)

Finally we calculate the CPU usage percentage:

cpu_usage = 100 * ((total_time / Hertz) / seconds)

See also

Top and ps not showing the same cpu result

How to get total cpu usage in Linux (c++)

Calculating CPU usage of a process in Linux



Related Topics



Leave a reply



Submit