Skip to main content

cbtop/frequency_control/
governor.rs

1/// CPU governor types
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum CpuGovernor {
4    /// Performance governor (max frequency)
5    Performance,
6    /// Powersave governor (min frequency)
7    Powersave,
8    /// Ondemand governor (dynamic)
9    Ondemand,
10    /// Conservative governor (gradual)
11    Conservative,
12    /// Schedutil governor (scheduler-based)
13    Schedutil,
14    /// Userspace governor (manual)
15    Userspace,
16    /// Unknown governor
17    Unknown,
18}
19
20/// Governor spec: (variant, string name).
21const GOVERNOR_SPECS: &[(CpuGovernor, &str)] = &[
22    (CpuGovernor::Performance, "performance"),
23    (CpuGovernor::Powersave, "powersave"),
24    (CpuGovernor::Ondemand, "ondemand"),
25    (CpuGovernor::Conservative, "conservative"),
26    (CpuGovernor::Schedutil, "schedutil"),
27    (CpuGovernor::Userspace, "userspace"),
28];
29
30impl CpuGovernor {
31    /// Get governor name
32    pub fn name(&self) -> &'static str {
33        GOVERNOR_SPECS
34            .iter()
35            .find(|(v, _)| v == self)
36            .map(|(_, n)| *n)
37            .unwrap_or("unknown")
38    }
39
40    /// Parse from string
41    pub fn parse(s: &str) -> Self {
42        let lower = s.trim().to_lowercase();
43        GOVERNOR_SPECS
44            .iter()
45            .find(|(_, n)| *n == lower)
46            .map(|(v, _)| *v)
47            .unwrap_or(Self::Unknown)
48    }
49
50    /// Check if deterministic (fixed frequency)
51    pub fn is_deterministic(&self) -> bool {
52        matches!(self, Self::Performance | Self::Powersave | Self::Userspace)
53    }
54}