From 5a48251486c7447c904a2a756fda5210d9005f69 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 16 Sep 2021 07:19:59 +0200 Subject: [PATCH 1/4] Add trace_tracy feature for Tracy profiling --- Cargo.toml | 1 + crates/bevy_internal/Cargo.toml | 1 + crates/bevy_log/Cargo.toml | 5 +++-- crates/bevy_log/src/lib.rs | 12 ++++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0fafd91be7970..7a0eec40f6455 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index de6f3607a2acc..f7b3f79f4b7a5 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -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"] diff --git a/crates/bevy_log/Cargo.toml b/crates/bevy_log/Cargo.toml index d4e63e9640db2..42dcf98a88bc5 100644 --- a/crates/bevy_log/Cargo.toml +++ b/crates/bevy_log/Cargo.toml @@ -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" diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index 98abddc89d929..085845a5b1ab6 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -96,7 +96,7 @@ impl Plugin for LogPlugin { { let fmt_layer = tracing_subscriber::fmt::Layer::default(); let subscriber = subscriber.with(fmt_layer); - #[cfg(feature = "tracing-chrome")] + #[cfg(all(feature = "tracing-chrome", not(feature = "tracing-tracy")))] { let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new() .name_fn(Box::new(|event_or_span| match event_or_span { @@ -118,7 +118,15 @@ impl Plugin for LogPlugin { .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(not(feature = "tracing-chrome"))] + #[cfg(all(feature = "tracing-tracy", not(feature = "tracing-chrome")))] + { + let tracy_layer = tracing_tracy::TracyLayer::new(); + let subscriber = subscriber.with(tracy_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"); + } + + #[cfg(all(not(feature = "tracing-chrome"), not(feature = "tracing-tracy")))] { 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"); From 5a60cc348b5d190cc8b8a3787f25bb77c8ce5464 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 16 Sep 2021 10:26:11 +0200 Subject: [PATCH 2/4] bevy_log: Make it possible to use tracy and chrome tracing simultaneously --- crates/bevy_log/src/lib.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index 085845a5b1ab6..f63df751f24a7 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -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(all(feature = "tracing-chrome", not(feature = "tracing-tracy")))] - { + #[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(), @@ -113,24 +111,24 @@ 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(all(feature = "tracing-tracy", not(feature = "tracing-chrome")))] - { - let tracy_layer = tracing_tracy::TracyLayer::new(); - let subscriber = subscriber.with(tracy_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"); - } + #[cfg(all(feature = "tracing-chrome", feature = "tracing-tracy"))] + let subscriber = subscriber.with(chrome_layer).with(tracy_layer); + #[cfg(all(feature = "tracing-chrome", not(feature = "tracing-tracy")))] + let subscriber = subscriber.with(chrome_layer); + #[cfg(all(not(feature = "tracing-chrome"), feature = "tracing-tracy"))] + let subscriber = subscriber.with(tracy_layer); - #[cfg(all(not(feature = "tracing-chrome"), not(feature = "tracing-tracy")))] - { - 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")] From 8a13e5e7219e2698141cadf092d7da3dcccfe2d9 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 16 Sep 2021 10:29:26 +0200 Subject: [PATCH 3/4] cargo_features.md: Document optional trace_tracy feature --- docs/cargo_features.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/cargo_features.md b/docs/cargo_features.md index b7dd276f2b73d..839515e17da37 100644 --- a/docs/cargo_features.md +++ b/docs/cargo_features.md @@ -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.| From 328ddcabd6f7e4dbd9bafd511cafbc589b11e796 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 16 Sep 2021 14:59:03 +0200 Subject: [PATCH 4/4] bevy_log: Simplify subscriber logic --- crates/bevy_log/src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index f63df751f24a7..3578ad3d57d83 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -120,11 +120,9 @@ impl Plugin for LogPlugin { let fmt_layer = tracing_subscriber::fmt::Layer::default(); let subscriber = subscriber.with(fmt_layer); - #[cfg(all(feature = "tracing-chrome", feature = "tracing-tracy"))] - let subscriber = subscriber.with(chrome_layer).with(tracy_layer); - #[cfg(all(feature = "tracing-chrome", not(feature = "tracing-tracy")))] + #[cfg(feature = "tracing-chrome")] let subscriber = subscriber.with(chrome_layer); - #[cfg(all(not(feature = "tracing-chrome"), feature = "tracing-tracy"))] + #[cfg(feature = "tracing-tracy")] let subscriber = subscriber.with(tracy_layer); bevy_utils::tracing::subscriber::set_global_default(subscriber)