Skip to content

Commit 064af63

Browse files
committed
Add trace_tracy feature for Tracy profiling (#2832)
# 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
1 parent 42409fc commit 064af63

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bevy_wgpu = ["bevy_internal/bevy_wgpu"]
5050
bevy_winit = ["bevy_internal/bevy_winit"]
5151

5252
trace_chrome = ["bevy_internal/trace_chrome"]
53+
trace_tracy = ["bevy_internal/trace_tracy"]
5354
trace = ["bevy_internal/trace"]
5455
wgpu_trace = ["bevy_internal/wgpu_trace"]
5556

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ categories = ["game-engines", "graphics", "gui", "rendering"]
1313
wgpu_trace = ["bevy_wgpu/trace"]
1414
trace = [ "bevy_app/trace", "bevy_ecs/trace" ]
1515
trace_chrome = [ "bevy_log/tracing-chrome" ]
16+
trace_tracy = [ "bevy_log/tracing-tracy" ]
1617

1718
# Image format support for texture loading (PNG and HDR are enabled by default)
1819
hdr = ["bevy_render/hdr"]

crates/bevy_log/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ keywords = ["bevy"]
1313
bevy_app = { path = "../bevy_app", version = "0.5.0" }
1414
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }
1515

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

1920
[target.'cfg(target_os = "android")'.dependencies]
2021
android_log-sys = "0.2.0"

crates/bevy_log/src/lib.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ impl Plugin for LogPlugin {
9494

9595
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
9696
{
97-
let fmt_layer = tracing_subscriber::fmt::Layer::default();
98-
let subscriber = subscriber.with(fmt_layer);
9997
#[cfg(feature = "tracing-chrome")]
100-
{
98+
let chrome_layer = {
10199
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
102100
.name_fn(Box::new(|event_or_span| match event_or_span {
103101
tracing_chrome::EventOrSpan::Event(event) => event.metadata().name().into(),
@@ -113,16 +111,22 @@ impl Plugin for LogPlugin {
113111
}))
114112
.build();
115113
app.world.insert_non_send(guard);
116-
let subscriber = subscriber.with(chrome_layer);
117-
bevy_utils::tracing::subscriber::set_global_default(subscriber)
118-
.expect("Could not set global default tracing subscriber. If you've already set up a tracing subscriber, please disable LogPlugin from Bevy's DefaultPlugins");
119-
}
114+
chrome_layer
115+
};
116+
117+
#[cfg(feature = "tracing-tracy")]
118+
let tracy_layer = tracing_tracy::TracyLayer::new();
119+
120+
let fmt_layer = tracing_subscriber::fmt::Layer::default();
121+
let subscriber = subscriber.with(fmt_layer);
122+
123+
#[cfg(feature = "tracing-chrome")]
124+
let subscriber = subscriber.with(chrome_layer);
125+
#[cfg(feature = "tracing-tracy")]
126+
let subscriber = subscriber.with(tracy_layer);
120127

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

128132
#[cfg(target_arch = "wasm32")]

docs/cargo_features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
|dynamic|Forces bevy to be dynamically linked, which improves iterative compile times.|
2424
|trace|Enables system tracing (useful in tandem with a feature like trace_chrome).|
2525
|trace_chrome|Enables [tracing-chrome](https://github.com/thoren-d/tracing-chrome) as bevy_log output. This allows you to visualize system execution.|
26+
|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.|
2627
|wgpu_trace|For tracing wgpu.|
2728
|dds|DDS picture format support.|
2829
|tga|TGA picture format support.|

0 commit comments

Comments
 (0)