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.
Causes of high CPU utilization
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:
1. System idle process
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.
2. Autostart programs and background processes
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.
3. Virus and malware
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.
4. Browser
Even the browser can cause high CPU usage if:
- Several tabs are opened at the same time
- Multiple plugins and add-ons are running in the background
- Browser tabs with ads or auto-playing videos are open
5. Programs with high CPU requirement
Applications or programs that require high processing power, including video editing software and high-resolution video games—can easily drive up CPU usage.
Effects of high CPU utilization
High CPU utilization adversely impacts system performance in the following ways:
- Lag in CPU performance
- Frequent rebooting of systems
- Delay in response from application
- Overheating of CPU
It's essential to check the CPU usage regularly to understand the cause and impact of high CPU utilization.
Determining CPU utilization
Below are some of the most popular commands used for real-time monitoring of the CPU usage in a Linux system.
top
Command
The 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:
Fig 1: Output of top command
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
Command
The 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.
Fig 2: Output of mpstat command
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 command
Thus, 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
Monitoring CPU utilization per process
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
command
The 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.
Fig 4: Output of ps command
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 processes
The 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.
Conclusion
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.
Was this article helpful?
Sorry to hear that. Let us know how we can improve the article.