DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · DEBUGGING · 21 OF 187

Linux Performance Tools and the USE Method

A method instead of guessing: the USE method (utilisation, saturation, errors for every resource) and the tools that answer each box: uptime, vmstat, mpstat, pidstat, iostat (our disk test: 524 writes/s, 536 MB/s, 1.28 ms per write, 67% utilised), free, ss, dmesg, pressure stall information (I/O pressure hit 11.7% while writing), and strace and perf when the problem is inside one process.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Utilisation
  • saturation
  • errors
  • vmstat, mpstat, pidstat, iostat -x, free, ss, dmesg, PSI
  • strace and perf inside

Notes

The USE method

The USE method, popularised by a performance engineer at Netflix, is simple. For every resource, CPU, memory, disk and network, ask three questions. Utilisation: how busy is it? Saturation: is work queuing because it is too busy? Errors: is anything failing? Saturation is the one people forget, and it is usually what users feel. Go through the boxes in order, and you rarely miss the real bottleneck.

USE
  • U utilisation: how busy? CPU 90%, disk 67% busy
  • S saturation: how much work is waiting? run queue, disk queue, swapping
  • E errors: anything failing? dmesg, NIC drops, I/O errors
  • resources: CPU, memory, disk, network, plus limits

The first 60 seconds

Start with a quick pass over the whole machine, about a minute. Uptime for the load trend. dmesg for kernel errors like OOM kills or disk failures. vmstat for runnable tasks, swapping and I O wait. mpstat for a single hot core. pidstat for which processes. iostat for disks, free for available memory, and ss and ip for network totals and errors. Each of these answers one box of the USE method.

In order
uptime          load trend
dmesg -T | tail  kernel errors
                 (OOM, disk)
vmstat 1         r, si/so, wa
mpstat -P ALL 1  hot cores
pidstat 1        per process
iostat -xz 1     disks
free -m          available
ss -s; ip -s link  network

CPU

For the CPU: utilisation is user plus system time, per core with mpstat, so one maxed-out core is not hidden by an average. Saturation is the run queue: r in vmstat larger than the number of CPUs, a high load, or cgroup throttling in containers. Errors are rare, but dmesg reports hardware machine check errors. Last episode showed what saturation does: ShopRung's slowest requests got eight times slower.

CPU boxes
  • U: vmstat us+sy, mpstat per core (one core at 100%?)
  • S: run queue r > CPU count, load average, cgroup throttling
  • E: rare; dmesg for machine check errors
  • last episode: p95 1.6 → 13 ms under saturation

Memory

For memory: utilisation from free, comparing used with available. Saturation means the kernel is struggling to find memory: swap activity in vmstat, or pressure stall information in proc pressure memory. Errors are OOM kills in dmesg. On our quiet machine, seven thousand four hundred and thirty eight megabytes were available, and memory pressure was zero.

Memory boxes
  • U: free -m: used vs available
  • S: swapping (vmstat si/so), page reclaim, pressure stall info /proc/pressure/memory
  • E: OOM kills in dmesg, allocation failures
  • ours: 7,438 MB available, memory pressure 0

Disk

For disks, iostat with minus x answers all three. We wrote a large file directly to disk and measured: five hundred and twenty four writes a second, about five hundred and thirty six megabytes a second, each write taking one point two eight milliseconds on average, a queue size of point seven five, and sixty seven percent utilised. Utilisation is percent util; saturation shows as a growing queue and rising await; errors are I O errors in dmesg, and of course a full filesystem.

Our write test: iostat -x
w/s       524      writes/sec
wkB/s     536,576  ~536 MB/s
w_await   1.28 ms  per write
aqu-sz    0.75     queue
%util     67.2

U: %util  S: aqu-sz, await
E: dmesg I/O errors, df full

Pressure stall info

