Skip to content

Commit 124f65a

Browse files
atlv24hymmmockersf
authored
add plugin tracing spans (#20722)
# Objective - easily diagnose startup performance issues ## Solution - add plugin tracing spans ## Testing - i have not been able to get any tracing to work in any of bevy. idk if this code works, but it looks like it should. is tracing broken or do i have a setup issue? --------- Co-authored-by: Mike Hsu <mike.hsu@gmail.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
1 parent 99d511a commit 124f65a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

crates/bevy_app/src/app.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,16 @@ impl App {
255255
/// Runs [`Plugin::finish`] for each plugin. This is usually called by the event loop once all
256256
/// plugins are ready, but can be useful for situations where you want to use [`App::update`].
257257
pub fn finish(&mut self) {
258+
#[cfg(feature = "trace")]
259+
let _finish_span = info_span!("plugin finish").entered();
258260
// plugins installed to main should see all sub-apps
259261
// do hokey pokey with a boxed zst plugin (doesn't allocate)
260262
let mut hokeypokey: Box<dyn Plugin> = Box::new(HokeyPokey);
261263
for i in 0..self.main().plugin_registry.len() {
262264
core::mem::swap(&mut self.main_mut().plugin_registry[i], &mut hokeypokey);
265+
#[cfg(feature = "trace")]
266+
let _plugin_finish_span =
267+
info_span!("plugin finish", plugin = hokeypokey.name()).entered();
263268
hokeypokey.finish(self);
264269
core::mem::swap(&mut self.main_mut().plugin_registry[i], &mut hokeypokey);
265270
}
@@ -270,11 +275,16 @@ impl App {
270275
/// Runs [`Plugin::cleanup`] for each plugin. This is usually called by the event loop after
271276
/// [`App::finish`], but can be useful for situations where you want to use [`App::update`].
272277
pub fn cleanup(&mut self) {
278+
#[cfg(feature = "trace")]
279+
let _cleanup_span = info_span!("plugin cleanup").entered();
273280
// plugins installed to main should see all sub-apps
274281
// do hokey pokey with a boxed zst plugin (doesn't allocate)
275282
let mut hokeypokey: Box<dyn Plugin> = Box::new(HokeyPokey);
276283
for i in 0..self.main().plugin_registry.len() {
277284
core::mem::swap(&mut self.main_mut().plugin_registry[i], &mut hokeypokey);
285+
#[cfg(feature = "trace")]
286+
let _plugin_cleanup_span =
287+
info_span!("plugin cleanup", plugin = hokeypokey.name()).entered();
278288
hokeypokey.cleanup(self);
279289
core::mem::swap(&mut self.main_mut().plugin_registry[i], &mut hokeypokey);
280290
}
@@ -481,6 +491,9 @@ impl App {
481491

482492
self.main_mut().plugin_build_depth += 1;
483493

494+
#[cfg(feature = "trace")]
495+
let _plugin_build_span = info_span!("plugin build", plugin = plugin.name()).entered();
496+
484497
let f = AssertUnwindSafe(|| plugin.build(self));
485498

486499
#[cfg(feature = "std")]

crates/bevy_app/src/sub_app.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ impl SubApp {
405405
let mut hokeypokey: Box<dyn Plugin> = Box::new(crate::HokeyPokey);
406406
for i in 0..self.plugin_registry.len() {
407407
core::mem::swap(&mut self.plugin_registry[i], &mut hokeypokey);
408+
#[cfg(feature = "trace")]
409+
let _plugin_finish_span =
410+
info_span!("plugin finish", plugin = hokeypokey.name()).entered();
408411
self.run_as_app(|app| {
409412
hokeypokey.finish(app);
410413
});
@@ -419,6 +422,9 @@ impl SubApp {
419422
let mut hokeypokey: Box<dyn Plugin> = Box::new(crate::HokeyPokey);
420423
for i in 0..self.plugin_registry.len() {
421424
core::mem::swap(&mut self.plugin_registry[i], &mut hokeypokey);
425+
#[cfg(feature = "trace")]
426+
let _plugin_cleanup_span =
427+
info_span!("plugin cleanup", plugin = hokeypokey.name()).entered();
422428
self.run_as_app(|app| {
423429
hokeypokey.cleanup(app);
424430
});

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ impl SystemMeta {
4343
pub(crate) fn new<T>() -> Self {
4444
let name = DebugName::type_name::<T>();
4545
Self {
46+
// These spans are initialized during plugin build, so we set the parent to `None` to prevent
47+
// them from being children of the span that is measuring the plugin build time.
4648
#[cfg(feature = "trace")]
47-
system_span: info_span!("system", name = name.clone().as_string()),
49+
system_span: info_span!(parent: None, "system", name = name.clone().as_string()),
4850
#[cfg(feature = "trace")]
49-
commands_span: info_span!("system_commands", name = name.clone().as_string()),
51+
commands_span: info_span!(parent: None, "system_commands", name = name.clone().as_string()),
5052
name,
5153
flags: SystemStateFlags::empty(),
5254
last_run: Tick::new(0),
@@ -68,8 +70,8 @@ impl SystemMeta {
6870
#[cfg(feature = "trace")]
6971
{
7072
let name = new_name.as_ref();
71-
self.system_span = info_span!("system", name = name);
72-
self.commands_span = info_span!("system_commands", name = name);
73+
self.system_span = info_span!(parent: None, "system", name = name);
74+
self.commands_span = info_span!(parent: None, "system_commands", name = name);
7375
}
7476
self.name = new_name.into();
7577
}

0 commit comments

Comments
 (0)