Module 1 · Lesson 68 of 103

Load average and CPU

The learner can apply the USE checklist to CPU and interpret load average, top, mpstat and vmstat to prove whether slowness is compute or I/O.

  • Beginner to advanced
  • 8 min read
  • 5 min lab
What you will need

Ubuntu 24.04 (WSL2 with systemd enabled, VM or container) with sysstat, iotop and stress-ng installed; a Raspberry Pi for the load-test lab

Your place in the module
By the end you can 3 objectives
  • Apply the USE method to CPU, memory, disk and network, and name the saturated resource with evidence.
  • Interpret load average, top, mpstat and vmstat to separate CPU saturation from I/O wait.
  • Explain why a high load average can coexist with an idle CPU
Video coming soonLoad average and CPU
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from rsync, snapshots and safe scheduling
  • rsync -a preserves metadata; --link-dest hard-links unchanged files into cheap snapshots.
  • Lock scheduled jobs with flock; cron can overlap runs, systemd timers do not.
  • A trailing slash on the source copies contents, not the directory; rehearse --delete with --dry-run before trusting it.
01

Why systems get slow, and how to prove it

3 min read

The previous lesson set up scheduled jobs with cron and systemd timers, which can themselves cause slowness at odd hours. This lesson answers why systems get slow and how load average and CPU tools prove the cause.

"The board is slow." That sentence starts most troubleshooting sessions, and it contains almost no information. Slow at what? Since when? Under which load? Your job is to replace the feeling with numbers. A is like the narrowest pipe in a plumbing system: everything upstream of it queues up.

The USE method, popularised by performance engineer Brendan Gregg, gives you three questions per resource. Utilization: how busy is it, as a fraction of its capacity? Saturation: how much work is queued, waiting for it? is often the more honest signal: a disk can look only half busy while requests wait milliseconds in line. Errors: is it reporting failures, such as dropped packets or I/O errors?

Run the checklist over the four resources that matter on almost every Linux machine:

Resource Utilization Saturation Errors
CPU per-core busy time (top, mpstat) tasks waiting for CPU (vmstat r) machine checks in dmesg (rare)
Memory used versus available (free) swap activity (vmstat si/so) out-of-memory kills in the kernel log
Disk busy time (iostat %util) queue depth and await (iostat -x) I/O errors in dmesg
Network throughput versus link speed drops, retransmits, queue backlog ip -s link error counters

You do not need exotic tools. Most of the evidence lives in , which means the same commands work on a server, a container and a tiny board. The sysstat package adds mpstat, pidstat and iostat; install it with sudo apt install sysstat iotop where it is missing.

This lesson also connects to the previous one. The backup job you scheduled with cron or a timer can itself be the culprit: an rsync run or a deep find walk shows up as disk load at 02:30. By the end you will know how to catch it, and how to put a ceiling on it so it never starves the service it shares the board with.

The USE checklistCPUtop, mpstatvmstat r, loaddmesg (rare)Memoryfree -hsi/so, reclaimOOM killsDiskiostat %utilawait, aqu-szdmesg I/O errorsNetworkrate vs linkspeeddrops,retransmitsip -s linkerrorsU = busy, S = queued work, E = errors
The USE checklist: utilization, saturation and errors for CPU, memory, disk and network.
02

Load average: a queue length, not a percentage

4 min read

Start with the command everyone runs first:

bash
uptime
nproc

Typical output (yours will differ):

text
 14:22:03 up 6 days,  3:41,  2 users,  load average: 0.42, 0.35, 0.31

nproc prints the number of processing units available, for example 4. The is the three numbers at the end: one, five and fifteen minute averages. Read them as a trend. If the one-minute value is far above the fifteen-minute value, load is rising right now; if it is far below, the spike is already over.

The rule of thumb: compare load with nproc. On a four-core board, a load of 4 means the CPUs are, on average, fully occupied with no queue. A load of 8 means roughly twice as much work as the CPUs can absorb, so tasks wait. A load of 0.4 on four cores is a quiet machine. Load is a count, so it has no upper bound and it is never a percentage.

1Read the raw source

The same numbers live in a file, plus two extras:

bash
cat /proc/loadavg

Typical output (yours will differ):

text
0.42 0.35 0.31 2/612 4251

The fourth field is 2/612: two currently runnable scheduling entities out of 612 that exist. The fifth is the most recently created process ID. Scripts that poll /proc/loadavg are cheap; the kernel keeps it current.

2Explain high load with an idle CPU

Here is the surprise that catches everyone. Linux counts not only runnable tasks but also tasks in uninterruptible sleep, the D state: a task waiting inside the kernel for something it cannot be interrupted from, most often disk I/O (or an NFS server that stopped answering). A board whose SD card has stalled can show a load of 20 while top reports the CPU almost entirely idle. The tasks are not computing; they are queued on storage.

