How To Display CPU Usage From Commandline

Display CPU Usage From Commandline

This tutorial describes how to display CPU usage from commandline using mpstat utility. Of course, top command will show you the CPU usage and the running processes. Just like top command, mpstat also displays the activities of each processor. Using this handy utility, we can easily monitor the CPU utilization in any Unix-like operating systems. If you have multi-core processor on your system, mpstat is opt to display the usage of each processor core.

Display CPU Usage From Commandline

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

To install 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.

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

mpstat

Here is the sample output from my Arch Linux desktop:

Linux 4.9.6-1-ARCH (sk) 02/08/2017 _x86_64_ (4 CPU)

02:19:51 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:19:51 PM all 12.77 0.10 1.93 2.63 0.00 0.10 0.00 0.00 0.00 82.47

Let us break down the above output and see what each option means.

  • 02:19:51 – 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.

Like I said, the above 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

Sample output:

Linux 4.9.6-1-ARCH (sk) 02/08/2017 _x86_64_ (4 CPU)

02:30:58 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:30:58 PM 0 12.46 0.09 2.10 3.90 0.00 0.39 0.00 0.00 0.00 81.07

This would be helpful if you have a multi-core CPU. Similarly, to view the statistics of the second processor core, run:

mpstat -P 1

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.

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:

Linux 4.9.6-1-ARCH (sk) 02/08/2017 _x86_64_ (4 CPU)

02:36:52 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:37:52 PM all 6.76 0.04 1.29 0.47 0.00 0.01 0.00 0.00 0.00 91.43
02:38:52 PM all 7.61 0.10 1.30 0.46 0.00 0.02 0.00 0.00 0.00 90.51
02:39:52 PM all 7.60 0.13 1.38 0.50 0.00 0.01 0.00 0.00 0.00 90.38
02:40:52 PM all 9.64 0.13 1.93 0.71 0.00 0.06 0.00 0.00 0.00 87.53
02:41:52 PM all 6.13 0.02 1.17 0.80 0.00 0.00 0.00 0.00 0.00 91.88
Average: all 7.55 0.08 1.41 0.59 0.00 0.02 0.00 0.00 0.00 90.35

As I mentioned, the above command will display only the first 5 reports of every one minute interval. It is also possible that we can continuously display the CPU statistics at regular interval.

The following command displays the CPU activity every one minute, continuously.

mpstat -P ON 1

Sample output:

01:17:50 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
01:17:51 PM all 3.27 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 96.47
01:17:51 PM 0 1.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 98.99
01:17:51 PM 1 10.10 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 89.90
01:17:51 PM 2 1.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00
01:17:51 PM 3 1.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00

Please note that the above command will run continuously until you stop it manually. To stop the process, press CTRL+C.

To check the version of mpstat, run:

mpstat -V

For more details, refer the man pages.

man mpstat

Hope this guide helpful. If you find this guide useful, share it on your social networks and support OSTechNix.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...