Sub topic 7
Reading the graph: finding hotspots
This chapter assumes you can already produce flamegraph.svg with cargo flamegraph and that your build has debug symbols. It also assumes you know that a flamegraph is built from sampled call stacks. The goal here is to read the picture and pick the function worth optimizing.
The working model
Each box is a function. Its width is the share of samples in which that function appeared on the stack. A box sitting on top of another is a callee of the box below it. The root, usually main or a thread start function, spans nearly the full width.
Two rules matter most:
- The horizontal axis is not time. Boxes are sorted by name, so left-to-right order means nothing.
- The vertical axis is stack depth. A tall tower means deep calls, not slow calls.
Colors are random or grouped by crate, and they carry no meaning about cost.
Self time and total time
A box's total width includes its callees. Its self time is the part of its width not covered by boxes above it. Hotspots are usually found by self time, because that is where the CPU actually spent cycles.
| What you see | What it usually means |
|---|---|
| Wide box with nothing above it | High self time. The function's own code is hot. |
| Wide box with a wide box on top | The cost is in the callee. Look at the callee. |
| Wide box with many thin boxes on top | Cost is spread over many callees. Look for a shared pattern or reduce calls from the parent. |
| Narrow, tall tower | Deep recursion or layers of abstraction. Not a problem unless it is also wide. |
A worked example
Profile the slow prime counter from earlier in the tutorial:
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
(2..n).all(|d| n % d != 0)
}
fn count_primes(limit: u64) -> usize {
(0..limit).filter(|&n| is_prime(n)).count()
}
fn main() {
println!("{}", count_primes(100_000));
}In the graph, main and count_primes span the full width. Above them, iterator frames from the standard library (filter, count, fold) are also wide. At the top you should see is_prime, or the all closure it contains, covering almost all of that width. Because the compiler may inline is_prime into its caller, the label can differ between builds. Either way, the answer is the same: nearly all samples end in the trial-division loop.
A routine for reading
- Click the widest tall-enough tower to zoom in. The SVG supports this, and the percentage shown is the share of all samples.
- Follow the widest path upward until the boxes stop getting narrower or you reach a plateau.
- Judge that function by its share of total samples. A function at 3% can save at most 3%, however ugly it looks.
- Look at its callers too. Sometimes the better fix is to call the function less often, not to make it faster.
Common errors
- Reading width as wall-clock time. The graph counts CPU samples. A thread that is waiting for I/O or a lock produces few samples. This is covered in the off-CPU chapter.
- Trusting a thin profile. At 99 Hz, a 2-second run gives about 200 samples. A 1% box is then two samples, which is noise. Run longer or use a larger workload.
- Merging unrelated paths. Identical function names in different call paths appear as separate boxes. Use the search function in the SVG to see the combined share.
- Optimizing the first wide box.
mainis always wide. Hotspots are at the top of wide paths, not the bottom.
Exercise
In a flamegraph, parse_line is a wide box. Directly above it is a single box, str::split, covering about 80% of its width. Which function should you investigate first, and why?
Show answer
Investigate str::split (and how parse_line calls it). parse_line has only about 20% self time, since 80% of its width is spent in the callee. Check whether the split is needed per call, or whether it can be done less often or replaced with a cheaper approach.