Skip to main content

trueno/monitor/
memory.rs

1//! GPU memory metrics (TRUENO-SPEC-010 Section 4.1.2)
2
3/// GPU memory metrics
4#[derive(Debug, Clone, Copy, Default)]
5pub struct GpuMemoryMetrics {
6    /// Total VRAM in bytes
7    pub total: u64,
8    /// Used VRAM in bytes
9    pub used: u64,
10    /// Free VRAM in bytes
11    pub free: u64,
12    /// Number of active allocations (if tracked)
13    pub allocations: u64,
14}
15
16impl GpuMemoryMetrics {
17    /// Create new memory metrics
18    #[must_use]
19    pub const fn new(total: u64, used: u64, free: u64) -> Self {
20        Self { total, used, free, allocations: 0 }
21    }
22
23    /// Calculate usage percentage (0.0 - 100.0)
24    #[must_use]
25    pub fn usage_percent(&self) -> f64 {
26        if self.total == 0 {
27            0.0
28        } else {
29            (self.used as f64 / self.total as f64) * 100.0
30        }
31    }
32
33    /// Get used VRAM in megabytes
34    #[must_use]
35    pub fn used_mb(&self) -> u64 {
36        self.used / (1024 * 1024)
37    }
38
39    /// Get free VRAM in megabytes
40    #[must_use]
41    pub fn free_mb(&self) -> u64 {
42        self.free / (1024 * 1024)
43    }
44}