1. Introduction

We live in an era where computers are abundant, and along with them come resource-hungry applications like games and productivity tools. However, since most of us have limited resources, it’s important to be mindful of our system’s performance and resource usage. Understanding CPU (Central Processing Unit) load is key to monitoring our systems and ensuring smooth operation by optimizing them.

On Linux systems, there are several methods to measure CPU load. Let’s first learn formally what CPU load is and then explore the most common commands to inspect and view the current CPU usage of a Linux system.

2. What Is CPU Load?

CPU load is a measure of how much processing power our computer’s CPU is using, indicating the percentage of the CPU’s capacity being utilized at a given time. It’s like looking at how busy the CPU is, with higher percentages indicating a heavier workload.

Technically, CPU load represents the average amount of computational work processed by the CPU over a specific period, including active tasks and those waiting in the queue for CPU time. For example, a CPU load of 80% suggests that 80% of the CPU’s capacity is currently in use, leaving only 20% available for additional tasks.

Understanding CPU load can help us assess system performance and identify if the CPU is under strain, indicating potential bottlenecks or the need for upgrades to ensure efficient task handling.

3. top Command

The top command is a real-time system monitoring tool that displays information about processes and resource usage, such as CPU, memory, and swap space. It provides a dynamic and interactive view of the system’s performance.

We can include various options to customize the output or behavior of the command. One of the most important options of the top command that will be helpful to us is the -b, or batch mode. This mode makes the command behave non-interactively. We can also pair this command with the –n option, which specifies the number of iterations the top command runs and prints metrics for until it stops.

Let’s see a practical example of running the top command non-interactively and saving the output in a file for later analysis:

$ top -n 1 -b > top.log
$ cat top.log
top - 23:29:20 up 5 min,  0 users,  load average: 0.00, 0.00, 0.00
Tasks:   5 total,   1 running,   4 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7626.2 total,   7228.9 free,    267.7 used,    129.6 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   7165.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    1 root      20   0    1804   1192   1104 S   0.0   0.0   0:00.01 init
    ...
    9 monsi     20   0   11508   8260   3308 S   0.0   0.1   0:00.06 bash
    ...

As we can see, the output includes details of running processes such as the PID (process ID), user, CPU%, MEM%, and command. As we can see, the terminal we’re running for the example has PID 9, and it is a bash terminal taking up very little CPU and memory.

By default, processes are sorted by CPU usage, with the most resource-intensive processes listed at the top. Most of the information here is self-explanatory. Let’s try to understand the abbreviations in the %Cpu(s) row in detail:

  • us: User space processes
  • sy: System/kernel space processes
  • ni: Processes with a user-defined priority
  • id: Idle CPU time
  • wa: Time spent waiting for I/O operations
  • hi: Hardware interrupts
  • si: Software interrupts
  • st: Time stolen from a virtual machine

So, top can be useful if we need to know and log CPU load information at any point in time.

4. uptime Command

The uptime command displays the system’s current uptime, indicating how long the system has been running since its last reboot. It also provides information about the average system load over the last 1, 5, and 15 minutes. So, this command can be useful for knowing the CPU load history for the past couple of minutes.

Let’s see a practical example of logging the uptime information:

$ uptime
23:49:21 up 25 min,  0 users,  load average: 0.00, 0.00, 0.00

This output indicates that the system has been running for 25 minutes since the last reboot and currently has no users logged in. The load average, displayed as three numbers separated by commas, represents the average system load over the last 1, 5, and 15 minutes, respectively. In this case, all three load average values are 0.00, indicating that the system is not under significant load.

Since the output of uptime is only one line long, we can use this command as a continuous logging solution by leveraging the while loop structure in the shell:

$ while true; do uptime >> uptime.log; sleep 1; done
[pressed q to quit]
$ cat uptime.log
23:50:10 up 26 min,  0 users,  load average: 0.00, 0.00, 0.00
23:50:11 up 26 min,  0 users,  load average: 0.00, 0.00, 0.00
23:50:12 up 26 min,  0 users,  load average: 0.00, 0.00, 0.00
...

The while loop structure used as the first command consists of the condition true, which always evaluates to true, so the loop runs indefinitely.

Inside the loop, uptime >> uptime.log appends the output of the uptime command to the file uptime.log, and sleep 1 pauses execution for 1 second before the loop repeats. Lastly, by printing the uptime.log file, we can see that it is saving the output of the uptime command after each second with the CPU load information.

5. ps Command

The ps command is used to view information about active processes running on the system. It provides detailed insights into process IDs, CPU usage, memory consumption, and other relevant metrics.

Let’s understand the syntax of the ps command that we’ll use to view CPU load:

ps -e -o pid,ppid,%cpu,%mem,args --sort=%cpu

Here, the –e or -A option instructs ps to print information for all the processes. The -o option allows us to use a user-defined format. This we can see in the next part of the command. Here, we’ve defined the format for five key pieces of information:

  • pid: Process IDentifier
  • ppid: Parent Process IDentifier
  • %cpu: Percentage of CPU load by each process
  • %mem: Percentage of memory usage by each process
  • args: Command and its arguments associated with each process

Let’s see an example of the output from this command:

$ ps -eo pid,ppid,%cpu,%mem,args > ps.log
$ cat ps
  PID  PPID %CPU %MEM COMMAND
    1     0  0.0  0.0 /init
    7     1  0.0  0.0 /init
    8     7  0.0  0.0 /init
    9     8  0.0  0.1 -bash
    ...

From the output, we can see that it displays the processes, their pid and ppid information, as well as the CPU load. From this example, again, we can see that the CPU in the example is not pushed that much. We can also see the command names that started those processes. We could also use the method of the previous section and use a while loop to continuously print the CPU load.

6. proc/loadavg File

In Linux, the /proc/loadavg file serves as a valuable resource for monitoring system load. It provides real-time information about the average system load over various time intervals. System administrators and users can utilize this file to measure the system’s performance and identify the CPU load. This file is maintained by the Linux kernel and is updated every 5 seconds.

Let’s print the file to see what its contents are:

$ cat /proc/loadavg
0.00 0.00 0.00 1/177 230

We can see that the file typically consists of five values separated by spaces. This represents the average system load over different time periods.

More specifically, the first three values, 0.00 0.00 0.00, indicate the average system load over the last 1, 5, and 15 minutes, respectively. The fourth value, 1/177, represents the number of currently running processes over the total number of processes. The fifth value, 230, denotes the PID of the most recently created process.

7. Conclusion

In this article, we’ve explored different methods for logging CPU load and other system load details in Linux. Viewing CPU load in Linux isn’t always straightforward, and there are multiple approaches to retrieving and storing this information.

We’ve learned about three Linux commands and one file for fetching CPU load information. These are top, uptimeps, and the proc/loadavg file. And each offers distinct advantages over others.

uptime and proc/loadavg offer basic insights, while ps provides in-depth, process-level resource usage data. The top command sits between the two, providing a blend of fundamental and process-level information. With knowledge of these commands, we’re equipped to effectively monitor and log CPU load on any Linux system.