The ability to accurately measure CPU usage of a system is essential to ensure optimal operation of the applications or databases running on that system.
The mpstat Linux command is a utility that collects and displays information about CPU utilization and performance statistics.
To view which processes on the system are using more CPU resources, use the Linux top command.
The mpstat binary is part of the sysstat package which also provides the sar command utility. Use Linux which command to see if mpstat command is available or not.

[[email protected]:~] $ which mpstat
/usr/bin/mpstat
[[email protected]:~] $ rpm -qf /usr/bin/mpstat
sysstat-9.0.4-33.el6.x86_64

Use mpstat -V command to view the version of mpstat installed on your system.

[[email protected]:~] $ mpstat -V
sysstat version 9.0.4
(C) Sebastien Godard (sysstat <at> orange.fr)

We’ll now go over some examples to help us understand how to use the mpstat command in Linux.

Example 1: Executing the mpstat command without any options.

Like most utilities provided by sysstat, mpstat has some options that can be used with the command.
When the mpstat command is run without any option we see the below output:

[[email protected]:~] $ mpstat
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:14:38 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:14:38 AM all 29.39 0.00 1.66 12.04 0.00 0.12 0.00 0.00 56.79

In case you get a “command not found error“, then your system is probably missing the sysstat package.
You may install it using the following commands:

Install sysstat command in Linux Redhat/Centos/Fedora

yum install sysstat 

Install sysstat command in Linux Ubuntu/Debian

apt-get install sysstat

You may be read the article where we covered installing sysstat in detail.

Let’s understand and interpret the information displayed by the mpstat command.

The first line displays the OS type, kernel version, hostname, date, architecture, and number of CPUs.
We do not see this line when the command is run on a Solaris system.

  • 09:14:38 AM: This denotes the time at which mpstat command was run
  • all: means All CPUs on the system
  • %usr: Percentage of CPU utilization that occurred while executing at the user level which includes application processes, user running jobs, etc
  • %nice: Percentage of CPU utilization that occurred while executing at the user level with nice priority. This would imply processes which have a nice custom value defined by the user.
  • %sys: Percentage of CPU utilization that occurred while executing at the kernel level. The %sys comprises of the CPU resources used by the OS kernel to maintain optimum system function.
  • %iowait: Percentage of time that the CPUs were idle during which the system had an outstanding disk I/O request.
  • %irq: show the percentage of time spent by the CPU or CPUs to service hardware interrupts
  • %soft: show the percentage of time spent by the CPU or CPUs to service software interrupts
  • %steal: Percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor. This category of CPU utilization would occur in case of virtual machines only.
  • %guest: show the percentage of time spent by the CPU or CPUs to run a virtual processor. This is likely to occur when the system is provided virtualized resources to other entities namely virtual machines.
  • %idle: Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

Example 2:  Print proper utilization of all processors on the system.

To do this, we execute the mpstat command with the -P ALL option. Given below is an example.

[[email protected]:~] $ mpstat -P ALL
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:42:08 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:08 AM all 29.40 0.00 1.65 12.05 0.00 0.12 0.00 0.00 56.77
09:42:08 AM 0 28.50 0.00 1.52 12.85 0.00 0.13 0.00 0.00 57.00
09:42:08 AM 1 29.12 0.00 1.54 12.45 0.00 0.13 0.00 0.00 56.75
09:42:08 AM 2 30.67 0.00 1.51 12.08 0.00 0.12 0.00 0.00 55.62
09:42:08 AM 3 30.61 0.00 1.70 11.62 0.00 0.11 0.00 0.00 55.96
09:42:08 AM 4 30.64 0.00 1.82 11.46 0.00 0.11 0.00 0.00 55.96
09:42:08 AM 5 26.83 0.00 1.83 11.87 0.00 0.12 0.00 0.00 59.35

My system has 6 CPUs, and therefore mpstat is printing one line of information for each CPU numbered 0 through 5.

Example 3: Print CPU utilization statistics for all CPUs after specified time intervals.

Related concept:   Linux Directory Structure explained: /dev folder

We can use mpstat to print CPU utilization statistics of all individual CPUs multiple times with each output printing after a specified time interval.
The syntax for this is: mpstat -P ALL <time interval> <number of iterations>
The time interval is specified in seconds.
Here is an example:

