1pub const CATEGORY_NAMES: &[&str] = &[
10 "cpu", "io", "ram", "rambw", "storage", "net", "pressure", "proc", "cgroup",
11];
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub struct Categories {
16 pub cpu: bool,
17 pub io: bool,
18 pub ram: bool,
19 pub rambw: bool,
20 pub storage: bool,
21 pub net: bool,
22 pub pressure: bool,
23 pub proc: bool,
24 pub cgroup: bool,
25}
26
27impl Categories {
28 pub const ALL: Categories = Categories {
29 cpu: true,
30 io: true,
31 ram: true,
32 rambw: true,
33 storage: true,
34 net: true,
35 pressure: true,
36 proc: true,
37 cgroup: true,
38 };
39
40 pub fn any(&self) -> bool {
42 *self != Categories::default()
43 }
44
45 fn flag(&mut self, name: &str) -> Option<&mut bool> {
47 Some(match name {
48 "cpu" => &mut self.cpu,
49 "io" => &mut self.io,
50 "ram" => &mut self.ram,
51 "rambw" => &mut self.rambw,
52 "storage" => &mut self.storage,
53 "net" => &mut self.net,
54 "pressure" => &mut self.pressure,
55 "proc" => &mut self.proc,
56 "cgroup" => &mut self.cgroup,
57 _ => return None,
58 })
59 }
60
61 pub fn has(&self, name: &str) -> bool {
63 let mut copy = *self;
64 copy.flag(name).is_some_and(|on| *on)
65 }
66
67 pub fn add(&mut self, name: &str) -> Result<(), String> {
70 match self.flag(name) {
71 Some(flag) => {
72 *flag = true;
73 Ok(())
74 }
75 None => Err(name.to_string()),
76 }
77 }
78
79 pub fn list(&self) -> Vec<&'static str> {
81 CATEGORY_NAMES
82 .iter()
83 .filter(|name| self.has(name))
84 .copied()
85 .collect()
86 }
87
88 pub fn names(&self) -> String {
90 self.list().join(",")
91 }
92}
93
94pub const fn platform_supported() -> bool {
96 cfg!(target_os = "linux")
97}
98
99pub fn unavailable(name: &str) -> Option<String> {
107 if !platform_supported() {
108 return Some(
109 "the host's readings come from /proc and are available on Linux only".to_string(),
110 );
111 }
112 match name {
113 "rambw" if crate::mem::read_membw_bytes().is_none() => Some(
114 "\
115the kernel resctrl interface is not available at /sys/fs/resctrl/mon_data.
116
117To enable memory-bandwidth monitoring:
118 1. The CPU must support bandwidth monitoring (Intel RDT / AMD QoS) —
119 check for the `cqm_mbm_total` flag: grep -m1 cqm_mbm_total /proc/cpuinfo
120 2. The kernel must be built with CONFIG_X86_CPU_RESCTRL (standard on
121 mainstream distro kernels).
122 3. Mount the interface: sudo mount -t resctrl resctrl /sys/fs/resctrl
123
124Most virtual machines cannot support it."
125 .to_string(),
126 ),
127 "pressure" if crate::pressure::read_pressure().is_none() => Some(
128 "the kernel's pressure stall information is not readable at /proc/pressure. \
129 It needs a kernel built with CONFIG_PSI (4.20 or later) that was not booted \
130 with `psi=0`, and a container must be given the host's /proc/pressure."
131 .to_string(),
132 ),
133 "cgroup" if !crate::cgroup::hierarchy_available() => Some(format!(
134 "the unified control group hierarchy (cgroup v2) is not mounted at {}.",
135 crate::cgroup::ROOT
136 )),
137 _ => None,
138 }
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn a_set_is_built_by_name_and_listed_in_order() {
147 let mut cats = Categories::default();
148 assert!(!cats.any());
149 for name in ["proc", "cpu", "cgroup"] {
150 cats.add(name).unwrap();
151 }
152 assert_eq!(cats.names(), "cpu,proc,cgroup");
153 assert!(cats.has("proc") && !cats.has("io") && !cats.has("disk"));
154 assert_eq!(cats.add("disk"), Err("disk".to_string()));
155 }
156
157 #[test]
158 fn every_name_is_a_category() {
159 let mut all = Categories::default();
160 for name in CATEGORY_NAMES {
161 all.add(name).unwrap();
162 }
163 assert_eq!(all, Categories::ALL);
164 }
165
166 #[cfg(target_os = "linux")]
168 #[test]
169 fn the_proc_categories_need_nothing_more() {
170 for name in ["cpu", "io", "ram", "storage", "net", "proc"] {
171 assert_eq!(unavailable(name), None, "{name}");
172 }
173 }
174}