How to Use perf and flamegraph with Rust Applications

Profile Rust applications with perf and visualize bottlenecks using flamegraphs.

Use perf to record instruction counts with stack traces, then convert the output to a flamegraph using flamegraph.pl.

  1. Build your Rust application in release mode with debug symbols enabled.
cargo build --release
  1. Record the performance data while running your binary, capturing the call graph.
sudo perf record -g -e instructions -- ./target/release/your_binary
  1. Generate a stack dump from the recorded data.
perf script | stackcollapse-perf.pl > out.stack
  1. Convert the stack dump into an SVG flamegraph.
flamegraph.pl out.stack > flamegraph.svg

Open flamegraph.svg in your browser to visualize the performance bottlenecks.