Skip to main content

trueno/monitor/
config.rs

1//! Monitor configuration and error types (TRUENO-SPEC-010 Section 8.2)
2
3use std::time::Duration;
4
5// ============================================================================
6// Monitor Configuration (TRUENO-SPEC-010 Section 8.2)
7// ============================================================================
8
9/// Configuration for GPU monitoring
10#[derive(Debug, Clone)]
11pub struct MonitorConfig {
12    /// Polling interval for metrics collection
13    pub poll_interval: Duration,
14    /// History buffer size (number of samples to retain)
15    pub history_size: usize,
16    /// Enable background collection thread
17    pub background_collection: bool,
18}
19
20/// 60 seconds of history at 100ms polling interval
21const DEFAULT_HISTORY_SIZE: usize = 600;
22/// 60 seconds of history at 50ms high-frequency polling
23const HIGH_FREQ_HISTORY_SIZE: usize = 1200;
24/// 60 seconds of history at 500ms low-overhead polling
25const LOW_OVERHEAD_HISTORY_SIZE: usize = 120;
26
27impl Default for MonitorConfig {
28    fn default() -> Self {
29        Self {
30            poll_interval: Duration::from_millis(100),
31            history_size: DEFAULT_HISTORY_SIZE,
32            background_collection: false,
33        }
34    }
35}
36
37impl MonitorConfig {
38    /// Create config for high-frequency monitoring
39    #[must_use]
40    pub fn high_frequency() -> Self {
41        Self {
42            poll_interval: Duration::from_millis(50),
43            history_size: HIGH_FREQ_HISTORY_SIZE,
44            background_collection: true,
45        }
46    }
47
48    /// Create config for low-overhead monitoring
49    #[must_use]
50    pub fn low_overhead() -> Self {
51        Self {
52            poll_interval: Duration::from_millis(500),
53            history_size: LOW_OVERHEAD_HISTORY_SIZE,
54            background_collection: false,
55        }
56    }
57}
58
59// ============================================================================
60// Error Types
61// ============================================================================
62
63/// Errors from GPU monitoring operations
64#[derive(Debug, Clone)]
65pub enum MonitorError {
66    /// No GPU device available
67    NoDevice,
68    /// Invalid device index
69    InvalidDevice(u32),
70    /// GPU backend initialization failed
71    BackendInit(String),
72    /// Metrics query failed
73    QueryFailed(String),
74    /// Feature not available on this GPU/platform
75    NotAvailable(String),
76}
77
78impl std::fmt::Display for MonitorError {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            Self::NoDevice => write!(f, "No GPU device available"),
82            Self::InvalidDevice(idx) => write!(f, "Invalid device index: {idx}"),
83            Self::BackendInit(msg) => write!(f, "Backend initialization failed: {msg}"),
84            Self::QueryFailed(msg) => write!(f, "Metrics query failed: {msg}"),
85            Self::NotAvailable(msg) => write!(f, "Feature not available: {msg}"),
86        }
87    }
88}
89
90impl std::error::Error for MonitorError {}