monitoring kit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![feature(iter_array_chunks, generic_const_exprs, portable_simd)]
use anyhow::*;
use atools::prelude::*;
use collar::CollectArray;
use hwmons::{hwmons, Hwmon};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::{read_to_string as read, File};
use std::io::Read;
use std::io::Seek;
use std::mem::replace;
use std::path::Path;
use std::sync::OnceLock;

#[derive(Copy, Clone, Debug)]
pub enum ViewCore {
    All(u64),
    One(u64),
}
impl Display for ViewCore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ViewCore::All(x) => write!(f, "..#{x}"),
            ViewCore::One(x) => write!(f, "#{x}"),
        }
    }
}

pub static CORE: OnceLock<ViewCore> = OnceLock::new();

#[derive(Debug, Clone, Default, PartialEq)]
pub struct CpuInfo {
    pub name: String,
    pub speed: f64,
    pub count: u64,
}

#[implicit_fn::implicit_fn]
pub fn temps() -> Result<Hwmon> {
    if let x @ Some(_) = hwmons().find(_.contains("Package")) {
        if let Some(c) = core() {
            hwmons().find(_.contains(&c.to_string()))
        } else {
            x
        }
        .ok_or(anyhow!("no intel hwmon"))?
    } else {
        hwmons()
            .find(_.contains("Tccd"))
            .ok_or(anyhow!("no amd hwmon"))?
    }
    .load()
}

impl CpuInfo {
    pub fn read() -> Result<Self> {
        let x = String::from_utf8(
            std::process::Command::new("lscpu")
                .env("LC_ALL", "C")
                .output()
                .context("lscpuless")?
                .stdout,
        )
        .context("unable to parse lscpu output to UTF-8")?;
        let x = x
            .lines()
            .filter_map(|l| l.split_once(":").map(|(a, b)| (a, b.trim())))
            .collect::<HashMap<_, _>>();
        let name = x["Model name"];
        let name = if name.contains("Intel") {
            let name = name
                .replace("Core", "")
                .replace("(TM)", "")
                .replace("Intel", "")
                .replace("(R)", "");
            let name = regex::Regex::new(r"[0-9]+th Gen")
                .unwrap()
                .replace(&name, "")
                .replace("  ", " ")
                .trim()
                .replace("  ", " ");
            regex::Regex::new(r"@ [0-9]+\.[0-9]+GHz")
                .unwrap()
                .replace(&name, "")
                .to_lowercase()
        } else {
            name.replace("AMD Ryzen", "Ryzen")
                .replace("Processor", "")
                .trim()
                .to_owned()
        };

        Ok(Self {
            name: name,
            speed: x["CPU max MHz"].parse::<f64>().context("cpu mhz??")? / 1000.0,
            count: x["CPU(s)"].parse()?,
        })
    }
}

pub fn sped(core: u64) -> Result<f64> {
    Ok(read(format!(
        "/sys/devices/system/cpu/cpu{core}/cpufreq/scaling_cur_freq"
    ))
    .context("speed")?
    .replace('\n', "")
    .parse::<u64>()
    .context("reading speeds")? as f64
        / 1e6)
}

pub fn speed() -> Result<f64> {
    Ok(match *CORE.get().unwrap() {
        ViewCore::All(x) => (0..x).map(sped).sum::<Result<f64, _>>()? / x as f64,
        ViewCore::One(x) => sped(x)?,
    })
}

pub fn core() -> Option<u64> {
    CORE.get().copied().and_then(|x| match x {
        ViewCore::One(x) => Some(x),
        _ => None,
    })
}

pub fn usage() -> Result<f64> {
    let x = read("/proc/stat")?;
    let x = x
        .lines()
        .nth(core().map_or(0, |x| x as usize + 1))
        .ok_or(anyhow!("no procstat"))?;

    // https://www.linuxhowtos.org/System/procstat.htm
    let x @ [_user, _nice, _system, idle, iowait, _irq, _softirq] = x
        .split_whitespace()
        .skip(1)
        .map(|x| x.parse::<u64>())
        .try_collect_array()?;

    static LAST: Mutex<[u64; 2]> = Mutex::new([0; 2]);
    let [pi, pt] = &mut *LAST.lock();

    let idle = idle + iowait;
    let tot = x.sum();

    let idle = (idle - replace(pi, idle)) as f64;
    let tot = (tot - replace(pt, tot)) as f64;
    let r = (tot - idle) / tot;
    Ok((r == r).then_some(r).unwrap_or(0.0))
}