Skip to content

Commit

Permalink
Merge torrust#749: Add configuration for cargo flamegprah and docs fo…
Browse files Browse the repository at this point in the history
…r profiling

6a7275e docs: [torrust#746] for profiling (Jose Celano)
bfdeafc test: [torrust#746] profiling: add configuration to generate flamegraphs (Jose Celano)

Pull request description:

  Add configuration for `cargo flamegprah` and docs for profiling.

ACKs for top commit:
  josecelano:
    ACK 6a7275e

Tree-SHA512: 77cb464b21a5913609a433980b551de6bae6bcdee679a39748ee8733641b812bdeb36b02b8811f9cc8fdef1c39bc07dcc6011722697dc23e727082d653aab143
  • Loading branch information
josecelano committed Mar 21, 2024
2 parents 47c2fe2 + 6a7275e commit a7845a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
/target
/tracker.*
/tracker.toml
callgrind.out
callgrind.out
flamegraph.svg
perf.data*
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ opt-level = 1
debug = 1
lto = "fat"
opt-level = 3

[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]
6 changes: 6 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"binstall",
"Bitflu",
"bools",
"Bragilevsky",
"bufs",
"Buildx",
"byteorder",
Expand Down Expand Up @@ -45,10 +46,12 @@
"dtolnay",
"elif",
"filesd",
"flamegraph",
"Freebox",
"gecos",
"Grcov",
"hasher",
"heaptrack",
"hexlify",
"hlocalhost",
"Hydranode",
Expand Down Expand Up @@ -105,6 +108,7 @@
"rerequests",
"ringbuf",
"rngs",
"rosegment",
"routable",
"rusqlite",
"RUSTDOCFLAGS",
Expand Down Expand Up @@ -138,11 +142,13 @@
"uroot",
"Vagaa",
"valgrind",
"Vitaly",
"Vuze",
"Weidendorfer",
"Werror",
"whitespaces",
"XBTT",
"Xdebug",
"Xeon",
"Xtorrent",
"Xunlei",
Expand Down
Binary file added docs/media/kcachegrind-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions docs/profiling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Profiling

## Using flamegraph

```console
TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" cargo flamegraph --bin=profiling -- 60
```

![flamegraph](./media/flamegraph.svg)

## Using valgrind and kcachegrind

You need to:

1. Build an run the tracker for profiling.
2. Make requests to the tracker while it's running.

Build and the binary for profiling:

```console
RUSTFLAGS='-g' cargo build --release --bin profiling \
&& export TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" \
&& valgrind \
--tool=callgrind \
--callgrind-out-file=callgrind.out \
--collect-jumps=yes \
--simulate-cache=yes \
./target/release/profiling 60
```

> NOTICE: You should make requests to the services you want to profile. For example, using the [UDP load test](./benchmarking.md#run-udp-load-test).
After running the tracker with `<valgrind` it generates a file `callgrind.out`
that you can open with `kcachegrind`.

```console
kcachegrind callgrind.out
```

![kcachegrind screenshot](./media/kcachegrind-screenshot.png)

## Links

Profiling tools:

- [valgrind](https://valgrind.org/).
- [kcachegrind](https://kcachegrind.github.io/).
- [flamegraph](https://github.com/flamegraph-rs/flamegraph).

Talks about profiling:

- [Profiling Rust Programs with valgrind, heaptrack, and hyperfine](https://www.youtube.com/watch?v=X6Xz4CRd6kw&t=191s).
- [RustConf 2023 - Profiling async applications in Rust by Vitaly Bragilevsky](https://www.youtube.com/watch?v=8FAdY_0DpkM).
- [Profiling Code in Rust - by Vitaly Bragilevsky - Rust Linz, December 2022](https://www.youtube.com/watch?v=JRMOIE_wAFk&t=8s).
- [Xdebug 3 Profiling: 2. KCachegrind tour](https://www.youtube.com/watch?v=h-0HpCblt3A).

## Acknowledgments

Many thanks to [Vitaly Bragilevsky](https://github.com/bravit) and others for sharing the talks about profiling.

0 comments on commit a7845a9

Please sign in to comment.