heim_cpu/stats.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use std::fmt;
use heim_common::prelude::*;
use crate::sys;
/// CPU statistics.
///
/// See [OS-specific extensions](./os/index.html) for more statistics.
pub struct CpuStats(sys::CpuStats);
wrap!(CpuStats, sys::CpuStats);
impl CpuStats {
/// Returns number of context switches (voluntary + involuntary) since system boot.
pub fn ctx_switches(&self) -> u64 {
self.as_ref().ctx_switches()
}
/// Returns number of interrupts since system boot.
pub fn interrupts(&self) -> u64 {
self.as_ref().interrupts()
}
}
impl fmt::Debug for CpuStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("CpuStats")
.field("ctx_switches", &self.ctx_switches())
.field("interrupts", &self.interrupts())
.finish()
}
}
/// Returns future which will resolve into [CpuStats].
///
/// [CpuStats]: ./struct.CpuStats.html
pub fn stats() -> impl Future<Output = Result<CpuStats>> {
sys::stats().map_ok(Into::into)
}