Linux mpstat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mpstat command to monitor and analyze CPU utilization metrics across multiple CPUs. The mpstat command is a powerful tool for identifying performance bottlenecks, optimizing resource allocation, and troubleshooting system issues. You will start by understanding the purpose and usage of the mpstat command, and then explore CPU utilization metrics in more detail, including CPU time spent in user mode, kernel mode, and idle. Finally, you will analyze CPU performance across multiple CPUs to gain a deeper understanding of your system's resource utilization.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") subgraph Lab Skills linux/ps -.-> lab-422824{{"`Linux mpstat Command with Practical Examples`"}} linux/top -.-> lab-422824{{"`Linux mpstat Command with Practical Examples`"}} end

Understand the Purpose and Usage of mpstat Command

In this step, you will learn about the purpose and usage of the mpstat command in Linux. The mpstat command is a powerful tool for monitoring and analyzing CPU utilization metrics across multiple CPUs.

The mpstat command provides detailed information about CPU activity, including CPU utilization, interrupts, context switches, and more. It can be used to identify performance bottlenecks, optimize resource allocation, and troubleshoot system issues.

Let's start by running the mpstat command to see its basic 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 output shows various CPU utilization metrics for all CPUs on the system. The %usr column represents the percentage of CPU time spent in user mode, %sys represents the percentage of CPU time spent in kernel mode, and %idle represents the percentage of time the CPU was idle.

You can also use the mpstat command to monitor CPU utilization for a specific CPU. To do this, you can pass the CPU number as an argument:

sudo mpstat -P 0

This will show the CPU utilization metrics for the first CPU (CPU 0).

In the next step, you will learn how to explore CPU utilization metrics in more detail using the mpstat command.

Explore CPU Utilization Metrics with mpstat

In this step, you will learn how to use the mpstat command to explore CPU utilization metrics in more detail.

First, let's take a closer look at the output of 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 option shows the CPU utilization metrics for each individual CPU, in addition to the overall system average.

You can also use the mpstat command to monitor CPU utilization over time. For example, to monitor CPU utilization every 2 seconds for 10 seconds:

sudo mpstat 2 5

This will output the CPU utilization metrics every 2 seconds, for a total of 5 iterations (10 seconds).

Additionally, you can use the mpstat command to filter the output based on specific criteria. For example, to show the CPU utilization for only the user mode:

sudo mpstat -u

This will output the %usr column, which represents the percentage of CPU time spent in user mode.

In the next step, you will learn how to analyze CPU performance across multiple CPUs using the mpstat command.

Analyze CPU Performance Across Multiple CPUs

In this final step, you will learn how to use the mpstat command to analyze CPU performance across multiple CPUs on your system.

One of the key benefits of the mpstat command is its ability to provide detailed information about the performance of individual CPUs. This can be particularly useful when troubleshooting performance issues or optimizing resource allocation.

Let's start by running the mpstat command with the -P ALL option to see the CPU utilization metrics for each individual 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 shows the CPU utilization metrics for each individual CPU, as well as the overall system average. You can use this information to identify any imbalances or hotspots in your CPU usage.

You can also use the mpstat command to monitor CPU performance over time. For example, to monitor CPU utilization every 2 seconds for 10 seconds:

sudo mpstat -P ALL 2 5

This will output the CPU utilization metrics for each CPU every 2 seconds, for a total of 5 iterations (10 seconds).

By analyzing the CPU utilization metrics across multiple CPUs, you can identify performance bottlenecks, optimize resource allocation, and ensure that your system is running efficiently.

Summary

In this lab, you learned about the purpose and usage of the mpstat command in Linux. The mpstat command is a powerful tool for monitoring and analyzing CPU utilization metrics across multiple CPUs. You explored how to use the mpstat command to obtain detailed information about CPU activity, including CPU utilization, interrupts, context switches, and more. You also learned how to monitor CPU utilization for specific CPUs. Additionally, you explored the various CPU utilization metrics provided by the mpstat command, such as %usr, %sys, and %idle, which can be used to identify performance bottlenecks, optimize resource allocation, and troubleshoot system issues.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like