Linux CPU Utilization - How To Check Linux CPU Usage

Published March 18, 2024

CPU utilization is a crucial metric for measuring system performance and identifying potential bottlenecks in Linux systems. This article explores the concept of CPU utilization, factors contributing to high CPU usage, and various command-line tools and graphical utilities for monitoring and troubleshooting CPU utilization in Linux environments.

Understanding Linux CPU Utilization

What is CPU Utilization?

CPU utilization refers to the amount of time the processor spends processing instructions and doing tasks. It is a key metric for measuring system performance and finding potential bottlenecks. In Linux, CPU time is given to processes based on their priorities and the available system resources. Each process is given a share of the CPU's processing power, and the operating system handles the scheduling and running of these processes.

Factors Contributing to High CPU Utilization

High CPU utilization can be caused by different factors, impacting system performance and responsiveness. Some common causes include:
  1. Intensive processes: CPU-intensive tasks, such as video encoding, scientific simulations, or complex calculations, can use a large portion of the CPU's resources, leading to high utilization.

  2. Insufficient resources: When a system lacks enough processor cores or memory to handle the workload efficiently, processes may compete for limited resources, resulting in high CPU usage.

  3. Background tasks: Services, daemons, and background processes running on the system can contribute to high average cpu usage, even if they are not actively used by the user.

  4. Inefficient applications: Poorly optimized or buggy applications can use too many CPU cycles, causing high utilization and impacting overall system performance.

Finding and fixing the root causes of high CPU utilization is important for keeping optimal system performance and ensuring a smooth user experience.

Understanding Linux CPU Load Averages

CPU load averages provide a measure of the system's workload over a period of time. In Linux, load averages are represented by three numbers, usually shown as "1-minute, 5-minute, 15-minute" averages. These numbers indicate the average number of processes that are in a runnable or uninterruptible state during the respective time intervals.

For example, a load average of "1.0" means that, on average, one process was ready to run or waiting for disk I/O during the specified time period. A load average higher than the number of CPU cores suggests that processes are competing for CPU time, and the system may be overloaded.

It's important to note that load averages alone do not provide a complete picture of CPU utilization. They are a measure of system load, which includes CPU usage, I/O wait, and other factors. To get a more accurate understanding of CPU utilization, it's recommended to use tools that provide detailed CPU usage statistics, such as top, htop, or mpstat.

Checking Linux CPU Usage with Command-Line Tools

Using the "top" Command

The "top" command is a useful tool for checking system resources in real-time, including processor utilization. It shows a dynamic view of the running processes and their effect on the system. To use the "top" command, open a terminal and type `top`.

The output of the "top" command shows various system statistics, with the current CPU usage information in the top-right corner. Here's an example:

top - 10:00:00 up 2 days, 15:30,  1 user,  load average: 0.50, 0.75, 0.90
Tasks: 100 total,   2 running,  98 sleeping,   0 stopped,   0 zombie
%Cpu(s): 10.0 us,  5.0 sy,  0.0 ni, 85.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7974.2 total,   4515.4 free,   1678.3 used,   1780.5 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   5921.1 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  1234 john      20   0 1500000 300000  25000 S  25.0   3.7   1:30.00 stress-ng
  5678 jane      20   0  800000 150000  10000 S  10.0   1.8   0:45.00 video_encode

In this example, the "%Cpu(s)" line shows the CPU usage breakdown:

  • "us" (user): CPU time spent running user processes
  • "sy" (system): CPU time spent running system processes
  • "ni" (nice): CPU time spent running niced (prioritized) processes
  • "id" (idle): CPU time spent idle
  • "wa" (I/O wait): CPU time spent waiting for I/O operations

The top command also shows a list of running processes sorted by their CPU usage. This helps identify which processes are using the most CPU resources.

Using the "htop" Command

The "htop" command is an interactive process viewer and manager that provides a user-friendly interface for checking system resources, including CPU utilization. To use "htop", you may need to install it first using your distribution's package manager, for example:
sudo apt install htop        # For Debian/Ubuntu
sudo dnf install htop        # For Fedora

Once installed, run htop in the terminal to start the interface:

htop

The "htop" interface shows real-time system statistics, including processor usage per core and a list of running processes. It uses color-coding and visual graphs to show CPU utilization, making it simple to understand the information.

To check CPU usage with "htop":

  1. Look at the CPU meter at the top of the interface, which shows the overall CPU usage and per-core utilization.
  2. Check the list of processes and their individual CPU usage percentage in the "%CPU" column.
  3. Use the arrow keys to move and the "F1" key to access help and more options.

The "htop" command provides an easy way to check CPU usage and manage processes interactively.

Checking CPU Usage with "mpstat"

