Ben Chuanlong Du's Blog

It is never too late to learn.

Profile Rust Applications Using Flamegraph

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

  1. not-perf is the best CPU profiling tool for Rust application.

  2. Valgrind is another good alternative to flamegraph if performance is not a big issue. However, profiling an application using valgrind is about 50-200 times slower than running the application, so it might not be a good option for long-running applications. not-perf is a better alternative in this case.

  3. It is suggested that you use Flamegraph in a virtual machine (via multipass) or a Docker container. This is because Flamegraph relies on perf which require sudo permission to install and configure, which is easier and safer to do in an isolated environment. If you use a Docker container, make sure that the Docker image is compatible with the Linux kernel on the host machine ! Otherwise, you will either fail to install perf or install a non-compatible one. Generally speaking, it is a good choice to run a Ubuntu Docker container on a Ubunut host machine with matching releasing versions.

  4. You have to configure perf_event_paranoid to be -1. This can be done by manually setting the value in the file /proc/sys/kernel/perf_event_paranoid to be -1. Or equivalently, you can run the following command

    sudo sysctl -w kernel.perf_event_paranoid=-1
    

    Changes made by the above approaches are temporary. To persist the above setting, you can add a line kernel.perf_event_paranoid = -1 into the file /etc/sysctl.conf .

Installation on Debian

wajig update
wajig install linux-perf
cargo install flamegraph

Installation on Ubuntu

wajig update 
wajig install linux-tools-common linux-tools-generic linux-tools-`uname -r`
cargo install flamegraph

Usage

Run the following command to generate a SVG visualization of performance profiling.

cargo flamegraph

If sudo permission is needed, then add the --sudo option.

cargo flamegraph --sudo

If you encounter issues (see #62 and #159 ) with the above commands, you can try run the flamegraph on rust binary directly.

sudo ~/.cargo/bin/flamegraph -- target/release/your_binary [options]

Notice that it is best to

  1. Start a Docker container with the option --cap-add SYS_ADMIN if you use flamegraph in a Docker container. For more discussions, please refer to running perf in docker & kubernetes .

  2. Enable debug info (if you are profiling the release build which is the default). You can achive this by adding the following configuration into your Cargo.toml file.

    [profile.release]
    debug = true
    
  3. View the generated SVG file using a browser (e.g., Chrome) instead of using a image viewer app.

perf

Running perf in docker & kubernetes

Security implications of changing “perf_event_paranoid”

Run perf without root-rights

Flamegraph shows every caller is [unknown]? echo 0 |sudo tee /proc/sys/kernel/kptr_restrict

References

Comments