1 use crate::from_iter;
2 use crate::ProcResult;
3 use std::io::Read;
4 
5 /// Provides scheduler statistics of the process, based on the `/proc/<pid>/schedstat` file.
6 #[derive(Debug, Clone)]
7 pub struct Schedstat {
8     /// Time spent on the cpu.
9     ///
10     /// Measured in nanoseconds.
11     pub sum_exec_runtime: u64,
12     /// Time spent waiting on a runqueue.
13     ///
14     /// Measured in nanoseconds.
15     pub run_delay: u64,
16     /// \# of timeslices run on this cpu.
17     pub pcount: u64,
18 }
19 
20 impl Schedstat {
from_reader<R: Read>(mut r: R) -> ProcResult<Schedstat>21     pub fn from_reader<R: Read>(mut r: R) -> ProcResult<Schedstat> {
22         let mut line = String::new();
23         r.read_to_string(&mut line)?;
24         let mut s = line.split_whitespace();
25 
26         let schedstat = Schedstat {
27             sum_exec_runtime: expect!(from_iter(&mut s)),
28             run_delay: expect!(from_iter(&mut s)),
29             pcount: expect!(from_iter(&mut s)),
30         };
31 
32         if cfg!(test) {
33             assert!(s.next().is_none());
34         }
35 
36         Ok(schedstat)
37     }
38 }
39