Sub topic 3
Anatomy of a flamegraph
This chapter assumes you know what a call stack is and how a sampling profiler collects stack samples. It explains how to read the picture a flamegraph draws from those samples.
From samples to rectangles
A sampling profiler records the call stack many times per second. Each sample is one list of function names, from the outermost caller to the function that was running. A flamegraph merges those samples into one picture.
Tools usually store the merged data in the "folded" format. Each line is one distinct stack, with frames separated by semicolons, followed by the number of samples that had that exact stack:
main;count_primes;is_prime 870
main;count_primes 12
main 3This is illustrative data for the prime-counting program. Here 885 samples were taken in total. In 870 of them, is_prime was the function running. In 12, count_primes was running itself, and in 3, main was.
The renderer turns each line into a column of boxes. Where lines share a prefix, their boxes merge into one wider box.
The axes
The two axes mean different things, and mixing them up is the most common reading mistake.
| Axis | Meaning |
|---|---|
| Vertical | Call depth. The root (such as `main`) is at the bottom. Each box sits on its caller. |
| Horizontal | Share of samples. A box's width is the fraction of samples whose stack contains that frame. |
Three consequences follow from this:
- Height shows how deep the calls go. It does not show cost. A tall, narrow tower is cheap.
- Width includes the frame's children.
count_primesis nearly as wide asmainbecause nearly all its time is spent insideis_prime. - The top edge of the graph shows where the CPU was actually executing. A box with nothing above it spent its whole width running its own code.
Left-to-right order means nothing
Boxes at the same level are sorted alphabetically by name, not by time. The left side is not "earlier" and the right side is not "later". The renderer sorts this way so identical stacks merge into one box. If a function is called from many places, its time is spread over many separate boxes, one per call path.
Self time and total time
Each box has two costs:
- Total time is the box's full width. It counts the frame and everything it called.
- Self time is the part of the width not covered by boxes above it. This is time spent in the function's own instructions.
A wide box with a wide child has low self time. A wide box with nothing on top has high self time. Hotspots are usually boxes with high self time. In our example, count_primes has a total of 882 samples but a self time of only 12. is_prime has 870 for both, which makes it the hotspot.
Colors and interactivity
Colors are usually random warm shades chosen to separate neighboring boxes. In the default flamegraph palette they carry no meaning, so don't read "red" as "slow". Width is the only measure of cost.
The SVG files that cargo flamegraph and inferno produce are interactive when opened in a browser:
- Hovering over a box shows its name, sample count, and percentage.
- Clicking a box zooms in so it fills the full width. This makes small children readable.
- The search box highlights every frame whose name matches, and shows the total percentage matched. This helps when a function is called from many paths.
Common misreadings
- Treating width as wall-clock time. A CPU flamegraph counts samples taken while the program was on the CPU. Time spent waiting is absent.
- Reading the horizontal axis as a timeline. Use a tracing tool if you need to see events in order.
- Ignoring small samples. With few samples, a narrow box may be noise. A longer run or a higher sampling frequency fixes this.
Exercise
A folded profile has these lines: main;parse 40, main;parse;read_line 160, main;render 100. Which function has the highest self time, and what is parse's total time as a percentage of all samples?
Show answer
read_line has the highest self time, with 160 of 300 samples. parse has a total of 200 samples (40 + 160), which is about 67% of the 300 samples.