monitoring kit
Diffstat (limited to 'cpu/src/temps.rs')
| -rw-r--r-- | cpu/src/temps.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/cpu/src/temps.rs b/cpu/src/temps.rs new file mode 100644 index 0000000..ea0031f --- /dev/null +++ b/cpu/src/temps.rs @@ -0,0 +1,87 @@ +#![feature(let_chains, iter_array_chunks, array_chunks, portable_simd, iter_chain)] +use anyhow::{ensure, Result}; +use comat::cwrite; +use cpu::*; +use grapher::Grapher; +use std::array; +use std::io::Write; +use std::io::{stdout, Read}; +use std::thread::sleep; +use std::time::Duration; +use termion::color::*; +use termion::cursor::Hide; +use termion::raw::IntoRawMode; +use termion::screen::IntoAlternateScreen; +use termion::{async_stdin, clear, cursor, style}; + +fn main() -> Result<()> { + fn inter([a, b, c]: [f32; 3], [d, e, f]: [f32; 3], fc: f32) -> [f32; 3] { + [a + (d - a) * fc, b + (e - b) * fc, c + (f - c) * fc] + } + + let info = CpuInfo::read()?; + let core = std::env::args() + .nth(1) + .and_then(|x| x.parse::<u64>().ok()) + .map_or(ViewCore::All(info.count), ViewCore::One); + CORE.set(core).unwrap(); + match core { + ViewCore::One(x) => ensure!(x < info.count, "not enough cores"), + _ => (), + } + let mut t = Temps::load()?; + + let mut g = Grapher::new()?; + g.push_point(t.read()? / 105.0); + + let mut d = 0.1; + + let mut stdout = stdout().into_raw_mode()?.into_alternate_screen()?; + let mut stdin = async_stdin(); + write!(stdout, "{}{}{}", Hide, clear::All, style::Reset).unwrap(); + + 'out: loop { + let (_, h) = termion::terminal_size()?; + + let mut key = 0; + while stdin.read(array::from_mut(&mut key)).unwrap() != 0 { + match key { + b'q' => break 'out, + b'+' => d = (d + 0.1f32).min(1.0), + b'-' if d >= 0.2 => d -= 0.1, + b'-' if d >= 0.02 => d -= 0.01, + b'-' => d = 0.00833, + _ => (), + } + } + + g.draw(|y| { + Some( + inter( + [228, 197, 63].map(|x| x as f32 / 255.0), // yellow + [0x5c, 0xc7, 0xdd].map(|x| x as f32 / 255.0), // blue + y as f32 / (h - 1) as f32, + ) + .map(|x| (x * 255.0) as u8), + ) + })?; + + write!(g.buffer, "{}{}", White.fg_str(), cursor::Goto(1, 1))?; + let name = &*info.name; + let speed = speed()?; + let temp = t.read()?; + let fps = (1f32 / d).round(); + cwrite!( + g.buffer, + " {fps}fps ──── {name} {core} @ {speed:.2} GHz ──── {red}{temp}{reset} °C", + )?; + stdout.write_all(&g.buffer)?; + stdout.flush()?; + + g.push_point(temp / 105.); + + sleep(Duration::from_secs_f32(d)); + } + println!("\x1B[?7l"); + Ok(()) +} |