[[email protected]:~] $ mpstat -P ALL 2 5
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:42:38 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:40 AM all 32.56 0.00 1.25 55.23 0.00 0.08 0.00 0.00 10.88
09:42:40 AM 0 70.35 0.00 2.01 25.63 0.00 0.50 0.00 0.00 1.51
09:42:40 AM 1 28.79 0.00 1.01 70.20 0.00 0.00 0.00 0.00 0.00
09:42:40 AM 2 29.50 0.00 0.50 70.00 0.00 0.00 0.00 0.00 0.00
09:42:40 AM 3 17.26 0.00 0.51 50.25 0.00 0.51 0.00 0.00 31.47
09:42:40 AM 4 22.38 0.00 2.38 55.71 0.00 0.00 0.00 0.00 19.52
09:42:40 AM 5 27.27 0.00 1.01 59.60 0.00 0.00 0.00 0.00 12.12
09:42:40 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:42 AM all 32.28 0.00 1.49 55.71 0.00 0.17 0.00 0.00 10.35
09:42:42 AM 0 40.00 0.00 2.00 25.00 0.00 0.00 0.00 0.00 33.00
09:42:42 AM 1 24.62 0.00 2.51 71.36 0.00 0.50 0.00 0.00 1.01
09:42:42 AM 2 24.75 0.00 0.50 74.75 0.00 0.00 0.00 0.00 0.00
09:42:42 AM 3 43.69 0.00 0.97 55.34 0.00 0.00 0.00 0.00 0.00
09:42:42 AM 4 35.96 0.00 1.48 49.75 0.00 0.00 0.00 0.00 12.81
09:42:42 AM 5 24.75 0.00 1.52 57.58 0.00 0.00 0.00 0.00 16.16
09:42:42 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:44 AM all 33.17 0.00 0.76 57.85 0.00 0.08 0.00 0.00 8.14
09:42:44 AM 0 25.25 0.00 0.00 67.68 0.00 0.00 0.00 0.00 7.07
09:42:44 AM 1 21.65 0.00 1.55 62.89 0.00 0.00 0.00 0.00 13.92
09:42:44 AM 2 21.11 0.00 0.50 78.39 0.00 0.00 0.00 0.00 0.00
09:42:44 AM 3 48.02 0.00 0.50 27.72 0.00 0.00 0.00 0.00 23.76
09:42:44 AM 4 51.74 0.00 1.00 45.77 0.00 0.50 0.00 0.00 1.00
09:42:44 AM 5 30.15 0.00 1.01 65.33 0.00 0.00 0.00 0.00 3.52
09:42:44 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:46 AM all 50.97 0.00 2.52 36.50 0.00 0.67 0.00 0.00 9.34
09:42:46 AM 0 61.00 0.00 1.00 27.00 0.00 0.00 0.00 0.00 11.00
09:42:46 AM 1 49.50 0.00 1.00 47.50 0.00 0.50 0.00 0.00 1.50
09:42:46 AM 2 40.31 0.00 5.10 32.14 0.00 1.02 0.00 0.00 21.43
09:42:46 AM 3 63.13 0.00 3.54 14.65 0.00 1.52 0.00 0.00 17.17
09:42:46 AM 4 46.73 0.00 3.52 44.22 0.00 1.01 0.00 0.00 4.52
09:42:46 AM 5 44.50 0.00 2.00 53.00 0.00 0.50 0.00 0.00 0.00
09:42:46 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:42:48 AM all 37.39 0.00 1.58 47.71 0.00 0.08 0.00 0.00 13.24
09:42:48 AM 0 88.00 0.00 0.00 9.00 0.00 0.00 0.00 0.00 3.00
09:42:48 AM 1 27.27 0.00 3.03 64.14 0.00 0.00 0.00 0.00 5.56
09:42:48 AM 2 20.00 0.00 2.00 37.50 0.00 0.50 0.00 0.00 40.00
09:42:48 AM 3 35.18 0.00 1.01 47.74 0.00 0.00 0.00 0.00 16.08
09:42:48 AM 4 29.35 0.00 1.00 64.68 0.00 0.00 0.00 0.00 4.98
09:42:48 AM 5 24.12 0.00 1.51 64.32 0.00 0.00 0.00 0.00 10.05
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
Average: all 37.24 0.00 1.52 50.63 0.00 0.22 0.00 0.00 10.40
Average: 0 56.97 0.00 1.00 30.79 0.00 0.10 0.00 0.00 11.13
Average: 1 30.43 0.00 1.82 63.20 0.00 0.20 0.00 0.00 4.35
Average: 2 27.08 0.00 1.71 58.68 0.00 0.30 0.00 0.00 12.24
Average: 3 41.52 0.00 1.30 39.22 0.00 0.40 0.00 0.00 17.56
Average: 4 37.08 0.00 1.87 52.07 0.00 0.30 0.00 0.00 8.68
Average: 5 30.18 0.00 1.41 59.96 0.00 0.10 0.00 0.00 8.35

