2024-07-09
mpstat’s Outputmpstat’s output can seem daunting at first, but with a little explanation, it becomes readily understandable. By default, mpstat displays average CPU utilization statistics since the system booted. However, its real power lies in its ability to provide real-time updates and historical data.
The key metrics you’ll see include:
The simplest way to use mpstat is to run it without any arguments:
mpstatThis will show the average CPU statistics since boot. To get a more detailed view, including per-core statistics, use the -P option:
mpstat -P ALL-P ALL displays statistics for all CPUs and cores. You can specify a particular CPU core using a number, for example:
mpstat -P 0 # Statistics for CPU 0For continuous monitoring, specify the interval and number of samples:
mpstat 2 5 # Display statistics every 2 seconds for 5 samplesThis command will show CPU utilization every two seconds for five iterations. This is useful for observing CPU behavior during resource-intensive tasks.
While the default output is detailed, you might only need certain metrics. Using the -u option displays only user and system statistics:
mpstat -u 2 5Similarly, the -I option can be used to show interrupt statistics (IRQ and softirq):
mpstat -I SUM 2 5 # SUM gives aggregated interrupt statsBy analyzing the output of mpstat, you can identify performance bottlenecks. For instance:
%iowait suggests disk I/O is a limiting factor.%user combined with low %idle could indicate a CPU-bound process.%system might hint at kernel-level problems or inefficient drivers.Understanding these relationships is key to using mpstat effectively for performance tuning and troubleshooting.
mpstat also offers the ability to average statistics over specific time intervals using the -A option:
mpstat -A 10 1 # Averages over 10 seconds for one sampleThis provides a smoother view of CPU utilization compared to snapshots taken at short intervals.
Using these examples and understanding the core metrics, you can effectively use mpstat to gain a better understanding of your Linux system’s performance and optimize its resource usage. This detailed understanding will allow you to proactively address performance issues before they impact your system’s stability and responsiveness.