I want to calculate CPU time consumption of a particular process in system mode and user mode using statistics present in /proc/<PID>.

There are two fields utime and stime in /proc/<PID>/stat file, whose documentation says:

  • utime: Amount of time that this process has been scheduled in user mode
  • stime: Amount of time that this process has been scheduled in kernel(system) mode

I'm calculating CPU time consumption(in seconds) in system mode using following formula: (stime / No of clock ticks). For CPU time consumption in user mode I'm using the formula: (utime/ No of clock ticks)

I validated output of atop command for system and user CPU time but my calculation is coming out wrong.

NOTE: Please let me know if I'm evaluating wrong fields for system and user CPU time for a process? Or what is the right way to calculate?

P.S I'm dividing these fields by No of Clock ticks in a second in order to calculate system CPU time and User CPU time in seconds as utime and stime are expressed in terms of CPU clock ticks.

  • 1
    How do you determine the number of clock ticks? – Gilles Nov 25 '14 at 20:49
  • using sysconf(_SC_CLK_TCK) function – diago Nov 26 '14 at 5:44
  • I think that's correct. You can make ps print the multiplier that it uses by running ps -- info, it's the Hertz value. Show us the exact code you're using to read the values and make the calculations. – Gilles Nov 26 '14 at 10:04
  • yes 'ps --info' is displaying the correct value of Hertz. I am using following code to calculate the CPU time consumption for process in system mode. 'stime' I am reading from file '/proc/<PID>/stat'. Code: clockTicks = sysconf(_SC_CLK_TCK); system_cpu_time = ( stime / clockTicks); – diago Nov 26 '14 at 10:57
  • Please post your actual code, i.e. something that we could run on our machine to try to reproduce your problem. – Gilles Nov 26 '14 at 17:12

Your Answer

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

Browse other questions tagged or ask your own question.