euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Default zero-initialized lock-free telemetry counters.
impl Default for StreamingTelemetryCounters {
    /// Creates zeroed atomic telemetry counters and an empty p99 latency ring buffer.
    ///
    /// # Returns
    ///
    /// - `Self` - Zero-initialized `StreamingTelemetryCounters`.
    fn default() -> Self {
        Self {
            active_jobs: AtomicU32::new(0),
            coalesced_hits: AtomicU64::new(0),
            cancelled_jobs: AtomicU64::new(0),
            ram_used_bytes: AtomicUsize::new(0),
            vram_used_bytes: AtomicUsize::new(0),
            textures_mip_tail: AtomicU32::new(0),
            textures_mid_mip: AtomicU32::new(0),
            textures_full_res: AtomicU32::new(0),
            evictions_to_mip_tail: AtomicU64::new(0),
            decode_p99_us: AtomicU64::new(0),
            vfs_syscalls_saved: AtomicU64::new(0),
            decode_samples_us: [const { AtomicU64::new(0) }; STREAMING_DECODE_SAMPLE_WINDOW],
            decode_sample_count: AtomicUsize::new(0),
        }
    }
}

/// Lock-free atomic updates, rolling p99 decode estimation, and snapshot capture for `StreamingTelemetryCounters`.
impl StreamingTelemetryCounters {
    /// Atomically increments the active in-flight streaming job counter.
    pub(crate) fn inc_active_jobs(&self) {
        let Self { active_jobs, .. } = self;
        let _: u32 = active_jobs.fetch_add(1, Ordering::Relaxed);
    }

    /// Atomically decrements the active in-flight streaming job counter without underflow.
    pub(crate) fn dec_active_jobs(&self) {
        let Self { active_jobs, .. } = self;
        let mut current: u32 = active_jobs.load(Ordering::Relaxed);
        while current > 0 {
            match active_jobs.compare_exchange_weak(
                current,
                current - 1,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(observed) => current = observed,
            }
        }
    }

