How to use the command "mpstat" (with examples)

How to use the command "mpstat" (with examples)

The mpstat command is a useful tool for monitoring CPU performance and utilization. It provides detailed statistics about each processor in a system, including metrics such as idle time, user time, system time, and more. With the ability to specify the sampling interval and number of reports, mpstat allows users to monitor CPU statistics over time.

Use Case 1: Display CPU statistics every 2 seconds

mpstat 2

Motivation

By using the mpstat command with a specific sampling interval, we can continuously monitor CPU statistics and observe how the processor is being utilized over time. This can be useful for identifying performance bottlenecks, detecting CPU-intensive tasks, and optimizing system resources.

Explanation

The 2 argument specifies the sampling interval, indicating that CPU statistics should be displayed every 2 seconds. The default output of mpstat includes various metrics such as CPU utilization (%), user time (%), idle time (%), and more.

Example Output

Linux 5.4.0-77-generic (hostname)   07/20/2021   _x86_64_   (1 CPU)

13:45:00  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
13:45:02  all    3.67    0.00    6.20    0.12    0.00    0.06    0.00    0.00    0.00   89.94
13:45:04  all    3.65    0.00    5.94    0.10    0.00    0.04    0.00    0.00    0.00   90.28
13:45:06  all    3.76    0.00    6.04    0.12    0.00    0.05    0.00    0.00    0.00   90.04
...

Use Case 2: Display 5 reports, one by one, at 2 second intervals

mpstat 2 5

Motivation

In some cases, it may be sufficient to capture a finite number of CPU statistical reports rather than continuously monitoring them. This can be useful for gathering data for a specific time period or analyzing the CPU behavior during a particular task or workload.

Explanation

Similar to the previous use case, the 2 argument specifies the sampling interval of 2 seconds. The 5 argument specifies the number of reports that should be generated. This means that mpstat will display 5 reports, one after the other, with a 2-second interval between each report.

Example Output

Linux 5.4.0-77-generic (hostname)   07/20/2021   _x86_64_   (1 CPU)

13:45:00  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
13:45:02  all    3.67    0.00    6.20    0.12    0.00    0.06    0.00    0.00    0.00   89.94
13:45:04  all    3.65    0.00    5.94    0.10    0.00    0.04    0.00    0.00    0.00   90.28
13:45:06  all    3.76    0.00    6.04    0.12    0.00    0.05    0.00    0.00    0.00   90.04
13:45:08  all    3.69    0.00    6.28    0.14    0.00    0.06    0.00    0.00    0.00   89.82
13:45:10  all    3.81    0.00    6.20    0.12    0.00    0.06    0.00    0.00    0.00   89.82

Use Case 3: Display 5 reports, one by one, from a given processor, at 2 second intervals

mpstat -P 0 2 5

Motivation

When dealing with multi-processor systems, it can be helpful to focus on statistics for specific processors rather than the entire system. This allows us to understand the behavior of individual processors and identify any imbalances or inefficiencies in system resource utilization.

Explanation

The -P 0 argument specifies that CPU statistics should be reported for a specific processor (in this case, processor 0). This restricts the output of mpstat to only show statistics for the chosen processor. The 2 and 5 arguments indicate the sampling interval and number of reports, respectively, as explained in the previous use cases.

Example Output

Linux 5.4.0-77-generic (hostname)   07/20/2021   _x86_64_   (1 CPU)

13:45:00  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
13:45:02    0    3.67    0.00    6.20    0.12    0.00    0.06    0.00    0.00    0.00   89.94
13:45:04    0    3.65    0.00    5.94    0.10    0.00    0.04    0.00    0.00    0.00   90.28
13:45:06    0    3.76    0.00    6.04    0.12    0.00    0.05    0.00    0.00    0.00   90.04
13:45:08    0    3.69    0.00    6.28    0.14    0.00    0.06    0.00    0.00    0.00   89.82
13:45:10    0    3.81    0.00    6.20    0.12    0.00    0.06    0.00    0.00    0.00   89.82

Conclusion

In this article, we explored different use cases of the mpstat command. We learned how to continuously monitor CPU statistics, capture a finite number of reports, and focus on specific processors. By using mpstat, users can gain insights into CPU utilization, identify performance issues, and optimize system resources.

Related Posts

Using the setserial Command (with examples)

Using the setserial Command (with examples)

The setserial command is a powerful tool for managing and configuring serial ports in Linux.

Read More
KeePass2 Command Line Examples (with examples)

KeePass2 Command Line Examples (with examples)

Opening the most recently opened password database keepass2 Motivation: Sometimes you want to quickly open the most recently accessed password database in KeePass 2 without having to navigate to the file manually.

Read More
Using the Railway Command (with examples)

Using the Railway Command (with examples)

Railway is a powerful platform that allows developers to easily deploy and manage their code.

Read More