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
Issue:
- 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 ?.
- 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 ?
Thanks
Ankit