    /// Atomically increments the coalesced duplicate request hit counter.
    pub(crate) fn add_coalesced_hit(&self) {
        let Self { coalesced_hits, .. } = self;
        let _: u64 = coalesced_hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Atomically increments the cancelled job counter.
    pub(crate) fn add_cancelled_job(&self) {
        let Self { cancelled_jobs, .. } = self;
        let _: u64 = cancelled_jobs.fetch_add(1, Ordering::Relaxed);
    }

    /// Atomically adds `delta` saved OS syscalls from `.euvpak` virtual filesystem lookups.
    ///
    /// # Arguments
    ///
    /// - `u64` - Number of loose-file syscalls avoided.
    pub(crate) fn add_vfs_syscalls_saved(&self, delta: u64) {
        let Self {
            vfs_syscalls_saved, ..
        } = self;
        let _: u64 = vfs_syscalls_saved.fetch_add(delta, Ordering::Relaxed);
    }

    /// Atomically increments the cumulative texture demotion-to-mip-tail counter.
    pub(crate) fn add_eviction_to_mip_tail(&self) {
        let Self {
            evictions_to_mip_tail,
            ..
        } = self;
        let _: u64 = evictions_to_mip_tail.fetch_add(1, Ordering::Relaxed);
    }

    /// Publishes exact live decoded RAM and GPU VRAM byte footprints.
    ///
    /// # Arguments
    ///
    /// - `usize` - Current resident RAM bytes.
    /// - `usize` - Current resident VRAM bytes.
    pub(crate) fn set_memory_usage(&self, ram_bytes: usize, vram_bytes: usize) {
        let Self {
            ram_used_bytes,
            vram_used_bytes,
            ..
        } = self;
        ram_used_bytes.store(ram_bytes, Ordering::Relaxed);
        vram_used_bytes.store(vram_bytes, Ordering::Relaxed);
    }

    /// Publishes exact texture residency tier counts (`MipTail`, `MidMip`, `FullRes`).
    ///
    /// # Arguments
    ///
    /// - `u32` - Count of textures currently resident at `MipTail`.
    /// - `u32` - Count of textures currently resident at `MidMip`.
    /// - `u32` - Count of textures currently resident at `FullRes`.
    pub(crate) fn set_texture_tier_counts(&self, mip_tail: u32, mid_mip: u32, full_res: u32) {
        let Self {
            textures_mip_tail,
            textures_mid_mip,
            textures_full_res,
            ..
        } = self;
        textures_mip_tail.store(mip_tail, Ordering::Relaxed);
        textures_mid_mip.store(mid_mip, Ordering::Relaxed);
        textures_full_res.store(full_res, Ordering::Relaxed);
    }

    /// Records a single completed decode duration in microseconds and updates the lock-free rolling `decode_p99_us`.
    ///
    /// # Arguments
    ///
    /// - `u64` - Single-job decode duration in microseconds.
    pub(crate) fn record_decode_us(&self, elapsed_us: u64) {
        let Self {
            decode_samples_us,
            decode_sample_count,
            decode_p99_us,
            ..
        } = self;
        let seq: usize = decode_sample_count.fetch_add(1, Ordering::Relaxed);
        let slot: usize = seq & (STREAMING_DECODE_SAMPLE_WINDOW - 1);
        decode_samples_us[slot].store(elapsed_us, Ordering::Relaxed);
        let populated: usize = seq.saturating_add(1).min(STREAMING_DECODE_SAMPLE_WINDOW);
        let mut scratch: [u64; STREAMING_DECODE_SAMPLE_WINDOW] =
            [0u64; STREAMING_DECODE_SAMPLE_WINDOW];
        for (dst, src) in scratch
            .iter_mut()
            .take(populated)
            .zip(decode_samples_us.iter())
        {
            *dst = src.load(Ordering::Relaxed);
        }
        let active_slice: &mut [u64] = &mut scratch[..populated];
        active_slice.sort_unstable();
        let p99_idx: usize = (populated.saturating_mul(99) / 100).min(populated.saturating_sub(1));
        decode_p99_us.store(active_slice[p99_idx], Ordering::Relaxed);
    }

    /// Captures a consistent relaxed snapshot combined with the active memory ceilings.
    ///
    /// # Arguments
    ///
    /// - `usize` - Configured RAM budget ceiling in bytes.
    /// - `usize` - Configured VRAM budget ceiling in bytes.
    ///
    /// # Returns
    ///
    /// - `StreamingTelemetrySnapshot` - Point-in-time streaming telemetry snapshot.
    #[must_use]
    pub fn snapshot(
        &self,
        ram_budget_bytes: usize,
        vram_budget_bytes: usize,
    ) -> StreamingTelemetrySnapshot {
        let Self {
            active_jobs,
            coalesced_hits,
            cancelled_jobs,
            ram_used_bytes,
            vram_used_bytes,
            textures_mip_tail,
            textures_mid_mip,
            textures_full_res,
            evictions_to_mip_tail,
            decode_p99_us,
            vfs_syscalls_saved,
            ..
        } = self;
        StreamingTelemetrySnapshot {
            active_jobs: active_jobs.load(Ordering::Relaxed),
            coalesced_hits: coalesced_hits.load(Ordering::Relaxed),
            cancelled_jobs: cancelled_jobs.load(Ordering::Relaxed),
            ram_used_bytes: ram_used_bytes.load(Ordering::Relaxed),
            ram_budget_bytes,
            vram_used_bytes: vram_used_bytes.load(Ordering::Relaxed),
            vram_budget_bytes,
            textures_mip_tail: textures_mip_tail.load(Ordering::Relaxed),
            textures_mid_mip: textures_mid_mip.load(Ordering::Relaxed),
            textures_full_res: textures_full_res.load(Ordering::Relaxed),
            evictions_to_mip_tail: evictions_to_mip_tail.load(Ordering::Relaxed),
            decode_p99_us: decode_p99_us.load(Ordering::Relaxed),
            vfs_syscalls_saved: vfs_syscalls_saved.load(Ordering::Relaxed),
        }
    }
}