oxedyne/fe2o3/fe2o3_sys/src/uptime.rs
1.2 KiB, 1 run
created by r1870400018:10596, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! System uptime from `/proc/uptime`. |
| 2 | |
| 3 | use crate::parse::{ |
| 4 | parse_num, |
| 5 | read_to_string, |
| 6 | tokens, |
| 7 | }; |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | |
| 11 | /// Seconds since boot and seconds spent idle (summed across |
| 12 | /// every CPU). Both are floating-point as reported by the |
| 13 | /// kernel so sub-second precision is preserved. |
| 14 | #[derive(Clone, Copy, Debug, Default, PartialEq)] |
| 15 | pub struct Uptime { |
| 16 | /// Wall-clock seconds since boot. |
| 17 | pub seconds: f64, |
| 18 | /// Idle seconds, summed across every CPU. |
| 19 | pub idle_total: f64, |
| 20 | } |
| 21 | |
| 22 | impl Uptime { |
| 23 | /// Read `/proc/uptime`. |
| 24 | pub fn sample() -> Outcome<Self> { |
| 25 | let content = res!(read_to_string("/proc/uptime")); |
| 26 | Self::from_line(content.trim()) |
| 27 | } |
| 28 | |
| 29 | /// Parse one `/proc/uptime` line of the form |
| 30 | /// `<uptime_seconds> <idle_seconds>`. |
| 31 | pub fn from_line(line: &str) -> Outcome<Self> { |
| 32 | let toks = tokens(line); |
| 33 | if toks.len() < 2 { |
| 34 | return Err(err!( |
| 35 | "uptime line has {} tokens: {:?}", |
| 36 | toks.len(), line; |
| 37 | Input, Size, Decode)); |
| 38 | } |
| 39 | Ok(Self { |
| 40 | seconds: res!(parse_num::<f64>(toks[0], "uptime.seconds")), |
| 41 | idle_total: res!(parse_num::<f64>(toks[1], "uptime.idle_total")), |
| 42 | }) |
| 43 | } |
| 44 | } |