mpstat Command

Display CPU statistics and performance metrics for multiprocessor systems.

Syntax

mpstat [OPTIONS] [INTERVAL [COUNT]] mpstat [OPTIONS] -P {cpu_list|ALL} [INTERVAL [COUNT]]

The mpstat command displays activities for each available processor, processor 0 being the first one. Global average activities among all processors are also reported.

Common Options

Option Description
-A Display all statistics (equivalent to -u -I ALL)
-I {SUM|CPU|SCPU|ALL} Display interrupt statistics
-P {cpu_list|ALL} Display statistics for specific processors
-u Display CPU utilization report (default)
-V Print version number and exit
-o JSON Display statistics in JSON format
-T Display timestamp for each report
-h Display help message

CPU Metrics Explained

Metric Description Good Range
%usr Percentage of CPU used by user processes < 70%
%nice Percentage of CPU used by nice processes Variable
%sys Percentage of CPU used by system processes < 30%
%iowait Percentage of CPU waiting for I/O operations < 10%
%irq Percentage of CPU servicing hardware interrupts < 5%
%soft Percentage of CPU servicing software interrupts < 5%
%steal Percentage of CPU stolen by hypervisor < 5%
%guest Percentage of CPU used by guest OS Variable
%idle Percentage of CPU idle time > 20%

Basic Examples

Basic CPU statistics

# Display current CPU statistics mpstat # Display statistics every 2 seconds mpstat 2 # Display statistics every 5 seconds, 10 times mpstat 5 10 # Display statistics with timestamp mpstat -T 1

Basic CPU utilization monitoring

Per-CPU statistics

# Display statistics for all CPUs mpstat -P ALL # Display statistics for specific CPU mpstat -P 0 # Display statistics for multiple CPUs mpstat -P 0,1,2 # Monitor all CPUs every second mpstat -P ALL 1

Monitor individual CPU cores and overall system

Interrupt statistics

# Display interrupt statistics mpstat -I SUM # Display per-CPU interrupt statistics mpstat -I CPU # Display software interrupt statistics mpstat -I SCPU # Display all interrupt statistics mpstat -I ALL

Monitor interrupt activity and distribution

Advanced Usage

Comprehensive monitoring

# Display all available statistics mpstat -A # All statistics with per-CPU breakdown mpstat -A -P ALL # Comprehensive monitoring every 5 seconds mpstat -A -P ALL 5 # All stats with timestamps mpstat -A -T 1

Complete system monitoring with all metrics

Output formatting

# JSON output format mpstat -o JSON 1 3 # JSON with all CPUs mpstat -o JSON -P ALL 1 3 # Save output to file mpstat 1 60 > cpu_stats.txt # Append timestamp and save mpstat -T 1 60 >> cpu_monitoring.log

Format output for analysis and logging

Specific monitoring scenarios

# Monitor high I/O wait systems mpstat 1 | grep -v "Average" # Monitor specific CPU under load mpstat -P 0 1 | awk '$3 > 80 {print}' # Monitor interrupt distribution mpstat -I CPU -P ALL 1 # Monitor virtualized environment mpstat 1 | awk '{print $1, $12, $13}' # Focus on steal and guest

Targeted monitoring for specific performance issues

Performance Analysis

Identifying CPU bottlenecks

# High CPU utilization check mpstat 1 5 | awk 'NR>3 {if($4+$5 > 80) print "High CPU:", $0}' # I/O wait analysis mpstat 1 10 | awk 'NR>3 {if($7 > 10) print "High I/O wait:", $0}' # System vs user CPU usage mpstat 1 5 | awk 'NR>3 {print "User:", $4, "System:", $6}' # Interrupt load analysis mpstat -I SUM 1 5

Analyze CPU performance patterns and bottlenecks

Load balancing analysis

# Check CPU load distribution mpstat -P ALL 1 5 # Identify unbalanced CPUs mpstat -P ALL 1 | awk '$3 != "all" && $4 > 90 {print "Overloaded CPU:", $3}' # Monitor interrupt distribution across CPUs mpstat -I CPU -P ALL 1 # Check for CPU affinity issues mpstat -P ALL 2 10 | grep -E "(CPU|all)"

