Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Netstat command in Linux
Next article icon

mpstat Command in Linux with Examples

Last Updated : 23 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

mpstat is a powerful and versatile command-line tool within a Linux system that allows detailed reporting of various processor-related statistics. Indeed, part of the sysstat package, mpstat delivers comprehensive CPU utilization and performance data, thus becoming an essential utility for both system administrators and performance analysts as well as developers aiming to optimize system resources.

mpstat is most useful with multi-processor environments, and statistics can be gathered for each individual CPU core. It sets up the first processor to be CPU 0, the second to be CPU 1, and so forth. Therefore, by employing mpstat, you can analyze systems with multiple cores in great detail in search of bottlenecks, balance the workloads, or fine-tune the system performance.

Installation of mpstat

Before you get going with the usage of mpstat, verify it is installed on your system. Installation instructions depend on your Linux distribution:

In Red Hat based Linux

sudo yum install sysstat

In ubuntu based Linux

 sudo apt install sysstat

Immediately after installation, you may need to activate the sysstat service in order for it to collect the data properly:

sudo systemctl enable sysstat && sudo systemctl start sysstat

Syntax

mpstat [options] [interval [count]]

where,

  • options: Numerous flags to customize the output
  • interval: Secs between reports
  • count: Number of reports

Basic Example

mpstat
Basic Example

This command prints an aggregate view of CPU-usage statistics since system startup. There is much useful metrics like:

  • %usr: Percentage of CPU spent at user-level
  • %sys: Percentage of CPU spent at kernel level
  • %iowait: Percent of CPU spent idle due to wait of input/output requests
  • %idle: CPU idle time, not including wait time for I/O operations

Common options for mpstat Command

Option

Description

-P

print CPU utilization of specified processor(s)

-A

Display all CPU statistics and report percpu statistics

-u

print CPU utilization

-I

print interrupt statistics

-n

print network statistics

–dec=

Print in specified number of decimal places, from 0 to 2.

All CPUs, 2 second intervals for 5 reports:

mpstat -P ALL 2 5
2024-10-12_15-06

This will print out an overview of the details of the performance for each CPU, on a 10-second period, updating every 2 seconds. It is also very useful in detecting imbalances of workload distribution among cores.

Detailed interrupt statistics:

mpstat -I ALL
Detailed interrupt statistics

This command would print out some statistics detailing interrupts, from both hardware and software points of view. It is particularly very helpful in tracking interrupt-related performance problems.

Print statistics with higher resolution:

mpstat -P ALL --dec=2 1 2
Print statistics with higher resolution

This command will print statistics of all CPUs to two decimal places of precision, updating every one second for 3 reports. Increased precision improves the analysis.

Understanding mpstat

To properly analyze your system, you need to be familiar with how to read the output of mpstat. Here’s a more detailed explanation of the most important metrics.

Metric

Description

Interpretation

%usr

User-level CPU usage

High values indicate CPU-bound application workloads

%sys

System CPU usage percentage

This could imply excessive number of system calls or some driver problem

%iowait

CPU idled while waiting for I/O completion

High %iowait can imply some form of I/O bottleneck (disks or networks)

%idle

CPU idled time, excluding iowait

Low values indicate high %cpu utilization

%irq

CPU usage for servicing hardware interrupts

High values may indicate problems due to hardware or drivers

%soft

percentage of time spent servicing software interrupts

high values may indicate that network or I/O processing is going on

Real-World Scenarios and Troubleshooting

Let’s look at a few real world scenarios where mpstat can be quite helpful:

Scenario 1: Find the CPU-bound processes

mpstat -P ALL 1 10 | grep -v CPU | awk '$12 < 10 {print $3, $12}'
2024-10-12_15-22

This command identifies the CPUs which are idling less than 10%, thus possibly CPU-bound processes. Combine it with ‘top’ or ‘ps’ to figure which ones are really eating high CPU usage time

Scenario 2: Identify I/O bottlenecks

mpstat 1 60 | awk '$6 > 10 {print "High I/O wait at " strftime("%H:%M:%S"), "iowait: " $6}'

This command will monitor I/O wait times for 60 seconds, alerting when they exceed 10%. High I/O waits might be indicative of a disk or network bottleneck.

Scenario 3: Interrupt Handling Analysis

mpstat -I SUM -P ALL 1 5
2024-10-12_15-26

mpstat -I SUM -P ALL 1 5 Output

This command can be used to analyze how interrupts are being distributed across the CPUs. A very uneven or skewed distribution might suggest that some interrupt balancing or CPU affinity adjustments need to be done.

Conclusion

This is one powerful tool the Linux system administrator will want to have in his bag of tricks. It provides detailed, real-time information on CPU performance and can be quite useful for performance tuning, capacity planning, and troubleshooting. Once you master mpstat along with other system monitoring tools, you are virtually at the helm of running your system at its best.

With the advent of multi-core processors and highly virtualized environments, mpstat becomes even more relevant, as with regular monitoring and analysis via mpstat, performance issues can be detected before they start to hurt the users, ensuring a smooth and efficient function of your Linux systems.



Next Article
Netstat command in Linux
author
manav014
Improve
Article Tags :
  • Linux-Unix
  • Technical Scripter
  • linux-command
  • Linux-system-commands

Similar Reads

  • lshw command in Linux with Examples
    The 'lshw' (List Hardware) command in Linux/Unix is a powerful tool for extracting detailed information about the system's hardware configuration. This tool retrieves data from files in the '/proc' directory and is capable of reporting on a wide array of components, including memory configuration, C
    3 min read
  • lsmod command in Linux with Examples
    lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command lin
    1 min read
  • How to List Open Files in Linux | lsof Command
    In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, w
    7 min read
  • lsusb command in Linux with Examples
    The 'lsusb' command in Linux is a useful utility for displaying information about USB buses and the devices connected to them. It provides a detailed view of the USB hardware connected to your system, including details such as speed, bus number, device class, and type. This command is particularly v
    2 min read
  • mailq Command in Linux with Examples
    mailq i.e. "mail-queue", this command in Linux prints the mail queue i.e. the list of messages that are there in the mail queue. You should have a mail-server setup on your Linux machine, to use this command, there are ways i.e MTA's(Mail Transfer agent) you can use like sendmail which uses the serv
    3 min read
  • How to Read Manual Pages in Linux | man Command
    The "man" command, short for manual, is a powerful tool in the Linux operating system that allows users to access detailed information about various commands, utilities, and system calls. The "man" command provides comprehensive documentation, helping users understand how to use and configure differ
    9 min read
  • md5sum Command in Linux with Examples
    The md5sum is designed to verify data integrity using MD5 (Message Digest Algorithm 5). MD5 is 128-bit cryptographic hash and if used properly it can be used to verify file authenticity and integrity. Example : Input : md5sum /home/mandeep/test/test.cppOutput : c6779ec2960296ed9a04f08d67f64422 /home
    5 min read
  • How to Create Directory in Linux | mkdir Command
    In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y
    7 min read
  • modinfo command in Linux with Examples
    The modinfo command in Linux is used to display information about a Linux kernel module. It extracts detailed information from the modules available in the system and provides insights into their properties, dependencies, parameters, and more. If the module name is provided without a file name, the
    3 min read
  • more command in Linux with Examples
    The 'more' command in Linux is a useful tool for viewing text files in the command prompt, particularly when dealing with large files like log files. It displays the content one screen at a time, allowing users to scroll through the text easily. This command is especially handy for reviewing long ou
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences