Skip to content

Instrument asset loading and processing. #12988

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

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ embedded_watcher = ["file_watcher"]
multi-threaded = ["bevy_tasks/multi-threaded"]
asset_processor = []
watch = []
trace = []

[dependencies]
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" }
Expand Down
38 changes: 38 additions & 0 deletions crates/bevy_asset/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ use crate::{
use bevy_ecs::prelude::*;
use bevy_tasks::IoTaskPool;
use bevy_utils::tracing::{debug, error, trace, warn};
#[cfg(feature = "trace")]
use bevy_utils::{
tracing::{info_span, instrument::Instrument},
ConditionalSendFuture,
};
use bevy_utils::{HashMap, HashSet};
use futures_io::ErrorKind;
use futures_lite::{AsyncReadExt, AsyncWriteExt, StreamExt};
Expand Down Expand Up @@ -479,6 +484,8 @@ impl AssetProcessor {
/// Register a new asset processor.
pub fn register_processor<P: Process>(&self, processor: P) {
let mut process_plans = self.data.processors.write();
#[cfg(feature = "trace")]
let processor = InstrumentedAssetProcessor(processor);
process_plans.insert(std::any::type_name::<P>(), Arc::new(processor));
}

Expand Down Expand Up @@ -1031,6 +1038,37 @@ impl AssetProcessorData {
}
}

#[cfg(feature = "trace")]
struct InstrumentedAssetProcessor<T>(T);

#[cfg(feature = "trace")]
impl<T: Process> Process for InstrumentedAssetProcessor<T> {
type Settings = T::Settings;
type OutputLoader = T::OutputLoader;

fn process<'a>(
&'a self,
context: &'a mut ProcessContext,
meta: AssetMeta<(), Self>,
writer: &'a mut crate::io::Writer,
) -> impl ConditionalSendFuture<
Output = Result<<Self::OutputLoader as crate::AssetLoader>::Settings, ProcessError>,
> {
// Change the processor type for the `AssetMeta`, which works because we share the `Settings` type.
let meta = AssetMeta {
meta_format_version: meta.meta_format_version,
processed_info: meta.processed_info,
asset: meta.asset,
};
let span = info_span!(
"asset processing",
processor = std::any::type_name::<T>(),
asset = context.path().to_string(),
);
self.0.process(context, meta, writer).instrument(span)
}
}

/// The (successful) result of processing an asset
#[derive(Debug, Clone)]
pub enum ProcessResult {
Expand Down
37 changes: 36 additions & 1 deletion crates/bevy_asset/src/server/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use crate::{
use async_broadcast::RecvError;
use bevy_tasks::IoTaskPool;
use bevy_utils::tracing::{error, warn};
#[cfg(feature = "trace")]
use bevy_utils::{
tracing::{info_span, instrument::Instrument},
ConditionalSendFuture,
};
use bevy_utils::{HashMap, TypeIdMap};
use std::{any::TypeId, sync::Arc};
use thiserror::Error;
Expand All @@ -30,6 +35,8 @@ impl AssetLoaders {
let loader_asset_type = TypeId::of::<L::Asset>();
let loader_asset_type_name = std::any::type_name::<L::Asset>();

#[cfg(feature = "trace")]
let loader = InstrumentedAssetLoader(loader);
let loader = Arc::new(loader);

let (loader_index, is_new) =
Expand All @@ -41,7 +48,7 @@ impl AssetLoaders {

if is_new {
let mut duplicate_extensions = Vec::new();
for extension in loader.extensions() {
for extension in AssetLoader::extensions(&*loader) {
let list = self
.extension_to_loaders
.entry((*extension).into())
Expand Down Expand Up @@ -292,6 +299,34 @@ impl MaybeAssetLoader {
}
}

#[cfg(feature = "trace")]
struct InstrumentedAssetLoader<T>(T);

#[cfg(feature = "trace")]
impl<T: AssetLoader> AssetLoader for InstrumentedAssetLoader<T> {
type Asset = T::Asset;
type Settings = T::Settings;
type Error = T::Error;

fn load<'a>(
&'a self,
reader: &'a mut crate::io::Reader,
settings: &'a Self::Settings,
load_context: &'a mut crate::LoadContext,
) -> impl ConditionalSendFuture<Output = Result<Self::Asset, Self::Error>> {
let span = info_span!(
"asset loading",
loader = std::any::type_name::<T>(),
asset = load_context.asset_path().to_string(),
);
self.0.load(reader, settings, load_context).instrument(span)
}

fn extensions(&self) -> &[&str] {
self.0.extensions()
}
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = ["game-engines", "graphics", "gui", "rendering"]
[features]
trace = [
"bevy_app/trace",
"bevy_asset?/trace",
"bevy_core_pipeline?/trace",
"bevy_ecs/trace",
"bevy_log/trace",
Expand Down