Debian Codegraph @

src/collection/disks.rs

raw symbols SCIP
1 //! Data collection about disks (e.g. I/O, usage, space).
2
3 cfg_select! {
4 target_os = "freebsd" => {
5 mod freebsd;
6 #[cfg(feature = "zfs")]
7 mod io_counters;
8 #[cfg(feature = "zfs")]
9 mod zfs_io_counters;
10 #[cfg(feature = "zfs")]
11 pub use io_counters::IoCounters;
12 pub(crate) use self::freebsd::*;
13 }
14 target_os = "windows" => {
15 mod windows;
16 pub(crate) use self::windows::*;
17 }
18 target_os = "linux" => {
19 mod unix;
20 #[cfg(feature = "zfs")]
21 mod zfs_io_counters;
22 pub(crate) use self::unix::*;
23 }
24 target_os = "macos" => {
25 mod unix;
26 pub(crate) use self::unix::*;
27 }
28 _ => {
29 mod other;
30 pub(crate) use self::other::*;
31 }
32 }
33
34 use rustc_hash::FxHashMap as HashMap;
35
36 use crate::app::filter::Filter;
37
38 #[derive(Clone, Debug, Default)]
39 pub struct DiskHarvest {
40 pub name: String,
41
42 /// Current disk mount point. Empty string if not currently mounted.
43 pub mount_point: String,
44
45 /// Windows also contains an additional volume name field.
46 #[cfg(target_os = "windows")]
47 pub volume_name: Option<String>,
48
49 // TODO: Maybe unify all these?
50 pub free_space: Option<u64>,
51 pub used_space: Option<u64>,
52 pub total_space: Option<u64>,
53 }
54
55 #[derive(Clone, Debug)]
56 pub struct IoData {
57 pub read_bytes: u64,
58 pub write_bytes: u64,
59 }
60
61 pub type IoHarvest = HashMap<String, Option<IoData>>;
62
63 cfg_select! {
64 any(target_os = "linux", target_os = "macos", target_os = "windows") => {
65 mod io_counters;
66 pub use io_counters::IoCounters;
67 use crate::collection::DataCollector;
68
69 /// Returns the I/O usage of certain mount points.
70 pub fn get_io_usage(_collector: &DataCollector) -> anyhow::Result<IoHarvest> {
71 let mut io_hash: HashMap<String, Option<IoData>> = HashMap::default();
72
73
74 // TODO: Maybe rewrite this to not do a result of vec of result...
75 for io in io_stats()?.into_iter() {
76 let mount_point = io.device_name().to_string_lossy();
77
78 io_hash.insert(
79 mount_point.to_string(),
80 Some(IoData {
81 read_bytes: io.read_bytes(),
82 write_bytes: io.write_bytes(),
83 }),
84 );
85 }
86
87 Ok(io_hash)
88 }
89 }
90 not(target_os = "freebsd") => {
91 use crate::collection::DataCollector;
92 pub fn get_io_usage(_collector: &DataCollector) -> anyhow::Result<IoHarvest> {
93 anyhow::bail!("Unsupported OS");
94 }
95 }
96 _ => {}
97 }
98
99 /// Whether to keep the current disk entry given the filters, disk name, and
100 /// disk mount. Precedence ordering in the case where name and mount filters
101 /// disagree, "allow" takes precedence over "deny".
102 ///
103 /// For implementation, we do this as follows:
104 ///
105 /// 1. Is the entry allowed through any filter? That is, does it match an entry
106 /// in a filter where `is_list_ignored` is `false`? If so, we always keep
107 /// this entry.
108 /// 2. Is the entry denied through any filter? That is, does it match an entry
109 /// in a filter where `is_list_ignored` is `true`? If so, we always deny this
110 /// entry.
111 /// 3. Anything else is allowed.
112 pub fn keep_disk_entry(
113 disk_name: &str, mount_point: &str, disk_filter: &Option<Filter>, mount_filter: &Option<Filter>,
114 ) -> bool {
115 match (disk_filter, mount_filter) {
116 (Some(d), Some(m)) => match (d.ignore_matches(), m.ignore_matches()) {
117 (true, true) => !(d.has_match(disk_name) || m.has_match(mount_point)),
118 (true, false) => {
119 if m.has_match(mount_point) {
120 true
121 } else {
122 d.should_keep(disk_name)
123 }
124 }
125 (false, true) => {
126 if d.has_match(disk_name) {
127 true
128 } else {
129 m.should_keep(mount_point)
130 }
131 }
132 (false, false) => d.has_match(disk_name) || m.has_match(mount_point),
133 },
134 (Some(d), None) => d.should_keep(disk_name),
135 (None, Some(m)) => m.should_keep(mount_point),
136 (None, None) => true,
137 }
138 }
139
140 #[cfg(test)]
141 mod test {
142 use regex::Regex;
143
144 use super::keep_disk_entry;
145 use crate::app::filter::Filter;
146
147 fn run_filter(disk_filter: &Option<Filter>, mount_filter: &Option<Filter>) -> Vec<usize> {
148 let targets = [
149 ("/dev/nvme0n1p1", "/boot"),
150 ("/dev/nvme0n1p2", "/"),
151 ("/dev/nvme0n1p3", "/home"),
152 ("/dev/sda1", "/mnt/test"),
153 ("/dev/sda2", "/mnt/boot"),
154 ];
155
156 targets
157 .into_iter()
158 .enumerate()
159 .filter_map(|(itx, (name, mount))| {
160 if keep_disk_entry(name, mount, disk_filter, mount_filter) {
161 Some(itx)
162 } else {
163 None
164 }
165 })
166 .collect()
167 }
168
169 #[test]
170 fn test_keeping_disk_entry() {
171 let disk_ignore = Some(Filter::new(true, vec![Regex::new("nvme").unwrap()]));
172 let disk_keep = Some(Filter::new(false, vec![Regex::new("nvme").unwrap()]));
173 let mount_ignore = Some(Filter::new(true, vec![Regex::new("boot").unwrap()]));
174 let mount_keep = Some(Filter::new(false, vec![Regex::new("boot").unwrap()]));
175
176 assert_eq!(run_filter(&None, &None), vec![0, 1, 2, 3, 4]);
177
178 assert_eq!(run_filter(&disk_ignore, &None), vec![3, 4]);
179 assert_eq!(run_filter(&disk_keep, &None), vec![0, 1, 2]);
180
181 assert_eq!(run_filter(&None, &mount_ignore), vec![1, 2, 3]);
182 assert_eq!(run_filter(&None, &mount_keep), vec![0, 4]);
183
184 assert_eq!(run_filter(&disk_ignore, &mount_ignore), vec![3]);
185 assert_eq!(run_filter(&disk_keep, &mount_ignore), vec![0, 1, 2, 3]);
186
187 assert_eq!(run_filter(&disk_ignore, &mount_keep), vec![0, 3, 4]);
188 assert_eq!(run_filter(&disk_keep, &mount_keep), vec![0, 1, 2, 4]);
189 }
190 }