Analyze workload distribution across CPU cores

Practical Examples

System monitoring scripts

# Basic monitoring script #!/bin/bash echo "CPU Monitoring Report - $(date)" echo "================================" mpstat 1 5 | tail -1 # Alert on high CPU usage #!/bin/bash CPU_USAGE=$(mpstat 1 1 | awk 'END{print 100-$NF}') if (( $(echo "$CPU_USAGE > 90" | bc -l) )); then echo "ALERT: High CPU usage: ${CPU_USAGE}%" fi # Log CPU stats to file mpstat -T 60 >> /var/log/cpu_stats.log &

Automated CPU monitoring and alerting

Performance troubleshooting

# Diagnose performance issues echo "=== CPU Overview ===" mpstat echo "=== Per-CPU Breakdown ===" mpstat -P ALL echo "=== Interrupt Analysis ===" mpstat -I SUM echo "=== 30-second monitoring ===" mpstat 1 30 | tail -5

Comprehensive performance analysis workflow

Capacity planning

# Collect baseline metrics mpstat 300 288 > daily_cpu_baseline.txt # 24 hours, 5-min intervals # Peak usage analysis mpstat 1 3600 | awk 'NR>3 {sum+=(100-$NF)} END {print "Avg CPU:", sum/(NR-3)}' # Generate hourly summary for hour in {0..23}; do echo "Hour $hour:" mpstat 3600 1 | tail -1 done

Long-term monitoring for capacity planning

Integration with Other Tools

Combining with system tools

# CPU and memory together echo "CPU Stats:" && mpstat 1 1 | tail -1 echo "Memory Stats:" && free -h # CPU with process information mpstat 1 1 | tail -1 && echo "Top CPU processes:" && ps aux --sort=-%cpu | head -5 # CPU with load average mpstat 1 1 | tail -1 && uptime # CPU with I/O statistics mpstat 1 1 | tail -1 && iostat -x 1 1 | tail -5

Combine mpstat with other monitoring tools

Data collection and analysis

# Collect data for analysis mpstat -o JSON 1 60 > cpu_data.json # Parse JSON output mpstat -o JSON 1 3 | jq '.sysstat.hosts[0].statistics[].cpu-load[]' # Create CSV for spreadsheet analysis mpstat 1 60 | awk 'NR>3 {print $1","$4","$6","$7","$NF}' > cpu_stats.csv # Real-time dashboard data while true; do mpstat 1 1 | tail -1 | awk '{print $4,$6,$7,$NF}' sleep 1 done

Collect and format data for further analysis

Troubleshooting

Common Issues
  • Command not found - sysstat package not installed
  • No data displayed - Insufficient permissions or system issues
  • Inconsistent readings - System under heavy load
  • Missing CPU cores - CPU hotplug or virtualization issues

Installation and setup

# Install sysstat package # Ubuntu/Debian sudo apt update && sudo apt install sysstat # RHEL/CentOS/Fedora sudo yum install sysstat # or sudo dnf install sysstat # Verify installation mpstat -V # Check if service is running systemctl status sysstat

Install and configure sysstat package

Common troubleshooting

# Check system load uptime # Verify CPU information lscpu cat /proc/cpuinfo | grep processor | wc -l # Check for CPU throttling cat /proc/cpuinfo | grep MHz # Monitor system messages dmesg | grep -i cpu # Check process consuming CPU top -o %CPU

Diagnose system and CPU-related issues

Best Practices

Monitoring Guidelines
  • Use appropriate intervals - 1-5 seconds for troubleshooting, 5-15 minutes for trending
  • Monitor all CPUs on multi-core systems with -P ALL
  • Include timestamps for historical analysis
  • Combine with other tools for complete system view
  • Set up automated alerts for critical thresholds
  • Archive historical data for capacity planning
Performance Interpretation
  • High %usr - Application bottleneck, consider optimization
  • High %sys - System call overhead, kernel bottleneck
  • High %iowait - I/O bottleneck, check disk performance
  • High %steal - Virtualization overhead, resource contention
  • Uneven CPU distribution - Load balancing issues
  • High interrupt rates - Hardware or driver issues

See also