Linux mpstat command
1. Overview of the mpstat command
mpstat (Multi-Processor Statistics) is a performance monitoring tool in Linux systems and is part of the sysstat toolkit. It is mainly used to monitor CPU usage and can display detailed statistics for each CPU core.
1.1 Main features
- Display detailed statistics of CPU usage
- Monitor the performance of a single CPU core or all cores
- Provide the proportion of CPU time spent in user mode, kernel mode, idle, and other states
- Support interval sampling and multiple sampling
1.2 Use Cases
- System performance tuning
- CPU bottleneck analysis
- Multi-core CPU load balancing check
- System monitoring and troubleshooting
II. Installation and basic usage
2.1 Installation method
Most Linux distributions can be installed via a package manager:
# Ubuntu/Debian
sudo apt install sysstat
# CentOS/RHEL
sudo yum install sysstat
# Arch Linux
sudo pacman -S sysstat
2.2 Basic command format
mpstat [选项] [间隔时间] [采样次数]
Simple Example
Example
Display current statistics for all CPU cores
mpstat
Sample every 2 seconds, 5 times in total
mpstat
2
5
III. Detailed command options
| Option | Description |
|---|---|
| -P {ALL|CPU number} | Specify the CPU core to display (ALL means all cores) |
| -u | Display CPU usage (default option) |
| -I {SUM|CPU|SCPU|ALL} | Display interrupt statistics |
| -V | Display version information |
| -o JSON | Output results in JSON format |
3.1 Common option combinations
Example
Monitor all CPU cores, refreshing every 1 second
mpstat
-P
ALL
1
Monitor only the first CPU core (CPU0)
mpstat
-P
0
1
5
4. Interpretation of output results
Executing mpstat -P ALL 1 Typical output:
Linux 5.4.0-91-generic (hostname) 03/15/2023 _x86_64_ (4 CPU)
10:30:45 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:30:46 AM all 5.25 0.00 1.25 0.25 0.00 0.25 0.00 0.00 0.00 93.00
10:30:46 AM 0 6.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 93.00
10:30:46 AM 1 4.00 0.00 1.00 1.00 0.00 1.00 0.00 0.00 0.00 93.00
10:30:46 AM 2 6.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 0.00 92.00
10:30:46 AM 3 5.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 94.00
4.1 Meaning of each field
| Field | Description |
|---|---|
| %usr | Percentage of time spent executing user-mode programs |
| %nice | Percentage of time spent executing low-priority user-mode programs |
| %sys | Percentage of time spent executing kernel-mode programs |
| %iowait | Percentage of time the CPU waits for I/O operations |
| %irq | Percentage of time spent handling hardware interrupts |
| %soft | Percentage of time spent handling software interrupts |
| %steal | Percentage of time a virtual CPU waits for the actual CPU |
| %guest | Percentage of time spent running a virtual processor |
| %gnice | Percentage of time spent running low-priority guests |
| %idle | Percentage of CPU idle time |
V. Practical Application Cases
5.1 Monitor CPU usage
Example
Monitor all CPU cores, refreshing every 2 seconds, displayed 5 times in total
mpstat
-P
ALL
2
5
5.2 Identify CPU bottlenecks
Key points to focus on:
- %usr too high: user-mode programs consume a lot of CPU
- %sys too high: frequent system calls
- %iowait too high: there may be an I/O bottleneck
5.3 Generate a performance report
Example
Redirect the output to a file
mpstat
-P
ALL
1
60
cpu_stats.log
VI. Frequently Asked Questions
6.1 What is the difference between the mpstat and top commands?
top: Display the overall system status dynamically in real timempstat: Focus on CPU statistics, providing more detailed per-core data
6.2 What does a very low %idle indicate?
It means the CPU is very busy and needs to be judged together with other metrics:
- If %usr is high: applications consume a lot of CPU
- If %sys is high: too many system calls
- If %iowait is high: I/O has become a bottleneck
6.3 How to monitor a specific CPU core?
Example
Monitor only CPU core 2
mpstat
-P
2
1
VII. Summary
mpstat is an important tool for Linux system performance monitoring, especially suitable for analyzing the usage of multi-core CPUs. Through this article, you should be able to:
- Understand the basic principles and usage scenarios of mpstat
- Master the common command options of mpstat
- Correctly interpret the output of mpstat
- Apply mpstat for performance analysis in real work
It is recommended to combine it with other tools such as vmstat, iostat Using them together allows a more comprehensive analysis of system performance issues.
Linux Command Reference