How to Use the Command 'mpstat' (with examples)
- Linux
- December 17, 2024
The mpstat
command is a versatile utility used in Linux-based systems to report CPU statistics. It provides comprehensive insights into the system’s CPU performance by displaying a variety of metrics. These metrics typically include CPU utilization, interrupts, and context switches, among others. By leveraging this information, system administrators and IT professionals can monitor and optimize CPU performance, thereby ensuring efficient resource management and troubleshooting any performance-related issues.
Use Case 1: Display CPU statistics every 2 seconds
Code:
mpstat 2
Motivation:
Monitoring CPU performance at regular intervals is crucial for diagnosing potential issues, ensuring system stability, and optimizing resource utilization. By displaying CPU statistics at two-second intervals, system administrators can capture real-time data that might indicate spikes in CPU activity or other abnormal behaviors, helping to quickly identify and address possible problems.
Explanation:
mpstat
: This is the command used to display the CPU statistics.2
: The argument “2” instructs the tool to refresh and display the CPU statistics every two seconds. By setting this interval, users can continuously monitor CPU activity at a short and consistent pace, which is helpful for tracking transient performance fluctuations or spikes.
Example Output:
Linux 5.11.0-34-generic (hostname) 10/14/2021 _x86_64_ (4 CPU)
12:34:56 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
12:34:58 AM all 2.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 0.00 97.50
12:35:00 AM all 1.50 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 98.25
...
Use Case 2: Display 5 reports, one by one, at 2-second intervals
Code:
mpstat 2 5
Motivation:
Generating a specific number of CPU reports at defined intervals is useful for capturing a snapshot of system performance over a short period, particularly when diagnosing intermittent issues or performing a quick inspection of resource usage. This use case allows for a focused observation without continuously running the command.
Explanation:
mpstat
: This command is used to report CPU statistics.2
: The “2” indicates the time interval (in seconds) at which CPU statistics are displayed.5
: This argument specifies that a total of five reports will be generated. By setting a limit on the number of reports, users can collect a definite set of data points for analysis or documentation purposes.
Example Output:
Linux 5.11.0-34-generic (hostname) 10/14/2021 _x86_64_ (4 CPU)
12:34:56 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
12:34:58 AM all 2.00 0.00 0.30 0.00 0.00 0.00 0.00 0.00 0.00 97.70
12:35:00 AM all 3.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 0.00 96.50
...
Use Case 3: Display 5 reports from a specific CPU at 2-second intervals
Code:
mpstat -P 0 2 5
Motivation:
Analyzing the performance of a specific CPU in a multi-processor environment is critical for workload balancing, resource allocation, and diagnosing performance issues that might only be affecting a single core. Displaying statistics for a designated processor allows targeted observation and analysis.
Explanation:
mpstat
: The command for reporting CPU statistics.-P 0
: The-P
flag specifies that the command should report statistics for a particular processor. The “0” indicates that statistics should be gathered for CPU core number 0. This is particularly useful for identifying issues isolated to a single processor, such as an affinity problem.2
: This argument sets the interval at two seconds.5
: This specifies the number of reports to generate. This allows for a concise yet informative snapshot of a single CPU’s performance over a defined period.
Example Output:
Linux 5.11.0-34-generic (hostname) 10/14/2021 _x86_64_ (4 CPU)
12:34:56 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
12:34:58 AM 0 1.00 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 98.80
12:35:00 AM 0 1.20 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 98.40
...
Conclusion:
The mpstat
command is a powerful tool for real-time monitoring and analysis of CPU performance in Linux environments. Whether you’re observing system-wide activity or focusing on specific processors, the command offers flexibility and precision necessary for maintaining an efficient and stable computing environment. By understanding and utilizing these use cases, system administrators can better manage CPU resources, optimize performance, and diagnose potential issues swiftly and effectively.