//! 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, pub ram_total_mb: Option, pub gpus: Vec, } /// 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 { 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 { 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![], } } /// The NVIDIA driver version (`581.57`), None without nvidia-smi. pub async fn driver_version() -> Option { let mut cmd = tokio::process::Command::new("nvidia-smi"); cmd.args(["--query-gpu=driver_version", "--format=csv,noheader"]) .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() => String::from_utf8_lossy(&o.stdout).lines().next().map(|l| l.trim().to_owned()), _ => None, } } /// CUDA 13 needs driver 580 or newer (desktop-app.md, "PC 점검"). pub const MIN_DRIVER_MAJOR: u32 = 580; pub fn driver_ok(version: &str) -> bool { version.split('.').next().and_then(|m| m.parse::().ok()).is_some_and(|m| m >= MIN_DRIVER_MAJOR) } 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, Option) { 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::().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, Option) { 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::() 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, Option) { (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 }]); } #[test] fn drivers_from_580_run_cuda_13() { assert!(driver_ok("581.57") && driver_ok("595.91.07") && driver_ok("580.0")); assert!(!driver_ok("572.16") && !driver_ok("") && !driver_ok("abc")); } #[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); } } }