How to Calculate CPU Utilization in Linux?

The CPU (Central Processing Unit) is the heart of a computer, responsible for carrying out instructions from the operating system and applications. CPU utilization is a crucial metric for system administrators to monitor the performance of their Linux servers. In this article, we will discuss how to calculate CPU utilization in Linux.

Understanding CPU Utilization

Before we dive into the calculation methods, it's essential to understand what CPU utilization means. CPU utilization is the percentage of time that the CPU is busy performing tasks compared to the total time. The CPU can be busy executing user processes, system processes, or idle. If the CPU utilization is high, it indicates that the system is working hard, and if it is low, the system is idle.

Measuring CPU Utilization

There are several tools available in Linux to measure CPU utilization. Some of the commonly used ones are:

  • top: The top command is a basic utility that shows system statistics and the processes that are running. It provides a real-time view of the system's performance and shows the CPU utilization in percentage.
  • mpstat: The mpstat command is a part of the sysstat package, which provides system statistics for Linux systems. The mpstat command displays the CPU utilization for each processor, including the average.
  • sar: The sar command is another utility that is part of the sysstat package. It collects and reports system activity information, including CPU utilization.

Calculating CPU Utilization

There are different methods to calculate CPU utilization in Linux. We will discuss some of the commonly used ones.

Using top command:

To use the top command to calculate CPU utilization, follow these steps:

  • Step 1: Open a terminal and type the top command.
  • Step 2: Look for the line that says "%Cpu(s)" at the top of the screen.
  • Step 3: The line will show the CPU utilization percentage for each core or processor.

The output of the top command looks like this:

top - 10:40:12 up 25 days, 4:10, 1 user, load average: 0.00, 0.01, 0.05

Tasks: 235 total, 1 running, 234 sleeping, 0 stopped, 0 zombie

%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

KiB Mem : 16119412 total, 3596528 free, 9218740 used, 3308144 buff/cache

KiB Swap: 16676860 total, 16545740 free, 131120 used. 5871764 avail Mem

The "%Cpu(s)" line shows that 0.3% of the CPU is being used for user processes, 0.3% is being used for system processes, and 99.3% is idle.

Using mpstat command:

To use the mpstat command to calculate CPU utilization, follow these steps:

  • Step 1: Open a terminal and type the mpstat command.
  • Step 2: Look for the line that says "%idle" in the output.
  • Step 3: Subtract the value of "%idle" from 100 to get the CPU utilization percentage.

The output of the mpstat command looks like this:

Linux 5.10.0-0.bpo.5-amd64 (hostname) 02/22/2023 x86_64 (1 CPU)

10:40:12 PM CPU %user %

0 all 0.01 0.00

1 all 0.00 0.00

2 all 0.00 0.00

3 all 0.00 0.00

4 all 0.00 0.00

5 all 0.00 0.00

6 all 0.00 0.00

7 all 0.00 0.00

8 all 0.00 0.00

9 all 0.00 0.00

The "%user" column shows the percentage of CPU time spent in user processes. The "%" column shows the percentage of time spent in the respective state. In the above output, the "%user" column shows 0.01%, indicating that only 0.01% of CPU time is being used for user processes.

Using sar command:

To use the sar command to calculate CPU utilization, follow these steps:

  • Step 1: Open a terminal and type the sar command.
  • Step 2: Look for the line that says "%user" in the output.
  • Step 3: Subtract the value of "%idle" from 100 to get the CPU utilization percentage.

The output of the sar command looks like this:

Linux 5.10.0-0.bpo.5-amd64 (hostname) 02/22/2023 x86_64 (1 CPU)

10:40:12 PM CPU %user %nice %system %iowait %steal %idle

10:50:12 PM all 0.01 0.00 0.01 0.00 0.00 99.98

The "%user" column shows the percentage of CPU time spent in user processes. The "%system" column shows the percentage of CPU time spent in system processes. The "%idle" column shows the percentage of CPU time that is idle. In the above output, the "%user" column shows 0.01%, indicating that only 0.01% of CPU time is being used for user processes.

Conclusion

CPU utilization is an important metric to monitor the performance of a Linux system. There are different tools available to measure CPU utilization, and several methods to calculate it. The top, mpstat, and sar commands are commonly used to measure CPU utilization in Linux. By using the steps mentioned in this article, system administrators can calculate CPU utilization and take appropriate actions to optimize the performance of their systems.

Post a Comment

Previous Post Next Post

Contact Form