The above command prints CPU utilization statistics for each CPU 5 times at an interval of 2 seconds.
Note that mpstat also prints the average CPU utilization for the specified period.

Example 4: View details on interrupts received by CPUs.

Peripherals or hardware devices send interrupts to CPUs to indicate that they require attention.
The CPU on receiving these interrupts invokes an interrupt handler to deal with them.
We can use the mpstat with the -I option to view statistics on interrupts received as shown in the below example:

[[email protected]:~] $ mpstat -I ALL
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
02:52:55 PM CPU intr/s
02:52:55 PM all 5402.51
02:52:55 PM CPU 0/s 1/s 3/s 4/s 7/s 8/s 9/s 12/s 14/s 15/s 
16/s 24/s 25/s 26/s 27/s 28/s 29/s 30/s 31/s 32/s 33/s 34/s 
35/s 36/s 37/s 38/s 39/s 40/s 41/s 42/s 43/s 44/s 45/s 46/s 
47/s 48/s 49/s 50/s 51/s 52/s 53/s 54/s 55/s 56/s 57/s 58/s 
59/s 60/s 61/s 62/s 63/s 64/s 65/s 66/s NMI/s LOC/s SPU/s PMI/s 
IWI/s RES/s CAL/s TLB/s TRM/s THR/s MCE/s MCP/s ERR/s MIS/s
02:52:55 PM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.80 35.44 50.45 
47.47 0.00 0.00 19.27 16.06 18.08 12.91 0.00 0.00 463.30 0.00 0.00 
0.00 120.19 135.52 4.56 0.00 0.00 0.00 0.00 0.00 0.00
02:52:55 PM 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.28 35.36 37.12 
35.72 0.00 0.00 22.10 17.34 13.12 18.30 0.00 0.00 474.68 0.00 0.00 
0.00 111.90 135.73 4.48 0.00 0.00 0.00 0.00 0.00 0.00
02:52:55 PM 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.76 30.64 48.59 
34.52 0.00 0.00 13.34 18.34 17.11 19.14 0.00 0.00 451.57 0.00 0.00 
0.00 102.31 128.55 4.39 0.00 0.00 0.00 0.00 0.00 0.00
02:52:55 PM 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.61 38.48 51.07 
37.47 0.00 0.00 21.07 14.09 15.60 18.81 0.00 0.00 444.91 0.00 0.00 
0.00 88.34 118.65 4.40 0.00 0.00 0.00 0.00 0.00 0.00
02:52:55 PM 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 7.98 46.57 44.64 
56.59 0.00 0.00 12.25 19.10 16.02 20.22 0.00 0.00 449.68 0.00 0.00 
0.00 87.08 114.99 4.34 0.00 0.00 0.00 0.00 0.00 0.00
02:52:55 PM 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 15.58 69.95 82.95 
79.87 1.11 0.00 21.89 14.86 18.23 18.67 0.00 0.00 419.72 0.00 0.00 
0.00 95.21 107.55 4.50 0.00 0.00 0.00 0.00 0.00 0.00

All of the above output indicates that the statistics gathered were combined to be displayed for all CPUs. The intr/s field denotes the number of interrupts received per second.
The data being displayed here is being presented from the /proc/interrupts file.
Given below is a short snippet from the file.

[[email protected]:~] $ tail -14 /proc/interrupts
NMI: 0 0 0 0 0 0 Non-maskable interrupts
LOC: 2261653659 2317250914 2204409240 2171924023 2195203375 2048948436 Local timer interrupts
SPU: 0 0 0 0 0 0 Spurious interrupts
PMI: 0 0 0 0 0 0 Performance monitoring interrupts
IWI: 0 0 0 0 0 0 IRQ work interrupts
RES: 586716320 546258661 499448237 431215706 425084228 464796343 Rescheduling interrupts
CAL: 661620465 662604090 627581461 579245095 561349938 525031399 Function call interrupts
TLB: 22276450 21886426 21442870 21499400 21200559 21946169 TLB shootdowns
TRM: 0 0 0 0 0 0 Thermal event interrupts
THR: 0 0 0 0 0 0 Threshold APIC interrupts
MCE: 0 0 0 0 0 0 Machine check exceptions
MCP: 16272 16272 16272 16272 16272 16272 Machine check polls
ERR: 0
MIS: 0

Example 5: Print CPU utilization and interrupt statistics together.

Related concept:   SAR command: ksar for GUI monitoring in Linux - Part 4

