Lesson 4 of 9 · 24m
CPU Analysis: Load, Run Queues, and Flame Graphs
Reading load average correctly, separating user/system/iowait time, and using perf to find where cycles actually go.
CPU is where performance mythology is thickest. Load average is treated as a CPU meter, %CPU in top is treated as truth, and someone inevitably blames garbage collection before looking at a profile. The operating system is already telling you more than that. Learn to read scheduler pressure first, then use sampling profilers to find the code paths consuming cycles.
Start with vmstat and mpstat
vmstat 1 5
mpstat -P ALL 1 5In vmstat, the r column is runnable tasks waiting for CPU. If r is consistently larger than the CPU count, threads are queueing. The columns us, sy, wa, and st separate user work, kernel work, I/O wait, and hypervisor steal. mpstat reveals whether one hot CPU is saturated while others idle — common with single-threaded processes, poor IRQ affinity, or a lock convoy.
r b us sy wa st
24 0 92 6 0 0 # CPU-saturated, probably compute-bound
2 18 8 5 65 0 # not a CPU problem; tasks are blocked on I/OLoad average is not CPU usage
Linux load average counts runnable tasks plus tasks in uninterruptible sleep — usually disk or NFS I/O. A load of 80 on a 16-core host can mean CPU saturation, a stalled filesystem, or a pile of blocked container threads. Check r and b before saying "CPU-bound."
Perf finds the hot path
Sampling is usually low-risk and quick:
sudo perf record -F 99 -a -g -- sleep 30
sudo perf report --stdio | head -40-F 99 samples 99 times per second, -a records system-wide, and -g captures call graphs. Look for repeated frames, not one exotic leaf function. In real incidents the top frames usually fall into a small set of stories: application computation, serialization overhead, lock contention, syscall storms, or kernel memory management.
For a specific process:
sudo perf record -p 1842 -g -- sleep 20
sudo perf top -p 1842Flame graphs make call stacks visual, but the raw report is often enough. Do not tune a function because it is wide if it is merely the parent of unavoidable work — follow inclusive versus exclusive cost.
Context switches and interrupts
pidstat -w 1
cat /proc/interrupts | headVoluntary context switches often mean blocking I/O or lock waiting; involuntary switches mean the scheduler preempted a runnable thread. A sudden interrupt spike can come from network packet rates or a misbehaving device. CPU tuning without checking these is guesswork.
Field notes
- On cloud VMs, high steal time means the hypervisor neighbor is the bottleneck; no amount of local tuning fixes that.
- CPU frequency scaling can distort benchmarks. Record the governor and frequency before comparing runs.
- Kernel CPU time is still your workload's cost. Syscall-heavy code often looks "efficient" in user-space profiles only.
- Never attach heavyweight tracing to production before trying low-frequency sampling; observability has a performance cost too.
Was this lesson helpful?