Skip to content

Commit

Permalink
Add trace_tracy feature for Tracy profiling (#2832)
Browse files Browse the repository at this point in the history
# Objective

[Tracy](https://github.com/wolfpld/tracy) is:
> A real time, nanosecond resolution, remote telemetry, hybrid frame and sampling profiler for games and other applications.

With the `trace_tracy` feature enabled, you run your bevy app and either a headless server (`capture`) or a live, interactive profiler UI (`Tracy`), and connect that to your bevy application to then stream the metric data and events, and save it or inspect it live/offline.

Previously when I implemented the spans across systems and stages and I was trying out different profiling tools, Tracy was too unstable on macOS to use. But now, quite some months later, it is working stably with Tracy 0.7.8. You can see timelines, aggregate statistics of mean system/stage execution times, and much more. It's very useful!

![Screenshot_2021-09-15_at_18 07 19](https://user-images.githubusercontent.com/302146/133554920-350d3d45-fbb8-479f-91f7-7a7a4f9f5873.png)

## Solution

- Use the `tracing-tracy` crate which supports our tracing spans
- Expose via the non-default feature `trace_tracy` for consistency with other `trace*` features
  • Loading branch information
superdump committed Sep 16, 2021
1 parent 42409fc commit 064af63
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bevy_wgpu = ["bevy_internal/bevy_wgpu"]
bevy_winit = ["bevy_internal/bevy_winit"]

trace_chrome = ["bevy_internal/trace_chrome"]
trace_tracy = ["bevy_internal/trace_tracy"]
trace = ["bevy_internal/trace"]
wgpu_trace = ["bevy_internal/wgpu_trace"]

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["game-engines", "graphics", "gui", "rendering"]
wgpu_trace = ["bevy_wgpu/trace"]
trace = [ "bevy_app/trace", "bevy_ecs/trace" ]
trace_chrome = [ "bevy_log/tracing-chrome" ]
trace_tracy = [ "bevy_log/tracing-tracy" ]

# Image format support for texture loading (PNG and HDR are enabled by default)
hdr = ["bevy_render/hdr"]
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

tracing-subscriber = {version = "0.2.15", features = ["registry"]}
tracing-chrome = { version = "0.3.0", optional = true }
tracing-subscriber = {version = "0.2.22", features = ["registry"]}
tracing-chrome = { version = "0.3.1", optional = true }
tracing-tracy = { version = "0.7.0", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
android_log-sys = "0.2.0"
Expand Down
28 changes: 16 additions & 12 deletions crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ impl Plugin for LogPlugin {

#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
{
let fmt_layer = tracing_subscriber::fmt::Layer::default();
let subscriber = subscriber.with(fmt_layer);
#[cfg(feature = "tracing-chrome")]
{
let chrome_layer = {
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
.name_fn(Box::new(|event_or_span| match event_or_span {
tracing_chrome::EventOrSpan::Event(event) => event.metadata().name().into(),
Expand All @@ -113,16 +111,22 @@ impl Plugin for LogPlugin {
}))
.build();
app.world.insert_non_send(guard);
let subscriber = subscriber.with(chrome_layer);
bevy_utils::tracing::subscriber::set_global_default(subscriber)
.expect("Could not set global default tracing subscriber. If you've already set up a tracing subscriber, please disable LogPlugin from Bevy's DefaultPlugins");
}
chrome_layer
};

#[cfg(feature = "tracing-tracy")]
let tracy_layer = tracing_tracy::TracyLayer::new();

let fmt_layer = tracing_subscriber::fmt::Layer::default();
let subscriber = subscriber.with(fmt_layer);

#[cfg(feature = "tracing-chrome")]
let subscriber = subscriber.with(chrome_layer);
#[cfg(feature = "tracing-tracy")]
let subscriber = subscriber.with(tracy_layer);

#[cfg(not(feature = "tracing-chrome"))]
{
bevy_utils::tracing::subscriber::set_global_default(subscriber)
.expect("Could not set global default tracing subscriber. If you've already set up a tracing subscriber, please disable LogPlugin from Bevy's DefaultPlugins");
}
bevy_utils::tracing::subscriber::set_global_default(subscriber)
.expect("Could not set global default tracing subscriber. If you've already set up a tracing subscriber, please disable LogPlugin from Bevy's DefaultPlugins");
}

#[cfg(target_arch = "wasm32")]
Expand Down
1 change: 1 addition & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
|dynamic|Forces bevy to be dynamically linked, which improves iterative compile times.|
|trace|Enables system tracing (useful in tandem with a feature like trace_chrome).|
|trace_chrome|Enables [tracing-chrome](https://github.com/thoren-d/tracing-chrome) as bevy_log output. This allows you to visualize system execution.|
|trace_tracy|Enables [Tracy](https://github.com/wolfpld/tracy) as bevy_log output. This allows `Tracy` to connect to and capture profiling data as well as visualize system execution in real-time, present statistics about system execution times, and more.|
|wgpu_trace|For tracing wgpu.|
|dds|DDS picture format support.|
|tga|TGA picture format support.|
Expand Down

0 comments on commit 064af63

Please sign in to comment.