DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · INTERNALS · 20 OF 187

Linux Load Average and CPU Scheduling Explained

What load average really measures and how to read it against the number of CPUs, watched live: four busy loops on a 2-CPU machine pushed the 1-minute load from 0.38 to 2.67 in 60 seconds while vmstat showed 4 to 5 runnable tasks and 99% user CPU. Plus CPU time types (us, sy, wa, st), the scheduler and nice, and how ShopRung's p95 latency went from 1.6 ms to 13 ms under CPU contention.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Load = runnable + D-state tasks
  • compare with nproc
  • us sy wa st
  • fair scheduler, nice
  • throttling
  • p95 1.6 → 13 ms

Notes

Load average

The load average counts tasks that are running or ready to run, averaged over one, five and fifteen minutes. On Linux it also counts tasks stuck waiting on disk, in state D, which surprises many people. Our machine was quiet: point three eight, point one eight, point zero seven. The key is to compare load with the number of CPUs. A load of two on a two CPU machine means fully busy; on a sixteen CPU machine it means nearly idle.

/proc/loadavg
0.38 0.18 0.07 1/104 1166

1, 5 and 15 minute averages
of tasks running or ready
  (R) plus, on Linux, tasks
  waiting on disk (D)
1/104: running now / total
compare to CPU count: nproc

Overload it

Let's overload it. We started four endless loops on our two CPU machine and sampled the load every fifteen seconds. One minute load climbed from point three eight to one point one eight, one point eight one, two point two nine and two point six seven, heading towards four, the number of tasks that wanted a CPU. The five and fifteen minute numbers rose much more slowly. That lag is why you read all three: rising means getting worse, falling means recovering.

Our run: load every 15 s
start   0.38 0.18 0.07
t=15 s  1.18 0.37 0.13
t=30 s  1.81 0.55 0.19
t=45 s  2.29 0.72 0.26
t=60 s  2.67 0.88 0.32

climbing toward 4: four tasks
  wanting 2 CPUs

Kinds of CPU time

vmstat shows where CPU time goes. Under our load, r, the number of runnable tasks, was four, and ninety nine percent of the time was us, user code: our loops. sy is time in the kernel, from system calls and I O. id is idle. wa means CPUs idle while waiting on disk, a storage problem, not a CPU one. And st, steal, is time the hypervisor gave to other virtual machines; high steal in the cloud means a noisy neighbour or an undersized instance.

vmstat 1 (our run, loaded)
r  b  ... us sy id wa st
4  0  ... 99  0  0  0  1

r   runnable tasks
us  your programs
sy  the kernel (syscalls, I/O)
id  idle   wa  waiting on disk
st  stolen by the hypervisor

The scheduler

The scheduler decides which task runs on each CPU, switching thousands of times a second so everything makes progress. By default it is fair: equal shares for equal priority. You can change a task's share with nice. Nineteen is the lowest priority, negative values are higher and need root. Containers use cgroup CPU shares and quotas, and a container that hits its quota is throttled, paused until the next period, which shows up as sudden latency.

How Linux shares CPUs
  • each CPU runs one task at a time, switching thousands of times a second
  • fair scheduling: equal shares by default
  • nice -n 19: lowest priority
  • nice -n -5: higher (root)
  • containers: cgroup CPU shares and quotas (throttling)

What users feel

What do users feel? We timed thirty requests to ShopRung's products API. Calm: median under one millisecond, ninety fifth percentile one point six. Then we started six busy loops on the two CPUs. ShopRung now had to wait its turn: median six point five milliseconds, ninety fifth percentile thirteen, about eight times slower. When we reniced the loops to nineteen, the lowest priority, ShopRung got the CPU first again: median point six, ninety fifth four point five.

30 requests each (our runs)
calm               p50 0.9 ms
                   p95 1.6 ms
6 busy loops on
  2 CPUs           p50 6.5 ms
                   p95 13.0 ms
loops reniced     p50 0.6 ms
  to nice 19       p95 4.5 ms

Find the hog

To find what is using the CPU, sort processes by CPU. ps with p c p u sorted descending showed our shell loops at the top, about thirty two percent each, six of them sharing two CPUs. top does the same live; press capital P to sort by CPU. pidstat from the sysstat package prints per-process usage every second, which is easier to read in a ticket. Then decide: is it expected work, a runaway job, or a bug?

Our run
$ ps -eo pid,pcpu,comm
    --sort=-pcpu | head
 PID  %CPU COMMAND
1862  31.9 sh
1858  31.8 sh
1860  31.8 sh
$ top        live, press P
$ pidstat 1  per process

High load, low CPU

Read the numbers together. High load with high user CPU means CPU bound work: find the hog, scale out, or optimise. High load with low CPU but high I O wait means processes stuck in D state waiting on disk or network storage, so look at iostat, not at the code. High steal means the cloud host is contended. And if one core sits at a hundred percent while others idle, the program is single-threaded, so adding CPUs will not help.

Read them together
  • load high, us high: CPU-bound: find the hog, scale out, optimise
  • load high, us low, wa high: waiting on disk or NFS (D state): check iostat
  • st high: cloud host contention
  • one core 100%: single-threaded

Common mistakes

Mistakes. Judging load without knowing the CPU count. Assuming high load always means CPU, when it can be disk waits. Averages that hide one maxed-out core. Ignoring steal time on cloud machines. CPU limits so tight that containers are constantly throttled. And killing a useful batch job when renicing it would have protected users.

Avoid these
  • judging load without nproc
  • "load 10 = CPU problem" (could be disk waits)
  • averaging away one hot core
  • ignoring steal on cloud VMs
  • tight container CPU limits causing throttling
  • killing instead of renicing batch jobs

Say it like this

In the interview: it depends on the number of CPUs. On two CPUs a load of eight is heavy overload; on thirty two it is fine. I check the trend across the one, five and fifteen minute values, then vmstat: high user time means CPU-bound, high I O wait means tasks stuck on disk, high steal means the cloud host. Then top or ps sorted by CPU finds the process, and nice or limits protect the important one.

In the interview
  • "Depends on cores: on 2 CPUs
  • it is overloaded, on 32 it is fine. I check the trend across 1, 5, 15, then vmstat: high us
  • means CPU-bound, high wa means
  • D-state disk waits, high st
  • means the host. Then top or ps
  • by %CPU to find the process."

Interview questions

Q1What does the load average measure?

The average number of tasks running or waiting to run, plus (on Linux) tasks in uninterruptible sleep, over 1, 5 and 15 minutes.

Q2Is a load average of 4 high?

Compare with the CPU count (nproc): 4 on 2 CPUs means overloaded, 4 on 16 CPUs is light.

Q3What do us, sy, wa and st mean in top/vmstat?

User code, kernel time, idle while waiting for I/O, and time stolen by the hypervisor for other VMs.

Q4High load but low CPU usage: why?

Tasks are probably in D state waiting on disk or network storage; check iostat, vmstat wa and ps for D-state processes.

Q5What does nice do?

Changes a process's scheduling priority: 19 is lowest, negative values (root only) are higher; renice changes a running process.

Q6What is CPU throttling in containers?

When a cgroup uses up its CPU quota for a period it is paused until the next period, causing latency spikes; visible in cgroup cpu.stat.

Q7What is CPU steal time?

Time a virtual CPU wanted to run but the hypervisor ran something else; high steal suggests a contended host or an undersized burstable instance.

Q8How do you find the process using the most CPU?

top (sort by CPU), ps -eo pid,pcpu,comm --sort=-pcpu | head, or pidstat 1.