Checking the amount of CPU usage in Linux
Understanding CPU utilization is important for measuring overall system performance. From Linux geeks to system administrators, everyone knows how critical it can be to monitor CPU usage in Linux.
In this tutorial, we’ll go over some options for checking CPU usage in Linux.
prerequisites
- A computer based on a Linux operating system (such as Ubuntu and CentOS)
- Having an account with sudo access
- A command line window (Ctrl-Alt-T on Ubuntu and Menu > Applications > Utilities > Terminal on CentOS)
- (Optional) An installer program such as apt or yum, usually available by default.
top command to view Linux processor load
Open a terminal window and enter the following command.
1
|
top |
The system should respond to you by displaying a list of all running processes . You will also get a summary of users, profiles, CPU load and memory usage.
This list may change; Why do background tasks start and end? One of the useful options in this context is the -i option in this command.
1
|
top –i |
As a result, all background processes are hidden and it becomes easier for you to organize the list. To end the top command, just press the q key on the keyboard.
Other useful options when running the top command include:
- M – Setting the list of tasks based on the amount of memory usage
- P – Adjust task list based on CPU usage
- N – set list of tasks based on process ID
- T – Setting the list of tasks based on execution time
To have top command help, you can use the letter h when running the command or type the following command.
1
|
man top |
As a result, the help page of the top command will open for you.
mpstat command to display CPU activity
Mpstat is part of the software package called sysstat. Note that many RHEL-based Linux distributions have this software installed by default. But for Debian and Ubuntu systems, you will definitely need to install the sysstat package.
To do this, enter the following command in a terminal window.
1
|
sudo apt-get install sysstat |
Wait for the installation process to complete.
If you are using an older version of Red Hat or CentOS (eg 4.x), you can use the up2date tool to install sysstat.
1
|
sudo up2date install sysstat |
On newer installations of CentOS or Red Hat (versions 5.x and higher) it is possible to install the sysstat package using the following command.
1
|
sudo yum install sysstat |
When the installation process is complete, you can use the mpstat command as follows in the terminal.
1
|
mpstat |
As a result, the system displays the usage of each processor or processor core.
The first line is the series of column labels and the second line will be the value of each of these columns.
- %usr – percentage of CPU usage at user level
- %nice – CPU percentage for user processes labeled “nice”
- %sys – percentage of CPU usage at the system level (Linux kernel)
- %iowait – Percentage of CPU spent reading and writing to disk.
- %irq – Percentage of CPU dedicated to handling hardware interrupts.
- %soft – Percentage of CPU dedicated to handling software interrupts.
- %steal – CPU allocation percentage for managing virtual processors
- %guest – Percentage of CPU usage during the execution of a virtual processor
- %idle – percentage of CPU usage when the system is free (no process and disk reading and writing)
In the meantime, you can use some options in the mpstat command.
The -P option allows you to specify a specific processor for logging.
1
|
mpstat –P 0 |
As a result, you will have a report about the first processor (CPU 0).
1
|
mpstat –P ALL |
This command, like the basic form of mpstat, will show you the usage of all CPUs. Also, the processes related to each of the processors are shown. Meanwhile, the mpstat command only provides you with a general picture of CPU usage.
To get a set of reports, enter one number as the time interval and the next number as the number of reports.
1
|
mpstat 5 7 |
In this example, 7 reports are generated with time intervals of 5 seconds.
sar command to display CPU usage
sar is a tool for managing system resources. The effectiveness of this tool is not limited to CPU usage. But at the same time, you can monitor the CPU performance by using the u option in it.
For this purpose, enter the following command.
1
|
sar –u 5 |
The -u option is used to display CPU performance. Also, the number 5 means that this report should be displayed every 5 seconds. The execution of this command continues indefinitely. You can use Ctrl-C to stop it.
iostat command to view average CPU usage
In a terminal, enter the following command.
1
|
iostat |
As a result, the system shows the average CPU usage since the last startup. Also, the amount of input/output load, or in other words, the activities related to reading and writing the disk, will be provided to you.
Other options for monitoring CPU performance
Nmon monitoring tool
Nmon is a special monitoring tool developed by IBM employees. To install Nmon in Ubuntu, just enter the following command.
1
|
sudo apt-get install nmon |
Use the following commands to install on CentOS.
1
2
3
|
sudo yum epel-release sudo yum install nmon |
The command required to run Nmon is as follows.
1
|
nmon |
As a result, this tool will run for you and all the options will be displayed. Press C to view CPU usage. To return to the previous mode, press C again. Use the H key to have a list of commands as well. Press the Q key to close.
Graphical tools option
In many systems, the CPU cycle is not spent on a graphical user interface (GUI). However, you may have a GUI style version or use a Linux client system. Also, some Linux versions such as Ubuntu have a built-in monitoring tool.
To run Ubuntu system monitoring, enter the following command in the terminal.
1
|
gnome-system-monitor |
As a result, an application similar to the Windows task-manager will open for you, where you can see the status of tasks and the amount of CPU usage. Usually the GUI has a “task manager” or “system monitor” application. This tool can be used for real-time CPU performance monitoring.
Conclusion
There are several ways to check CPU usage in Linux. In this tutorial, we reviewed some basic methods for this purpose through internal Linux tools or third-party applications. These commands will be useful for checking the performance of your processor and system and will give you better control.