Recipe
Flame Graph Primer
Visualize CPU time as stacked rectangles so you can spot hot paths instantly.
A flame graph answers one question: where is my program spending time? Each box is a stack frame. Width equals CPU samples. The x-axis is alphabetical, not chronological — merge frames to see aggregate cost.
Anatomy
- Bottom row — entry point (main, _start). Widest bar.
- Middle layers — call chain. Each sits atop its caller.
- Top edge — leaf functions consuming CPU directly.
- Color — warm (pink) = user code; cool (violet) = kernel or runtime.
Reading the graph
Look for wide plateaus — functions that dominate the profile. A single wide frame near the top means a hot leaf. A wide frame deep in the stack means a hot caller. Narrow towers are noise; ignore them until you fix the obvious bottlenecks.
Generating one
- Capture samples with
perf record -F 99 -g ./binary(Linux) or ETW CPU sampling (Windows). - Convert to folded stacks:
perf script | stackcollapse-perf.pl. - Render SVG:
flamegraph.pl > out.svg.
Pro tip: Search the SVG (Ctrl+F) for a function name. The matching frames highlight instantly — no need to scan the entire graph.