Optimizing CPU Performance in Linux: A Comprehensive Guide
Introduction
Understanding and optimizing CPU performance is crucial for maintaining the efficiency of Linux systems. This guide will provide insights into basic commands and methodologies to analyze and optimize CPU performance.
Using the top Command to Analyze CPU Load
One of the most fundamental tools for monitoring system performance in Linux is the top command. Here’s an updated example of the top command output:
top - 18:07:42 up 5 days, 18:20, 2 users, load average: 0.65, 0.35, 0.23 Tasks: 197 total, 3 running, 194 sleeping, 0 stopped, 0 zombie %Cpu(s): 37.0 us, 3.7 sy, 0.0 ni, 58.1 id, 0.0 wa, 0.0 hi, 1.2 si, 0.0 st MiB Mem : 7649.3 total, 1286.3 free, 2084.5 used, 4278.5 buff/cache MiB Swap: 4096.0 total, 4095.7 free, 0.3 used. 5160.2 avail Mem
One important metric to consider is the load average, which represents the average system load over the last 1, 5, and 15 minutes. Here, the values indicate a moderate workload on the system.
Interpreting Load Averages:
- The first value shows the average CPU load over the last minute (0.65).
- The second value represents the average load over the last 5 minutes (0.35).
- The third value indicates the average load over the last 15 minutes (0.23).
Assessing CPU Allocation with lscpu
Next, it’s important to check how many CPUs are allocated to understand the load in relation to available resources:
lscpu | grep CPU CPU op-mode(s): 32-bit, 64-bit CPU(s): 2 On-line CPU(s) list: 0,1
If the load average approaches or exceeds the number of CPUs, which in this case is 2, the server may experience slowness or lag. Here we see the load average is 0.65 which is still lower than 2. So, the server is not busy yet.
Analyzing CPU Utilization
Analyze the following section of the top command output to understand why the system might be experiencing increased load:
%Cpu(s): 37.0 us, 3.7 sy, 0.0 ni, 58.1 id, 0.0 wa, 0.0 hi, 1.2 si, 0.0 st
With the CPU showing 37% usage by user processes and 58.1% idle, it suggests that while there is moderate activity, there is still substantial idle capacity available for additional loads. However, if kernel space usage is typically over 60%, this could indicate I/O or other system-related issues that result in system call slowness, like operations that run from kernel space leading to CPU overload. In our case, the kernel space usage at 3.7% (identified by 3.7 sy) does not suggest such issues. For instance, when a user runs a command like rm, the system call unlink is executed in kernel space, which can impact performance if frequently used in a heavy I/O scenario.
Identifying Resource-Intensive Processes
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 51420 apache 20 0 397908 71520 17504 S 8.6 0.9 6:31.21 php-fpm 51421 apache 20 0 398128 73736 19496 S 8.6 0.9 6:33.04 php-fpm ... (additional processes)
Processes such as php-fpm run by the apache user are significantly consuming CPU resources. This could be a focal point for performance optimization, especially if these processes consistently show high CPU usage.
Other than the top command you can check the top CPU-intensive processes by the following command
sudo ps -eo pid,user,ppid,cmd,%mem,%cpu --sort=-%cpu | head PID USER PPID CMD %MEM %CPU 50383 root 50381 /usr/libexec/oracle-cloud-a 3.8 0.7 52386 mysql 1 /usr/libexec/mysqld --based 6.9 0.2 1 root 0 /usr/lib/systemd/systemd -- 0.2 0.0 ..........................................................