From be98210ee62a0f0aaca98c8be53320db323373c9 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 24 Jun 2020 23:11:27 +0200 Subject: [PATCH] WIP: CPU profiling on Rust --- content/posts/rust-cpu-profiling.md | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 content/posts/rust-cpu-profiling.md diff --git a/content/posts/rust-cpu-profiling.md b/content/posts/rust-cpu-profiling.md new file mode 100644 index 0000000..5f3f548 --- /dev/null +++ b/content/posts/rust-cpu-profiling.md @@ -0,0 +1,40 @@ +--- +title: "CPU profiling in Rust" +date: 2020-06-24T16:37:30+02:00 +draft: false +tags: ["coding", "rust"] +--- + +Recently I've been hacking with Rust's [recently-stabilized async/await support](https://blog.rust-lang.org/2019/11/07/Async-await-stable.html), and especially trying to understand how suitable it will be for building real-time audio stuff on top of. More on this topic soon I hope, but during the exploration process I discovered a nice bit of Rust tooling to share. + +A key requirement of both real-time and audio software is of course performance. Habitually I keep [htop](https://hisham.hm/htop/) or one of its [Rusty cousins](https://www.wezm.net/v2/posts/2020/rust-top-alternatives/) running in a separate tmux pane to expose general performance characteristics such as CPU usage immediately, but after identifying a general problem it's usually necessary to dive deeper and understand exactly which function calls in a particular program are the culprit(s). + +[Flamegraph](https://github.com/flamegraph-rs/flamegraph) is a crate that generates SVG diagrams, known as flamegraphs, that analyze a program's stack activity and visualizes it in a format that makes it easy to identify the stack frames (typically function calls) that a program is spending the most time in. It's neither particularly high tech stuff - flamegraphs are typically generated by triggering a periodic interrupt and building a chronological picture of the makeup of the stack - nor an exact science. But generally if your program is exhibiting poor performance then analyzing stack activity is a good place to start, and flamegraphs make the experience much prettier. + +Note that flamegraphs were originally invented by Brendan Gregg who has a great writeup and a number of other resources available [here](http://www.brendangregg.com/flamegraphs.html). It's also worth noting that although flamegraphs are a relatively new method of visualising stack activity, the underlying approach is widely seen in other platforms - and popularly in [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/rendering-tools)'s "timeline" tab. + +## Flamegraphs in Rust + +This brings us to the [cargo-flamegraph crate](https://crates.io/crates/flamegraph), which is a piece of Cargo tooling to allow trivial CPU profiling of Rust binaries[^1]. It in turn depends heavily on a second crate, [Inferno](https://github.com/jonhoo/inferno), to generate the actual graphs from raw dtrace data. + +First step is to install using Cargo: + +```bash +cargo install flamegraph +``` + +I also had to install some other dependencies: + +```bash +sudo apt-get install -y linux-tools-common linux-tools-generic +``` + +as well as some kernel version-specific packages: + +```bash +# Note: may not be necessary on other kernel versions +sudo apt-get linux-tools-5.3.0-59-generic linux-cloud-tools-5.3.0-59-generic +``` + + +[^1]: Or any other binary for that matter, but I'm mostly interested in profiling binaries that I build using Rust.