How to measure the average CPU utilization of a Linux process

Last updated on February 19, 2021 by Dan Nanni

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 can measure the average CPU usage of Linux processes.

In the following let's find out how to measure the average CPU utilization of a particular Linux process, or process-level CPU usage of all available Linux processes with pidstat tool.

Install sysstat on Linux

In order to use pidstat, you need to install the sysstat package as follows.

Install sysstat on Ubuntu, Debian or Linux Mint

$ sudo apt install sysstat

Install sysstat on CentOS, RHEL or Fedora

$ sudo yum install sysstat

Build sysstat from the Source

If the sysstat package is not available on your Linux system, you can easily build it from the source as follows.

$ tar -xf sysstat-12.5.3.tar.gz
$ cd sysstat-12.5.3/
$ ./configure
$ make
$ sudo make install

Measure Average CPU Usage of a Process with 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 11579

In this example, I am measuring the average CPU usage of a Linux process with PID 11579, which is measured over five-second interval. This command will refresh the average CPU usage every five second. If you press Ctrl-C, the command will exit and print the overall average CPU usage. The output indicates that the process consumes 5.5% of a single CPU core on average. 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 on your system). The %usr and %system columns indicate how much of the CPU is executed in the user space or in the kernel, respectively.

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).

Measure Process-level CPU Usage on the System with pidstat

If you would like to measure process-level CPU usage for all running processes, you can simply use -p ALL option with pidstate.

$ pidstat 60 -p ALL

The above command will report the average CPU usage for all running processes every one minute.

If you want to feed the output of pidstat into a shell script, you can use the following bash script. This script measures the process-level average CPU usage of the entire system (averaged over one minute).

#!/bin/bash
output=$(timeout 60.1 pidstat 60 -p ALL)
IFS='
'
for line in $output; do
    # extract N-th column with awk
    pid=`echo $line | awk '{ print $4}'`    # PID
    pcpu=`echo $line | awk '{ print $9}'`   # percentage CPU
    cpu=`echo $line | awk '{ print $10}'`   # CPU ID
    cmd=`echo $line | awk '{ print $11}'`   # command
    echo "$pid $pcpu $cpu $cmd"
done

If you find this tutorial useful, feel free to share it with your friends on your social network.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean