In this tutorial we will explain different tools which can be used to check CPU utilization in Linux and Unix.
Understanding CPU utilization in simple terms involves two main concepts: CPU metrics and interpreting CPU load and usage.
- Basics of CPU Metrics:
- CPU Usage: This is how much your CPU is being used at any given time. It's like a car's engine running at a certain speed. A higher percentage means it's working harder.
- CPU Cores: Modern CPUs have multiple cores, like having several engines. More cores can handle more tasks simultaneously.
- Interpreting CPU Load and Usage:
- CPU Load: This tells you how many processes are waiting to use the CPU. Imagine a queue at a coffee shop; a longer queue means more waiting.
- Usage Over Time: Monitoring CPU usage over time helps identify when your computer is working hardest. It's like noticing when the coffee shop is busiest.
In short, CPU utilization is about how much your CPU is working (usage) and how many tasks are waiting to use it (load). Understanding these helps you gauge your computer's performance and efficiency.
Different tools to check CPU utilization in Linux
1. Using "top" command
Using the top command in Linux is a widely adopted method for monitoring CPU utilization. This command provides real-time data about system resource usage, including CPU usage. Here's a step-by-step guide to using top
for checking CPU utilization:
- Open Terminal: Launch the terminal on your Linux system.
- Execute the Command: Type
top
and hit Enter. - Interpret the Output:
- The command displays a dynamic list of processes.
- The "%CPU" column shows the percentage of CPU usage for each process.
- The "%CPU(s)" line at the top indicates the overall CPU usage by all processes.
- Process Sorting:
- By default, processes are sorted in descending order of CPU usage. The process at the top uses the most CPU resources.
- To specifically sort processes by CPU usage, press
Shift + P
. This will rearrange the processes so that those consuming the most CPU are listed first, providing a clear view of the most resource-intensive tasks.
- Exiting
top
: To exit, press the "q" key. - Interactive Features of
top
:
2. Using "htop" command
Using the htop command is another effective way to monitor CPU utilization in Linux. Here's a guide on how to use htop
:
- Opening Terminal: Open your terminal interface.
- Installing
htop
(if necessary):- For Debian/Ubuntu-based distributions:
sudo apt-get install htop
- For Red Hat/CentOS-based distributions:
sudo yum install htop
- For Fedora:
sudo dnf install htop
- For Arch Linux:
sudo pacman -S htop
- For Debian/Ubuntu-based distributions:
- Running
htop
: Simply typehtop
and press Enter. - Interface Overview:
- CPU usage is displayed at the top with individual bars for each core.
- Below is a list of processes with their respective CPU and memory usage.
- Sorting Processes by CPU Usage:
- Press
F6
to open the sort menu. - Use the arrow keys to select
%CPU
, then Enter to sort by CPU usage.
- Press
- Interactive Features:
- To kill a process, select it with arrow keys and press
F9
. - To search or filter processes, press
F3
and type the process name.
- To kill a process, select it with arrow keys and press
- Exiting
htop
:- Press
F10
orq
to exit.
- Press
3. Using "vmstat" command
The vmstat (Virtual Memory Statistics) command in Linux is used for monitoring system performance, including CPU utilization. Here's a guide on how to use vmstat
:
- Opening Terminal: Launch your terminal window.
- Basic Command:
- Type
vmstat
to display a summary of key system statistics.
- Type
- Using Intervals:
vmstat [interval] [count]
: Update statistics every[interval]
seconds,[count]
times. For example,vmstat 5 10
will update every 5 seconds, 10 times.
- Key Metrics:
- CPU fields:
us
(user time),sy
(system time),id
(idle time). - Memory fields:
swpd
(used swap space),free
(free memory). - Swap fields:
si
(memory swapped in),so
(memory swapped out). - I/O fields:
bi
(blocks in),bo
(blocks out).
- CPU fields:
- Additional Options:
-a
: Show active/inactive memory.-d
: Display disk statistics.-s
: Provide a table of event counters and memory statistics.
- Interpreting the Output:
- The first row of output shows averages since the last reboot.
- Subsequent rows show real-time data based on the specified interval.
- Stopping vmstat: If running with intervals, stop it with
Ctrl+C
.
4. Using "mpstat" command
The mpstat command in Linux is used for monitoring CPU utilization across different processors and is a versatile tool, ideal for detailed CPU monitoring, especially in multi-core systems. Here's how to use it:
- Opening Terminal: Launch the terminal from your Linux environment.
- Basic Usage:
- Simply type
mpstat
and press Enter. This provides an overview of CPU usage on all processors. - The output shows various metrics per CPU, including user time (
%usr
), system time (%sys
), and idle time (%idle
).
- Simply type
- Using Intervals:
- For real-time monitoring, use
mpstat
followed by a time interval in seconds. For example,mpstat 2
will refresh the CPU usage data every 2 seconds.
- For real-time monitoring, use
- Monitoring Specific CPUs:
- Use
mpstat -P ALL
to see statistics for each individual CPU core. - To monitor a specific core, use
mpstat -P
followed by the core number, likempstat -P 1
for CPU 1.
- Use
- Additional Options:
mpstat -u
displays CPU utilization.mpstat -V
shows the version ofmpstat
.
- Interpreting the Data:
%usr
: Percentage of CPU utilization in user mode.%sys
: Percentage of CPU utilization in system mode.%idle
: Percentage of time the CPU is idle.
- Exiting
mpstat
: To stop the command when running with intervals, useCtrl+C
.
5. Using "sar" command
The sar (System Activity Reporter) command in Linux is a powerful tool primarily used for monitoring system performance, including CPU utilization. Here's a detailed explanation focusing on CPU utilization:
- Opening Terminal: Begin by launching your terminal.
- Installation:
- If not installed, you can install
sar
using your distribution’s package manager, likesudo apt install sysstat
on Ubuntu.
- If not installed, you can install
- Basic CPU Monitoring:
- Execute
sar -u
to display CPU activity. - The output includes several key metrics:
%user
: Time CPU spent executing user processes.%nice
: Time CPU spent executing low-priority user processes.%system
: Time CPU spent on system (kernel) processes.%iowait
: Time CPU was idle during which the system had pending disk I/O requests.%idle
: Time CPU was idle and system did not have any outstanding disk I/O requests.
- Execute
- Real-Time Monitoring with Intervals:
sar -u [interval] [count]
provides continuous updates.- For example,
sar -u 2 3
updates CPU stats every 2 seconds, three times in total.
- Historical Data:
sar
stores historical data in/var/log/sysstat/
.- Use
sar -u -f /var/log/sysstat/saXX
to view data for a specific day (XX
is the file number).
- Advanced Options:
sar -P ALL
shows per-processor statistics.sar -u ALL
provides extended CPU usage statistics.
- Analyzing Output:
- Understanding these metrics helps in identifying CPU performance issues.
- For instance, a high
%iowait
might indicate disk-related bottlenecks.
- Scheduling Reports:
sar
can be scheduled to run at regular intervals using cron jobs, providing ongoing monitoring.
- Exporting Data:
- Data from
sar
can be exported for further analysis or reporting purposes.
- Data from
6. Using "iostat" command
The iostat command in Linux, commonly used for monitoring system input/output device loading, can also provide valuable insights into CPU utilization. Here's a detailed explanation:
- Opening Terminal: Start by opening your terminal.
- Running Basic Command:
- Execute
iostat
to get a report that includes CPU statistics. - The CPU section of the report shows usage percentages in different modes:
%user
,%nice
,%system
,%iowait
, and%idle
.
- Execute
- Using Intervals for Continuous Monitoring:
iostat [interval] [count]
updates the report at[interval]
seconds for[count]
times. For example,iostat 5 3
gives a report every 5 seconds, three times.
- Understanding CPU Metrics:
%user
: Percentage of CPU utilization that occurred while executing at the user level.%nice
: Percentage of CPU utilization that occurred while executing at the user level with nice priority.%system
: Percentage of CPU utilization that occurred while executing at the system (kernel) level.%iowait
: Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.%idle
: Time when the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.
- Installation:
iostat
is part of thesysstat
package. Install it if not present, e.g.,sudo apt-get install sysstat
on Debian/Ubuntu.
- Advanced Usage:
iostat -c
displays only CPU statistics.iostat -d
provides device utilization statistics, useful for correlating CPU activity with disk usage.
- Analyzing and Exporting Data:
- Regular monitoring with
iostat
helps in understanding how I/O load impacts CPU performance. - The output can be redirected to a file for later analysis.
- Regular monitoring with
- Use in Performance Tuning:
- By analyzing
iostat
output, you can identify if high I/O wait is causing CPU performance issues.
- By analyzing
7. Using "ps" command
The ps command in Linux is a powerful tool for monitoring system processes, and when used with specific options, it can be an efficient way to monitor CPU utilization. Using the command ps -eo pid,ppid,%cpu,cmd --sort=-%cpu | head -10
, you can extract detailed information about the processes consuming the most CPU resources.
ps
: The primary command for displaying information about active processes.-eo pid,ppid,%cpu,cmd
: These options customize the output:pid
: Process ID.ppid
: Parent Process ID, indicating the process that spawned this process.%cpu
: Percentage of CPU used by the process.cmd
: Command line of the process.
--sort=-%cpu
: Sorts the output by CPU usage in descending order.| head -10
: Pipes the output tohead
command to display only the top 10 processes.
Summary
In Linux, monitoring CPU utilization is crucial for system performance and can be efficiently done using commands like top
, htop
, vmstat
, mpstat
, sar
, iostat
, and ps
. The top
command offers real-time system metrics, easily sortable by CPU usage, while htop
provides a more user-friendly interface with similar capabilities. vmstat
is valuable for a snapshot view of system performance, including CPU load and memory usage. mpstat
offers detailed CPU statistics, especially useful in multi-core environments. The sar
command is essential for long-term CPU monitoring, providing comprehensive reports. iostat
is adept at showing how I/O operations affect CPU performance. Lastly, the ps
command, particularly with specific flags, can pinpoint high CPU usage processes, aiding in quick diagnostics and effective resource management. These tools collectively form a robust framework for Linux system administrators to monitor, analyze, and optimize CPU utilization.