Use more ergonomic span syntax (#4246)

Tracing added support for "inline span entering", which cuts down on a lot of complexity:

```rust
let span = info_span!("my_span").entered();
```

This adapts our code to use this pattern where possible, and updates our docs to recommend it.

This produces equivalent tracing behavior. Here is a side by side profile of "before" and "after" these changes.
![image](https://user-images.githubusercontent.com/2694663/158912137-b0aa6dc8-c603-425f-880f-6ccf5ad1b7ef.png)
This commit is contained in:
Carter Anderson 2022-03-18 04:19:21 +00:00
parent c1a2378790
commit de677dbfc9
9 changed files with 53 additions and 75 deletions

View file

@ -109,9 +109,7 @@ impl App {
/// See [`Schedule::run_once`] for more details. /// See [`Schedule::run_once`] for more details.
pub fn update(&mut self) { pub fn update(&mut self) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let bevy_frame_update_span = info_span!("frame"); let _bevy_frame_update_span = info_span!("frame").entered();
#[cfg(feature = "trace")]
let _bevy_frame_update_guard = bevy_frame_update_span.enter();
self.schedule.run(&mut self.world); self.schedule.run(&mut self.world);
for sub_app in self.sub_apps.values_mut() { for sub_app in self.sub_apps.values_mut() {
(sub_app.runner)(&mut self.world, &mut sub_app.app); (sub_app.runner)(&mut self.world, &mut sub_app.app);
@ -124,9 +122,7 @@ impl App {
/// level documentation. /// level documentation.
pub fn run(&mut self) { pub fn run(&mut self) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let bevy_app_run_span = info_span!("bevy_app"); let _bevy_app_run_span = info_span!("bevy_app").entered();
#[cfg(feature = "trace")]
let _bevy_app_run_guard = bevy_app_run_span.enter();
let mut app = std::mem::replace(self, App::empty()); let mut app = std::mem::replace(self, App::empty());
let runner = std::mem::replace(&mut app.runner, Box::new(run_once)); let runner = std::mem::replace(&mut app.runner, Box::new(run_once));

View file

@ -31,9 +31,8 @@ impl ParallelSystemExecutor for SingleThreadedExecutor {
for system in systems { for system in systems {
if system.should_run() { if system.should_run() {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let system_span = bevy_utils::tracing::info_span!("system", name = &*system.name()); let _system_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("system", name = &*system.name()).entered();
let _system_guard = system_span.enter();
system.system_mut().run((), world); system.system_mut().run((), world);
} }
} }

View file

@ -158,9 +158,7 @@ impl ParallelExecutor {
/// `update_archetypes` and updates cached `archetype_component_access`. /// `update_archetypes` and updates cached `archetype_component_access`.
fn update_archetypes(&mut self, systems: &mut [ParallelSystemContainer], world: &World) { fn update_archetypes(&mut self, systems: &mut [ParallelSystemContainer], world: &World) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let span = bevy_utils::tracing::info_span!("update_archetypes"); let _span = bevy_utils::tracing::info_span!("update_archetypes").entered();
#[cfg(feature = "trace")]
let _guard = span.enter();
let archetypes = world.archetypes(); let archetypes = world.archetypes();
let new_generation = archetypes.generation(); let new_generation = archetypes.generation();
let old_generation = std::mem::replace(&mut self.archetype_generation, new_generation); let old_generation = std::mem::replace(&mut self.archetype_generation, new_generation);
@ -186,9 +184,7 @@ impl ParallelExecutor {
world: &'scope World, world: &'scope World,
) { ) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let span = bevy_utils::tracing::info_span!("prepare_systems"); let _span = bevy_utils::tracing::info_span!("prepare_systems").entered();
#[cfg(feature = "trace")]
let _guard = span.enter();
self.should_run.clear(); self.should_run.clear();
for (index, (system_data, system)) in for (index, (system_data, system)) in
self.system_metadata.iter_mut().zip(systems).enumerate() self.system_metadata.iter_mut().zip(systems).enumerate()

View file

@ -336,9 +336,7 @@ impl Schedule {
pub fn run_once(&mut self, world: &mut World) { pub fn run_once(&mut self, world: &mut World) {
for label in &self.stage_order { for label in &self.stage_order {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = ?label); let _stage_span = bevy_utils::tracing::info_span!("stage", name = ?label).entered();
#[cfg(feature = "trace")]
let _stage_guard = stage_span.enter();
let stage = self.stages.get_mut(label).unwrap(); let stage = self.stages.get_mut(label).unwrap();
stage.run(world); stage.run(world);
} }

View file

@ -212,9 +212,8 @@ impl SystemStage {
for container in &mut self.parallel { for container in &mut self.parallel {
let system = container.system_mut(); let system = container.system_mut();
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let span = bevy_utils::tracing::info_span!("system_commands", name = &*system.name()); let _span = bevy_utils::tracing::info_span!("system_commands", name = &*system.name())
#[cfg(feature = "trace")] .entered();
let _guard = span.enter();
system.apply_buffers(world); system.apply_buffers(world);
} }
} }
@ -836,12 +835,11 @@ impl Stage for SystemStage {
for container in &mut self.exclusive_at_start { for container in &mut self.exclusive_at_start {
if should_run(container, &self.run_criteria, default_should_run) { if should_run(container, &self.run_criteria, default_should_run) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let system_span = bevy_utils::tracing::info_span!( let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system", "exclusive_system",
name = &*container.name() name = &*container.name()
); )
#[cfg(feature = "trace")] .entered();
let _guard = system_span.enter();
container.system_mut().run(world); container.system_mut().run(world);
} }
} }
@ -858,12 +856,11 @@ impl Stage for SystemStage {
for container in &mut self.exclusive_before_commands { for container in &mut self.exclusive_before_commands {
if should_run(container, &self.run_criteria, default_should_run) { if should_run(container, &self.run_criteria, default_should_run) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let system_span = bevy_utils::tracing::info_span!( let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system", "exclusive_system",
name = &*container.name() name = &*container.name()
); )
#[cfg(feature = "trace")] .entered();
let _guard = system_span.enter();
container.system_mut().run(world); container.system_mut().run(world);
} }
} }
@ -873,12 +870,11 @@ impl Stage for SystemStage {
for container in &mut self.parallel { for container in &mut self.parallel {
if container.should_run { if container.should_run {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let span = bevy_utils::tracing::info_span!( let _span = bevy_utils::tracing::info_span!(
"system_commands", "system_commands",
name = &*container.name() name = &*container.name()
); )
#[cfg(feature = "trace")] .entered();
let _guard = span.enter();
container.system_mut().apply_buffers(world); container.system_mut().apply_buffers(world);
} }
} }
@ -888,12 +884,11 @@ impl Stage for SystemStage {
for container in &mut self.exclusive_at_end { for container in &mut self.exclusive_at_end {
if should_run(container, &self.run_criteria, default_should_run) { if should_run(container, &self.run_criteria, default_should_run) {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let system_span = bevy_utils::tracing::info_span!( let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system", "exclusive_system",
name = &*container.name() name = &*container.name()
); )
#[cfg(feature = "trace")] .entered();
let _guard = system_span.enter();
container.system_mut().run(world); container.system_mut().run(world);
} }
} }

View file

@ -177,15 +177,12 @@ impl Plugin for RenderPlugin {
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| { app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let render_span = bevy_utils::tracing::info_span!("renderer subapp"); let _render_span = bevy_utils::tracing::info_span!("renderer subapp").entered();
#[cfg(feature = "trace")]
let _render_guard = render_span.enter();
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = let _stage_span =
bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush"); bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush")
#[cfg(feature = "trace")] .entered();
let _stage_guard = stage_span.enter();
// reserve all existing app entities for use in render_app // reserve all existing app entities for use in render_app
// they can only be spawned using `get_or_spawn()` // they can only be spawned using `get_or_spawn()`
@ -203,9 +200,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "extract"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "extract").entered();
let _stage_guard = stage_span.enter();
// extract // extract
extract(app_world, render_app); extract(app_world, render_app);
@ -213,9 +209,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "prepare"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "prepare").entered();
let _stage_guard = stage_span.enter();
// prepare // prepare
let prepare = render_app let prepare = render_app
@ -227,9 +222,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "queue"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "queue").entered();
let _stage_guard = stage_span.enter();
// queue // queue
let queue = render_app let queue = render_app
@ -241,9 +235,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "sort"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "sort").entered();
let _stage_guard = stage_span.enter();
// phase sort // phase sort
let phase_sort = render_app let phase_sort = render_app
@ -255,9 +248,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "render"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "render").entered();
let _stage_guard = stage_span.enter();
// render // render
let render = render_app let render = render_app
@ -269,9 +261,8 @@ impl Plugin for RenderPlugin {
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let stage_span = bevy_utils::tracing::info_span!("stage", name = "cleanup"); let _stage_span =
#[cfg(feature = "trace")] bevy_utils::tracing::info_span!("stage", name = "cleanup").entered();
let _stage_guard = stage_span.enter();
// cleanup // cleanup
let cleanup = render_app let cleanup = render_app

View file

@ -60,9 +60,7 @@ impl RenderGraphRunner {
Self::run_graph(graph, None, &mut render_context, world, &[])?; Self::run_graph(graph, None, &mut render_context, world, &[])?;
{ {
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let span = info_span!("submit_graph_commands"); let _span = info_span!("submit_graph_commands").entered();
#[cfg(feature = "trace")]
let _guard = span.enter();
queue.submit(vec![render_context.command_encoder.finish()]); queue.submit(vec![render_context.command_encoder.finish()]);
} }
Ok(()) Ok(())
@ -170,15 +168,12 @@ impl RenderGraphRunner {
smallvec![None; node_state.output_slots.len()]; smallvec![None; node_state.output_slots.len()];
{ {
let mut context = RenderGraphContext::new(graph, node_state, &inputs, &mut outputs); let mut context = RenderGraphContext::new(graph, node_state, &inputs, &mut outputs);
#[cfg(feature = "trace")] {
let span = info_span!("node", name = node_state.type_name); #[cfg(feature = "trace")]
#[cfg(feature = "trace")] let _span = info_span!("node", name = node_state.type_name).entered();
let guard = span.enter();
node_state.node.run(&mut context, render_context, world)?; node_state.node.run(&mut context, render_context, world)?;
}
#[cfg(feature = "trace")]
drop(guard);
for run_sub_graph in context.finish() { for run_sub_graph in context.finish() {
let sub_graph = graph let sub_graph = graph

View file

@ -45,8 +45,7 @@ pub fn render_system(world: &mut World) {
} }
{ {
let span = info_span!("present_frames"); let _span = info_span!("present_frames").entered();
let _guard = span.enter();
// Remove ViewTarget components to ensure swap chain TextureViews are dropped. // Remove ViewTarget components to ensure swap chain TextureViews are dropped.
// If all TextureViews aren't dropped before present, acquiring the next swap chain texture will fail. // If all TextureViews aren't dropped before present, acquiring the next swap chain texture will fail.

View file

@ -17,11 +17,20 @@ After running your app a `json` file in the "chrome tracing format" will be prod
Add spans to your app like this (these are in `bevy::prelude::*` and `bevy::log::*`, just like the normal logging macros). Add spans to your app like this (these are in `bevy::prelude::*` and `bevy::log::*`, just like the normal logging macros).
```rust ```rust
{
// creates a span and starts the timer
let my_span = info_span!("span_name", name = "span_name").entered();
do_something_here();
} // my_span is dropped here ... this stops the timer
// You can also "manually" enter the span if you need more control over when the timer starts
// Prefer the previous, simpler syntax unless you need the extra control.
let my_span = info_span!("span_name", name = "span_name"); let my_span = info_span!("span_name", name = "span_name");
{ {
// starts the span's timer // starts the span's timer
let guard = my_span.enter(); let guard = my_span.enter();
do_something_here();
} // guard is dropped here ... this stops the timer } // guard is dropped here ... this stops the timer
``` ```