Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache System Tracing Spans #9390

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
cache system spans
  • Loading branch information
hymm committed Aug 15, 2023
commit 32ac77d8c6b6ab812f1256c4d1df704484a210b2
29 changes: 24 additions & 5 deletions crates/bevy_ecs/src/schedule/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_tasks::{ComputeTaskPool, Scope, TaskPool, ThreadExecutor};
use bevy_utils::default;
use bevy_utils::syncunsafecell::SyncUnsafeCell;
#[cfg(feature = "trace")]
use bevy_utils::tracing::{info_span, Instrument};
use bevy_utils::tracing::{info_span, Instrument, Span};
use std::panic::AssertUnwindSafe;

use async_channel::{Receiver, Sender};
Expand Down Expand Up @@ -112,6 +112,12 @@ pub struct MultiThreadedExecutor {
panic_payload: Arc<Mutex<Option<Box<dyn Any + Send>>>>,
/// When set, stops the executor from running any more systems.
stop_spawning: bool,
/// Cached tracing spans for system tasks
#[cfg(feature = "trace")]
system_task_spans: Vec<Span>,
hymm marked this conversation as resolved.
Show resolved Hide resolved
/// Cached tracing spans for systems
#[cfg(feature = "trace")]
system_spans: Vec<Span>,
}

impl Default for MultiThreadedExecutor {
Expand Down Expand Up @@ -147,13 +153,24 @@ impl SystemExecutor for MultiThreadedExecutor {
self.unapplied_systems = FixedBitSet::with_capacity(sys_count);

self.system_task_metadata = Vec::with_capacity(sys_count);
#[cfg(feature = "trace")]
{
self.system_task_spans = Vec::with_capacity(sys_count);
self.system_spans = Vec::with_capacity(sys_count);
}
for index in 0..sys_count {
self.system_task_metadata.push(SystemTaskMetadata {
archetype_component_access: default(),
dependents: schedule.system_dependents[index].clone(),
is_send: schedule.systems[index].is_send(),
is_exclusive: schedule.systems[index].is_exclusive(),
});

#[cfg(feature = "trace")]
{
self.system_task_spans.push(info_span!("system_task", name = &*schedule.systems[index].name()));
hymm marked this conversation as resolved.
Show resolved Hide resolved
self.system_spans.push(info_span!("system", name = &*schedule.systems[index].name()));
}
}

self.num_dependencies_remaining = Vec::with_capacity(sys_count);
Expand Down Expand Up @@ -282,6 +299,10 @@ impl MultiThreadedExecutor {
apply_final_deferred: true,
panic_payload: Arc::new(Mutex::new(None)),
stop_spawning: false,
#[cfg(feature = "trace")]
system_task_spans: Vec::new(),
#[cfg(feature = "trace")]
system_spans: Vec::new(),
}
}

Expand Down Expand Up @@ -488,9 +509,7 @@ impl MultiThreadedExecutor {
let system = unsafe { &mut *systems[system_index].get() };

#[cfg(feature = "trace")]
let task_span = info_span!("system_task", name = &*system.name());
#[cfg(feature = "trace")]
let system_span = info_span!("system", name = &*system.name());
let system_span = self.system_spans[system_index].clone();

let sender = self.sender.clone();
let panic_payload = self.panic_payload.clone();
Expand Down Expand Up @@ -524,7 +543,7 @@ impl MultiThreadedExecutor {
};

#[cfg(feature = "trace")]
let task = task.instrument(task_span);
let task = task.instrument(self.system_task_spans[system_index].clone());

let system_meta = &self.system_task_metadata[system_index];
self.active_access
Expand Down