This tutorial describes how to display CPU usage from commandline using mpstat utility. Most of us use top command to show the CPU usage and the running processes. Just like top command, mpstat also displays the activities of each processor. This handy utility can help you to easily monitor the CPU utilization in any Unix-like operating systems. If you have multi-core processor on your system, mpstat is a definitive choice to display the usage of each processor core.
Install mpstat / sysstat in Linux
mpstat is available in the default repositories of the most modern Linux operating systems.
To install it on Arch Linux and its derivatives, run:
$ sudo pacman -S sysstat
On RHEL, CentOS, Fedora:
$ sudo dnf install sysstat
Or,
$ sudo yum install sysstat
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install sysstat
The typical syntax of mpstat command is:
mpstat [ -P { cpu | ALL } ] [ -V ] [ interval [ count ] ]
Let us see some real-time examples.
Display CPU Usage From Commandline Using Mpstat
The quick way to check the CPU usage is to run:
$ mpstat
Here is the sample output from my Arch Linux desktop:
Let me break down the above output and tell you what each option does.
- 02:18:23 - The time when you run mpstat.
- all - Indicates that statistics are calculated as averages among all processors.
- %usr - Displays the percentage of CPU utilization that occurred while executing at the user level (application).
- %nice - Displays the percentage of CPU utilization that occurred while executing at the user level with nice priority.
- %sys - Displays the percentage of CPU utilization that occurred while executing at the system level (kernel).
- %iowait - Displays the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
- %irq - Displays the percentage of time spent by the CPU or CPUs to service hardware interrupts.
- %soft - Displays the percentage of time spent by the CPU or CPUs to service software interrupts.
- %steal - Displays the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor.
- %guest - Displays the percentage of time spent by the CPU or CPUs to run a virtual processor.
- %gnice - Displays the percentage of time spent by the CPU or CPUs to run a niced guest.
- %idle - Displays the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.
As I already mentioned in the introductory section, the mpstat command will display the all processor cores activities. If you want to view the CPU usage of a specific processor core, for example the first processor core, run:
$ mpstat -P 0
To display the second core, run:
$ mpstat -P 1
This would be helpful if you have a multi-core CPU. Please note that processor core number will start from 0(zero). If you have quadcore CPU, each processor core will be referred as cpu0, cpu1, cpu2, and cpu3.
To display the CPU usage of all processors, use the following command:
$ mpstat -P ALL
We can also the display the CPU activities in specific intervals. Say for example, to display 5 reports at interval of 60 seconds (i.e 1 minute), run the following command:
$ mpstat 60 5
Sample output would be:
Related read:
To display the details interrupts received by CPUs, run:
$ mpstat -l ALL
The interrupts statistics are taken from /etc/interrupts file. You can check it using the 'tail' command like below:
$ tail /etc/interrupts
You can combine both interrupts and CPU usage of all processors using -A option like below.
$ mpstat -A
To check the version of mpstat, run:
$ mpstat -V
For more details, refer the man pages.
$ man mpstat