Cpustat – Monitors CPU Utilization by Running Processes in Linux
Cpustat is a powerful system performance measure program for Linux, written using Go programming language. It attempts to reveal CPU utilization and saturation in an effective way, using The Utilization Saturation and Errors (USE) Method (a methodology for analyzing the performance of any system).
It extracts higher frequency samples of every process being executed on the system and then summarizes these samples at a lower frequency. For instance, it can measure every process every 200ms and summarize these samples every 5 seconds, including min/average/max values for certain metrics.
Suggested Read: 20 Command Line Tools to Monitor Linux Performance
Cpustat outputs data in two possible ways: a pure text list of the summary interval and a colorful scrolling dashboard of each sample.
How to Install Cpustat in Linux
You must have Go (GoLang) installed on your Linux system in order to use cpustat, click on the link below to follow the GoLang installation steps that is if you do not have it installed:
Once you have installed Go, type the go get command below to install it, this command will install the cpustat binary in your GOBIN variable:
# go get github.com/uber-common/cpustat
How to Use Cpustat in Linux
When the installation process completes, run cpustat as follows with root privileges using the sudo command that is if your controlling the system as a non-root user, otherwise you’ll get the error as shown:
$ $GOBIN/cpustat This program uses the netlink taskstats interface, so it must be run as root.
Note: To run cpustat as well as all other Go programs you have installed on your system like any other commands, include GOBIN variable in your PATH environment variable. Open the link below to learn how to set the PATH variable in Linux.
This is how cpustat works; the /proc directory is queried to get the current list of process IDs for every interval, and:
Again, each sleep interval is adjusted to account for the amount of time consumed fetching all of these stats. Furthermore, each sample also records the time it took to scale each measurement by the actual elapsed time between samples. This attempts to account for delays in cpustat itself.
When run without any arguments, cpustat will display the following by default: sampling interval: 200ms, summary interval: 2s (10 samples), showing top 10 procs, user filter: all, pid filter: all as shown in the screenshot below:
$ sudo $GOBIN/cpustat
Cpustat – Monitor Linux CPU Usage
From the output above, the following are the meanings of the system-wide summary metrics displayed before the fields:
Still from the output above, for a given process, the different columns mean:
Note that long running child processes can often confuse this measurement, because the time is reported only when the child process exits. However, this is useful for measuring the impact of frequent cron jobs and health checks where the CPU time is often consumed by many child processes.
The following command displays the top 10 root user processes running on the system:
$ sudo $GOBIN/cpustat -u root
Find Root User Running Processes
To display output in a fancy terminal mode, use the -t flag as follows:
$ sudo $GOBIN/cpustat -u roo -t
Running Process Usage of Root User
$ sudo $GOBIN/cpustat -n 20
You can also write CPU profile to a file using the -cpuprofile option as follows and then use the cat command to view the file:
$ sudo $GOBIN/cpustat -cpuprofile cpuprof.txt $ cat cpuprof.txt
To display help info, use the -h flag as follows:
$ sudo $GOBIN/cpustat -h
Find additional info from the cpustat Github Repository: https://github.com/uber-common/cpustat
That’s all! In this article, we showed you how to install and use cpustat, a useful system performance measure tool for Linux. Share your thoughts with us via the comment section below.