Skip to main content

euv_core/runtime/streaming/telemetry/
impl.rs

1use super::*;
2
3/// Default zero-initialized lock-free telemetry counters.
4impl Default for StreamingTelemetryCounters {
5    /// Creates zeroed atomic telemetry counters and an empty p99 latency ring buffer.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Zero-initialized `StreamingTelemetryCounters`.
10    fn default() -> Self {
11        Self {
12            active_jobs: AtomicU32::new(0),
13            coalesced_hits: AtomicU64::new(0),
14            cancelled_jobs: AtomicU64::new(0),
15            ram_used_bytes: AtomicUsize::new(0),
16            vram_used_bytes: AtomicUsize::new(0),
17            textures_mip_tail: AtomicU32::new(0),
18            textures_mid_mip: AtomicU32::new(0),
19            textures_full_res: AtomicU32::new(0),
20            evictions_to_mip_tail: AtomicU64::new(0),
21            decode_p99_us: AtomicU64::new(0),
22            vfs_syscalls_saved: AtomicU64::new(0),
23            decode_samples_us: [const { AtomicU64::new(0) }; STREAMING_DECODE_SAMPLE_WINDOW],
24            decode_sample_count: AtomicUsize::new(0),
25        }
26    }
27}
28
29/// Lock-free atomic updates, rolling p99 decode estimation, and snapshot capture for `StreamingTelemetryCounters`.
30impl StreamingTelemetryCounters {
31    /// Atomically increments the active in-flight streaming job counter.
32    pub(crate) fn inc_active_jobs(&self) {
33        let Self { active_jobs, .. } = self;
34        let _: u32 = active_jobs.fetch_add(1, Ordering::Relaxed);
35    }
36
37    /// Atomically decrements the active in-flight streaming job counter without underflow.
38    pub(crate) fn dec_active_jobs(&self) {
39        let Self { active_jobs, .. } = self;
40        let mut current: u32 = active_jobs.load(Ordering::Relaxed);
41        while current > 0 {
42            match active_jobs.compare_exchange_weak(
43                current,
44                current - 1,
45                Ordering::Relaxed,
46                Ordering::Relaxed,
47            ) {
48                Ok(_) => break,
49                Err(observed) => current = observed,
50            }
51        }
52    }
53
54    /// Atomically increments the coalesced duplicate request hit counter.
55    pub(crate) fn add_coalesced_hit(&self) {
56        let Self { coalesced_hits, .. } = self;
57        let _: u64 = coalesced_hits.fetch_add(1, Ordering::Relaxed);
58    }
59
60    /// Atomically increments the cancelled job counter.
61    pub(crate) fn add_cancelled_job(&self) {
62        let Self { cancelled_jobs, .. } = self;
63        let _: u64 = cancelled_jobs.fetch_add(1, Ordering::Relaxed);
64    }
65
66    /// Atomically adds `delta` saved OS syscalls from `.euvpak` virtual filesystem lookups.
67    ///
68    /// # Arguments
69    ///
70    /// - `u64` - Number of loose-file syscalls avoided.
71    pub(crate) fn add_vfs_syscalls_saved(&self, delta: u64) {
72        let Self {
73            vfs_syscalls_saved, ..
74        } = self;
75        let _: u64 = vfs_syscalls_saved.fetch_add(delta, Ordering::Relaxed);
76    }
77
78    /// Atomically increments the cumulative texture demotion-to-mip-tail counter.
79    pub(crate) fn add_eviction_to_mip_tail(&self) {
80        let Self {
81            evictions_to_mip_tail,
82            ..
83        } = self;
84        let _: u64 = evictions_to_mip_tail.fetch_add(1, Ordering::Relaxed);
85    }
86
87    /// Publishes exact live decoded RAM and GPU VRAM byte footprints.
88    ///
89    /// # Arguments
90    ///
91    /// - `usize` - Current resident RAM bytes.
92    /// - `usize` - Current resident VRAM bytes.
93    pub(crate) fn set_memory_usage(&self, ram_bytes: usize, vram_bytes: usize) {
94        let Self {
95            ram_used_bytes,
96            vram_used_bytes,
97            ..
98        } = self;
99        ram_used_bytes.store(ram_bytes, Ordering::Relaxed);
100        vram_used_bytes.store(vram_bytes, Ordering::Relaxed);
101    }
102
103    /// Publishes exact texture residency tier counts (`MipTail`, `MidMip`, `FullRes`).
104    ///
105    /// # Arguments
106    ///
107    /// - `u32` - Count of textures currently resident at `MipTail`.
108    /// - `u32` - Count of textures currently resident at `MidMip`.
109    /// - `u32` - Count of textures currently resident at `FullRes`.
110    pub(crate) fn set_texture_tier_counts(&self, mip_tail: u32, mid_mip: u32, full_res: u32) {
111        let Self {
112            textures_mip_tail,
113            textures_mid_mip,
114            textures_full_res,
115            ..
116        } = self;
117        textures_mip_tail.store(mip_tail, Ordering::Relaxed);
118        textures_mid_mip.store(mid_mip, Ordering::Relaxed);
119        textures_full_res.store(full_res, Ordering::Relaxed);
120    }
121
122    /// Records a single completed decode duration in microseconds and updates the lock-free rolling `decode_p99_us`.
123    ///
124    /// # Arguments
125    ///
126    /// - `u64` - Single-job decode duration in microseconds.
127    pub(crate) fn record_decode_us(&self, elapsed_us: u64) {
128        let Self {
129            decode_samples_us,
130            decode_sample_count,
131            decode_p99_us,
132            ..
133        } = self;
134        let seq: usize = decode_sample_count.fetch_add(1, Ordering::Relaxed);
135        let slot: usize = seq & (STREAMING_DECODE_SAMPLE_WINDOW - 1);
136        decode_samples_us[slot].store(elapsed_us, Ordering::Relaxed);
137        let populated: usize = seq.saturating_add(1).min(STREAMING_DECODE_SAMPLE_WINDOW);
138        let mut scratch: [u64; STREAMING_DECODE_SAMPLE_WINDOW] =
139            [0u64; STREAMING_DECODE_SAMPLE_WINDOW];
140        for (dst, src) in scratch
141            .iter_mut()
142            .take(populated)
143            .zip(decode_samples_us.iter())
144        {
145            *dst = src.load(Ordering::Relaxed);
146        }
147        let active_slice: &mut [u64] = &mut scratch[..populated];
148        active_slice.sort_unstable();
149        let p99_idx: usize = (populated.saturating_mul(99) / 100).min(populated.saturating_sub(1));
150        decode_p99_us.store(active_slice[p99_idx], Ordering::Relaxed);
151    }
152
153    /// Captures a consistent relaxed snapshot combined with the active memory ceilings.
154    ///
155    /// # Arguments
156    ///
157    /// - `usize` - Configured RAM budget ceiling in bytes.
158    /// - `usize` - Configured VRAM budget ceiling in bytes.
159    ///
160    /// # Returns
161    ///
162    /// - `StreamingTelemetrySnapshot` - Point-in-time streaming telemetry snapshot.
163    #[must_use]
164    pub fn snapshot(
165        &self,
166        ram_budget_bytes: usize,
167        vram_budget_bytes: usize,
168    ) -> StreamingTelemetrySnapshot {
169        let Self {
170            active_jobs,
171            coalesced_hits,
172            cancelled_jobs,
173            ram_used_bytes,
174            vram_used_bytes,
175            textures_mip_tail,
176            textures_mid_mip,
177            textures_full_res,
178            evictions_to_mip_tail,
179            decode_p99_us,
180            vfs_syscalls_saved,
181            ..
182        } = self;
183        StreamingTelemetrySnapshot {
184            active_jobs: active_jobs.load(Ordering::Relaxed),
185            coalesced_hits: coalesced_hits.load(Ordering::Relaxed),
186            cancelled_jobs: cancelled_jobs.load(Ordering::Relaxed),
187            ram_used_bytes: ram_used_bytes.load(Ordering::Relaxed),
188            ram_budget_bytes,
189            vram_used_bytes: vram_used_bytes.load(Ordering::Relaxed),
190            vram_budget_bytes,
191            textures_mip_tail: textures_mip_tail.load(Ordering::Relaxed),
192            textures_mid_mip: textures_mid_mip.load(Ordering::Relaxed),
193            textures_full_res: textures_full_res.load(Ordering::Relaxed),
194            evictions_to_mip_tail: evictions_to_mip_tail.load(Ordering::Relaxed),
195            decode_p99_us: decode_p99_us.load(Ordering::Relaxed),
196            vfs_syscalls_saved: vfs_syscalls_saved.load(Ordering::Relaxed),
197        }
198    }
199}