I am running Ubuntu 14.04 on my machine. I need to get the CPU utilization for a process running at a required monitoring interval.

I have tried the following have a few questions about them.
I considered calculating the effective usage between 2 points as follows:

Process run time(P) = utime + stime + cstime + cutime

Total CPU run time(C) = I am getting this value from /proc/stat.

Considering a monitoring interval t, CPU utilization is calculated as

CPU usage = ( P2 - P1 ) / (C2 - C1 ) * 100


  1. The value CPU usage read comprises the sum total of all 4 ( on my machine ) cores. But top shows a value divided by 4. I found that there is an Irix mode which if turned on( by default ) gives values multiplied by 4. What I want to know is that which value is correct ? Should I multiply my value by 4 ?.
  2. I want to get the usage at sampling periods of lets say 1ms. But apparently /proc/stat is not updated at the same rate, and nether is top. What is the smallest sampling period that I can go down to with using this method ?

Please let me know. Also if any other sugestions, please let me know. Also can we write a kernel module to do the same at my required sampling rate ? Does any such module exists ?


1 Answer 1


How about using cpustat? This has the ability to monitor specific processes and allows one to monitor at a rate of 0.3333Hz upwards. The -a option allows one to specify the total CPU time in terms of all the CPU ticks rather than just one CPU tick.

For example, 2 samples a second on the compiz process for 120 samples:

cpustat -a -p $(pidof compiz) 0.5 120 

I developed this tool for Ubuntu to measure CPU utilisation on low powered devices, and hence I have optimised it to use as small a CPU overhead as possible. For more information, refer to the cpustat project page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.