HomeLinuxHow to monitor CPU usage in linux

How to monitor CPU usage in linux

In the realm of Linux system administration, monitoring and optimizing CPU utilization play pivotal roles in ensuring system performance, stability, and efficiency. The CPU serves as the brain of the system, executing instructions and processing data for various tasks. Efficient management of CPU resources is essential for smooth operation, especially in environments with high computational demands, such as servers, workstations, and cloud instances.

What is CPU Utilization

CPU usage involves tracking the amount of processing power consumed by running processes and system tasks. This data is instrumental in identifying resource-intensive applications, diagnosing performance bottlenecks, and troubleshooting issues related to CPU utilization. Additionally, monitoring CPU usage provides insights into system workload patterns, allowing administrators to make informed decisions regarding resource allocation and capacity planning.

How Veeble simplifies CPU Monitoring?
Traditionally, it involves using various command-line tools to check and analyze CPU load. However, if you are using Veeble service, this process is greatly simplified. Veeble provides a user-friendly interface that allows you to monitor your CPU load and other system metrics without the need to use the command line.
Veeble's interface not only saves time but also provides a more accessible way for users of all technical levels to keep an eye on their system's performance. This ease of use is particularly beneficial for those who may not be comfortable with command-line operations or for administrators who prefer a quick visual overview.
Here are the steps to access the CPU load information in your Veeble Server
1. Login to your Client Zone.
2. Select your service.
3. Click on the Manage option.
4. In this control panel, you can see the CPU usage.



Additionally, beyond monitoring CPU usage, this control panel allows you to monitor other important metrics such as disk usage, network usage, and memory usage. This comprehensive view helps you keep track of your service's overall performance and resource utilization without using any command-line tools.

How to Check Linux CPU Utilization

Monitoring CPU usage is essential for understanding system performance and ensuring optimal resource utilization in Linux environments. By tracking CPU usage, we can identify processes consuming excessive resources, detect performance bottlenecks, and make informed decisions to optimize system performance. In this section we explores various methods and commands to check CPU usage in Linux, providing insights into system health and facilitating effective performance management.

Top

The top command in Linux is a powerful utility for monitoring system resources, including CPU usage. When you run the top command in a terminal, it displays a real-time view of processes, resource usage, and system information.

[root@nayanatest ~]# top
  • At the top of the top output, you’ll see general system information such as uptime, number of users, load averages, and CPU usage summary.
  • Load averages represent the average number of processes waiting for CPU time over different time intervals (1, 5, and 15 minutes).
  • CPU usage summary shows the overall CPU usage percentage and breakdown by user processes, system processes, and idle time.

Below the system information, you’ll find a list of running processes sorted by various criteria (default is CPU usage).

htop

htop is a powerful interactive process viewer and system monitor for Linux systems. Moreover it provides a user-friendly and visually appealing interface for monitoring system resources, including CPU usage, memory usage, and process information.

apt install htop // On Debian/Ubuntu-based systems
yum install htop // On CentOS/RHEL-based systems

Once installation is completed:

[root@nayanatest ~]# htop

At the top of the htop interface, you’ll see system information such as uptime, load average, and CPU usage summary.

The main section of htop displays a list of running processes with detailed information.

  • The processes are color-coded to indicate their status and resource usage.
  • CPU usage is displayed as a percentage for each process.
  • Memory usage (RAM) is also shown for each process.
  • You can navigate through the process list using the arrow keys (up/down).

Interacting with htop

Arrow keys to navigate through the process list.
 F2 or Senter Setup mode, where you can customize htop settings such as colors, columns displayed, and update frequency.
F3 or /to search/filter processes by name.
F4 or ^Uto filter processes by user.
F5 or tto switch between different CPU time display modes.
F6 or ^Tto change the sorting order of processes.
F9 or kto send a signal (e.g., terminate) to a selected process.
F10 or qto quit htop and return to the terminal.

htop provides a more interactive and detailed view compared to the standard top command. It’s particularly useful for system administrators and users who want to monitor and manage system resources in real-time with a user-friendly interface.

Vmstat

The vmstat command in Linux is a versatile tool used to display system resource usage statistics, including information about virtual memory, CPU usage, I/O (input/output) operations, and system processes. Here’s how to use vmstat and understand its output. By default, vmstat displays a summary of system statistics since the last reboot.

The output includes information about processes, memory, swap usage, CPU usage, and I/O activity.

[root@nayanatest ~]# vmstat
Procs
rThe number of processes waiting for CPU time (runnable).
bThe number of processes in uninterruptible sleep state.
Memory
swpdAmount of virtual memory used (in kilobytes).
free Amount of free memory (in kilobytes).
buffAmount of memory used for buffers (in kilobytes).
cacheAmount of memory used for cache (in kilobytes).
Swap
si Amount of memory swapped in from disk (in kilobytes per second).
so Amount of memory swapped out to disk (in kilobytes per second).
CPU
usPercentage of CPU time spent in user-space processes.
syPercentage of CPU time spent in system (kernel) processes.
idPercentage of CPU time spent idle.
waPercentage of CPU time spent waiting for I/O operations to complete.
stPercentage of CPU time stolen from a virtual machine (if applicable).
IO
biBlocks received from block devices (in blocks per second).
boBlocks sent to block devices (in blocks per second).

