Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the CPU usage of a process by PID in Linux from C?

Tags:

c

linux

cpu-usage

I want to programmatically [in C] calculate CPU usage % for a given process ID in Linux.

How can we get the realtime CPU usage % for a given process?

To make it further clear:

  • I should be able to determine the CPU usage for the provided processid or process.
  • The process need not be the child process.
  • I want the solution in 'C' language.
like image 828
codingfreak Avatar asked Sep 14 '09 08:09

codingfreak


People also ask

How is CPU usage calculated in C?

Re: Get CPU usage (%) in a C programThe (pseudo-)file /proc/uptime contains two numbers: the system uptime, and the CPU time "used" (wasted) by the idle task, both in seconds. From this you can calculate the average CPU usage since boot.

How do I check memory usage on PID?

The ps command can also be used to monitor memory usage of individual processes. The ps v PID command provides the most comprehensive report on memory-related statistics for an individual process, such as: Page faults. Size of working segment that has been touched.


1 Answers

You need to parse out the data from /proc/<PID>/stat. These are the first few fields (from Documentation/filesystems/proc.txt in your kernel source):

Table 1-3: Contents of the stat files (as of 2.6.22-rc3) ..............................................................................  Field          Content   pid           process id   tcomm         filename of the executable   state         state (R is running, S is sleeping, D is sleeping in an                 uninterruptible wait, Z is zombie, T is traced or stopped)   ppid          process id of the parent process   pgrp          pgrp of the process   sid           session id   tty_nr        tty the process uses   tty_pgrp      pgrp of the tty   flags         task flags   min_flt       number of minor faults   cmin_flt      number of minor faults with child's   maj_flt       number of major faults   cmaj_flt      number of major faults with child's   utime         user mode jiffies   stime         kernel mode jiffies   cutime        user mode jiffies with child's   cstime        kernel mode jiffies with child's 

You're probably after utime and/or stime. You'll also need to read the cpu line from /proc/stat, which looks like:

cpu  192369 7119 480152 122044337 14142 9937 26747 0 0 

This tells you the cumulative CPU time that's been used in various categories, in units of jiffies. You need to take the sum of the values on this line to get a time_total measure.

Read both utime and stime for the process you're interested in, and read time_total from /proc/stat. Then sleep for a second or so, and read them all again. You can now calculate the CPU usage of the process over the sampling time, with:

user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before); sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before); 

Make sense?

like image 111
caf Avatar answered Sep 17 '22 12:09

caf