euv_core/runtime/assets/struct.rs
1use super::*;
2
3/// Generation-stamped handle preventing ABA aliasing after asset eviction or release.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
5pub struct AssetHandle {
6 /// Zero-based slot index inside `AssetManager`.
7 #[get(type(copy))]
8 pub(crate) slot_id: u32,
9 /// Monotonic slot generation stamped when the handle was issued.
10 #[get(type(copy))]
11 pub(crate) generation: u32,
12}
13
14/// Explicit RAM and VRAM memory ceiling configuration for `AssetManager`.
15#[derive(Clone, Copy, Data, Debug, Eq, New, PartialEq)]
16pub struct AssetBudgetConfig {
17 /// Maximum resident CPU RAM bytes permitted before LRU eviction triggers.
18 #[get(type(copy))]
19 pub(crate) ram_budget_bytes: usize,
20 /// Maximum resident GPU VRAM bytes permitted before LRU eviction triggers.
21 #[get(type(copy))]
22 pub(crate) vram_budget_bytes: usize,
23}
24
25/// Telemetry snapshot summarizing residency, budgets, and cache efficiency for `AssetManager`.
26#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
27pub struct AssetManagerStats {
28 /// Count of resident `Ready` assets.
29 #[get(type(copy))]
30 pub(crate) live_assets: usize,
31 /// Count of in-flight cancelable `Pending` loads.
32 #[get(type(copy))]
33 pub(crate) pending_assets: usize,
34 /// Currently resident CPU RAM bytes across live assets.
35 #[get(type(copy))]
36 pub(crate) ram_used_bytes: usize,
37 /// Configured CPU RAM byte ceiling.
38 #[get(type(copy))]
39 pub(crate) ram_budget_bytes: usize,
40 /// Currently resident GPU VRAM bytes across live assets.
41 #[get(type(copy))]
42 pub(crate) vram_used_bytes: usize,
43 /// Configured GPU VRAM byte ceiling.
44 #[get(type(copy))]
45 pub(crate) vram_budget_bytes: usize,
46 /// Cumulative cache lookup hits.
47 #[get(type(copy))]
48 pub(crate) hits: u64,
49 /// Cumulative cache lookup misses.
50 #[get(type(copy))]
51 pub(crate) misses: u64,
52 /// Cumulative deterministic LRU evictions performed.
53 #[get(type(copy))]
54 pub(crate) evictions: u64,
55 /// Cumulative in-flight load cancellations completed.
56 #[get(type(copy))]
57 pub(crate) cancellations: u64,
58}
59
60/// Internal per-slot record tracking ownership, bytes, LRU recency, and `ResourceAccountant` handles.
61#[derive(Clone, Data, Debug)]
62pub(crate) struct AssetSlotEntry {
63 /// Generation-stamped handle identifying this live slot.
64 #[get(type(copy))]
65 pub(crate) handle: AssetHandle,
66 /// Logical lookup key (URI, asset path, or content hash).
67 pub(crate) key: String,
68 /// Single authoritative resource owner recorded in `ResourceAccountant`.
69 #[get(type(copy))]
70 pub(crate) owner: ResourceOwnerId,
71 /// Memory residency domain (`Ram`, `Vram`, or `Hybrid`).
72 #[get(type(copy))]
73 pub(crate) storage: AssetStorageKind,
74 /// Current load lifecycle state (`Pending` or `Ready`).
75 #[get(type(copy))]
76 pub(crate) state: AssetLoadState,
77 /// Resident CPU RAM bytes charged to this entry.
78 #[get(type(copy))]
79 pub(crate) ram_bytes: usize,
80 /// Resident GPU VRAM bytes charged to this entry.
81 #[get(type(copy))]
82 pub(crate) vram_bytes: usize,
83 /// Monotonic access tick used for deterministic LRU eviction ordering.
84 #[get(type(copy))]
85 pub(crate) last_access_tick: u64,
86 /// When `true`, entry is protected from automatic LRU eviction until unpinned or released.
87 #[get(type(copy))]
88 pub(crate) pinned: bool,
89 /// Decoded CPU payload bytes (empty for pure `Vram` or `Pending` entries).
90 pub(crate) payload: Vec<u8>,
91 /// `ResourceAccountant` handle for in-flight `AsyncTask` tracking (`0` when none).
92 #[get(type(copy))]
93 pub(crate) task_account_handle: u64,
94 /// `ResourceAccountant` handle for CPU `CacheAllocation` tracking (`0` when none).
95 #[get(type(copy))]
96 pub(crate) ram_account_handle: u64,
97 /// `ResourceAccountant` handle for GPU `GpuTexture` tracking (`0` when none).
98 #[get(type(copy))]
99 pub(crate) vram_account_handle: u64,
100}
101
102/// Cancelable budgeted LRU asset cache wired directly to `ResourceAccountant` for leak attribution.
103#[derive(Clone, Data, Debug)]
104pub struct AssetManager {
105 /// Active RAM and VRAM byte ceiling configuration.
106 #[get(type(copy))]
107 pub(crate) budget: AssetBudgetConfig,
108 /// Slot table storing active or pending asset entries.
109 #[get(pub(crate))]
110 #[get_mut(pub(crate))]
111 #[set(skip)]
112 pub(crate) entries: Vec<Option<AssetSlotEntry>>,
113 /// Per-slot monotonic generation counters preventing ABA handle reuse.
114 #[get(pub(crate))]
115 #[get_mut(pub(crate))]
116 #[set(skip)]
117 pub(crate) generations: Vec<u32>,
118 /// LIFO free-list of reusable slot indices.
119 #[get(pub(crate))]
120 #[get_mut(pub(crate))]
121 #[set(skip)]
122 pub(crate) free_slots: Vec<u32>,
123 /// Fast string-key to live `AssetHandle` index.
124 #[get(pub(crate))]
125 #[get_mut(pub(crate))]
126 #[set(skip)]
127 pub(crate) key_index: HashMap<String, AssetHandle>,
128 /// Current aggregate resident CPU RAM bytes.
129 #[get(type(copy))]
130 pub(crate) ram_used_bytes: usize,
131 /// Current aggregate resident GPU VRAM bytes.
132 #[get(type(copy))]
133 pub(crate) vram_used_bytes: usize,
134 /// Monotonic logical clock stamped on asset insertion and hits.
135 #[get(pub(crate), type(copy))]
136 #[get_mut(skip)]
137 #[set(pub(crate))]
138 pub(crate) access_clock: u64,
139 /// Cumulative lookup hits.
140 #[get(type(copy))]
141 pub(crate) hits: u64,
142 /// Cumulative lookup misses.
143 #[get(type(copy))]
144 pub(crate) misses: u64,
145 /// Cumulative LRU evictions.
146 #[get(type(copy))]
147 pub(crate) evictions: u64,
148 /// Cumulative pending load cancellations.
149 #[get(type(copy))]
150 pub(crate) cancellations: u64,
151}