| 1 | //! Data collection about disks (e.g. I/O, usage, space). |
| 2 | |
| 3 | cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/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 unixrust-analyzer cargo bottom 0.14.9 collection/disks/unix/; |
| 20 | #[cfg(feature = "zfs")] |
| 21 | mod zfs_io_countersrust-analyzer cargo bottom 0.14.9 collection/disks/zfs_io_counters/; |
| 22 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) use selfrust-analyzer cargo bottom 0.14.9 collection/disks/::unixrust-analyzer cargo bottom 0.14.9 collection/disks/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_hashrust-analyzer cargo rustc-hash 2.1.3 crate/::FxHashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap# as HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#; |
| 35 | |
| 36 | use craterust-analyzer cargo bottom 0.14.9 crate/::apprust-analyzer cargo bottom 0.14.9 app/::filterrust-analyzer cargo bottom 0.14.9 app/filter/::Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#; |
| 37 | |
| 38 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 39 | pub struct DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest# { |
| 40 | pub namerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 41 | |
| 42 | /// Current disk mount point. Empty string if not currently mounted. |
| 43 | pub mount_pointrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#mount_point.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 44 | |
| 45 | /// Windows also contains an additional volume name field. |
| 46 | #[cfg(target_os = "windows")] |
| 47 | pub volume_name: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 48 | |
| 49 | // TODO: Maybe unify all these? |
| 50 | pub free_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#free_space.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>, |
| 51 | pub used_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#used_space.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>, |
| 52 | pub total_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#total_space.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>, |
| 53 | } |
| 54 | |
| 55 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 56 | pub struct IoDatarust-analyzer cargo bottom 0.14.9 collection/disks/IoData# { |
| 57 | pub read_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/IoData#read_bytes.: u64, |
| 58 | pub write_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/IoData#write_bytes.: u64, |
| 59 | } |
| 60 | |
| 61 | pub type IoHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/IoHarvest# = HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<IoDatarust-analyzer cargo bottom 0.14.9 collection/disks/IoData#>>; |
| 62 | |
| 63 | cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! { |
| 64 | any(target_os = "linux", target_os = "macos", target_os = "windows") => { |
| 65 | mod io_countersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/; |
| 66 | pub use io_countersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/::IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#; |
| 67 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#; |
| 68 | |
| 69 | /// Returns the I/O usage of certain mount points. |
| 70 | pub fn get_io_usagerust-analyzer cargo bottom 0.14.9 collection/disks/get_io_usage().(_collector: &DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<IoHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/IoHarvest#> { |
| 71 | let mut io_hash: HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<IoDatarust-analyzer cargo bottom 0.14.9 collection/disks/IoData#>> = HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().(); |
| 72 | |
| 73 | |
| 74 | // TODO: Maybe rewrite this to not do a result of vec of result... |
| 75 | for io in io_statsrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/io_stats().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() { |
| 76 | let mount_point = io.device_namerust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/impl#[IoCounters]device_name().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(); |
| 77 | |
| 78 | io_hash.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().( |
| 79 | mount_point.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 80 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IoDatarust-analyzer cargo bottom 0.14.9 collection/disks/IoData# { |
| 81 | read_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/IoData#read_bytes.: io.read_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/impl#[IoCounters]read_bytes().(), |
| 82 | write_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/IoData#write_bytes.: io.write_bytesrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/impl#[IoCounters]write_bytes().(), |
| 83 | }), |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#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_entryrust-analyzer cargo bottom 0.14.9 collection/disks/keep_disk_entry().( |
| 113 | disk_name: &str, mount_point: &str, disk_filter: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#>, mount_filter: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#>, |
| 114 | ) -> bool { |
| 115 | match (disk_filter, mount_filter) { |
| 116 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(d), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(m)) => match (d.ignore_matchesrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]ignore_matches().(), m.ignore_matchesrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]ignore_matches().()) { |
| 117 | (true, true) => !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(d.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(disk_name) || m.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(mount_point)), |
| 118 | (true, false) => { |
| 119 | if m.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(mount_point) { |
| 120 | true |
| 121 | } else { |
| 122 | d.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(disk_name) |
| 123 | } |
| 124 | } |
| 125 | (false, true) => { |
| 126 | if d.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(disk_name) { |
| 127 | true |
| 128 | } else { |
| 129 | m.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(mount_point) |
| 130 | } |
| 131 | } |
| 132 | (false, false) => d.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(disk_name) || m.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(mount_point), |
| 133 | }, |
| 134 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(d), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => d.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(disk_name), |
| 135 | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(m)) => m.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(mount_point), |
| 136 | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => true, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod testrust-analyzer cargo bottom 0.14.9 collection/disks/test/ { |
| 142 | use regexrust-analyzer cargo regex 1.13.1 crate/::Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#; |
| 143 | |
| 144 | use superrust-analyzer cargo bottom 0.14.9 collection/disks/::keep_disk_entryrust-analyzer cargo bottom 0.14.9 collection/disks/keep_disk_entry().; |
| 145 | use craterust-analyzer cargo bottom 0.14.9 crate/::apprust-analyzer cargo bottom 0.14.9 app/::filterrust-analyzer cargo bottom 0.14.9 app/filter/::Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#; |
| 146 | |
| 147 | fn run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(disk_filter: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#>, mount_filter: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/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_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 158 | .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() |
| 159 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|(itx, (name, mount))| { |
| 160 | if keep_disk_entryrust-analyzer cargo bottom 0.14.9 collection/disks/keep_disk_entry().(name, mount, disk_filter, mount_filter) { |
| 161 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(itx) |
| 162 | } else { |
| 163 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 164 | } |
| 165 | }) |
| 166 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 167 | } |
| 168 | |
| 169 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 170 | fn test_keeping_disk_entryrust-analyzer cargo bottom 0.14.9 collection/disks/test/test_keeping_disk_entry().() { |
| 171 | let disk_ignore = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#::newrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]new().(true, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("nvme").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()])); |
| 172 | let disk_keep = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#::newrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]new().(false, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("nvme").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()])); |
| 173 | let mount_ignore = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#::newrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]new().(true, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("boot").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()])); |
| 174 | let mount_keep = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter#::newrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]new().(false, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("boot").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()])); |
| 175 | |
| 176 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4]); |
| 177 | |
| 178 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_ignore, &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![3, 4]); |
| 179 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_keep, &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2]); |
| 180 | |
| 181 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &mount_ignore), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3]); |
| 182 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &mount_keep), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 4]); |
| 183 | |
| 184 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_ignore, &mount_ignore), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![3]); |
| 185 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_keep, &mount_ignore), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3]); |
| 186 | |
| 187 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_ignore, &mount_keep), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 3, 4]); |
| 188 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(run_filterrust-analyzer cargo bottom 0.14.9 collection/disks/test/run_filter().(&disk_keep, &mount_keep), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 4]); |
| 189 | } |
| 190 | } |