Home Command line utilities How To Display CPU Usage From Commandline

How To Display CPU Usage From Commandline

By sk
Published: Updated: 6.2K views

This tutorial describes how to display CPU usage from commandline using mpstat utility. Most of us use top command to show the CPU usage and the running processes. Just like top command, mpstat also displays the activities of each processor. This handy utility can help you to easily monitor the CPU utilization in any Unix-like operating systems. If you have multi-core processor on your system, mpstat is a definitive choice to display the usage of each processor core.

Install mpstat / sysstat in Linux

mpstat is available in the default repositories of the most modern Linux operating systems.

To install it on Arch Linux and its derivatives, run:

$ sudo pacman -S sysstat

On RHEL, CentOS, Fedora:

$ sudo dnf install sysstat

Or,

$ sudo yum install sysstat

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install sysstat

The typical syntax of mpstat command is:

mpstat [ -P { cpu | ALL } ] [ -V ] [ interval [ count ] ]

Let us see some real-time examples.

Display CPU Usage From Commandline Using Mpstat

The quick way to check the CPU usage is to run:

$ mpstat

Here is the sample output from my Arch Linux desktop:

Check the CPU usage using mpstat

Check the CPU usage using mpstat

Let me break down the above output and tell you what each option does.

  • 02:18:23 - The time when you run mpstat.
  • all - Indicates that statistics are calculated as averages among all processors.
  • %usr - Displays the percentage of CPU utilization that occurred while executing at the user level (application).
  • %nice - Displays the percentage of CPU utilization that occurred while executing at the user level with nice priority.
  • %sys - Displays the percentage of CPU utilization that occurred while executing at the system level (kernel).
  • %iowait - Displays the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
  • %irq - Displays the percentage of time spent by the CPU or CPUs to service hardware interrupts.
  • %soft - Displays the percentage of time spent by the CPU or CPUs to service software interrupts.
  • %steal - Displays the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor.
  • %guest - Displays the percentage of time spent by the CPU or CPUs to run a virtual processor.
  • %gnice - Displays the percentage of time spent by the CPU or CPUs to run a niced guest.
  • %idle - Displays the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

As I already mentioned in the introductory section, the mpstat command will display the all processor cores activities. If you want to view the CPU usage of a specific processor core, for example the first processor core, run:

$ mpstat -P 0

To display the second core, run:

$ mpstat -P 1

This would be helpful if you have a multi-core CPU. Please note that processor core number will start from 0(zero). If you have quadcore CPU, each processor core will be referred as cpu0, cpu1, cpu2, and cpu3.

View the CPU usage of a specific processor core using mpstat

View the CPU usage of a specific processor core using mpstat

To display the CPU usage of all processors, use the following command:

$ mpstat -P ALL
Display the CPU usage of all processors using mpstat

Display the CPU usage of all processors using mpstat

We can also the display the CPU activities in specific intervals. Say for example, to display 5 reports at interval of 60 seconds (i.e 1 minute), run the following command:

$ mpstat 60 5

Sample output would be:

Display the CPU activities in specific Intervals using mpstat

Display the CPU activities in specific Intervals using mpstat


Related read:


To display the details interrupts received by CPUs, run:

$ mpstat -l ALL
Display the details interrupts received by CPUs using mpstat

Display the details interrupts received by CPUs using mpstat

The interrupts statistics are taken from /etc/interrupts file. You can check it using the 'tail' command like below:

$ tail /etc/interrupts

You can combine both interrupts and CPU usage of all processors using -A option like below.

$ mpstat -A
Display interrupts and CPU usage of all processors using mpstat

Display interrupts and CPU usage of all processors using mpstat

To check the version of mpstat, run:

$ mpstat -V

For more details, refer the man pages.

$ man mpstat

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More