Skip to content

Lesson 1 of 13 · 16m

The Bottleneck Isn't Where You Think

Wall time splits into on-CPU and off-CPU, and USE/RED find which resource owns the missing milliseconds before you touch code.

A request takes 800 ms. The box shows 6% CPU. Someone opens the codebase, finds the function they wrote last sprint, and starts "optimizing" it. That reflex is why most performance work is wasted: the time is almost never where the staring happens.

Wall time decomposes into two buckets. On-CPU time is the process actually executing instructions. Off-CPU time is everything else — waiting on a disk, waiting on a mutex, waiting on the network, waiting for a CPU to run on, sleeping out a cgroup throttle window. perf answers the first question. It answers nothing about the second, and on a service under contention the second bucket is where the 800 ms went.

The mental model that burns time

The wrong model is "slow means slow code." It produces diffs that shave a hot function from 40 ms to 35 ms while the request still waits 700 ms on a mutex held by a logging call. The right model is "slow means time went somewhere" — find where before deciding what to fix.

Off-CPU splits further into voluntary and involuntary waits. Voluntary: the thread asked to sleep — read() on a socket, futex_wait on a contended lock, fsync on a log file. Involuntary: the scheduler or the hypervisor took the CPU away — runnable threads queuing on saturated cores, a cgroup quota window, hypervisor steal time. Each has its own tool and its own fix, and none of them is a faster algorithm.

Amdahl's law does the arithmetic for you. If a function accounts for 8% of wall time, rewriting it to be infinitely fast buys you 8%. Not half. Eight percent. Engineers routinely spend a week optimizing code that is 5% of the request path because it was the code they understood, not the code that mattered.

USE, not vibes

Brendan Gregg's USE method is the checklist that replaces guessing: for every resource — CPU cores, memory, each disk, each network interface, the thread pool, the connection pool, every mutex — check utilization, saturation, and errors. Low CPU with high latency means the CPU is not the resource. So walk the others: vmstat's blocked column, iostat's await, cpu.stat's throttled counter, the pool's active-versus-max gauge.

For request-driven services, Tom Wilkie's RED variant applies the same idea one level up: rate, errors, duration per request type. A p99 duration that p50 never shows is saturation hiding in the tail, not a code problem.

Evidence from the field

Google's 2015 warehouse-scale profiling paper (Kanev et al., ISCA) sampled the fleet continuously and found cores stall on memory and instruction fetch far more often than they retire work — typical server binaries run at an IPC near 1 on hardware able to retire four instructions per cycle. The machines were not out of CPU; the cores were parked waiting on DRAM. Teams that profiled learned that. Teams that guessed "optimized" loops that were never the bottleneck.

The other canonical case: a service whose p99 traced entirely to a mutex serializing requests through a shared client. The on-CPU profile looked clean because every thread was blocked in futex_wait, not computing. The profile told the truth; it was being asked the wrong question.

The practice

Decompose one request before touching code:

  • perf stat -p <pid> sleep 10 for on-CPU density: instructions, IPC, context switches.
  • offcputime-bpfcc -p <pid> 10 for where the threads block.
  • vmstat 1, iostat -xz 1, mpstat -P ALL 1 for the resource picture.
  • The application's own pool and queue gauges for saturation that never reaches the kernel.

Order findings by fraction of wall time, not by which looks fixable. Fix the top one. Re-measure. The bottleneck moves — that is the point of the exercise.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.