Ben Chuanlong Du's Blog

It is never too late to learn.

CPU Profiling of Rust Applications Using Valgrind

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

It is suggested that you profile Rust application using not-perf . For more discussions, please refer to Profile Rust Applications .

Installation on Ubuntu

wajig install valgrind

Profile Your Application Using CallGrind

valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --collect-jumps=yes \
    your-program [program options]

Below is an example.

cargo build --profile release-debug
valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --collect-jumps=yes \
    ../ofcp_utils/target/release-debug/ofcp_utils score_r4_it_sim_prof \
        --file ../ofcp_utils/data/plays_r4_21.csv \
        --method sim \
        --runs 1000

Visualization

KCacheGrind

General Tips and Traps

  1. Profiling an application using valgrind is about 50-200 times slower than running the application. It is suggested that you use not-perf for profiling long-running Rust applicaitons.

  2. Valgrind seems to have some issues with Rust applications. Only performance data of public methods are dumped. A hack way to fix this issue is to mark all methods that you want to profile as pub in your Rust code.

References

Comments