How to measure the average CPU utilization of a Linux process

Sometimes you may want to know the CPU usage of a particular Linux process. As the CPU usage of a process can fluctuate over its lifetime, you will want to measure the average CPU usage or CPU utilization of the process. For this purpose, a Linux tool set called sysstat may come in handy, which contains a collection of performance monitoring tools for Linux, reporting statistics on disk I/O, CPU, memory, networking, and other system activities. One of the utilities contained in sysstat is pidstat which measures the average CPU usage of Linux processes. In order to use pidstat, you need to install sysstat as follows.

To install sysstat on Debian, Ubuntu or Linux Mint:

$ sudo apt-get install sysstat

To install sysstat on CentOS or RHEL 6.0 and higher:

$ sudo yum install sysstat

To use pidstat on CentOS/RHEL 5.* and lower, you need to download sysstat source code since the sysstat package that comes with CentOS 5.* does not contain pidstat.

Once you have installed sysstat, you can use pidstat to measure the average CPU usage of a Linux process as follows.

$ pidstat 5 -p 9280
Linux 2.6.32.12-0.7.1.xs5.6.100.323.170596xen (cn-nj)   08/17/2012    _i686_  (2 CPU)
 
02:09:02 PM       PID    %usr %system  %guest    %CPU   CPU  Command
02:09:07 PM      9280   15.40   11.40    0.00   26.80     0  tincd
02:09:12 PM      9280   19.96    6.79    0.00   26.75     0  tincd
02:09:17 PM      9280   17.40    9.20    0.00   26.60     0  tincd
. . . .

In the above example, I am measuring the average CPU usage of a Linux process with PID 9280 every five seconds. The "CPU" field next to "%CPU" denotes the CPU processor ID to which the process is attached (in case there are more than one CPU processors).

Note that you need to provide the "interval" parameter (e.g., 5) which specifies the amount of time in seconds between each report. Without the interval parameter, the %CPU reported by pidstat will be the average CPU usage over the period since system startup (boot).

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

One thought on “How to measure the average CPU utilization of a Linux process

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *