heim_cpu/times.rs
1use std::fmt;
2
3use heim_common::prelude::*;
4use heim_common::units::Time;
5
6use crate::sys;
7
8/// System CPU time.
9///
10/// ## Compatibility
11///
12/// For Linux additional information can be retrieved with [CpuTimeExt] extension trait.
13///
14/// [CpuTimeExt]: ./os/linux/trait.CpuTimeExt.html
15pub struct CpuTime(sys::CpuTime);
16
17wrap!(CpuTime, sys::CpuTime);
18
19impl CpuTime {
20 /// Returns time spent by normal processes executing in user mode.
21 ///
22 /// ## Compatibility
23 ///
24 /// * on Linux this also includes guest time
25 pub fn user(&self) -> Time {
26 self.as_ref().user()
27 }
28
29 /// Returns time spent by processes executing in kernel mode.
30 pub fn system(&self) -> Time {
31 self.as_ref().system()
32 }
33
34 /// Returns time spent doing nothing.
35 pub fn idle(&self) -> Time {
36 self.as_ref().idle()
37 }
38}
39
40impl fmt::Debug for CpuTime {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 f.debug_struct("CpuTime")
43 .field("user", &self.user())
44 .field("system", &self.system())
45 .field("idle", &self.idle())
46 .finish()
47 }
48}
49
50/// Returns future which will resolve into cumulative value of all [CPU times].
51///
52/// [CPU times]: struct.CpuTime.html
53pub fn time() -> impl Future<Output = Result<CpuTime>> {
54 sys::time().map_ok(Into::into)
55}
56
57/// Returns stream which will yield [CPU time] for each CPU.
58///
59/// [CPU time]: struct.CpuTime.html
60pub fn times() -> impl Stream<Item = Result<CpuTime>> {
61 sys::times().map_ok(Into::into)
62}