What is the standart profiling tool can I use to profile a Rust program? Ideally something that can show me which functions are slow, how much stack memory each function uses, how much heap memory is allocated and by whom, how many syscalls each function makes, and which memory isn’t being freed properly over time.
Bonus points if it can also detect cache misses and branch prediction misses, but that part isn’t strictly required.
I have tried this before using Samply, it already shows me which functions are slow. But it doesn’t show how much stack memory each function uses, how much heap memory is allocated per function, and how many syscalls each function makes. Is there a tool that can display those as well?
And can highlight which code suffers from cache misses and branch prediction misses, but it is not a must.
perf (or any perf-based tool, such as Hotspot) can do all of those except for memory allocations by passing appropriate event types:
Execution time: (default)
Syscalls: -e raw_syscalls:sys_enter
Cache misses: -e cache-misses
Branch mispredictions: -e branch-misses
Both perf report and Hotspot can highlight hot code on a per instruction basis (though interpreting the results can be challenging at times).
Per-function stack usage can be calculated at compile time using tools like cargo-call-stack. This could also be done by static or dynamic analysis, but I can't remember any tools that can do this off the top of my head.
For heap allocations on Linux there are a few tools that work (basically the same tools that work for C/C++): Heaptrack and Bytehound are my two gotos.
Someone on r/rust recently posted about memory profiling as well, and the blog post as well as the discussion on reddit was interesting: Memory analysis in Rust : rust
The excellent Rust Performance Book also has a chapter on profiling tools and another chapter on the heap.
I didn't see this mentioned yet, but heap analysis can be done at runtime with the Valgrind DHAT tool or the dhat crate. Both written by the same author (and one of the compiler performance gurus) Nicholas Nethercote.