monitoring kit
memory
bendn 11 months ago
parent 07f6937 · commit 00c6249
-rw-r--r--Cargo.toml2
-rw-r--r--cpu/src/main.rs12
-rw-r--r--grapher/src/lib.rs15
-rw-r--r--memory/Cargo.toml18
-rw-r--r--memory/src/main.rs75
5 files changed, 106 insertions, 16 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 2ca3a24..28b58cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
-members = ["cpu", "grapher"]
+members = ["cpu", "grapher", "memory"]
resolver = "2"
[profile.release]
diff --git a/cpu/src/main.rs b/cpu/src/main.rs
index adc0e29..9973dc6 100644
--- a/cpu/src/main.rs
+++ b/cpu/src/main.rs
@@ -224,12 +224,14 @@ fn main() -> Result<()> {
}
g.draw(|y| {
- inter(
- [243, 64, 64].map(|x| x as f32 / 255.0), // red
- [228, 197, 63].map(|x| x as f32 / 255.0), // yellow
- y as f32 / (h - 1) as f32,
+ Some(
+ inter(
+ [243, 64, 64].map(|x| x as f32 / 255.0), // red
+ [228, 197, 63].map(|x| x as f32 / 255.0), // yellow
+ y as f32 / (h - 1) as f32,
+ )
+ .map(|x| (x * 255.0) as u8),
)
- .map(|x| (x * 255.0) as u8)
})?;
write!(g.buffer, "{}{}", White.fg_str(), cursor::Goto(1, 1))?;
diff --git a/grapher/src/lib.rs b/grapher/src/lib.rs
index 09f2fbb..d4f5b8b 100644
--- a/grapher/src/lib.rs
+++ b/grapher/src/lib.rs
@@ -23,7 +23,7 @@ impl Grapher {
self.data.pop_back();
}
- pub fn draw(&mut self, mut color: impl FnMut(u16) -> [u8; 3]) -> Result<&mut Vec<u8>> {
+ pub fn draw(&mut self, mut color: impl FnMut(u16) -> Option<[u8; 3]>) -> Result<&mut Vec<u8>> {
let Grapher {
buffer: output,
data,
@@ -61,16 +61,11 @@ impl Grapher {
})
.collect::<Vec<_>>();
assert_eq!(string.len(), w as usize);
- write!(
- output,
- "\x1B[?7h{}{}{}",
- clear::All,
- Blue.fg_str(),
- cursor::Goto(1, 1)
- )?;
+ write!(output, "\x1B[?7h{}{}", clear::All, cursor::Goto(1, 1))?;
for y in 0..h as usize {
- let [r, g, b] = color(y as u16);
- write!(output, "{}", Rgb(r, g, b).fg_string())?;
+ if let Some([r, g, b]) = color(y as u16) {
+ write!(output, "{}", Rgb(r, g, b).fg_string())?;
+ }
for x in 0..w as usize {
output.extend(bl(string[x][y]));
diff --git a/memory/Cargo.toml b/memory/Cargo.toml
new file mode 100644
index 0000000..e56d7bd
--- /dev/null
+++ b/memory/Cargo.toml
@@ -0,0 +1,18 @@
+[package]
+name = "memory"
+version = "1.0.0"
+edition = "2021"
+authors = ["bend-n <[email protected]>"]
+repository = "https://github.com/bend-n/monitorkit"
+license = "MIT"
+rust-version = "1.85"
+
+[dependencies]
+anyhow = "1.0.97"
+atools = "0.1.6"
+collar = "1.0.1"
+comat = "0.1.3"
+parking_lot = "0.12.3"
+regex = "1.11.1"
+termion = "4.0.4"
+grapher = { path = "../grapher" }
diff --git a/memory/src/main.rs b/memory/src/main.rs
new file mode 100644
index 0000000..94497a6
--- /dev/null
+++ b/memory/src/main.rs
@@ -0,0 +1,75 @@
+#![feature(let_chains, iter_array_chunks, array_chunks, portable_simd, iter_chain)]
+use anyhow::{Context, Result};
+use comat::cwrite;
+use grapher::Grapher;
+use std::array;
+use std::collections::HashMap;
+use std::fs::read_to_string as read;
+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 info() -> Result<[u64; 2]> {
+ let x = read("/proc/meminfo").context("no!!")?;
+ let x = x
+ .lines()
+ .filter_map(|l| l.split_once(":").map(|(a, b)| (a, b.trim())))
+ .collect::<HashMap<_, _>>();
+ let [t, f] = [x["MemTotal"], x["MemAvailable"]].map(|x| {
+ x.bytes()
+ .filter(u8::is_ascii_digit)
+ .fold(0, |acc, x| acc * 10 + (x - b'0') as u64)
+ });
+ return Ok([t, t - f]);
+}
+
+fn usage() -> Result<f64> {
+ let [t, f] = info()?;
+ Ok(f as f64 / t as f64)
+}
+
+fn main() -> Result<()> {
+ let mut g = Grapher::new()?;
+ g.push_point(usage()?);
+
+ 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 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,
+ _ => (),
+ }
+ }
+ let [tot, used] = info()?.map(|x| x as f64 / (1024. * 1024.));
+ let tot = tot.round();
+ g.draw(|_| None)?;
+
+ write!(g.buffer, "{}{}", White.fg_str(), cursor::Goto(1, 1))?;
+ let fps = (1f32 / d).round();
+ cwrite!(g.buffer, " {fps}fps ──── {used:.2} / {tot:.2}",)?;
+ write!(stdout, "{}", Rgb(0x8d, 0x1a, 0xc5).fg_string())?;
+ stdout.write_all(&g.buffer)?;
+ stdout.flush()?;
+
+ g.push_point(usage()?);
+
+ sleep(Duration::from_secs_f32(d));
+ }
+ println!("\x1B[?7l");
+ Ok(())
+}