How to Check CPU Utilization in Linux Using the Command Line: A Deep Dive
The heart of any Linux system is its CPU, the central processing unit that crunches numbers and executes instructions. Monitoring its utilization is crucial for performance tuning, troubleshooting bottlenecks, and ensuring a smooth user experience. Thankfully, the Linux command line offers a wealth of tools to accomplish this, providing real-time insights into how your CPU is performing.
The Direct Answer: Several commands reveal CPU utilization in Linux via the command line. The most popular and versatile include:
top
: Provides a dynamic, real-time view of system processes, including CPU usage per process and overall CPU utilization.htop
: An improved, interactive version oftop
with color-coding and easier navigation. Often requires installation.vmstat
: Reports virtual memory statistics, but also includes CPU usage metrics like user, system, idle, and I/O wait time.mpstat
: Reports CPU usage statistics for each individual CPU core in a multi-processor system.sar
: Collects, reports, and saves system activity information, including CPU utilization, over time. This is excellent for historical analysis./proc/stat
: The underlying data source for many of these tools. You can directly read this file, although it requires parsing.
Each of these tools offers a unique perspective and level of detail, allowing you to choose the one that best suits your needs. Let’s explore each in more detail.
Understanding the Tools
top
– The Real-Time Overview
top
is the go-to command for a quick snapshot of your system’s health. When you run top
, you’ll see a constantly updating list of processes, ordered by CPU usage by default (though this can be changed). The important CPU metrics are displayed in the header:
- %us (user): Percentage of CPU time spent running user-level (non-kernel) processes. High %us often indicates a CPU-bound application.
- %sy (system): Percentage of CPU time spent running kernel-level processes. High %sy may indicate a system bottleneck (e.g., I/O issues, driver problems).
- %id (idle): Percentage of CPU time spent idle. This is the inverse of CPU utilization. A low %id means the CPU is heavily loaded.
- %wa (I/O wait): Percentage of CPU time spent waiting for I/O operations to complete. High %wa suggests disk or network I/O bottlenecks.
- %st (steal time): Percentage of CPU time stolen by the hypervisor in a virtualized environment.
- %hi (hardware interrupts): Percentage of CPU time servicing hardware interrupts.
- %si (software interrupts): Percentage of CPU time servicing software interrupts.
top
is interactive; you can press keys to sort by different columns (e.g., M
for memory, P
for CPU), kill processes (k
), or change the update interval (s
).
htop
– top
on Steroids
htop
offers a visually enhanced and more interactive experience compared to top
. It’s not usually installed by default, so you might need to use your distribution’s package manager (e.g., apt install htop
on Debian/Ubuntu, yum install htop
on CentOS/RHEL).
htop
provides:
- Color-coded output: Makes it easier to identify processes and resource usage.
- Vertical and horizontal scrolling: Allows you to view all processes and their attributes, even on systems with many cores or a small terminal.
- Mouse support: You can interact with processes using the mouse.
- Process tree view: You can see the parent-child relationships between processes.
- Easier process management: Killing processes and changing their priority is more intuitive.
vmstat
– A Broader System View
vmstat
(virtual memory statistics) provides a broader view of system activity, including CPU utilization. It’s typically used to monitor overall system performance, not just CPU usage.
When you run vmstat
, you’ll see output like this (followed by periodic updates):
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 404560 59868 159100 0 0 7 7 66 57 1 0 98 0 0
The relevant CPU columns are:
- us (user): Percentage of CPU time spent in user space.
- sy (system): Percentage of CPU time spent in kernel space.
- id (idle): Percentage of CPU time spent idle.
- wa (I/O wait): Percentage of CPU time spent waiting for I/O.
- st (steal time): Percentage of CPU time stolen by the hypervisor.
You can specify an interval to get periodic updates (e.g., vmstat 2
to update every 2 seconds).
mpstat
– Core-Specific Insights
mpstat
(MultiProcessor Statistics) is essential for understanding CPU utilization on multi-core systems. It provides CPU usage statistics per core, allowing you to identify if certain cores are overloaded while others are idle.
To see per-core statistics, use the -P ALL
option:
mpstat -P ALL
This will output statistics for each CPU core (CPU 0, CPU 1, CPU 2, etc.) and an “average” CPU utilization. The CPU columns are similar to those in vmstat
: %usr, %sys, %idle, %iowait, etc.
mpstat
is invaluable for diagnosing performance bottlenecks in multi-threaded applications.
sar
– Historical Data Analysis
sar
(System Activity Reporter) is designed for long-term system monitoring. It collects system activity data at regular intervals and saves it to files. This data can then be analyzed later to identify performance trends and bottlenecks.
By default, sar
data is often collected by a cron job and stored in files under /var/log/sa/
. To view CPU utilization data, use:
sar -u -f /var/log/sa/saXX
Where XX
is the day of the month (e.g., sa15
for the 15th of the month).
sar
‘s output includes the same CPU metrics as vmstat
and mpstat
, but it also includes a timestamp, making it possible to track CPU utilization over time.
/proc/stat
– The Raw Data Source
/proc/stat
is a virtual file that contains a wealth of system statistics, including CPU utilization. It’s the underlying data source for many of the tools mentioned above. While not as user-friendly as top
or mpstat
, directly reading /proc/stat
can be useful for scripting and automation.
To read /proc/stat
, use:
cat /proc/stat
The first line starting with cpu
contains aggregate CPU statistics. The fields are:
cpu user nice system idle iowait irq softirq steal guest guest_nice
Calculating CPU utilization from /proc/stat
requires taking two samples and calculating the differences in each field. This can be done with a script.
FAQs – Frequently Asked Questions
1. Why is high CPU utilization a problem?
High CPU utilization isn’t inherently a problem. It simply means your CPU is working hard. However, sustained high CPU utilization (near 100%) can lead to:
- Slow response times: Applications may become sluggish.
- System instability: In extreme cases, the system may become unresponsive.
- Increased power consumption: The CPU generates more heat.
The key is to understand why CPU utilization is high and whether it’s expected.
2. How do I identify the process causing high CPU utilization?
top
and htop
are excellent for this. They show a list of processes sorted by CPU usage. Look for the process at the top of the list with the highest %CPU value.
3. What’s the difference between %us
and %sy
in top
?
%us
represents the percentage of CPU time spent running user-level processes (applications). %sy
represents the percentage of CPU time spent running kernel-level processes (system calls, device drivers). High %us
often indicates a CPU-bound application, while high %sy
may suggest a system bottleneck.
4. What does %wa
(I/O wait) indicate?
%wa
represents the percentage of CPU time spent waiting for I/O operations to complete. A high %wa
often indicates a disk or network I/O bottleneck. The CPU is idle while waiting for data to be read from or written to storage or the network.
5. What is CPU “steal time” (%st
)?
Steal time (%st
) is the percentage of CPU time stolen by the hypervisor in a virtualized environment. It occurs when the virtual machine is ready to run, but the hypervisor is using the physical CPU for other tasks. High steal time can indicate resource contention on the host machine.
6. How can I log CPU utilization over time?
sar
is designed for this purpose. It collects system activity data at regular intervals and saves it to files. You can then analyze this data later to identify performance trends. Another option is writing a custom script to periodically read /proc/stat
and save the data to a file.
7. Is it normal for CPU utilization to spike occasionally?
Yes, occasional spikes in CPU utilization are normal. Many applications require bursts of CPU power to perform certain tasks. The key is whether the high CPU utilization is sustained or just temporary.
8. How can I reduce high CPU utilization?
The steps to reduce high CPU utilization depend on the cause. Some common strategies include:
- Optimizing code: If a specific application is causing high CPU usage, optimizing its code can help.
- Upgrading hardware: If the CPU is consistently overloaded, upgrading to a faster CPU may be necessary.
- Reducing the load on the system: Closing unnecessary applications or reducing the number of concurrent users can help.
- Addressing I/O bottlenecks: If
%wa
is high, investigate disk or network I/O issues. - Adjusting process priorities: The
nice
command can lower the priority of CPU-intensive processes.
9. Can I monitor CPU utilization remotely?
Yes, several tools allow you to monitor CPU utilization remotely. SSH is a common option; you can run the commands discussed above over an SSH connection. Monitoring tools like Nagios, Zabbix, and Prometheus can also collect and display CPU utilization data from remote systems.
10. What unit of measurement is used for CPU utilization?
CPU utilization is typically expressed as a percentage (%). 100% means the CPU is fully utilized, while 0% means it’s completely idle.
11. How to check the number of cores on my machine using the command line?
The lscpu
command shows detailed information about the CPU architecture, including the number of cores. The output can be grepped: lscpu | grep "CPU(s):"
. Alternatively, nproc
command returns the number of processing units available.
12. Why is the CPU utilization sometimes more than 100%?
CPU utilization cannot be more than 100% per core. However, in a multi-core system, the aggregate CPU utilization can be higher than 100% if you are summing all the CPU cores utilization. The individual CPU cores cannot exceed 100%.
Leave a Reply