mpstat Command in Linux

Introduction

In this hands-on lab, you'll discover how to leverage the Linux mpstat command for comprehensive CPU monitoring and analysis across multiple processing cores. Mastering mpstat is crucial for any systemadmin aiming to pinpoint performance limitations, fine-tune resource distribution, and resolve system-level issues. We'll begin with a foundational understanding of the mpstat command's purpose and syntax. Then, we'll dive into detailed CPU utilization metrics, focusing on CPU time allocation in user mode, kernel mode, and idle states. Finally, you'll gain practical experience in analyzing CPU performance across several CPUs, providing a deeper understanding of system resource consumption.

Understand the Purpose and Usage of mpstat Command

This section introduces the core function and practical application of the mpstat command within a Linux environment. The mpstat utility is an invaluable asset for system administrators needing to closely monitor CPU usage and performance across various CPU cores.

Through mpstat, you can access in-depth insights into CPU activity, covering aspects like CPU utilization percentages, interrupt frequencies, context switch counts, and more. This information is crucial for identifying performance bottlenecks, streamlining resource management, and effectively troubleshooting problems within the system.

Let's execute the basic mpstat command to observe its initial output:

sudo mpstat

Example output:

Linux 5.15.0-58-generic (labex-ubuntu)   05/11/2023      _x86_64_        (4 CPU)

07:00:01 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
07:00:01 PM  all    0.25    0.00    0.19    0.00    0.00    0.00    0.00    0.00    0.00   99.76

The presented results display a range of CPU usage statistics for all CPUs combined. The %usr column indicates the proportion of CPU time dedicated to user-level processes, %sys represents the time spent in kernel operations, and %idle shows the percentage of time the CPU remained inactive.

Furthermore, mpstat allows you to focus on a particular CPU's activity. To achieve this, specify the CPU number as a parameter:

sudo mpstat -P 0

This command displays metrics for the first CPU, denoted as CPU 0.

The subsequent section details how to thoroughly analyze CPU utilization metrics using the mpstat command.

Explore CPU Utilization Metrics with mpstat

This section details how to delve deeper into CPU utilization metrics using the mpstat command.

We'll start by closely examining the output generated by the mpstat command:

sudo mpstat -P ALL

Example output:

Linux 5.15.0-58-generic (labex-ubuntu)   05/11/2023      _x86_64_        (4 CPU)

07:05:01 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
07:05:01 PM  all    0.25    0.00    0.19    0.00    0.00    0.00    0.00    0.00    0.00   99.76
07:05:01 PM    0    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50
07:05:01 PM    1    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50
07:05:01 PM    2    0.25    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.75
07:05:01 PM    3    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50

The -P ALL flag instructs mpstat to display CPU utilization statistics for each CPU individually, alongside the overall system average.

mpstat also enables continuous monitoring of CPU utilization over time. As an illustration, to track CPU utilization every 2 seconds for a duration of 10 seconds, execute:

sudo mpstat 2 5

This command presents CPU utilization data every 2 seconds, repeated 5 times (totaling 10 seconds).

Furthermore, mpstat provides filtering capabilities to refine the output based on particular criteria. For example, to view only the CPU utilization attributed to user mode, use:

sudo mpstat -u

This displays the %usr column, indicating the CPU time percentage allocated to user-level processes.

The following section will guide you through analyzing CPU performance across multiple CPUs using the mpstat command.

Analyze CPU Performance Across Multiple CPUs

This concluding section teaches you how to employ mpstat to analyze CPU performance across multiple CPUs within your system.

A significant advantage of mpstat is its ability to deliver precise data on individual CPU performance. This is exceptionally valuable when diagnosing performance problems or optimizing resource allocation.

To begin, execute mpstat with the -P ALL option to view utilization statistics for each CPU:

sudo mpstat -P ALL

Example output:

Linux 5.15.0-58-generic (labex-ubuntu)   05/11/2023      _x86_64_        (4 CPU)

07:10:01 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
07:10:01 PM  all    0.25    0.00    0.19    0.00    0.00    0.00    0.00    0.00    0.00   99.76
07:10:01 PM    0    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50
07:10:01 PM    1    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50
07:10:01 PM    2    0.25    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.75
07:10:01 PM    3    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50

This output provides utilization metrics for each CPU and the overall system. You can use this data to identify any imbalances or hotspots in CPU usage.

You can also use mpstat to monitor CPU performance trends. For instance, to check CPU utilization every 2 seconds for a 10-second period:

sudo mpstat -P ALL 2 5

This will show the CPU utilization figures for each CPU every 2 seconds, repeated 5 times (10 seconds total).

By examining the CPU utilization metrics across all CPUs, you can efficiently identify bottlenecks, optimize resource allocation, and ensure your system operates at peak performance. As a systemadmin, mastering these techniques are very important.

Summary

This lab covered the fundamentals of the mpstat command in Linux, highlighting its significance in monitoring and analyzing CPU utilization across multiple CPUs. You learned how to use mpstat to gather comprehensive information on CPU activity, including CPU utilization, interrupts, and context switches. The lab also showed you how to monitor CPU utilization for specific CPUs. Furthermore, you explored key CPU utilization metrics like %usr, %sys, and %idle, which are instrumental in identifying bottlenecks, optimizing resource allocation, and resolving system problems. This tool is essential for any systemadmin working with Linux.

400+ Linux Commands