Skip to content

Commit ed43890

Browse files
feat(assets): Add feature flag cid_debug_trace to bones_asset (#422)
With feature flag, during asset load, each input used to update cid are tracked and then logged. One issue is that for dep_cid, I tried to get the AssetLoc for deps, but it appears to be None. Would be good to improve with some identifying information on dependency, right now just logs dep_cid and asset cid after update. If dep_cid differs, hard to tell which it is / why. Dump for each asset is a single trace call, which hopefully will avoid any interleaved output as this runs in parallel for multiple assets. Sample output: ``` 2024-07-11T01:45:13.558171Z INFO bones_asset::server: Cid trace schema: u!("jumpy::GameMeta") file path: "/game.yaml" Trace is in order of updates, which impacts result [Intermediate] Cid from schema fullname: u!("jumpy::GameMeta") cid: GqGtXP1KVcKyCvAHaUFYKt6miTj83mNfamjHYhfnScDm [Intermediate] Cid from contents: cid: Ee6Z9iFMXp9PZkHgH5UCdbT9RQqy1UwUBZPePWaqUXgu Dumping updates from sorted dependency cids: dep_cid: 1sp1JT5VhgEas7h5A55pmMLSo7SGj44VtnwKF3xVtjq, cid: 5MtTGxtP5C2ytUANTrQRw8cufhRvVykGALggTzgY1eFA, dep_asset_loc: None ... <truncated, quite long> ... dep_cid: 5VYCqN78NFaPYig354UZW9Pu6Bj8qoLHCDP59UXF2cr, cid: 8NCDAQU5ETULQ78FcSWtyHZePi5iWZciyAoRF2C2fyET, dep_asset_loc: None Final cid: A71MVvdDbboEKKx8tQULZh9baqoqDQn45PDeS6QxWrWQ ``` Hopefully useful for issues like: fishfolk/jumpy#995
1 parent cfce040 commit ed43890

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

framework_crates/bones_asset/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ keywords.workspace = true
1313
[features]
1414
default = []
1515

16+
# Enables debug logging of asset cid computation during loading.
17+
cid_debug_trace = []
18+
1619
[dependencies]
1720
bones_utils = { version = "0.3", path = "../bones_utils", features = ["serde"] }
1821
bones_schema = { version = "0.3", path = "../bones_schema", features = ["serde"] }

framework_crates/bones_asset/src/cid.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use serde::{Deserialize, Serialize};
22

3+
#[cfg(feature = "cid_debug_trace")]
4+
pub(crate) use cid_debug_trace::*;
5+
36
/// A unique content ID.
47
///
58
/// Represents the Sha-256 hash of the contents of a [`LoadedAsset`][crate::LoadedAsset].
@@ -31,3 +34,79 @@ impl Cid {
3134
self.0.copy_from_slice(&result);
3235
}
3336
}
37+
38+
#[cfg(feature = "cid_debug_trace")]
39+
mod cid_debug_trace {
40+
41+
use crate::{AssetLoc, Cid};
42+
use std::path::Path;
43+
44+
use bones_utils::{default, Ustr};
45+
46+
pub(crate) struct CidDebugTrace<'a> {
47+
pub schema_full_name: Ustr,
48+
pub file_path: &'a Path,
49+
50+
pub cid_after_schema_fullname: Cid,
51+
pub cid_after_contents: Cid,
52+
53+
/// Tuple of dep_cid, updated cid, and dep asset loc
54+
pub cid_after_deps: Vec<(Cid, Cid, Option<AssetLoc>)>,
55+
56+
pub final_cid: Cid,
57+
}
58+
59+
impl<'a> CidDebugTrace<'a> {
60+
pub(crate) fn new(schema_full_name: Ustr, file_path: &'a Path) -> Self {
61+
Self {
62+
schema_full_name,
63+
file_path,
64+
cid_after_schema_fullname: default(),
65+
cid_after_contents: default(),
66+
cid_after_deps: default(),
67+
final_cid: default(),
68+
}
69+
}
70+
}
71+
72+
impl<'a> std::fmt::Display for CidDebugTrace<'a> {
73+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74+
// dump asset meta
75+
writeln!(
76+
f,
77+
"Cid trace schema: {:?} file path: {:?}",
78+
self.schema_full_name, self.file_path
79+
)?;
80+
writeln!(f, "Trace is in order of updates, which impacts result")?;
81+
82+
// cid schema fullname update
83+
writeln!(
84+
f,
85+
"[Intermediate] Cid from schema fullname: {:?} cid: {}",
86+
self.schema_full_name, self.cid_after_schema_fullname
87+
)?;
88+
89+
// cid content update
90+
writeln!(
91+
f,
92+
"[Intermediate] Cid from contents: cid: {}",
93+
self.cid_after_contents
94+
)?;
95+
96+
// cid dependency update
97+
writeln!(f, "Dumping updates from sorted dependency cids:")?;
98+
for (dep_cid, updated_cid, dep_asset_loc) in self.cid_after_deps.iter() {
99+
writeln!(
100+
f,
101+
" dep_cid: {}, cid: {}, dep_asset_loc: {:?}",
102+
dep_cid, updated_cid, dep_asset_loc
103+
)?;
104+
}
105+
106+
// final cid
107+
writeln!(f, "Final cid: {}", self.final_cid)?;
108+
109+
Ok(())
110+
}
111+
}
112+
}

