1080*80 ad

Monitoring CPU Usage in Linux

Monitoring the performance of your system’s CPU is crucial for ensuring smooth operation and diagnosing issues in Linux. High CPU usage can indicate a process consuming too many resources, a lack of processing power for your workload, or even system instability. Fortunately, Linux provides several powerful command-line and graphical tools to help you keep an eye on CPU activity.

One of the most widely used tools is top. When you run top in your terminal, it displays a dynamic, real-time view of system processes, showing CPU and memory usage among other information. The top section provides summary information about the system, including the load average. The load average represents the average number of processes that are either running or waiting to run over the last 1, 5, and 15 minutes. A high load average, especially if consistently above the number of CPU cores, indicates your system is heavily burdened. The CPU usage line breaks down activity into categories:

  • %us: CPU time spent running non-kernel code (user space).
  • %sy: CPU time spent running kernel code (system space).
  • %ni: CPU time spent running processes with a manually set nice value.
  • %id: CPU time spent idle.
  • %wa: CPU time spent waiting for I/O to complete.
  • %hi: CPU time spent handling hardware interrupts.
  • %si: CPU time spent handling software interrupts.
  • %st: CPU time stolen from a virtual machine by the hypervisor.

Below the summary, top lists processes, ordered by default by CPU usage (%CPU column). This helps quickly identify which processes are consuming the most resources.

An enhanced and more user-friendly alternative to top is htop. htop provides a similar interactive process viewer but with a more visual layout, often displaying CPU usage across individual cores as bar graphs at the top. It’s easier to navigate and filter processes in htop compared to top, making it a popular choice for real-time monitoring.

For a statistical overview rather than a real-time process list, the vmstat command is very useful. vmstat reports information about processes, memory, paging, block I/O, traps, and CPU activity. Running vmstat with an interval (e.g., vmstat 5) will show updates every 5 seconds. The ‘cpu’ section of its output provides similar metrics to top (%us, %sy, %id, %wa), offering a concise summary of overall CPU state over time.

If you need to analyze historical performance data or gather detailed statistics over time, the sar (System Activity Reporter) command is invaluable. sar collects, reports, and saves system activity information. You can use sar -u to report CPU utilization. Running sar -u 5 10 for instance, will capture 10 samples of CPU usage at 5-second intervals. sar can also read historical data from log files, allowing you to investigate performance issues that occurred in the past.

Another command providing detailed I/O and CPU statistics is iostat. While often used for disk I/O analysis, iostat -c specifically shows CPU utilization statistics, similar to vmstat and sar -u. Adding an interval (e.g., iostat -c 5) shows output periodically.

For those who prefer graphical interfaces, many Linux distributions offer built-in system monitors (like GNOME System Monitor or KDE System Guard) that provide visual graphs of CPU usage over time, broken down by core. These tools offer an easy-to-understand overview and often include process lists similar to top and htop.

In summary, whether you prefer real-time interactive views (top, htop), statistical snapshots (vmstat, iostat), historical analysis (sar), or graphical monitoring, Linux provides a robust set of tools to effectively monitor your CPU usage and performance. Understanding the output of these commands is key to identifying performance bottlenecks and ensuring your system runs efficiently.

Source: https://kifarunix.com/how-to-measure-cpu-usage-in-linux/

900*80 ad

      1080*80 ad