So high load alone never proves a CPU problem. Pair it with the CPU breakdown in the next section: high load plus high us/sy means compute pressure; high load plus high wa and idle time means I/O pressure.

Going deeperHow the kernel computes load

The code lives in kernel/sched/loadavg.c. About every five seconds the kernel samples the number of runnable plus uninterruptible tasks and folds each sample into three exponentially decaying averages, so recent minutes weigh more than older ones. This is why a spike fades from the one-minute number long before it leaves the fifteen-minute number. Counting D state tasks is a Linux choice; some other Unix systems count only runnable threads, which is why load figures do not compare across operating systems. The decay constants are fixed-point approximations of 1/e for each window, so each update is a multiply and a shift rather than floating-point math.

Which states feed load averageRunning or runnableUninterruptible sleepInterruptible sleepTransitionswait for eventRunning or runnableInterruptible sleepevent arrivesInterruptible sleepRunning or runnableblock on I/ORunning or runnableUninterruptiblesleepI/O completesUninterruptiblesleepRunning or runnableRunning or runnableUninterruptible sleepInterruptible sleepTransitionswait for eventRunning orrunnableInterruptible sleepevent arrivesInterruptible sleepRunning orrunnableblock on I/ORunning orrunnableUninterruptible sleepI/O completesUninterruptible sleepRunning orrunnable
Which task states feed the load average: running, runnable and uninterruptible sleep count; ordinary sleep does not.
03

CPU: top, mpstat, and the run queue

4 min read

Run top and look at the summary line near the top. Typical form (yours will differ):

text
%Cpu(s):  3.1 us,  1.2 sy,  0.0 ni, 95.2 id,  0.4 wa,  0.0 hi,  0.1 si,  0.0 st

Each field is a share of CPU time: us user code, sy kernel code on behalf of processes, ni user code at raised nice values (lower priority), id idle, wa I/O wait, hi hardware interrupt handling, si software interrupt handling (network packet processing often lands here), st steal time, which only appears under a hypervisor and means the host gave your CPU to someone else.

Two readings matter most. High us with low id: computation is the load. High wa: the CPU is idle while I/O is in flight, which points at storage, not compute. Be careful with wa: it is idle time, so a fast CPU that finishes its work and waits shows wa too. It is a hint, and you confirm it with the disk tools below.

1Go per-core

One average can hide a single pegged core. Press 1 inside top to expand one line per CPU. A single-threaded program at 100% on one of four cores shows as 25% in the aggregate line. For a scripted view, use mpstat from sysstat:

bash
mpstat -P ALL 1

It prints one row per CPU per second. Watch the %idle and %iowait columns per core; a lone busy core with idle siblings is a parallelism problem, not a capacity problem.

2Find the process and the queue

Inside top, press P (shift+p) to sort by CPU usage and M to sort by memory. The per-process %CPU is in Irix mode by default: 100% means one whole core, so a four-thread worker can legitimately show 400%. Press I (shift+i) to switch to Solaris mode, which divides by the core count. From a script, ps does the same job:

bash
ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head -6

Saturation shows up in the . vmstat 1 prints it in the r column, with b counting tasks blocked in uninterruptible sleep:

bash
vmstat 1

Typical output (yours will differ; the first row is the average since boot, so judge the rows after it):

text
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 3521440 182332 2051824    0    0    12     4   88  150  3  1 96  0  0
 3  0      0 3521408 182332 2051824    0    0     0     0  620 1180 78  2 20  0  0

On a four-core machine, r sustained above 4 means tasks wait for CPU. htop is a friendlier interactive view with the same numbers; install it if you prefer it.

Tool availability

mpstat, pidstat and iostat come from the sysstat package; iotop and htop are separate packages. Minimal images and the default WSL2 image ship none of them: sudo apt install sysstat iotop htop. On WSL2, remember that all these numbers describe the Linux VM, not Windows; the Windows-side vmmem process is that VM's memory. BusyBox top and ps on small boards show fewer columns and no --sort; the underlying /proc files are still there.

The top CPU lineususer codesykernel codeniniced user codeididlewaidle, I/O inflighthihardwareinterruptssisoftwareinterruptsststolen byhypervisorshares of CPU time in the %Cpu(s)line
The fields of the top CPU line and what each share of time means.

Key takeaways

  • Load average counts runnable plus uninterruptible tasks; compare it with nproc, never with 100.
  • High wa with idle CPU points at storage, not compute.
  • mpstat -P ALL (or pressing 1 in top) reveals per-core load; a lone pegged core with idle siblings is a parallelism problem, not a capacity problem.
  • vmstat's r column counts the run queue and b counts blocked tasks; r sustained above nproc means tasks wait for CPU.
Reflect
  1. Explain to a colleague, in two sentences: apply the USE method to CPU, memory, disk and network, and name the saturated resource with evidence.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close