| 1 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::disksrust-analyzer cargo bottom 0.14.9 collection/disks/::IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#; |
| 2 | |
| 3 | /// Returns zpool I/O stats. Pulls data from `sysctl |
| 4 | /// kstat.zfs.{POOL}.dataset.{objset-*}` |
| 5 | #[cfg(target_os = "freebsd")] |
| 6 | pub fn zfs_io_stats() -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#>> { |
| 7 | use sysctl::Sysctl; |
| 8 | let zfs_ctls: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = sysctl::Ctl::new("kstat.zfs.")? |
| 9 | .into_iter() |
| 10 | .filter_map(|e| { |
| 11 | e.ok().and_then(|ctl| { |
| 12 | let name = ctl.name(); |
| 13 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(name) = name { |
| 14 | if name.contains("objset-") |
| 15 | && (name.contains("dataset_name") |
| 16 | || name.contains("nwritten") |
| 17 | || name.contains("nread")) |
| 18 | { |
| 19 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ctl) |
| 20 | } else { |
| 21 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 22 | } |
| 23 | } else { |
| 24 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 25 | } |
| 26 | }) |
| 27 | }) |
| 28 | .collect(); |
| 29 | |
| 30 | use itertoolsrust-analyzer cargo itertools 0.15.0 crate/::Itertoolsrust-analyzer cargo itertools 0.15.0 Itertools#; |
| 31 | let results: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#> = zfs_ctls |
| 32 | .iter() |
| 33 | .chunks(3) |
| 34 | .into_iter() |
| 35 | .filter_map(|chunk| { |
| 36 | let mut nread = 0; |
| 37 | let mut nwrite = 0; |
| 38 | let mut ds_name = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::new(); |
| 39 | for ctl in chunk { |
| 40 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(name) = ctl.name() { |
| 41 | if name.contains("dataset_name") { |
| 42 | ds_name = ctl.value_string().ok()?; |
| 43 | } else if name.contains("nread") { |
| 44 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sysctl::CtlValue::U64(val)) = ctl.value() { |
| 45 | nread = val; |
| 46 | } |
| 47 | } else if name.contains("nwritten") { |
| 48 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sysctl::CtlValue::U64(val)) = ctl.value() { |
| 49 | nwrite = val; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#::new(ds_name, nread, nwrite)) |
| 55 | }) |
| 56 | .collect(); |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(results) |
| 58 | } |
| 59 | |
| 60 | /// Returns zpool I/O stats. Pulls data from `/proc/spl/kstat/zfs/*/objset-*`. |
| 61 | #[cfg(target_os = "linux")] |
| 62 | pub fn zfs_io_statsrust-analyzer cargo bottom 0.14.9 collection/disks/zfs_io_counters/zfs_io_stats().() -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#>> { |
| 63 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(zpools) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().("/proc/spl/kstat/zfs") { |
| 64 | let zpools_vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> = zpools |
| 65 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|e| { |
| 66 | e.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|d| { |
| 67 | let p = d.pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().(); |
| 68 | if p.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_dir().() { Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(p) } else { Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# } |
| 69 | }) |
| 70 | }) |
| 71 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 72 | let results = zpools_vec |
| 73 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 74 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|zpool| { |
| 75 | // go through each pool |
| 76 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(datasets) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().(zpool) { |
| 77 | let datasets_vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> = |
| 78 | datasets // go through dataset |
| 79 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|e| { |
| 80 | e.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|d| { |
| 81 | let p = d.pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().(); |
| 82 | if p.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() && p.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("objset-") { |
| 83 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(p) |
| 84 | } else { |
| 85 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 86 | } |
| 87 | }) |
| 88 | }) |
| 89 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 90 | let io_counters: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#> = datasets_vec |
| 91 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 92 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|ds| { |
| 93 | // get io-counter from each dataset |
| 94 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(contents) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(ds) { |
| 95 | let mut read = 0; |
| 96 | let mut write = 0; |
| 97 | let mut name = ""; |
| 98 | contents.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|line| { |
| 99 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((label, value)) = line.split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().(' ') { |
| 100 | match label { |
| 101 | "dataset_name" => { |
| 102 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_type, val)) = |
| 103 | value.trim_startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start().().rsplit_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rsplit_once().(' ') |
| 104 | { |
| 105 | name = val; |
| 106 | } |
| 107 | } |
| 108 | "nwritten" => { |
| 109 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_type, val)) = |
| 110 | value.trim_startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start().().rsplit_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rsplit_once().(' ') |
| 111 | && let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(number) = val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>() |
| 112 | { |
| 113 | write = number; |
| 114 | } |
| 115 | } |
| 116 | "nread" => { |
| 117 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_type, val)) = |
| 118 | value.trim_startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start().().rsplit_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rsplit_once().(' ') |
| 119 | && let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(number) = val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>() |
| 120 | { |
| 121 | read = number; |
| 122 | } |
| 123 | } |
| 124 | _ => {} |
| 125 | } |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | let counter = IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#::newrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/impl#[IoCounters]new().(name.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), read, write); |
| 130 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(counter) |
| 131 | } else { |
| 132 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 133 | } |
| 134 | }) |
| 135 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 136 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(io_counters) |
| 137 | } else { |
| 138 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 139 | } |
| 140 | }) |
| 141 | .flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().() |
| 142 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); // combine io-counters |
| 143 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(results) |
| 144 | } else { |
| 145 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(anyhowrust-analyzer cargo anyhow 1.0.104 crate/::anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!!("Unable to open zfs proc directory")) |
| 146 | } |
| 147 | } |