| 1 | //! Based on [heim's implementation](https://github.com/heim-rs/heim/blob/master/heim-disk/src/sys/macos/counters.rs). |
| 2 | |
| 3 | use super::io_kit::{self, get_dict, get_disks, get_i64, get_string}; |
| 4 | use crate::collection::disks::IoCounters; |
| 5 | |
| 6 | fn get_device_io(device: io_kit::IoObject) -> anyhow::Result<IoCounters> { |
| 7 | let parent = device.service_parent()?; |
| 8 | |
| 9 | // XXX: Re: Conform check being disabled. |
| 10 | // |
| 11 | // Okay, so this is weird. |
| 12 | // |
| 13 | // The problem is that if I have this check - this is what sources like psutil |
| 14 | // use, for example (see https://github.com/giampaolo/psutil/blob/7eadee31db2f038763a3a6f978db1ea76bbc4674/psutil/_psutil_osx.c#LL1422C20-L1422C20) |
| 15 | // then this will only return stuff like disk0. |
| 16 | // |
| 17 | // The problem with this is that there is *never* a disk0 *disk* entry to |
| 18 | // correspond to this, so there will be entries like disk1 or whatnot. |
| 19 | // Someone's done some digging on the gopsutil repo (https://github.com/shirou/gopsutil/issues/855#issuecomment-610016435), and it seems |
| 20 | // like this is a consequence of how Apple does logical volumes. |
| 21 | // |
| 22 | // So with all that said, what I've found is that I *can* still get a mapping - |
| 23 | // but I have to disable the conform check, which... is weird. I'm not sure |
| 24 | // if this is valid at all. But it *does* seem to match Activity Monitor |
| 25 | // with regards to disk activity, so... I guess we can leave this for |
| 26 | // now...? |
| 27 | |
| 28 | // if !parent.conforms_to_block_storage_driver() { |
| 29 | // anyhow::bail!("{parent:?}, the parent of {device:?} does not conform to |
| 30 | // IOBlockStorageDriver") } |
| 31 | |
| 32 | let disk_props = device.properties()?; |
| 33 | let parent_props = parent.properties()?; |
| 34 | |
| 35 | let name = get_string(&disk_props, "BSD Name")?; |
| 36 | let stats = get_dict(&parent_props, "Statistics")?; |
| 37 | |
| 38 | let read_bytes = get_i64(&stats, "Bytes (Read)")? as u64; |
| 39 | let write_bytes = get_i64(&stats, "Bytes (Write)")? as u64; |
| 40 | |
| 41 | // let read_count = stats.get_i64("Operations (Read)")? as u64; |
| 42 | // let write_count = stats.get_i64("Operations (Write)")? as u64; |
| 43 | |
| 44 | Ok(IoCounters::new(name, read_bytes, write_bytes)) |
| 45 | } |
| 46 | |
| 47 | /// Returns an iterator of disk I/O stats. Pulls data through IOKit. |
| 48 | pub fn io_stats() -> anyhow::Result<Vec<IoCounters>> { |
| 49 | Ok(get_disks()?.filter_map(|d| get_device_io(d).ok()).collect()) |
| 50 | } |