聊聊 Linux Load Average

每当系统变慢或者有人抱怨机器负载高时,我们通常都会使用 top 或者 uptime 来看看 Load Average 的值:

1
2
# top 结果
top - 11:00:52 up 13 days, 20:20, 2 users, load average: 0.44, 0.27, 0.20

其中最后三个数字 0.44, 0.27, 0.20 分别表示过去1分钟、5分钟和15分钟的平均负载(Load Average)。

那么,今天我们就来聊聊这个平均负载。

对于平均负载,可能第一反应是单位时间内 CPU 的使用率,比如上面的近一分钟平均负载为0.44则表示过去一分钟的 CPU 平均使用率为 44%。然而,事实并不是这样,我们通过 man 来看下 uptime 中关于 Load Average 的解释:

System load averages is the average number of processes that are either in a
runnable or uninterruptable state. A process in a runnable state is either
using the CPU or waiting to use the CPU. A process in uninterruptable state
is waiting for some I/O access, eg waiting for disk. The averages are taken
over the three time intervals. Load averages are not normalized for the num‐
ber of CPUs in a system, so a load average of 1 means a single CPU system is
loaded all the time while on a 4 CPU system it means it was idle 75% of the
time.

简单来说,平均负载是指单位时间内,系统处于可运行状态不可中断状态的平均进程数,也就是平均活跃进程数,它和 CPU 使用率并没有直接关系。