Finding where CPU time goes
medium 6 minA sampling profiler interrupts execution at intervals and records the current stack.
Video lesson · Chapter 1 of 8
A serializer dominates a hot path
Theory
Read or listen first. About 1 min.
What it is
A sampling profiler interrupts execution at intervals and records the current stack. Repeated stacks estimate where the chosen event occurs. In a CPU flame graph, width represents samples aggregated below a frame, and vertical position represents call depth. The horizontal axis groups similar stacks and does not give the order of events.
Why it exists
After changing response formatting, CPU and latency rise at fixed request rate. Compare equal-duration profiles from matched versions and normalize by completed requests. A wide parent can enclose several expensive children. Identify the leaf work before optimizing the parent. A CPU profile will underrepresent time spent blocked off CPU.
If CPU per request stays flat while wall time grows, inspect off-CPU waits. Another thread’s optimization could have changed the sample fraction.
A serializer dominates a hot path
Inside this chapter
- CPU Profiling with perf
- Perf samples chosen events such as CPU cycles. Compare event selection, sample rate, symbol coverage and completed work before treating two profiles as comparable.
- System Activity with sar
- Sar provides interval or archived activity for CPU, memory, I/O and more. A coarse archive interval can average away a burst that caused a short deadline miss.
- CPU Statistics with mpstat
- Per-CPU statistics expose imbalance hidden by a node average. One saturated core can constrain a single-threaded loop while many other cores remain idle.
- Process Statistics with pidstat
- Per-task interval statistics distinguish which process contributes CPU, faults or switching. Capture worker identity across restarts because PID reuse can join unrelated samples.
- Flame Graph Generation
- A flame graph groups sampled stacks by their call paths. Width reflects sample weight for the selected event. Examine the hottest leaves and normalize by completed work.
How it works
Step through the idea, or press Play.
Choose an event. Decide whether CPU execution or some off-CPU wait can explain the symptom.
Understand it
Worked incident
A serializer dominates a hot path
| Observation | Serializer samples per 100 (samples) | Interpretation |
|---|---|---|
| Baseline | 12 | Baseline samples spend a small fraction in serialization. |
| Incident | 65 | The new version repeatedly serializes the same payload. |
| After intervention | 15 | Reuse the encoded representation where valid, then compare absolute CPU per request and the new profile. |
Choose the next investigation
Summary
The whole topic in one minute.
Sampling reports a share of the captured event. A share can grow when other work shrinks, even at constant function cost.
If CPU per request stays flat while wall time grows, inspect off-CPU waits. Another thread’s optimization could have changed the sample fraction.