Linux mpstat Command
Complete Linux Command Reference
1. mpstat Command Overview
mpstat (Multi-Processor Statistics) is a performance monitoring tool in Linux systems, 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 Functions
- Display detailed CPU usage statistics
- Monitor the performance of a single CPU core or all cores
- Provide the proportion of CPU time in user mode, kernel mode, idle, and other states
- Support interval sampling and multiple sampling functions
1.2 Applicable Scenarios
- System performance tuning
- CPU bottleneck analysis
- Multi-core CPU load balancing checks
- System monitoring and troubleshooting
2. Installation and Basic Usage
2.1 Installation Methods
Most Linux distributions can install it via the 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
mpstat
# Sample every 2 seconds, for a total of 5 samples
mpstat 2 5
3. Command Options Explained
| Option | Description |
|---|---|
| -P {ALL|CPU number} | Specify which CPU cores 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
mpstat -P ALL 1
# Monitor only the first CPU core (CPU0)
mpstat -P 0 1 5
4. Output Result Analysis
Executempstat -P ALL 1the typical output of:
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 virtual processors |
| %gnice | Percentage of time spent running low-priority guests |
| %idle | Percentage of CPU idle time |
5. Practical Application Cases
5.1 Monitoring CPU Usage
Example
mpstat -P ALL 2 5
5.2 Identifying CPU Bottlenecks
Key focus areas:
- %usr too high: User-mode programs consume a large amount of CPU
- %sys too high: Frequent system calls
- %iowait too high: There may be an I/O bottleneck
5.3 Generating Performance Reports
Example
mpstat -P ALL 1 60 > cpu_stats.log
6. Frequently Asked Questions
6.1 What is the difference between mpstat and top commands?
top: Displays overall system status in real timempstat: Focuses on CPU statistics, providing more detailed per-core data
6.2 What does a very low %idle indicate?
It indicates the CPU is very busy; you need to combine other metrics to judge:
- If %usr is high: Applications consume a lot of CPU
- If %sys is high: Too many system calls
- If %iowait is high: I/O becomes the bottleneck
6.3 How to monitor a specific CPU core?
Example
mpstat -P 2 1
7. Summary
mpstat is an important tool for performance monitoring in Linux systems, especially suitable for analyzing multi-core CPU usage. After studying 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 mpstat's output results
- Apply mpstat for performance analysis in actual work
It is recommended to combine it with other tools such asvmstat、iostatto get a more comprehensive analysis of system performance issues.