Examples of vmstat Usage

  • vmstat 2 5: Displays system statistics every 2 seconds, 5 times.
  • vmstat -s: Displays a summary of virtual memory statistics.

Iostat

The iostat command in Linux is used to monitor system input/output (I/O) statistics for devices such as disks and partitions. It provides valuable information about disk utilization, I/O operations, throughput, and response times. Here’s how to use iostat and interpret its output:

The iostat command is part of the sysstat package. To install –

apt install sysstat //On Debian/Ubuntu-based systems
yum install sysstat // On CentOS/RHEL-based systems
[root@nayanatest ~]# iostat

By default, iostat displays a summary of CPU and disk I/O statistics since the last reboot.The output includes information about CPU utilization and I/O activity for each disk device.

Customizing iostat Output:

  • You can specify options with iostat to customize the output or focus on specific aspects of I/O statistics.
  • For example, iostat -x displays extended statistics, iostat -d focuses on disk utilization, and iostat -c provides CPU statistics along with I/O data.

iostat is a valuable tool for system administrators to monitor disk performance, identify I/O bottlenecks, and optimize disk usage for better overall system performance in Linux systems.

mpstat

The mpstat command in Linux is used to report CPU statistics, especially in multi-processor systems. It can display utilization details for each individual CPU, which is valuable for identifying CPU bottlenecks and understanding the load distribution across different CPUs.

The mpstat command is part of the sysstat package. If it’s not already installed, you can install it . Once installed,

[root@nayanatest ~]# mpstat

This command provides a summary of CPU utilization.

mpstat -P ALL

[root@nayanatest ~]# mpstat -P ALL

This command displays statistics for all individual CPUs.

mpstat 2 5

[root@nayanatest ~]# mpstat 2 5

This command will display CPU statistics every 2 seconds, for a total of 5 times.

How to troubleshoot CPU loadwarning issues

-> Troubleshooting CPU load warning issues in Linux involves a systematic approach to identify and mitigate the causes of high CPU usage. 

-> Start by monitoring the CPU usage to understand which processes are consuming the most resources.

-> Determine if a specific process, application, or service is causing the high CPU usage.

-> Once you identify high CPU usage processes, investigate why they are consuming so many resources.

-> Manage processes to reduce CPU load. Kill unwanted process and restart problematic process

-> Can use  sar (System Activity Reporter) tool for system performance monitoring and analysis. It collects, reports, and analyzes system activity data, including CPU utilization, memory usage, disk activity, network traffic, and more. By using sar, we can identify performance bottlenecks, track resource usage trends, and optimize system configuration for better overall performance and reliability.

How to optimize your Linux system for better CPU performance

Tuning Kernel

Tuning kernel parameters is an important aspect of optimizing a Linux system. Kernel parameters can be adjusted to optimize performance for specific workloads, enhance security, and improve system stability. Using the sysctl command, we can modify these parameters temporarily or permanently.

Manage Services and Processes

Managing services and processes is crucial for optimizing a Linux system’s performance. Start by disabling unnecessary services to free up system resources; this can be done using `systemctl` to list and disable services. Regularly review running processes with commands like `top`, `htop`, or `ps` to identify resource-intensive tasks. Prioritize critical processes using `nice` and `renice` to adjust their scheduling priority . For more granular control, use `systemd` to manage service startup behavior, ensuring essential services start in the correct order and unnecessary ones are excluded. Additionally, clean up unused packages with `sudo apt autoremove` or `sudo yum autoremove` to minimize background resource consumption. This proactive management of services and processes helps maintain a streamlined, efficient Linux environment, enhancing overall system performance and stability.

Effcient software

Choosing efficient software is a critical aspect of optimizing a Linux system for performance. Efficient software is designed to use system resources judiciously, perform tasks quickly, and integrate well with other system components. Here are some key points and examples of efficient software choices for various tasks on a Linux system:

Consider hardware upgrades

Hardware upgrades can significantly enhance the performance and efficiency of a Linux system, making it capable of handling more demanding tasks and improving overall responsiveness. Key upgrades include increasing the system’s RAM, which allows for better multitasking and faster application performance. Additionally, for tasks involving graphics, upgrading the GPU can lead to smoother rendering and faster processing of visual data. Ensuring that your hardware components are compatible with your system and properly configured is essential to fully benefit from these upgrades.

Conclusion

Monitoring and optimizing CPU usage in Linux is essential for maintaining system performance and troubleshooting issues effectively. By using the provided commands and strategies, administrators can proactively manage CPU resources and ensure optimal system operation.

Scroll to Top