Linux

How to Check CPU Usage on Linux

Check CPU Usage on Linux

In the dynamic realm of Linux system administration, keeping a vigilant eye on CPU usage is paramount. Whether you’re running a web server, managing a database, or simply ensuring the smooth operation of your system, understanding how to monitor CPU usage effectively is an essential skill. In this comprehensive guide, we’ll delve deep into various Linux utilities—top, mpstat, sar, iostat, and vmstat—to empower you with the knowledge and expertise to keep your system’s CPU performance optimized.

Understanding the Linux CPU Monitoring Tools

top: An Interactive CPU Monitor

Linux’s command-line utility top is a versatile and interactive tool for monitoring CPU usage in real time. It provides a comprehensive view of system processes, including CPU usage, memory consumption, and much more.

Usage:

top

Interact with top:

    • Observe the list of processes.
    • Sort processes by CPU usage by pressing Shift + P.
    • Interpret the data in real time.

Example Result:

 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1500 meilana 20 0 1905296 130640 38520 R 18.3 6.4 0:03.45 firefox
2550 maria 20 0 1129768 94092 57648 S 16.6 4.6 0:02.80 gnome-terminal
2875 ulfa 20 0 680252 52448 32712 S 6.6 2.6 0:00.70 code
...

Tip: Press q to exit top.

Utilizing mpstat for CPU Metrics

mpstat, part of the sysstat package is a powerful tool that provides detailed CPU statistics.

Usage:

  • Install sysstat (if not already installed):
sudo apt install sysstat # On Debian/Ubuntu
sudo dnf install sysstat # On CentOS/RHEL

Run mpstat:

mpstat -P ALL

Example Result:

Linux 5.4.0-80-generic (hostname)    09/01/23    _x86_64_    (8 CPU)

12:32:25 PM  CPU   %usr  %nice   %sys %iowait   %irq  %soft  %steal  %guest  %gnice  %idle
12:32:25 PM  all   1.63   0.05   0.26    0.12   0.00   0.02    0.00    0.00    0.00  97.91
12:32:25 PM    0   0.48   0.05   0.26    0.11   0.00   0.02    0.00    0.00    0.00  99.08
...

Troubleshooting Tip: If mpstat is not found, ensure that sysstat is installed and configured correctly.

Analyzing CPU Performance with sar

The sar (System Activity Reporter) utility provides a historical view of system performance, including CPU usage. It’s a great tool for identifying trends and potential issues.

Usage:

sar -u

Example Result:

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      2.84      0.02      0.31      0.05      0.00     96.77
12:20:01 AM     all      3.24      0.01      0.37      0.05      0.00     96.34
...

Tip: Use sar -u -f /var/log/sa/sadd to view historical data from a specific date.

Profiling Disk Activity with iostat

While primarily a disk monitoring tool, iostat is invaluable for diagnosing CPU bottlenecks caused by I/O operations.

Usage:

iostat

Example Result:

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          15.23    0.00    0.80    3.19    0.00   80.78

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               3.22        43.60         0.17     840219       3152
sdb               0.00         0.00         0.00         44          0
...

Troubleshooting Tip: If iostat is not found, ensure that sysstat is installed and configured correctly.

Understanding System Behavior with vmstat

vmstat offers insights into various system parameters, including CPU, memory, and I/O statistics.

Usage:

vmstat 1

Example Result:

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    336  85348  11804 667628    0    0    54    33   56   62  9  2 89  0  0
 ...

Tip: Press Ctrl + C to stop vmstat.

Comparing and Choosing the Right Tool

Each of these tools has its strengths and specific use cases. Consider the following when choosing the right tool:

  • For real-time monitoring: top provides an interactive view.
  • For historical data analysis: sar is your best choice.
  • For diagnosing CPU-I/O issues: iostat is invaluable.
  • For comprehensive system analysis: vmstat covers various parameters.

Comparing and Choosing the Right Tool

Choosing the right tool depends on your specific monitoring needs. Here’s a quick comparison:

Tool Real-Time Installation Interactive Additional Metrics
top Yes Pre-installed Yes Memory, Processes
mpstat No sysstat No Per-processor
sar Yes sysstat No Disk, Memory
iostat No sysstat No Disk, Network
vmstat No Pre-installed No Memory, Processes

Conclusion

Monitoring CPU usage on Linux is an essential task for maintaining system performance and stability. By mastering the tools discussed in this guide—top, mpstat, sar, iostat, and vmstat—you can proactively identify and address CPU-related issues, ensuring your Linux systems run smoothly.

Remember, effective monitoring is a continuous process. Regularly reviewing CPU usage and system statistics will help you optimize your Linux environment and deliver a seamless experience to your users.

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button