CPU utilization is the amount of work a CPU does to perform the tasks given to it. It’s an important metric to track because high CPU usage can negatively impact the performance of a system.
CPU usage can vary according to the CPU core count, since the system performs faster and more efficiently in the case of a higher core system. If the CPU cores are unable to handle the heavy load of an application, it results in context switching of tasks. But too much context switching will also cause a rise in CPU usage.
Tracking and monitoring CPU efficiency are essential for handling system processes and resources. Luckily, there are a number of tools available to monitor this metric.
Since high CPU utilization indicates poor system performance, it should be avoided. Higher than average CPU usage can often be attributed to one of the following causes:
If a system is idle, the OS creates a process called the System Idle Process to prevent the system from shutting off. This process appears to show high CPU usage, but it actually indicates the percentage of CPU capacity not being used. If the idle process is taking up to 99% of the CPU's power, it means only 1% is being used to run actual tasks.
Autostart programs are applications that are launched automatically when booting the operating system and they continue to run in the background. Too many background processes running simultaneously on a computer consume CPU resources and unnecessarily cause high CPU usage.
If a system becomes extremely slow with a CPU usage of nearly 100%—but with no clear cause—the problem may be a virus or malware. These are often invisible and heavily utilize the CPU.
Even the browser can cause high CPU usage if:
Applications or programs that require high processing power, including video editing software and high-resolution video games—can easily drive up CPU usage.
High CPU utilization adversely impacts system performance in the following ways:
It's essential to check the CPU usage regularly to understand the cause and impact of high CPU utilization.
Below are some of the most popular commands used for real-time monitoring of the CPU usage in a Linux system.
top
CommandThe top
command is used to display a real-time, dynamic view of a running system. It displays various crucial system metrics along with a set of processes that are currently being managed by the Linux kernel. It is one of the most widely used commands for tracking the CPU utilization, process statistics, and memory utilization of a Linux system.
For example, running the top
command on a Linux system will display an output similar to the one shown below:
In the output above, the top command displays various important CPU statistics, including idle time and wait time. You can calculate the overall CPU utilization using the idle time using the formula given below:
CPU utilization = 100 - idle time
For example, in the output above, the idle time is 98.3%, so the CPU utilization can be calculated as follows:
CPU utilization = 100 - 98.3 = 1.7%
The top command generates a list of processes sorted by the parameter %CPU, or percentage of CPU consumed by a process. This list includes various details about each process and is refreshed automatically every 5 seconds.
mpstat
CommandThe mpstat
command provides information about CPU performance and utilization by giving CPU statistics for the whole system and each available processor.
Running the mpstat
command on a Linux system will display an output like the one shown in figure 2.
This command shows various CPU statistics including idle time, io wait time and steal time. Similar to the top
command, the idle time shown here can be used to compute the CPU utilization using the same formula.
The following command can be run to get statistics for each of the CPUs or processors:
mpstat -P ALL
This command will generate an output similar to the one shown in figure 3.
Fig 3: Output of mpstat -p ALL commandThus, the CPU utilization of processors CPU0 and CPU1 can be calculated as shown below:
CPU0 = 100 - 92.13 = 7.87
CPU1= 100 - 22.78 = 77.22
Checking the CPU utilization for each process is essential. A system may show high CPU utilization even though only a few processes account for most of the usage. Investigate individual processes to identify the cause and terminate them as needed.
In addition to top
command, ps
is another useful command for viewing process-level CPU statistics.
ps
commandThe ps
command provides information about the processes running in the Linux system. It’s used to view process-related information, including the user of the process, the type of terminal used, and the
PID
of the process.
Running the ps
command on a Linux system will generate an output similar to the one shown below.
You can also use the ps command to view the top 10 most CPU-intensive processes by running the following command:
ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
The output of this command will look similar to the figure below.
Fig 5: Top 10 most CPU consuming processesThe output of the above command provides process-related information such as process ID(PID), process user, and the command used to run the process. This information is sorted according to CPU utilization and helps a user identify the process using most of the CPU so they can take action accordingly.
CPU utilization represents the amount of work a CPU handles to process resources or manage an operating system’s tasks. This metric is used to determine system performance by estimating the average CPU usage over a certain period of time.
Several factors contribute to high CPU usage so it’s essential to find the root cause to reduce it. This article provides three useful commands—top
, mpstat
, and ps
to help users monitor and troubleshoot CPU related issues on Linux.
In most scenarios, these commands are sufficient to troubleshoot and reduce the high CPU utilization.
Write for Site24x7 is a special writing program that supports writers who create content for Site24x7 “Learn” portal. Get paid for your writing.
Apply Now