Files
javis_bot/app/src/system.rs
javis-bot 0973ad9815 feat(app): M5 step 2, service control and the dashboard data
- control: one Controller starts, stops and restarts the services (stopped,
  starting, running, stopping, failed); a failed start leaves nothing
  running and keeps its error with secrets masked. The app binary uses it.
- dashboard: built from the core's events alone: the bot's tag, the voice
  channel, live captions (screen only, cleared on leaving or stopping), the
  last 20 voice answers split into stages with medians (matched by the new
  AnswerAsked event), and missed requests without text.
- system: memory in use and per NVIDIA GPU its use and VRAM (nvidia-smi).
- DiscordCore::voice_members, WorkerClient::hello, the conversation's missed
  requests shared with the dashboard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 13:49:51 +09:00

99 lines
3.7 KiB
Rust

//! Memory and GPU use for the dashboard (`app/src/admin.spec.md`, "Dashboard data").
use std::process::Stdio;
use std::time::Duration;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Gpu {
pub name: String,
pub use_percent: u32,
pub vram_used_mb: u64,
pub vram_total_mb: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Usage {
pub ram_used_mb: Option<u64>,
pub ram_total_mb: Option<u64>,
pub gpus: Vec<Gpu>,
}
/// One line per GPU of `nvidia-smi --query-gpu=name,utilization.gpu,memory.used,memory.total
/// --format=csv,noheader,nounits`; lines that do not parse are skipped.
pub fn parse_nvidia_smi(out: &str) -> Vec<Gpu> {
out.lines().filter_map(|l| {
let f: Vec<&str> = l.split(',').map(str::trim).collect();
let [name, util, used, total] = f.as_slice() else { return None };
Some(Gpu { name: (*name).into(), use_percent: util.parse().ok()?, vram_used_mb: used.parse().ok()?,
vram_total_mb: total.parse().ok()? })
}).collect()
}
/// The GPUs `nvidia-smi` reports; empty without one (or when it does not answer within 2 s).
pub async fn gpus() -> Vec<Gpu> {
let mut cmd = tokio::process::Command::new("nvidia-smi");
cmd.args(["--query-gpu=name,utilization.gpu,memory.used,memory.total", "--format=csv,noheader,nounits"])
.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null()).kill_on_drop(true);
#[cfg(windows)]
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
match tokio::time::timeout(Duration::from_secs(2), cmd.output()).await {
Ok(Ok(o)) if o.status.success() => parse_nvidia_smi(&String::from_utf8_lossy(&o.stdout)),
_ => vec![],
}
}
pub async fn usage() -> Usage {
let (used, total) = memory_mb();
Usage { ram_used_mb: used, ram_total_mb: total, gpus: gpus().await }
}
#[cfg(target_os = "linux")]
fn memory_mb() -> (Option<u64>, Option<u64>) {
let Ok(s) = std::fs::read_to_string("/proc/meminfo") else { return (None, None) };
let kb = |key: &str| s.lines().find(|l| l.starts_with(key))
.and_then(|l| l.split_whitespace().nth(1)?.parse::<u64>().ok());
let (total, avail) = (kb("MemTotal:"), kb("MemAvailable:"));
(total.zip(avail).map(|(t, a)| (t - a) / 1024), total.map(|t| t / 1024))
}
#[cfg(windows)]
fn memory_mb() -> (Option<u64>, Option<u64>) {
use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
// SAFETY: the struct is sized and zeroed as the API asks.
let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() };
m.dwLength = std::mem::size_of::<MEMORYSTATUSEX>() as u32;
if unsafe { GlobalMemoryStatusEx(&mut m) } == 0 {
return (None, None);
}
let mb = 1024 * 1024;
(Some((m.ullTotalPhys - m.ullAvailPhys) / mb), Some(m.ullTotalPhys / mb))
}
#[cfg(not(any(target_os = "linux", windows)))]
fn memory_mb() -> (Option<u64>, Option<u64>) {
(None, None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nvidia_smi_lines_become_gpus_and_junk_is_skipped() {
let out = "NVIDIA GeForce RTX 5050, 37, 5120, 8151\nNVIDIA GeForce RTX 4090, [N/A], 1, 24564\n\n";
assert_eq!(parse_nvidia_smi(out), vec![Gpu { name: "NVIDIA GeForce RTX 5050".into(), use_percent: 37,
vram_used_mb: 5120, vram_total_mb: 8151 }]);
}
#[tokio::test]
async fn usage_reads_this_machines_memory() {
let u = usage().await;
if cfg!(any(target_os = "linux", windows)) {
let (used, total) = (u.ram_used_mb.unwrap(), u.ram_total_mb.unwrap());
assert!(used > 0 && used <= total);
}
}
}