Skip to main content

vyre_driver_wgpu/
stats.rs

1//! Runtime observability snapshot for the wgpu backend.
2//!
3//! Extracted from `lib.rs` per audit item #78  -  the stats type is
4//! lock-free observability data that has no business living inside
5//! the backend trait-impl module. Re-exported through
6//! `crate::WgpuBackendStats` so existing call sites do not change.
7
8use crate::WgpuBackend;
9use std::sync::Arc;
10
11/// Runtime observability snapshot for a [`crate::WgpuBackend`].
12///
13/// Feed into metrics pipelines (prometheus, OpenTelemetry, Datadog)
14/// for dashboards and alerting. Reads are lock-free; safe to call
15/// from a hot scrape loop.
16#[derive(Clone, Debug)]
17pub struct WgpuBackendStats {
18    /// Adapter name the backend is bound to (e.g. `"NVIDIA GeForce RTX 5090"`).
19    pub adapter_name: std::sync::Arc<str>,
20    /// Live entries in the pipeline cache.
21    pub pipeline_cache_entries: usize,
22    /// Soft cap before eviction triggers.
23    pub pipeline_cache_capacity: usize,
24    /// Estimated bytes retained by the pipeline cache.
25    pub pipeline_cache_bytes: usize,
26    /// Estimated byte cap before eviction triggers.
27    pub pipeline_cache_byte_capacity: usize,
28    /// Pipeline-cache lookup hits since backend construction.
29    pub pipeline_cache_hits: u64,
30    /// Pipeline-cache lookup misses since backend construction.
31    pub pipeline_cache_misses: u64,
32    /// Pipeline-cache insertions since backend construction.
33    pub pipeline_cache_insertions: u64,
34    /// Capacity-driven pipeline-cache evictions since backend construction.
35    pub pipeline_cache_evictions: u64,
36    /// Hit ratio over all pipeline-cache lookups.
37    pub pipeline_cache_hit_rate: f64,
38    /// Persistent buffer pool counters (allocations, hits, releases, evictions).
39    pub persistent_pool: crate::buffer::BufferPoolStats,
40}
41
42impl WgpuBackend {
43    /// Optimizer-facing capability snapshot for this live backend.
44    ///
45    /// Unlike adapter-only probes, this reflects the features that were
46    /// actually enabled on the device after backend construction.
47    #[must_use]
48    pub fn adapter_caps(&self) -> vyre_foundation::optimizer::AdapterCaps {
49        self.device_profile().into()
50    }
51
52    /// Driver-neutral capability profile for this live backend.
53    #[must_use]
54    pub fn device_profile(&self) -> vyre_driver::DeviceProfile {
55        crate::runtime::adapter_caps_probe::from_backend_profile(
56            &self.adapter_info,
57            &self.device_limits,
58            &self.enabled_features,
59        )
60    }
61
62    /// Observability snapshot  -  pipeline cache size, buffer-pool
63    /// stats, and adapter identity. SRE-friendly: consumers feed the
64    /// returned numbers into prometheus / OpenTelemetry / Datadog
65    /// pipelines for dashboards and alerting.
66    ///
67    /// Reads use atomic cache counters and the lock-free persistent-pool
68    /// pointer, so the call is safe for metrics-scrape loops.
69    #[must_use]
70    pub fn stats(&self) -> WgpuBackendStats {
71        let persistent_pool = self.current_persistent_pool().stats();
72        let pipeline_cache_hits = self.pipeline_cache.hits();
73        let pipeline_cache_misses = self.pipeline_cache.misses();
74        let pipeline_cache_lookup_rate_denominator =
75            pipeline_cache_hits as f64 + pipeline_cache_misses as f64;
76        let pipeline_cache_hit_rate = if pipeline_cache_lookup_rate_denominator == 0.0 {
77            0.0
78        } else {
79            pipeline_cache_hits as f64 / pipeline_cache_lookup_rate_denominator
80        };
81        WgpuBackendStats {
82            adapter_name: Arc::clone(&self.adapter_name),
83            pipeline_cache_entries: self.pipeline_cache.len(),
84            pipeline_cache_capacity: self.pipeline_cache.max_entries(),
85            pipeline_cache_bytes: self.pipeline_cache.cached_bytes(),
86            pipeline_cache_byte_capacity: self.pipeline_cache.max_bytes(),
87            pipeline_cache_hits,
88            pipeline_cache_misses,
89            pipeline_cache_insertions: self.pipeline_cache.insertions(),
90            pipeline_cache_evictions: self.pipeline_cache.evictions(),
91            pipeline_cache_hit_rate,
92            persistent_pool,
93        }
94    }
95}