Newer kernels add pressure stall information, a direct measure of saturation. During our write test, I O pressure showed eleven point seven three percent for the last ten seconds: for that share of time, tasks were stalled waiting on the disk. There are matching files for CPU and memory. Many modern tools, including Kubernetes, read these files, because one percentage tells you how much time work lost waiting.

I/O pressure during the test
some avg10=11.73
full avg10=11.73

some: % of time at least one
  task waited on I/O
full: % of time all tasks
  waited
also /proc/pressure/cpu and
  memory: saturation in one line

Network

For the network: ss shows sockets. ss with minus t l n p lists who listens on which port, and on our machine ShopRung's node process was listening on port eight thousand and eighty. ip minus s link shows packets, drops and errors per interface, and nstat shows TCP retransmits, a classic sign of a congested or lossy path. Part two goes much deeper.

Network boxes
$ ss -s        socket totals
$ ss -tlnp      who listens
  0.0.0.0:8080 node (ShopRung)
$ ip -s link    RX/TX, drops
$ nstat -az | grep -i retrans
U: bandwidth vs link speed
S: backlog queues, retransmits
E: drops, errors, resets

Inside one process

If every resource looks healthy but one application is slow, look inside it. strace minus c on a running process shows which system calls take the time; minus T shows each call's duration, which exposes slow DNS lookups, slow disk reads or connections waiting to time out. lsof lists its open files and sockets. And perf shows which functions burn CPU. Language-specific profilers come in part eleven.

Look inside
  • strace -c -p <pid>: which syscalls take the time
  • strace -f -T: each call's duration (slow DNS, slow disk, connect timeouts)
  • lsof -p <pid>: open files, sockets
  • perf top / perf record: which functions burn CPU

Common mistakes

Mistakes. Guessing instead of checking each resource in turn. Looking at utilisation but never saturation. Averages that hide one hot core or one slow disk. Skipping dmesg, where the kernel often states the answer. Changing two things at once, so you never learn which one helped. And fixing without measuring before and after.

Avoid these
  • guessing instead of checking every resource
  • looking at utilisation only, never saturation
  • averages hiding one hot core or one slow disk
  • ignoring dmesg
  • changing two things at once
  • no before/after measurement

Say it like this

In the interview: I use the USE method. For CPU, memory, disk and network I check utilisation, saturation and errors, with uptime, vmstat, mpstat, iostat minus x, free, ss and dmesg, plus pressure stall information. If the machine is healthy, I look inside the slow process with strace and perf. I change one thing at a time and measure before and after.

In the interview
  • "I use the USE method: for CPU, memory, disk and network I check
  • utilisation, saturation and errors with uptime, vmstat, mpstat, iostat -x, free, ss and dmesg. If the box is fine, I look
  • inside the process with strace
  • and perf, and measure the fix."

Interview questions

Q1What is the USE method?

For every resource (CPU, memory, disk, network, limits), check Utilisation, Saturation and Errors, to find bottlenecks systematically.

Q2Which tools do you run first on a slow Linux server?

uptime, dmesg -T | tail, vmstat 1, mpstat -P ALL 1, pidstat 1, iostat -xz 1, free -m and ss -s (plus ip -s link).

Q3How do you tell if a disk is the bottleneck?

iostat -x: high %util, growing aqu-sz and r_await/w_await, plus high I/O wait and I/O pressure stall information.

Q4What is pressure stall information?

Files in /proc/pressure (cpu, memory, io) showing the share of time tasks were stalled waiting for that resource: a direct saturation metric.

Q5How do you find which syscall is slow?

strace -c -p <pid> for a summary, or strace -f -T to see each call's duration.

Q6What does perf do?

Samples where CPU time is spent (perf top, perf record/report), down to functions, including the kernel.

Q7Why look at saturation, not just utilisation?

A resource can be 100% utilised without harm, but queuing (saturation) is what causes latency users feel.

Q8How do you check for network problems on a host?

ss -s and ss -tlnp for sockets, ip -s link for drops and errors, nstat for TCP retransmits.