- admin_api: the one entry point of the screens (app status, data folder, PC check, Discord token check and pickers, settings, service control and the dashboard, logs query, error report and zip export). - discord_setup: token, Message Content and invite link checks over REST. - app/ui: first-run wizard, dashboard, five settings screens and the log screen in plain HTML/CSS/JS (dark theme after DESIGN.md, Korean text). - jarvis-admin-dev: a loopback, token-guarded development server so the screens run in a browser against the real commands. Checked in a browser with the real test bot and voice worker: wizard steps, start/restart/stop, settings save, chat channels, logs and export. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.8 KiB
Rust
125 lines
4.8 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![],
|
|
}
|
|
}
|
|
|
|
/// The NVIDIA driver version (`581.57`), None without nvidia-smi.
|
|
pub async fn driver_version() -> Option<String> {
|
|
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::<u32>().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<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 }]);
|
|
}
|
|
|
|
#[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);
|
|
}
|
|
}
|
|
}
|