In one minute
- Expert tools that find exactly why something is slow: which function uses the CPU, which file causes disk work, which request waits inside the kernel.
- Basic tools tell you that the CPU or disk is busy. These tools tell you why. They are the same toolkit performance engineers at large tech companies use.
- First, the USE method finds which resource is the bottleneck. Then
perftakes thousands of quick snapshots of what the CPU is doing, and a flame graph turns them into one picture. eBPF tools run tiny, safe programs inside the kernel to watch events with very little slowdown. - 👉 When the basic checklist shows a resource is busy, but not what is causing it, and for slowness that comes and goes.
Think of it like: a doctor. The basic checks (temperature, pulse) say you are sick. A scan (perf, eBPF) shows exactly which part is the problem.
The USE method
A simple checklist: for every resource, check three things. (It comes from Brendan Gregg, a well-known performance engineer.)
| Meaning | CPU example | Disk example | |
|---|---|---|---|
| Utilization | How busy is it? | %CPU in top, mpstat | %util in iostat |
| Saturation | Is work waiting in a queue? | Run queue (r in vmstat) | aqu-sz in iostat |
| Errors | Is anything failing? | Rare | IO errors in dmesg |
Go through CPU, memory, disk and network, and check U, S and E for each. You will rarely miss the bottleneck.
Think of it like: a motorway. Utilization = how many lanes are full. Saturation = how long the traffic jam is. Errors = crashes.
Tools for each resource
mpstat -P ALL 1 # per-CPU utilization: one core at 100% means a single-threaded bottleneckpidstat 1 # per-process CPU over timeiostat -xz 1 # per-disk util, await (latency), queue sizesar -n DEV 1 # network throughput per interfacesar -q 1 # run queue and load historyThese come from the sysstat package. sar can also record history, so you can see what happened at 3 a.m. while you were asleep.
Reading iostat -xz
| Column | Healthy | Worry when |
|---|---|---|
r/s, w/s (reads/writes per second) | Any | Close to the disk's IOPS limit |
r_await, w_await (wait time) | A few ms on SSD | Tens of ms or more |
aqu-sz (queue size) | Below 1 | Always above what the disk can handle at once |
%util | Under 70% | Near 100% on HDDs (less meaningful on NVMe) |
On AWS: gp3 disks have a set IOPS and throughput allowance. Hitting it looks like high wait time and queue size, while %util looks only moderate.
CPU profiling with perf
perf takes many snapshots per second of what the CPU is running.
👉 You know a process uses lots of CPU and want to know which part of its code.
sudo perf top # live view of the hottest functionssudo perf stat -p 2211 sleep 10 # counters: cycles, instructions, context switchessudo perf record -F 99 -g -p 2211 -- sleep 30 # sample stacks at 99 Hz for 30 ssudo perf report # browse the recorded profile| Flag | Meaning |
|---|---|
-F 99 | Snapshots per second (99, not 100, so it does not line up with timers) |
-g | Also record which function called which (the call stack) |
-p PID / -a | One process / the whole system |
For Java, Node.js and Python: these languages compile code while running, which hides function names. Turn on symbol maps (for Java, -XX:+PreserveFramePointer with perf-map-agent, or use async-profiler) to get readable names.
Flame graphs
A flame graph turns thousands of perf snapshots into one picture.
You see in seconds where the time goes, instead of reading thousands of lines.
sudo perf record -F 99 -a -g -- sleep 30sudo perf script | stackcollapse-perf.pl | flamegraph.pl > cpu.svgHow to read it:
- Each box is a function. Width = share of CPU time. Wider = more time.
- Stacks grow upward. The top edge is what was actually running.
- Look for wide flat tops. Those functions are where the time goes.
- Colours are usually random and mean nothing.
Remember it: Wide = slow. Height = just how deep the calls go.
One flame graph often finds in minutes what days of guessing could not: a slow JSON converter, logging inside a busy loop, a lock, or a slow regex.
eBPF: safe programs inside the kernel
eBPF lets you run tiny programs inside the kernel to watch events: system calls, disk reads, network packets, function calls.
Why it is safe for production: the kernel checks every eBPF program before running it, and the slowdown is very small.
Where you meet it: it powers modern tools such as Cilium (Kubernetes networking), Falco (security), Pixie and Parca (observability), and always-on profilers.
Think of it like: fitting tiny, harmless sensors inside an engine while it runs, instead of stopping the car to open the bonnet.
Ready-made eBPF tools
These come in the bpfcc-tools package on Ubuntu (their names end in -bpfcc):
| Tool | Answers |
|---|---|
execsnoop | Which programs are being started (even short ones top misses) |
opensnoop | Which files are being opened, and which fail |
biolatency | How long disk reads and writes take |
biosnoop | Each disk read/write, with the program and how long it took |
tcpconnect / tcpaccept | New outgoing / incoming TCP connections |
tcpretrans | TCP packets sent again (a sign of network trouble) |
runqlat | How long tasks wait for a CPU |
profile | Low-cost CPU sampling |
oomkill | Details of each OOM kill |
sudo execsnoop-bpfccsudo biolatency-bpfcc 10 1sudo tcpretrans-bpfccA classic first step on a busy server: execsnoop. Often a cron job or health check is starting thousands of tiny programs per second, and top never shows them because they finish too fast.
bpftrace one-liners
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args->filename)); }'sudo bpftrace -e 'tracepoint:block:block_rq_issue { @bytes[comm] = sum(args->bytes); }'- Print every file opened, and by which program.
- Add up disk bytes per program, until you press Ctrl+C.
A performance investigation, step by step
- Define the problem: which number got worse, by how much, since when, and for whom?
- USE method on every resource, to find the one that is overloaded.
- Zoom in with per-process tools (
pidstat,iostat,biosnoop). - Profile the busy process (
perf, flame graphs, eBPF). - Fix one thing, measure again, and compare with before.
Real-world scenario: slow spikes every few minutes
The slowest requests (p99) jump every five minutes. Averages look fine.
vmstat 1 5iostat -xz 1 3iostatshows disk write wait (w_await) spiking at the same times.biosnoopshows a log-shipping agent writing big batches to disk every five minutes, fighting with the database's writes on the same disk.
Moving the agent's buffer to a separate disk (or changing how often it writes) removes the spikes.
The lesson: averages hid the problem. Percentiles and per-process disk tracing revealed it.
Common mistakes
- Optimising without measuring, or without a "before" number to compare.
- Looking only at averages. Users feel the slowest requests (p99), not the average.
- Leaving heavy tracing running (strace, high-frequency perf) in production.
- Guessing instead of profiling. The real slow spot is rarely where people expect.
Practice
- Apply the USE method to a disk: name one tool and one column for each of U, S and E.
- Explain how to read width and height in a flame graph.
- Which eBPF tool would find short-lived programs that
topnever shows? - Write the
perfcommand to record 30 seconds of call stacks for PID 2211.
Cheat sheet
| Command | Use |
|---|---|
mpstat -P ALL 1 | Use of each CPU core |
pidstat 1 | CPU per process over time |
iostat -xz 1 | Disk wait times and queues |
perf top | Busiest functions, live |
perf record -F 99 -g -p PID | Record CPU call stacks |
execsnoop-bpfcc | New programs being started |
biolatency-bpfcc | Disk wait-time chart |
tcpretrans-bpfcc | Re-sent TCP packets |
Key takeaways
- The USE method checks every resource for busyness, queues and errors.
perftakes snapshots of what the CPU is doing, and a flame graph turns them into a picture.- In a flame graph, wider boxes are where the time goes.
- eBPF tools watch the kernel safely, even in production.
- Look at the slowest requests (p99), not just averages, and measure before and after each fix.