Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Tips and Traps¶
not-perf is the best CPU profiling tool for Rust application.
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.
It is suggested that you use Flamegraph in a virtual machine (via multipass) or a Docker container. This is because Flamegraph relies on
perfwhich 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 installperfor 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.You have to configure
perf_event_paranoidto be-1. This can be done by manually setting the value in the file/proc/sys/kernel/perf_event_paranoidto be-1. Or equivalently, you can run the following command:::bash sudo sysctl -w kernel.perf_event_paranoid=-1Changes made by the above approaches are temporary. To persist the above setting, you can add a line
kernel.perf_event_paranoid = -1into the file/etc/sysctl.conf.
Installation on Debian¶
wajig update
wajig install linux-perf
cargo install flamegraphInstallation on Ubuntu¶
wajig update
wajig install linux-tools-common linux-tools-generic linux-tools-`uname -r`
cargo install flamegraphUsage¶
Run the following command to generate a SVG visualization of performance profiling.
:::bash
cargo flamegraphIf sudo permission is needed,
then add the --sudo option.
:::bash
cargo flamegraph --sudoIf you encounter issues
(see
#62
and
#159
) with the above commands,
you can try run the flamegraph on rust binary directly.
:::bash
sudo ~/.cargo/bin/flamegraph -- target/release/your_binary [options]Notice that it is best to
Start a Docker container with the option
--cap-add SYS_ADMINif you useflamegraphin a Docker container. For more discussions, please refer to runningperfin docker & kubernetes .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.tomlfile.[profile.release] debug = trueView 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”
Flamegraph shows every caller is [unknown]? echo 0 |sudo tee /proc/sys/kernel/kptr_restrict