framework_crates/bones_asset/src/server.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ use semver::VersionReq;
1919
use serde::{de::DeserializeSeed, Deserialize};
2020
use ulid::Ulid;
2121

22+
#[allow(unused_imports)]
23+
use tracing::info;
24+
2225
use crate::prelude::*;
26+
2327
use bones_utils::{
2428
parking_lot::{MappedMutexGuard, MutexGuard},
2529
*,
@@ -610,11 +614,29 @@ impl AssetServer {
610614
let mut dependencies = Vec::new();
611615

612616
let mut cid = Cid::default();
617+
618+
// NOTE: If changing cid computation logic, please update `CidDebugTrace` impl if possible.
619+
//
620+
// Tracks inputs to asset cid for debug tracing.
621+
#[cfg(feature = "cid_debug_trace")]
622+
let mut cid_debug = CidDebugTrace::new(schema.full_name, loc.path);
623+
613624
// Use the schema name and the file contents to create a unique, content-addressed ID for
614625
// the asset.
615626
cid.update(schema.full_name.as_bytes());
627+
628+
#[cfg(feature = "cid_debug_trace")]
629+
{
630+
cid_debug.cid_after_schema_fullname = cid;
631+
}
632+
616633
cid.update(contents);
617634

635+
#[cfg(feature = "cid_debug_trace")]
636+
{
637+
cid_debug.cid_after_contents = cid;
638+
}
639+
618640
let loader = MetaAssetLoadCtx {
619641
server: self,
620642
loc,
@@ -654,6 +676,22 @@ impl AssetServer {
654676
dep_cids.sort();
655677
for dep_cid in dep_cids {
656678
cid.update(dep_cid.0.as_slice());
679+
680+
#[cfg(feature = "cid_debug_trace")]
681+
{
682+
// TODO: Get dep_cid asset_loc for cid debug trace,
683+
// It is currently None, needs fix / alterative idenifying metadata.
684+
685+
let asset_loc = self.store.assets.get(&cid).map(|x| x.loc.clone());
686+
cid_debug.cid_after_deps.push((dep_cid, cid, asset_loc));
687+
}
688+
}
689+
690+
#[cfg(feature = "cid_debug_trace")]
691+
{
692+
// log asset cid trace
693+
cid_debug.final_cid = cid;
694+
info!("{cid_debug}");
657695
}
658696

659697
Ok(PartialAsset {

0 commit comments

Comments
 (0)