Skip to main content

euv_core/scene/lifecycle/
struct.rs

1use super::*;
2
3/// Generation-guarded token preventing stale async callbacks from mutating destroyed, evicted, or replaced pages.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
5pub struct PageGenerationToken {
6    /// Stable monotonic page identifier.
7    #[get(type(copy))]
8    pub(crate) page_id: u64,
9    /// Monotonic instance generation bumped on every eviction reconstruction and destruction.
10    #[get(type(copy))]
11    pub(crate) generation: u64,
12}
13
14/// Versioned restorable page snapshot preserved across `Freeze` and `Evict` (discarded on explicit `Destroy`).
15#[derive(Clone, Data, Debug, Default, PartialEq)]
16pub struct PageSnapshot {
17    /// Snapshot schema version for forward-compatible restoration validation.
18    #[get(type(copy))]
19    pub(crate) schema_version: u32,
20    /// Canonical route identifier.
21    pub(crate) route_key: String,
22    /// Generation of the page instance when this snapshot was captured.
23    #[get(type(copy))]
24    pub(crate) generation: u64,
25    /// Last primary scroll offset in logical pixels.
26    #[get(type(copy))]
27    pub(crate) scroll_offset: Point,
28    /// Optional focused element key restored on resume.
29    pub(crate) focus_key: String,
30    /// Whether `focus_key` contains an active focus target.
31    #[get(type(copy))]
32    pub(crate) has_focus_key: bool,
33    /// Serialized form/editor draft payload preserved across cold eviction.
34    pub(crate) draft_payload: String,
35    /// True when the page holds user-edited unsaved draft data.
36    #[get(type(copy))]
37    pub(crate) has_unsaved_draft: bool,
38    /// True once an unsaved draft has been durably persisted into `draft_payload` or external storage.
39    #[get(type(copy))]
40    pub(crate) draft_persisted: bool,
41    /// Last host/shared-store revision synchronized with this page.
42    #[get(type(copy))]
43    pub(crate) host_state_revision: u64,
44    /// Estimated byte footprint of this snapshot.
45    #[get(type(copy))]
46    pub(crate) estimated_bytes: usize,
47}
48
49/// Bounded cold-page retention policy enforced by `PageLifecycleController`.
50#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
51pub struct PageBudgetPolicy {
52    /// Maximum count of cold `Frozen` pages kept in memory simultaneously.
53    #[get(type(copy))]
54    pub(crate) max_frozen_pages: usize,
55    /// Maximum aggregate retained bytes across all `Frozen` pages.
56    #[get(type(copy))]
57    pub(crate) max_retained_bytes: usize,
58    /// Maximum byte size allowed for one retained `PageSnapshot`.
59    #[get(type(copy))]
60    pub(crate) max_snapshot_bytes: usize,
61}
62
63/// Complete deterministic lifecycle record for one routed page or nested sub-page.
64#[derive(Clone, Data, Debug, Default, PartialEq)]
65pub struct PageLifecycleEntry {
66    /// Unique monotonic page identifier.
67    #[get(type(copy))]
68    pub(crate) page_id: u64,
69    /// Canonical route identifier.
70    pub(crate) route_key: String,
71    /// Current live generation incremented on eviction reconstruction or destruction.
72    #[get(type(copy))]
73    pub(crate) generation: u64,
74    /// Parent page identifier when `has_parent` is `true` for parent-child lifecycle propagation.
75    #[get(type(copy))]
76    pub(crate) parent_page_id: u64,
77    /// Whether this page belongs to a parent page container.
78    #[get(type(copy))]
79    pub(crate) has_parent: bool,
80    /// Active lifecycle phase (`Active`, `Frozen`, `Evicted`, `Destroyed`, etc.).
81    #[get(type(copy))]
82    pub(crate) phase: LifecyclePhase,
83    /// Retention strategy (`Freeze`, `Destroy`, `Alive`).
84    #[get(type(copy))]
85    pub(crate) retention: PageRetention,
86    /// Core retained state/scene memory footprint in bytes while `Active` or `Frozen`.
87    #[get(type(copy))]
88    pub(crate) retained_bytes: usize,
89    /// Reconstructible cache footprint (decoded images, glyphs, pre-baked tiles) evicted BEFORE evicting the page.
90    #[get(type(copy))]
91    pub(crate) reconstructible_cache_bytes: usize,
92    /// Monotonic logical tick when this page was last active (governs LRU cold eviction).
93    #[get(type(copy))]
94    pub(crate) last_active_tick: u64,
95    /// Cumulative number of idempotent `Freeze` transitions executed on this page.
96    #[get(type(copy))]
97    pub(crate) freeze_count: u64,
98    /// Cumulative number of idempotent `Resume` transitions executed on this page.
99    #[get(type(copy))]
100    pub(crate) resume_count: u64,
101    /// Cumulative number of cold `Evict` transitions executed on this page.
102    #[get(type(copy))]
103    pub(crate) evict_count: u64,
104    /// Last host/shared-store revision synchronized onto this page.
105    #[get(type(copy))]
106    pub(crate) host_synced_revision: u64,
107    /// Versioned snapshot retained while `Frozen` or `Evicted` (cleared on `Destroy`).
108    pub(crate) snapshot: PageSnapshot,
109    /// Whether `snapshot` contains a captured versioned snapshot.
110    #[get(type(copy))]
111    pub(crate) has_snapshot: bool,
112    /// True while OS background suspension is active independently of route `Freeze`.
113    #[get(type(copy))]
114    pub(crate) os_background_suspended: bool,
115    /// Latest graphics surface generation observed on surface loss/recreation.
116    #[get(type(copy))]
117    pub(crate) surface_lost_generation: u64,
118    /// True while the graphics surface is currently lost independently of route `Freeze`.
119    #[get(type(copy))]
120    pub(crate) has_surface_lost: bool,
121}
122
123/// Structured report returned by `PageLifecycleController::enforce_budget`.
124#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
125pub struct PageEvictionReport {
126    /// Total reconstructible cache bytes drained from frozen pages prior to any page eviction.
127    #[get(type(copy))]
128    pub(crate) reconstructible_bytes_freed: usize,
129    /// Ordered list of cold page identifiers transitioned from `Frozen` to `Evicted`.
130    pub(crate) evicted_page_ids: Vec<u64>,
131    /// Number of frozen pages spared from silent eviction because they held unpersisted unsaved drafts.
132    #[get(type(copy))]
133    pub(crate) protected_unsaved_pages: usize,
134}
135
136/// Unified deterministic controller managing page `Freeze`, `Resume`, `Evict`, `Destroy`, generation guards, and boundary events.
137#[derive(Clone, Data, Debug)]
138pub struct PageLifecycleController {
139    /// Active cold-page count and retained-byte budget policy.
140    #[get(type(copy))]
141    pub(crate) budget: PageBudgetPolicy,
142    /// Registered pages keyed by `page_id`.
143    #[get(pub(crate))]
144    #[get_mut(pub(crate))]
145    #[set(skip)]
146    pub(crate) pages: HashMap<u64, PageLifecycleEntry>,
147    /// Next monotonic page identifier.
148    #[get(type(copy))]
149    #[get_mut(skip)]
150    #[set(skip)]
151    pub(crate) next_page_id: u64,
152    /// Monotonic logical activity tick for LRU cold eviction ordering.
153    #[get(type(copy))]
154    #[get_mut(skip)]
155    #[set(skip)]
156    pub(crate) active_clock: u64,
157    /// Current host/shared-store revision checked during `Resume` synchronization.
158    #[get(type(copy))]
159    pub(crate) host_revision: u64,
160    /// Cumulative count of stale async callbacks rejected by `PageGenerationToken` guards.
161    #[get(type(copy))]
162    #[get_mut(skip)]
163    #[set(skip)]
164    pub(crate) stale_callbacks_blocked: u64,
165    /// Cumulative count of `LifecycleBoundaryEvent::OsBackground` events processed.
166    #[get(type(copy))]
167    #[get_mut(skip)]
168    #[set(skip)]
169    pub(crate) os_background_events: u64,
170    /// Cumulative count of `LifecycleBoundaryEvent::OsForeground` events processed.
171    #[get(type(copy))]
172    #[get_mut(skip)]
173    #[set(skip)]
174    pub(crate) os_foreground_events: u64,
175    /// Cumulative count of `LifecycleBoundaryEvent::SurfaceLost` events processed.
176    #[get(type(copy))]
177    #[get_mut(skip)]
178    #[set(skip)]
179    pub(crate) surface_lost_events: u64,
180    /// Cumulative count of `LifecycleBoundaryEvent::SurfaceRecreated` events processed.
181    #[get(type(copy))]
182    #[get_mut(skip)]
183    #[set(skip)]
184    pub(crate) surface_recreated_events: u64,
185    /// Cumulative count of explicitly destroyed pages.
186    #[get(type(copy))]
187    #[get_mut(skip)]
188    #[set(skip)]
189    pub(crate) total_destroyed_pages: u64,
190}