refactor: flatten IoCounter return value (#1253)

* refactor: rewrite io stats collection function result

* refactor: flatten IoCounters vector results
This commit is contained in:
Clement Tsang 2023-07-11 01:18:58 -04:00 committed by GitHub
parent f7ff2fc180
commit b0cb308106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 14 deletions

View File

@ -64,7 +64,8 @@ cfg_if! {
pub fn get_io_usage() -> anyhow::Result<IoHarvest> {
let mut io_hash: HashMap<String, Option<IoData>> = HashMap::new();
for io in io_stats()?.into_iter().flatten() {
// TODO: Maybe rewrite this to not do a result of vec of result...
for io in io_stats()?.into_iter() {
let mount_point = io.device_name().to_string_lossy();
io_hash.insert(

View File

@ -29,6 +29,7 @@ struct FileSystem {
}
pub fn get_io_usage() -> error::Result<IoHarvest> {
// TODO: Should this (and other I/O collectors) fail fast? In general, should collection ever fail fast?
#[allow(unused_mut)]
let mut io_harvest: HashMap<String, Option<IoData>> =
get_disk_info().map(|storage_system_information| {
@ -43,7 +44,7 @@ pub fn get_io_usage() -> error::Result<IoHarvest> {
{
use crate::app::data_harvester::disks::zfs_io_counters;
if let Ok(zfs_io) = zfs_io_counters::zfs_io_stats() {
for io in zfs_io.into_iter().flatten() {
for io in zfs_io.into_iter() {
let mount_point = io.device_name().to_string_lossy();
io_harvest.insert(
mount_point.to_string(),

View File

@ -64,7 +64,7 @@ impl FromStr for IoCounters {
}
/// Returns an iterator of disk I/O stats. Pulls data from `/proc/diskstats`.
pub fn io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
pub fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
const PROC_DISKSTATS: &str = "/proc/diskstats";
let mut results = vec![];
@ -74,7 +74,9 @@ pub fn io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
// This saves us from doing a string allocation on each iteration compared to `lines()`.
while let Ok(bytes) = reader.read_line(&mut line) {
if bytes > 0 {
results.push(IoCounters::from_str(&line));
if let Ok(counters) = IoCounters::from_str(&line) {
results.push(counters);
}
line.clear();
} else {
break;

View File

@ -44,6 +44,6 @@ fn get_device_io(device: io_kit::IoObject) -> anyhow::Result<IoCounters> {
}
/// Returns an iterator of disk I/O stats. Pulls data through IOKit.
pub fn io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
Ok(get_disks()?.map(get_device_io).collect())
pub fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
Ok(get_disks()?.filter_map(|d| get_device_io(d).ok()).collect())
}

View File

@ -11,7 +11,7 @@ mod bindings;
use bindings::*;
/// Returns I/O stats.
pub(crate) fn io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
pub(crate) fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
let volume_io = all_volume_io()?;
Ok(volume_io
@ -23,6 +23,7 @@ pub(crate) fn io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
IoCounters::new(name, read_bytes, write_bytes)
})
.flatten()
.collect::<Vec<_>>())
}

View File

@ -2,7 +2,7 @@ use crate::app::data_harvester::disks::IoCounters;
/// Returns zpool I/O stats. Pulls data from `sysctl kstat.zfs.{POOL}.dataset.{objset-*}`
#[cfg(target_os = "freebsd")]
pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
pub fn zfs_io_stats() -> anyhow::Result<Vec<IoCounters>> {
use sysctl::Sysctl;
let zfs_ctls: Vec<_> = sysctl::Ctl::new("kstat.zfs.")?
.into_iter()
@ -27,7 +27,7 @@ pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
.collect();
use itertools::Itertools;
let results: Vec<anyhow::Result<IoCounters>> = zfs_ctls
let results: Vec<IoCounters> = zfs_ctls
.iter()
.chunks(3)
.into_iter()
@ -50,7 +50,7 @@ pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
}
}
}
Some(Ok(IoCounters::new(ds_name, nread, nwrite)))
Some(IoCounters::new(ds_name, nread, nwrite))
})
.collect();
Ok(results)
@ -58,7 +58,7 @@ pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
/// Returns zpool I/O stats. Pulls data from `/proc/spl/kstat/zfs/*/objset-*`.
#[cfg(target_os = "linux")]
pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
pub fn zfs_io_stats() -> anyhow::Result<Vec<IoCounters>> {
if let Ok(zpools) = std::fs::read_dir("/proc/spl/kstat/zfs") {
let zpools_vec: Vec<std::path::PathBuf> = zpools
.filter_map(|e| {
@ -90,7 +90,7 @@ pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
})
})
.collect();
let io_counters: Vec<anyhow::Result<IoCounters>> = datasets_vec
let io_counters: Vec<IoCounters> = datasets_vec
.iter()
.filter_map(|ds| {
// get io-counter from each dataset
@ -130,9 +130,9 @@ pub fn zfs_io_stats() -> anyhow::Result<Vec<anyhow::Result<IoCounters>>> {
}
}
});
let counter = IoCounters::new(name.to_owned(), read, write);
//log::debug!("adding io counter for zfs {:?}", counter);
Some(Ok(counter))
Some(counter)
} else {
None
}