Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_sys/src/load.rs

1.8 KiB, 1 run

created by r1870400018:10584, 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//! Load-average sampling from `/proc/loadavg`.
2
3use crate::parse::{
4 parse_num,
5 read_to_string,
6 tokens,
7};
8
9use oxedyne_fe2o3_core::prelude::*;
10
11/// The three classic load averages plus the runnable / total
12/// task counter the kernel writes after them.
13#[derive(Clone, Copy, Debug, Default, PartialEq)]
14pub struct LoadAvg {
15 /// One-minute load average.
16 pub one: f64,
17 /// Five-minute load average.
18 pub five: f64,
19 /// Fifteen-minute load average.
20 pub fifteen: f64,
21 /// Number of currently runnable scheduling entities.
22 pub runnable: u64,
23 /// Total number of scheduling entities.
24 pub total: u64,
25}
26
27impl LoadAvg {
28 /// Read `/proc/loadavg`.
29 pub fn sample() -> Outcome<Self> {
30 let content = res!(read_to_string("/proc/loadavg"));
31 Self::from_line(content.trim())
32 }
33
34 /// Parse one `/proc/loadavg` line of the form
35 /// `<one> <five> <fifteen> <runnable>/<total> <last_pid>`.
36 pub fn from_line(line: &str) -> Outcome<Self> {
37 let toks = tokens(line);
38 if toks.len() < 4 {
39 return Err(err!(
40 "loadavg line has {} tokens: {:?}",
41 toks.len(), line;
42 Input, Size, Decode));
43 }
44 let one = res!(parse_num::<f64>(toks[0], "loadavg.one"));
45 let five = res!(parse_num::<f64>(toks[1], "loadavg.five"));
46 let fifteen = res!(parse_num::<f64>(toks[2], "loadavg.fifteen"));
47 let (runnable, total) = match toks[3].split_once('/') {
48 Some((r, t)) => (
49 res!(parse_num::<u64>(r, "loadavg.runnable")),
50 res!(parse_num::<u64>(t, "loadavg.total")),
51 ),
52 None => (0, 0),
53 };
54 Ok(Self { one, five, fifteen, runnable, total })
55 }
56}