We use the mpstat command with the -A option to print details about CPU utilization and interrupt statistics together with the same command output.

[[email protected]:~] $ mpstat -A
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:43:32 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:43:32 AM all 29.40 0.00 1.65 12.05 0.00 0.12 0.00 0.00 56.77
09:43:32 AM 0 28.50 0.00 1.52 12.85 0.00 0.13 0.00 0.00 56.99
09:43:32 AM 1 29.12 0.00 1.54 12.45 0.00 0.13 0.00 0.00 56.75
09:43:32 AM 2 30.67 0.00 1.51 12.08 0.00 0.12 0.00 0.00 55.62
09:43:32 AM 3 30.61 0.00 1.70 11.62 0.00 0.11 0.00 0.00 55.96
09:43:32 AM 4 30.64 0.00 1.82 11.46 0.00 0.11 0.00 0.00 55.96
09:43:32 AM 5 26.83 0.00 1.83 11.87 0.00 0.12 0.00 0.00 59.35
09:43:32 AM CPU intr/s
09:43:32 AM all 5395.10
09:43:32 AM 0 204.24
09:43:32 AM 1 183.56
09:43:32 AM 2 187.55
09:43:32 AM 3 202.72
09:43:32 AM 4 222.42
09:43:32 AM 5 323.52
09:43:32 AM CPU 0/s 1/s 3/s 4/s 7/s 8/s 9/s 12/s 14/s 
15/s 28/s 29/s 30/s 31/s 32/s 33/s 34/s 35/s 36/s 37/s 
38/s 39/s 45/s 46/s 47/s 48/s 49/s 50/s 51/s 52/s 53/s 
54/s 55/s 56/s 62/s 63/s 64/s 65/s 66/s NMI/s LOC/s SPU/s 
PMI/s IWI/s RES/s CAL/s ERR/s MIS/s
09:43:32 AM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 5.80 19.33 16.11 18.14 12.95 0.00 0.00 462.74 0.00 
0.00 0.00 120.22 135.31 0.00 0.00
09:43:32 AM 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 5.27 22.18 17.40 13.14 18.36 0.00 0.00 474.14 0.00 
0.00 0.00 111.92 135.44 0.00 0.00
09:43:32 AM 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 5.76 13.32 18.35 17.16 19.17 0.00 0.00 451.04 0.00 
0.00 0.00 102.32 128.11 0.00 0.00
09:43:32 AM 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 5.60 21.09 14.12 15.61 18.80 0.00 0.00 444.36 0.00 
0.00 0.00 88.33 118.12 0.00 0.00
09:43:32 AM 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 7.98 12.27 19.15 16.05 20.28 0.00 0.00 449.09 0.00 
0.00 0.00 87.09 114.75 0.00 0.00
09:43:32 AM 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 15.60 21.89 14.88 18.25 18.67 0.00 0.00 419.14 0.00 
0.00 0.00 95.21 107.03 0.00 0.00

Obtain consolidated CPU statistics multiple times over specified time intervals.

Example 6: We can use mpstat to report consolidated CPU utilization statistics over specified intervals of time using the following syntax:

mpstat <interval> <number times to report data>

Given below is an example that runs the mpstat command and reports on CPU utilization statistics twice within a range of 1 second.

[[email protected]:~] $ mpstat 1 2
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:45:02 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:45:03 AM all 40.30 0.00 1.99 53.57 0.00 0.00 0.00 0.00 4.15
09:45:04 AM all 46.69 0.00 1.16 50.66 0.00 0.33 0.00 0.00 1.16
Average: all 43.50 0.00 1.57 52.11 0.00 0.17 0.00 0.00 2.65
[[email protected]:~] $

Example 7: Report utilization of a single CPU.

Related concept:   Puppet: How to remove node from master

To do this we use the mpstat command with the -P option followed by the processor number with the first processor being number 0.

In the below example we report on CPU utilization statistics for processor number 1.

[[email protected]:~] $ mpstat -P 1 
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU) 
09:45:13 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 
09:45:13 AM 1 29.12 0.00 1.54 12.45 0.00 0.13 0.00 0.00 56.75

 

In this example, we report on CPU utilization statistics for processor number 4.

[[email protected]:~] $ mpstat -P 4
Linux 2.6.32-696.6.3.el6.x86_64 (linuxnix.com) 10/28/2017 _x86_64_ (6 CPU)
09:45:17 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09:45:17 AM 4 30.64 0.00 1.82 11.46 0.00 0.11 0.00 0.00 55.96

This post concludes our mpstat command and how to use it with examples. In our next post, we will see how to use other interesting Linux command.