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.
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.
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.
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.
- 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.
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?
$ 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.
- 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.
- 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.
- "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."