Linux mpstat Command

What is mpstat?

mpstat is used to monitor cpu utilization on your system. It will be more useful if your system has multiple processors. The first processors will signed as CPU 0. The second one will be signed CPU 1 and so on.

The mpstat command writes to standard output activities for each available processor, processor 0 being the first one. Global average activities among all processors are also reported. The mpstat command can be used both on SMP and UP machines, but in the latter, only global average activities will be printed. If no activity has been selected, then the default report is the CPU utilization report:

 mpstat -P ALL 5 1

It shows the various stats for the CPUs in the system. The –P ALL options directs the command to display stats for all the CPUs, not just a specific one. The parameters 5 2 directs the command to run every 5 seconds and for 2 times. The above output shows the metrics for all the CPUs first (aggregated) and for each CPU individually. Finally, the average for all the CPUs has been shown at the end.

Let’s see what the column values mean:

%user   -Indicates the percentage of the processing for that CPU consumes by user processes. User processes are non-kernel processes used for applications such as an Oracle database. In this example output, the user CPU %age is very little.
%nice   -Indicates the percentage of CPU when a process was downgraded by nice command. The command nice has been described in an earlier installment. It brief, the command nice changes the priority of a process.
%system -Indicates the CPU percentage consumed by kernel processes
%iowait -Shows the percentage of CPU time consumed by waiting for an I/O to occur
%irq    -Indicates the %age of CPU used to handle system interrupts
%soft   -Indicates %age consumed for software interrupts
%idle   -Shows the idle time of the CPU
%intr/s -Shows the total number of interrupts received by the CPU per second

It’s possible that a poorly written application not using multi-threadd architecture runs on a multi-processor machine but does not use all the processors. As a result, one CPU overloads while others remain free. You can easily diagnose these sorts of issues via mpstat.

And now and example:

oracle@primary:PRI::$ mpstat -P ALL 5 1
Linux 3.10.0-862.11.6.el7.x86_64 (primary.localdomain) 10/15/2018 _x86_64_(2 CPU)

07:35:11 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
07:35:16 AM all 0.51 0.00 0.51 0.61 0.00 0.00 0.00 0.00 0.00 98.36
07:35:16 AM 0 0.82 0.00 0.62 0.21 0.00 0.00 0.00 0.00 0.00 98.36
07:35:16 AM 1 0.20 0.00 0.40 1.21 0.00 0.20 0.00 0.00 0.00 97.98

Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 0.51 0.00 0.51 0.61 0.00 0.00 0.00 0.00 0.00 98.36
Average: 0 0.82 0.00 0.62 0.21 0.00 0.00 0.00 0.00 0.00 98.36
Average: 1 0.20 0.00 0.40 1.21 0.00 0.20 0.00 0.00 0.00 97.98

Print CPU utilization using intervals

You may want to see the CPU utilization movement. To do this, you can use intervals. Here’s an example.
The below command is to show you 3 reports about CPU utilization with 3 seconds interval:

oracle@primary:PRI::$ mpstat 1 2
Linux 3.10.0-862.11.6.el7.x86_64 (primary.localdomain) 10/15/2018 _x86_64_(2 CPU)

07:40:37 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
07:40:38 AM all 1.03 0.00 0.00 0.51 0.00 0.00 0.00 0.00 0.00 98.46
07:40:39 AM all 0.51 0.00 0.51 1.03 0.00 0.00 0.00 0.00 0.00 97.95
Average: all 0.77 0.00 0.26 0.77 0.00 0.00 0.00 0.00 0.00 98.21

Leave Comment

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