heim_cpu/
stats.rs

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