Skip to content

Commit dfe9690

Browse files
committed
docs: Add section about using Tracy for profiling (#4534)
# Objective - Document how to do profiling with Tracy # Solution - The documentation of setting `RUST_LOG=info` in order to capture `wgpu` spans depends on #5182
1 parent 2c9bc0b commit dfe9690

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

docs/profiling.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Runtime Flame Graph: `tracing` spans
44

5-
Bevy has built-in [tracing](https://github.com/tokio-rs/tracing) spans to make it cheap and easy to profile Bevy ECS systems, render logic, engine iternals, and user app code. Enable the `trace` cargo feature to enable Bevy's built-in spans. You also need to select a `tracing` backend using the following cargo features:
5+
Bevy has built-in [tracing](https://github.com/tokio-rs/tracing) spans to make it cheap and easy to profile Bevy ECS systems, render logic, engine iternals, and user app code. Enable the `trace` cargo feature to enable Bevy's built-in spans.
6+
7+
If you also want to include `wgpu` tracing spans when profiling, they are emitted at the `tracing` `info` level so you will need to make sure they are not filtered out by the `LogSettings` resource's `filter` member which defaults to `wgpu=error`. You can do this by setting the `RUST_LOG=info` environment variable when running your application.
8+
9+
You also need to select a `tracing` backend using the following cargo features:
610

711
### Backend: trace_chrome
812

@@ -12,13 +16,45 @@ After running your app a `json` file in the "chrome tracing format" will be prod
1216

1317
![image](https://user-images.githubusercontent.com/2694663/141657409-6f4a3ad3-59b6-4378-95ba-66c0dafecd8e.png)
1418

19+
### Backend: trace_tracy
20+
21+
The [Tracy profiling tool](https://github.com/wolfpld/tracy) is:
22+
> A real time, nanosecond resolution, remote telemetry, hybrid frame and sampling profiler for games and other applications.
23+
24+
There are binaries available for Windows, and installation / build instructions for other operating systems can be found in the [Tracy documentation PDF](https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf).
25+
26+
It has a command line capture tool that can record the execution of graphical applications, saving it as a profile file. Tracy has a GUI to inspect these profile files. The GUI app also supports live capture, showing you in real time the trace of your app.
27+
28+
In one terminal, run:
29+
`./capture-release -o my_capture.tracy`
30+
This will sit and wait for a tracy-instrumented application to start, and when it does, it will automatically connect and start capturing. Note that on Windows, the capture tool is called `capture.exe`.
31+
32+
Then run your application, enabling the `trace_tracy` feature:
33+
`cargo run --release --features bevy/trace_tracy`
34+
35+
After running your app, you can open the captured profile file (`my_capture.tracy` in the example above) in the Tracy GUI application to see a timeline of the executed spans.
36+
37+
Alternatively, directly run the tracy GUI and then run your application, for live capture. However, beware that running the live capture on the same machine will be a competing graphical application, which may impact results. Pre-recording the profile data through the CLI tool is recommended for more accurate traces.
38+
39+
In any case, you'll see your trace in the GUI window:
40+
41+
![Tracy timeline demonstrating the performance breakdown of a Bevy app](https://user-images.githubusercontent.com/302146/163988636-25c017ab-64bc-4da7-a897-a80098b667ef.png)
42+
43+
There is a button to display statistics of mean time per call (MTPC) for all systems:
44+
45+
![A table in the Tracy GUI showing the MTPC (mean time per call) for all instrumented spans in the application](https://user-images.githubusercontent.com/302146/163988302-c21102d8-b7eb-476d-a741-a2c28d9bf8c1.png)
46+
47+
Or you can select an individual system and inspect its statistics (available through the "statistics" button in the top menu) to see things like the distribution of execution times in a graph, or statistical aggregates such as mean, median, standard deviation, etc. It will look something like this:
48+
49+
![A graph and statistics in the Tracy GUI showing the distribution of execution times of an instrumented span in the application](https://user-images.githubusercontent.com/302146/163988464-86e1a3ee-e97b-49ae-9f7e-4ff2b8b761ad.png)
50+
1551
### Adding your own spans
1652

1753
Add spans to your app like this (these are in `bevy::prelude::*` and `bevy::log::*`, just like the normal logging macros).
1854

1955
```rust
2056
{
21-
// creates a span and starts the timer
57+
// creates a span and starts the timer
2258
let my_span = info_span!("span_name", name = "span_name").entered();
2359
do_something_here();
2460
} // my_span is dropped here ... this stops the timer
@@ -28,7 +64,7 @@ Add spans to your app like this (these are in `bevy::prelude::*` and `bevy::log:
2864
// Prefer the previous, simpler syntax unless you need the extra control.
2965
let my_span = info_span!("span_name", name = "span_name");
3066
{
31-
// starts the span's timer
67+
// starts the span's timer
3268
let guard = my_span.enter();
3369
do_something_here();
3470
} // guard is dropped here ... this stops the timer

0 commit comments

Comments
 (0)