Sub topic 6
Your first flamegraph with cargo flamegraph
This chapter assumes you can build Rust programs in release mode, use a terminal, and know what a call stack is. You will profile a slow program with cargo flamegraph and get an SVG you can open in a browser.
What the tool does
cargo flamegraph is a Cargo subcommand. It builds your target, runs it under the platform profiler (perf on Linux), folds the sampled stacks, and writes flamegraph.svg in the current directory. You don't run those steps yourself.
Set up the build
A release build is optimized, so it reflects real performance. By default it has no debug info, so the graph shows poor names and no source lines. Add this to Cargo.toml:
[profile.release]
debug = trueThis keeps optimizations on and adds debug info. The speed of your program stays the same. Only the binary gets larger.
A program worth profiling
This program is slow on purpose. Almost all of its time goes into the trial-division loop in is_prime.
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));
}Install and run
Install the tool once, then run it from the project root. The --bin flag names the binary target.
cargo install flamegraph
cargo flamegraph --bin myapp
# for a benchmark target:
cargo flamegraph --bench my_bench -- --benchEverything after the lone -- goes to your program. For a benchmark target, --bench tells the harness to run the benchmarks and not just test them.
What happens during a run
While the program runs, the profiler records its call stack many times per second. Identical stacks are counted together. The SVG draws each function as a box whose width is proportional to how many samples included it.
Read the first result
Open flamegraph.svg in a browser. For the prime program you should see something like this:
mainat the bottom, spanning the full width, because every sample passes through it.count_primesdirectly above it, also nearly full width.is_prime, or the iterator code inlined into it, filling almost all the width above.
The wide plateau at the top is the hotspot. Click a box to zoom in, and use the search box to highlight a name. The next chapter covers reading these shapes in detail.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Names look like `[unknown]` or hex addresses | No debug info in the release build | Add `debug = true` under `[profile.release]` |
| The graph is very thin or nearly empty | The program ran too briefly to collect samples | Raise the workload size, such as the prime limit |
| Permission error from `perf` | Kernel restricts sampling | Adjust `perf_event_paranoid` or run with suitable privileges |
| Stacks end abruptly | The profiler could not unwind the stack | See the chapter on frame pointers and DWARF |
Exercise
You ran cargo flamegraph --bin myapp and the SVG shows mostly unreadable addresses instead of function names. What do you change, and why is it safe for performance measurements?
Show answer
Add debug = true under [profile.release] in Cargo.toml and run again. Debug info only adds symbol and line information to the binary. Optimization settings stay the same, so the code that runs is the same optimized code, and the measurements still reflect release performance.
Exercise
Why does main always appear as a full-width box at the bottom of the flamegraph?
Show answer
Every sampled stack starts in main (or in runtime code that calls it), so every sample includes that frame. Width is proportional to the share of samples containing the function, which makes main span the whole graph.