Sub topic 10
Under the hood: perf record and inferno
This chapter assumes you have already run cargo flamegraph and read its output. It also assumes you know why release builds need debug symbols and how frame pointers differ from DWARF unwinding. Here we take apart the pipeline that cargo flamegraph runs for you, so you can control each step.
The four stages
cargo flamegraph is a wrapper. Underneath, four steps turn a running program into an SVG:
perf recordsamples the program and writes raw data toperf.data.perf scriptconverts that binary file into text, one stack per sample.inferno-collapse-perffolds identical stacks into one line each, with a count.inferno-flamegraphdraws the folded stacks as an SVG.
Running the pipeline by hand
Install the inferno tools once with cargo install inferno. Then build with frame pointers and run the stages.
RUSTFLAGS="-C force-frame-pointers=yes" cargo build --release
perf record -F 99 -g -- ./target/release/myapp
perf script | inferno-collapse-perf | inferno-flamegraph > flame.svgThe flags matter:
-F 99samples 99 times per second. An odd number avoids lining up with periodic work that runs at 100 Hz or similar, which could bias the samples.-grecords the call stack with each sample. Without it you get only the function that was running, and no flamegraph.--separates perf's options from your program and its arguments.
If frame pointers are unavailable, use --call-graph dwarf instead of -g. It copies a slice of the stack with each sample and unwinds it later, so perf.data gets much larger.
What the folded format looks like
Run the collapse step on its own and look at the file:
perf script | inferno-collapse-perf > before.folded
head -n 3 before.foldedEach line is a semicolon-separated stack, outermost frame first, followed by a sample count:
myapp::main;myapp::count_primes;myapp::is_prime 1432
myapp::main;myapp::count_primes 12The folded file is plain text, so you can grep it, filter it, and compare it with a diff tool. A frame's width in the flamegraph is the sum of the counts on every line that contains it.
Why use the manual pipeline
- Reuse
perf.data. You can record once and render several graphs from it. - Filter before drawing. Run
grepon the folded file to keep only stacks that include a given function. - Change the recording. You can set the frequency, attach to a running process with
-p PID, or record only some events. - Keep the folded files. The next chapters use them for before-and-after comparisons.
Common errors
- "Permission denied" or empty output. Linux restricts
perffor unprivileged users throughkernel.perf_event_paranoid. Lower it withsudo sysctl kernel.perf_event_paranoid=1, or run perf withsudo. - Hex addresses instead of function names. The binary lacks symbols. Check that
debug = trueis set under[profile.release]and that you ran the release binary. - Very short stacks. The program was built without frame pointers, and
-gcannot walk the stack. Rebuild withforce-frame-pointers=yes, or switch to DWARF. - Too few samples. At 99 Hz, a program that runs for one second gives about 99 samples. Run it longer, or raise the frequency.
Exercise
You run perf record -F 99 -- ./target/release/myapp and then perf script | inferno-collapse-perf | inferno-flamegraph > flame.svg. The graph is a single flat bar with no call hierarchy. What did you leave out of the perf record command, and why does it cause this?
Show answer
You left out -g (or --call-graph dwarf). Without it, perf records only the instruction being executed in each sample, not the chain of callers. The collapse step then has no stack to fold, so every sample appears as a single frame.