Check CPU Utilization in Linux Like a PRO [7 Tools]


Linux

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.

  1. 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.
  2. 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:

  1. Open Terminal: Launch the terminal on your Linux system.
  2. Execute the Command: Type top and hit Enter.
  3. Interpret the Output:
  4. 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.
  5. Exiting top: To exit, press the "q" key.
  6. Interactive Features of top:
    • Press "k" to send signals, like terminate or kill, to a process.
    • Press "1" to view individual CPU core utilization (for systems with multiple cores).
    • Press "f" to customize the displayed columns​​.

 

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:

  1. Opening Terminal: Open your terminal interface.
  2. 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
  3. Running htop: Simply type htop and press Enter.
  4. Interface Overview:
  5. 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.
  6. 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.
  7. Exiting htop:
    • Press F10 or q to exit.

 

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:

  1. Opening Terminal: Launch your terminal window.
  2. Basic Command:
    • Type vmstat to display a summary of key system statistics.
  3. Using Intervals:
    • vmstat [interval] [count]: Update statistics every [interval] seconds, [count] times. For example, vmstat 5 10 will update every 5 seconds, 10 times.
  4. 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).
  5. Additional Options:
    • -a: Show active/inactive memory.
    • -d: Display disk statistics.
    • -s: Provide a table of event counters and memory statistics.
  6. 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.
  7. 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:

  1. Opening Terminal: Launch the terminal from your Linux environment.
  2. 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).
  3. 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.
  4. 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, like mpstat -P 1 for CPU 1.
  5. Additional Options:
    • mpstat -u displays CPU utilization.
    • mpstat -V shows the version of mpstat.
  6. 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.
  7. Exiting mpstat: To stop the command when running with intervals, use Ctrl+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:

  1. Opening Terminal: Begin by launching your terminal.
  2. Installation:
    • If not installed, you can install sar using your distribution’s package manager, like sudo apt install sysstat on Ubuntu.
  3. 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.
  4. 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.
  5. 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).
  6. Advanced Options:
    • sar -P ALL shows per-processor statistics.
    • sar -u ALL provides extended CPU usage statistics.
  7. Analyzing Output:
    • Understanding these metrics helps in identifying CPU performance issues.
    • For instance, a high %iowait might indicate disk-related bottlenecks.
  8. Scheduling Reports:
  9. Exporting Data:
    • Data from sar can be exported for further analysis or reporting purposes.

 

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:

  1. Opening Terminal: Start by opening your terminal.
  2. 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.
  3. 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.
  4. 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.
  5. Installation:
    • iostat is part of the sysstat package. Install it if not present, e.g., sudo apt-get install sysstat on Debian/Ubuntu.
  6. Advanced Usage:
    • iostat -c displays only CPU statistics.
    • iostat -d provides device utilization statistics, useful for correlating CPU activity with disk usage.
  7. 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.
  8. Use in Performance Tuning:
    • By analyzing iostat output, you can identify if high I/O wait is causing CPU performance issues.

 

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 to head 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.

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!