The "mpstat" command is part of the [sysstat](https://github.com/sysstat/sysstat) package and lets you check CPU utilization in detail. To use "mpstat", install the sysstat package if it's not already available:
sudo apt install sysstat     # For Debian/Ubuntu
sudo dnf install sysstat     # For Fedora

To check CPU usage with "mpstat", use the following command:

mpstat

This will show CPU usage statistics, including the percentage of time spent in user mode, system mode, idle, and I/O wait.

To get more detailed information, you can set the interval and count. For example, to show CPU usage every 2 seconds for a total of 5 times:

mpstat 2 5

The mpstat command also lets you view per-core CPU utilization by using the -P option followed by the core number or ALL for all cores:

mpstat -P ALL

This will show CPU usage statistics for each individual core, providing a more detailed view of CPU utilization.

Checking CPU Utilization with "vmstat"

The "vmstat" command is a useful tool that provides information about average cpu usage, memory utilization, and processes. To use "vmstat", simply run:
vmstat

This command will show a summary of system resource usage, including information about CPU activity. The CPU utilization information is shown in the last few columns:

  • "us" (user): Percentage of CPU time spent running user processes
  • "sy" (system): Percentage of CPU time spent running system processes
  • "id" (idle): Percentage of CPU idle time
  • "wa" (I/O wait): Percentage of CPU time spent waiting for I/O operations

To get real-time updates of CPU utilization, you can set the interval and count. For example, to show CPU usage every 2 seconds for a total of 5 times:

vmstat 2 5

The "vmstat" command provides a brief overview of system resource usage, including CPU utilization, letting you quickly check the overall system performance.

Using the "sar" Command for Historical Analysis

The "sar" command (System Activity Reporter) is a useful tool for collecting and analyzing system performance data, including CPU utilization over time. It is part of the sysstat package. To use "sar", install the sysstat package if it's not already available.

To check CPU usage with "sar", use the following command:

sar -u

This will show CPU usage statistics, including the percentage of time spent in user mode, system mode, idle, and I/O wait.

To view CPU usage data for a specific time interval, use the following syntax:

sar -u -s <start_time> -e <end_time>

Replace <start_time> and <end_time> with the desired start and end times in the format "HH:MM:SS".

The sar command also lets you create reports for a specific time period. For example, to create a report of CPU usage for the past 24 hours with 1-hour intervals:

sar -u -s 00:00:00 -e 23:59:59 -i 3600

The created report provides a historical view of CPU utilization, letting you analyze trends and patterns over time.

The "sar" command offers many options for collecting and analyzing system performance data. See the sar man page for more advanced usage and options.

Graphical Tools for Monitoring Linux CPU Performance

Glances: A System Monitoring Tool

Glances is a command-line tool that provides an overview of system resources, including processor utilization, in a visually appealing interface. It offers real-time monitoring and displays important information in a compact and easy-to-read format.

To install Glances on your Linux system, you can use the package manager for your distribution. For example, on Debian or Ubuntu-based systems, run:

sudo apt install glances

On Fedora, CentOS, or RHEL-based systems, use:

sudo dnf install glances

Once Glances is installed, you can launch it by running the command:

glances

The Glances interface will open, providing a real-time summary of various system resources, including CPU utilization.

In the CPU section of the Glances output, you can view the overall CPU utilization percentage and a breakdown of CPU usage per core. The CPU usage is shown by color-coded bars, making it easy to identify the level of utilization.

Glances also provides extra information, such as the number of running processes, system load averages, and top CPU-consuming processes. This view helps you quickly identify any potential issues or resource-intensive tasks.

Glances offers many configuration options and can be customized to suit your specific monitoring needs. It also supports various output modes, including web-based interfaces and exporting data to external monitoring systems.

Other Graphical Monitoring Tools

In addition to Glances, there are several other graphical tools available for monitoring processor usage on Linux systems. These tools provide intuitive interfaces and visual representations of CPU utilization, making it easier to track and analyze resource usage. Some popular graphical monitoring tools include:
  1. GNOME System Monitor: This is the default system monitoring tool for the GNOME desktop environment. It provides a graphical view of CPU usage, along with other system resources like memory and disk usage. GNOME System Monitor offers real-time monitoring and allows you to view CPU usage per process, making it convenient to identify resource-intensive applications.

  2. KDE System Monitor (KSysGuard): KDE System Monitor is the equivalent monitoring tool for the KDE Plasma desktop environment. It offers similar features to GNOME System Monitor, providing a graphical representation of CPU utilization and allowing you to monitor individual processes. KDE System Monitor also includes advanced features like setting up alerts and creating custom monitoring sensors.

  3. Conky: Conky is a customizable system monitor that can be configured to display various system information, including CPU usage, on the desktop. It provides a lightweight and flexible way to monitor CPU utilization in real-time. Conky allows you to create custom scripts and configurations to tailor the displayed information to your specific needs.

  4. Xfce Task Manager: For users of the Xfce desktop environment, the Xfce Task Manager is a simple and straightforward tool for monitoring system resources, including CPU usage. It provides a clean interface that displays CPU utilization percentages and allows you to manage running processes.

These graphical monitoring tools offer a user-friendly way to keep track processor usage on Linux systems, especially for users who prefer a visual representation of system resource utilization. They complement the command-line tools and provide an alternative approach to monitoring CPU performance on Linux desktops and servers with graphical environments.

Troubleshooting High CPU Usage on Linux Servers

Identifying CPU-Intensive Processes

When troubleshooting high CPU usage on Linux servers, the first step is to identify which processes are using the most CPU resources. Two common commands for this purpose are `top` and `ps`.

The top command shows a real-time view of the system's processes and their resource usage. To identify CPU-intensive processes using top, follow these steps:

  1. Open a terminal and run the top command.
  2. Look for processes with high values in the "%CPU" column, which shows the percentage of CPU time used by each process.
  3. Note processes that consistently appear at the top of the list with high CPU usage.

For example:

top - 10:30:00 up 2 days, 15:00,  1 user,  load average: 1.50, 1.75, 1.90
Tasks: 100 total,   3 running,  97 sleeping,   0 stopped,   0 zombie
%Cpu(s): 25.0 us, 15.0 sy,  0.0 ni, 60.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7974.2 total,   4515.4 free,   1678.3 used,   1780.5 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   5921.1 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  1234 john      20   0 1500000 300000  25000 R  20.0   3.7   1:30.00 stress-ng
  5678 jane      20   0  800000 150000  10000 R  15.0   1.8   0:45.00 video_encode

In this example, the stress-ng and video_encode processes are using significant CPU resources.

Another useful command is ps, which provides a snapshot of the current processes. To identify CPU-intensive processes using ps, use the following command:

ps aux --sort=-%cpu | head

This command lists all processes sorted by CPU usage in descending order and shows the top 10 CPU-consuming processes.

For example:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
john      1234 20.0  3.7 1500000 300000 ?       R    May01 01:30:00 stress-ng
jane      5678 15.0  1.8 800000 150000 ?        R    May01 00:45:00 video_encode

By using top and ps, you can quickly identify the processes causing high CPU usage on your Linux server.

Techniques for Reducing High CPU Utilization

Once you have identified the CPU-intensive processes, you can use various techniques to reduce high CPU utilization and manage the workload efficiently. Here are some strategies and best practices:
  1. Process optimization:

    • Review the identified CPU-intensive processes and analyze their resource needs.
    • Optimize the processes by adjusting their configuration, thread counts, or using more efficient algorithms.
    • Consider using caching or optimizing database queries to reduce CPU usage.
  2. Resource allocation:

    • Make sure the system has enough CPU cores and memory to handle the workload.
    • Use process priority and niceness to assign appropriate priorities to important processes.
    • Use Linux cgroups (control groups) to limit the CPU usage of specific processes or groups of processes.
  3. Load balancing:

    • Spread the workload across multiple servers or CPUs to avoid overloading a single resource.
    • Use load balancing techniques, such as a load balancer or distributed computing frameworks like Apache Hadoop or Apache Spark.
  4. Monitoring and alerting:

    • Set up monitoring systems to continuously track CPU usage and other system metrics.
    • Configure alerts to notify administrators when CPU usage exceeds predefined thresholds.
    • Use tools like Nagios, Zabbix, or Prometheus to monitor and alert on CPU usage.
  5. Scaling and auto-scaling:

    • Implement horizontal scaling by adding more servers to handle increased workload.
    • Use auto-scaling solutions, such as Kubernetes or AWS Auto Scaling, to automatically adjust the number of instances based on CPU usage.
  6. Code optimization:

    • Profile and optimize the application code to identify and fix performance bottlenecks.
    • Use profiling tools like perf, Valgrind, or GDB to analyze code performance and identify areas for optimization.
  7. Caching and resource sharing:

    • Implement caching to store frequently accessed data in memory, reducing CPU usage for repeated operations.
    • Share resources, such as database connections or thread pools, to minimize resource overhead.

Regularly monitor and review CPU usage to identify any new or recurring issues. Continuously optimizing processes, fine-tuning resource allocation, and implementing best practices can help maintain optimal CPU utilization on your Linux servers.

By following these techniques and best practices, you can effectively troubleshoot and manage high CPU usage on Linux servers, ensuring optimal performance and resource utilization.

Monitoring CPU Usage in Containerized Environments

Monitoring CPU Usage of Docker Containers

Monitoring CPU usage in Docker containers is important for making sure containerized applications perform well and use resources efficiently. Docker provides commands that let you monitor the CPU usage of individual containers.

One of the most useful commands is docker stats, which shows real-time resource usage statistics for running containers. To use docker stats, run the following command:

docker stats

This command will show a continuously updated list of containers and their CPU usage percentages, along with other resource metrics like memory usage and network I/O.

For example:

CONTAINER ID   NAME          CPU %     MEM USAGE / LIMIT     MEM %     NET I/O         BLOCK I/O       PIDS
1234abcd       web-server    10.25%    100MiB / 500MiB       20.00%    1.5MB / 800kB   10MB / 0B       25
5678efgh       database      5.50%     200MiB / 1GiB         19.53%    500kB / 1.2MB   50MB / 2MB      10

In this example, the web-server container is using 10.25% of the CPU, while the database container is using 5.50%.

Another useful command is docker top, which shows the processes running inside a specific container. To use docker top, run the following command:

docker top <container-id>

Replace <container-id> with the ID or name of the container you want to inspect. This command will display the processes running within the container, along with their CPU usage.

For example:

PID                 USER                TIME                COMMAND
2345                root                0:05                nginx
3456                www-data            0:03                php-fpm

Here, the nginx and php-fpm processes are running inside the container, and their CPU usage can be monitored.

To manage CPU resources in Docker containers, you can use the --cpus and --cpuset-cpus options when running containers. The --cpus option lets you specify the number of CPUs a container can use, while --cpuset-cpus lets you assign specific CPU cores to a container.

For example, to limit a container to use only 50% of the CPU, you can run:

docker run --cpus=0.5 <image>

By monitoring CPU usage and setting appropriate resource limits, you can make sure containers don't overwhelm the host system and maintain optimal performance.

Monitoring CPU Usage in Kubernetes Clusters

Monitoring CPU usage in Kubernetes clusters is important for making sure resources are used efficiently and identifying performance bottlenecks. Kubernetes provides several tools and mechanisms for monitoring CPU usage at different levels of the cluster.

One of the built-in tools for monitoring CPU usage in Kubernetes is the Metrics Server. The Metrics Server collects resource usage metrics from the Kubernetes nodes and provides an API for querying those metrics. To enable the Metrics Server, you need to deploy it in your cluster.

Once the Metrics Server is deployed, you can use the kubectl top command to view CPU usage for nodes and pods. For example, to view CPU usage for nodes, run:

kubectl top nodes

This command will display the CPU usage and other resource metrics for each node in the cluster.

For example:

NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
node-1         0.5          10%    1000Mi          25%
node-2         0.8          20%    1500Mi          30%

To view CPU usage for pods, run:

kubectl top pods

This command will show the CPU usage and other resource metrics for each pod in the current namespace.

For example:

NAME                     CPU(cores)   MEMORY(bytes)
web-server-5c6f7f7d5     100m         200Mi
database-6b5c79d4b       50m          100Mi

Here, the web-server pod is using 100 millicores (0.1 CPU) and the database pod is using 50 millicores (0.05 CPU).

Another tool for monitoring CPU usage in Kubernetes is the Kubernetes Dashboard. The Kubernetes Dashboard provides a web-based interface for monitoring and managing cluster resources. It displays CPU usage metrics for nodes and pods in a graphical format, making it easier to visualize and analyze resource utilization.

To get more advanced monitoring capabilities, you can use tools like Prometheus and Grafana. Prometheus is a monitoring and alerting system that can collect and store metrics from various sources, including Kubernetes. Grafana is a data visualization platform that lets you create dashboards and charts based on the metrics collected by Prometheus.

By deploying Prometheus and Grafana in your Kubernetes cluster, you can set up monitoring and alerting for CPU usage across pods, nodes, and the entire cluster. Prometheus can get metrics from the Kubernetes API server, as well as from individual pods and nodes using exporters.

With Grafana, you can create custom dashboards to visualize CPU usage metrics, set up alerts based on predefined thresholds, and gain insights into the performance and resource utilization of your Kubernetes cluster.

By using these tools and monitoring capabilities, you can effectively monitor and manage CPU usage in Kubernetes clusters, making sure optimal performance and resource allocation for your containerized applications.

Key Takeaways

  • Use command-line tools like top, htop, mpstat, vmstat, and sar to monitor CPU utilization in real-time and analyze historical data.
  • Identify CPU-intensive processes using top and ps commands to troubleshoot high CPU usage on Linux servers.
  • Employ techniques such as process optimization, resource allocation, load balancing, and scaling to reduce high CPU utilization and manage workload efficiently.
  • Monitor CPU usage in Docker containers using docker stats and docker top commands, and set resource limits with --cpus and --cpuset-cpus options.
  • Utilize Kubernetes tools like Metrics Server, Kubernetes Dashboard, Prometheus, and Grafana to monitor and manage CPU usage in Kubernetes clusters effectively.