Skip to content

Commit

Permalink
move span into system run
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Aug 15, 2023
1 parent 5abf79e commit 4e5ac92
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 36 deletions.
24 changes: 0 additions & 24 deletions crates/bevy_ecs/src/schedule/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ struct SystemTaskMetadata {
/// Cached tracing span for system task
#[cfg(feature = "trace")]
system_task_span: Span,
/// Cached tracing span for system
#[cfg(feature = "trace")]
system_span: Span,
}

/// The result of running a system that is sent across a channel.
Expand Down Expand Up @@ -164,8 +161,6 @@ impl SystemExecutor for MultiThreadedExecutor {
"system_task",
name = &*schedule.systems[index].name()
),
#[cfg(feature = "trace")]
system_span: info_span!("system", name = &*schedule.systems[index].name()),
});
}

Expand Down Expand Up @@ -499,24 +494,16 @@ impl MultiThreadedExecutor {
) {
// SAFETY: this system is not running, no other reference exists
let system = unsafe { &mut *systems[system_index].get() };

#[cfg(feature = "trace")]
let system_span = self.system_task_metadata[system_index].system_span.clone();

let sender = self.sender.clone();
let panic_payload = self.panic_payload.clone();
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
// SAFETY:
// - The caller ensures that we have permission to
// access the world data used by the system.
// - `update_archetype_component_access` has been called.
unsafe { system.run_unsafe((), world) };
}));
#[cfg(feature = "trace")]
drop(system_guard);
// tell the executor that the system finished
sender
.try_send(SystemResult {
Expand Down Expand Up @@ -565,21 +552,14 @@ impl MultiThreadedExecutor {
// SAFETY: this system is not running, no other reference exists
let system = unsafe { &mut *systems[system_index].get() };

#[cfg(feature = "trace")]
let system_span = self.system_task_metadata[system_index].system_span.clone();

let sender = self.sender.clone();
let panic_payload = self.panic_payload.clone();
if is_apply_deferred(system) {
// TODO: avoid allocation
let unapplied_systems = self.unapplied_systems.clone();
self.unapplied_systems.clear();
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
let res = apply_deferred(&unapplied_systems, systems, world);
#[cfg(feature = "trace")]
drop(system_guard);
// tell the executor that the system finished
sender
.try_send(SystemResult {
Expand All @@ -603,13 +583,9 @@ impl MultiThreadedExecutor {
scope.spawn_on_scope(task);
} else {
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
system.run((), world);
}));
#[cfg(feature = "trace")]
drop(system_guard);
// tell the executor that the system finished
sender
.try_send(SystemResult {
Expand Down
4 changes: 0 additions & 4 deletions crates/bevy_ecs/src/schedule/executor/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,9 @@ impl SystemExecutor for SimpleExecutor {
}

let system = &mut schedule.systems[system_index];
#[cfg(feature = "trace")]
let system_span = info_span!("system", name = &*name).entered();
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
system.run((), world);
}));
#[cfg(feature = "trace")]
system_span.exit();
if let Err(payload) = res {
eprintln!("Encountered a panic in system `{}`!", &*system.name());
std::panic::resume_unwind(payload);
Expand Down
8 changes: 0 additions & 8 deletions crates/bevy_ecs/src/schedule/executor/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,11 @@ impl SystemExecutor for SingleThreadedExecutor {

let system = &mut schedule.systems[system_index];
if is_apply_deferred(system) {
#[cfg(feature = "trace")]
let system_span = info_span!("system", name = &*name).entered();
self.apply_deferred(schedule, world);
#[cfg(feature = "trace")]
system_span.exit();
} else {
#[cfg(feature = "trace")]
let system_span = info_span!("system", name = &*name).entered();
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
system.run((), world);
}));
#[cfg(feature = "trace")]
system_span.exit();
if let Err(payload) = res {
eprintln!("Encountered a panic in system `{}`!", &*system.name());
std::panic::resume_unwind(payload);
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_ecs/src/system/exclusive_function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ where
}

fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {
#[cfg(feature = "trace")]
let _span_guard = self.system_meta.system_span.enter();

let saved_last_tick = world.last_change_tick;
world.last_change_tick = self.system_meta.last_run;

Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct SystemMeta {
is_send: bool,
pub(crate) last_run: Tick,
#[cfg(feature = "trace")]
pub(crate) system_span: Span,
#[cfg(feature = "trace")]
pub(crate) commands_span: Span,
}

Expand All @@ -39,6 +41,8 @@ impl SystemMeta {
is_send: true,
last_run: Tick::new(0),
#[cfg(feature = "trace")]
system_span: info_span!("system", name = name),
#[cfg(feature = "trace")]
commands_span: info_span!("system_commands", name = name),
}
}
Expand Down Expand Up @@ -452,6 +456,9 @@ where

#[inline]
unsafe fn run_unsafe(&mut self, input: Self::In, world: UnsafeWorldCell) -> Self::Out {
#[cfg(feature = "trace")]
let _span_guard = self.system_meta.system_span.enter();

let change_tick = world.increment_change_tick();

// SAFETY:
Expand Down

0 comments on commit 4e5ac92

Please sign in to comment.