feat(api): GPU-Leistung (power.draw) und Fehlerzustand der GPU-Abfrage (#1197, DASH-R1 AP3)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GmHhg7qE8FdhchzsBrwgjZ
This commit is contained in:
@ -578,7 +578,7 @@ impl Runtime {
|
||||
});
|
||||
}
|
||||
samples.sort_by(|a, b| a.model.cmp(&b.model));
|
||||
let gpus = read_gpus(nvidia_smi).await;
|
||||
let (gpus, gpu_status) = read_gpus(nvidia_smi).await;
|
||||
{
|
||||
let mut inner = self.shared.lock();
|
||||
// Inzwischen entladene Instanzen nicht wieder anzeigen.
|
||||
@ -598,6 +598,7 @@ impl Runtime {
|
||||
}
|
||||
self.metrics.set_instances(samples);
|
||||
self.metrics.set_gpus(gpus);
|
||||
self.metrics.set_gpu_status(gpu_status);
|
||||
}
|
||||
|
||||
/// Lehnt alle Wartenden ab und beendet alle Instanzen, auch solche, die
|
||||
@ -998,38 +999,125 @@ impl Shared {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fragt `nvidia-smi` nach Speicher, Auslastung und Temperatur je GPU. Ohne
|
||||
/// das Programm oder bei einem Fehler bleibt die Liste leer.
|
||||
async fn read_gpus(nvidia_smi: &Path) -> Vec<crate::metrics::GpuSample> {
|
||||
/// Felder der GPU-Abfrage; `power.draw` zuletzt, damit alte Treiber ohne das Feld
|
||||
/// über die kürzere Abfrage trotzdem Werte liefern.
|
||||
const GPU_FELDER: &str = "index,name,memory.used,memory.total,utilization.gpu,temperature.gpu";
|
||||
|
||||
/// Führt eine `nvidia-smi`-Abfrage aus. `Err` enthält die Art des Fehlers.
|
||||
async fn gpu_abfrage(nvidia_smi: &Path, felder: &str) -> Result<String, crate::metrics::GpuStatus> {
|
||||
use crate::metrics::GpuStatus;
|
||||
let output = tokio::time::timeout(
|
||||
Duration::from_secs(5),
|
||||
tokio::process::Command::new(nvidia_smi)
|
||||
.args([
|
||||
"--query-gpu=index,name,memory.used,memory.total,utilization.gpu,temperature.gpu",
|
||||
"--format=csv,noheader,nounits",
|
||||
format!("--query-gpu={felder}"),
|
||||
"--format=csv,noheader,nounits".to_string(),
|
||||
])
|
||||
.output(),
|
||||
)
|
||||
.await;
|
||||
match output {
|
||||
Ok(Ok(out)) if out.status.success() => {
|
||||
let mut gpus = parse_gpus(&String::from_utf8_lossy(&out.stdout));
|
||||
let header = tokio::time::timeout(
|
||||
Duration::from_secs(5),
|
||||
tokio::process::Command::new(nvidia_smi).output(),
|
||||
)
|
||||
.await;
|
||||
let cuda = match header {
|
||||
Ok(Ok(out)) if out.status.success() => {
|
||||
crate::metrics::parse_cuda_version(&String::from_utf8_lossy(&out.stdout))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
for gpu in &mut gpus {
|
||||
gpu.cuda_version = cuda.clone();
|
||||
}
|
||||
gpus
|
||||
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
|
||||
}
|
||||
_ => Vec::new(),
|
||||
Ok(Ok(out)) => Err(GpuStatus::Fehler(format!("beendet mit {}", out.status))),
|
||||
Ok(Err(e)) if e.kind() == std::io::ErrorKind::NotFound => Err(GpuStatus::KeinProgramm),
|
||||
Ok(Err(e)) => Err(GpuStatus::Fehler(e.to_string())),
|
||||
Err(_) => Err(GpuStatus::Fehler(
|
||||
"keine Antwort innerhalb von 5 s".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Fragt `nvidia-smi` nach Speicher, Auslastung, Temperatur und Leistung je GPU.
|
||||
/// Der Zustand sagt, ob „keine Werte“ heißt: Programm fehlt, Fehler oder wirklich
|
||||
/// keine GPU. Liefert die Abfrage mit `power.draw` einen Fehler (älterer
|
||||
/// Treiber), wird ohne das Feld wiederholt.
|
||||
async fn read_gpus(
|
||||
nvidia_smi: &Path,
|
||||
) -> (Vec<crate::metrics::GpuSample>, crate::metrics::GpuStatus) {
|
||||
use crate::metrics::GpuStatus;
|
||||
let mit_leistung = format!("{GPU_FELDER},power.draw");
|
||||
let text = match gpu_abfrage(nvidia_smi, &mit_leistung).await {
|
||||
Ok(text) => text,
|
||||
Err(GpuStatus::KeinProgramm) => return (Vec::new(), GpuStatus::KeinProgramm),
|
||||
Err(_) => match gpu_abfrage(nvidia_smi, GPU_FELDER).await {
|
||||
Ok(text) => text,
|
||||
Err(status) => return (Vec::new(), status),
|
||||
},
|
||||
};
|
||||
let mut gpus = parse_gpus(&text);
|
||||
let header = tokio::time::timeout(
|
||||
Duration::from_secs(5),
|
||||
tokio::process::Command::new(nvidia_smi).output(),
|
||||
)
|
||||
.await;
|
||||
let cuda = match header {
|
||||
Ok(Ok(out)) if out.status.success() => {
|
||||
crate::metrics::parse_cuda_version(&String::from_utf8_lossy(&out.stdout))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
for gpu in &mut gpus {
|
||||
gpu.cuda_version = cuda.clone();
|
||||
}
|
||||
(gpus, GpuStatus::Ok)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod gpu_tests {
|
||||
use super::*;
|
||||
use crate::metrics::GpuStatus;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
/// Legt ein Shell-Skript als `nvidia-smi`-Attrappe an.
|
||||
fn attrappe(name: &str, inhalt: &str) -> std::path::PathBuf {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("nexus-gpu-test-{}-{name}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let pfad = dir.join("nvidia-smi");
|
||||
std::fs::write(&pfad, format!("#!/bin/sh\n{inhalt}\n")).unwrap();
|
||||
std::fs::set_permissions(&pfad, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
pfad
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fehlendes_programm_ist_kein_programm_und_keine_nullwerte() {
|
||||
let (gpus, status) = read_gpus(Path::new("/nicht/vorhanden/nvidia-smi")).await;
|
||||
assert!(gpus.is_empty());
|
||||
assert_eq!(status, GpuStatus::KeinProgramm);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fehlerhaftes_programm_ist_ein_fehler() {
|
||||
let pfad = attrappe("fehler", "exit 9");
|
||||
let (gpus, status) = read_gpus(&pfad).await;
|
||||
assert!(gpus.is_empty());
|
||||
assert!(matches!(status, GpuStatus::Fehler(_)), "{status:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn leistung_wird_gelesen_und_na_bleibt_unbekannt() {
|
||||
let pfad = attrappe(
|
||||
"leistung",
|
||||
"case \"$*\" in *power.draw*) echo '0, NVIDIA Test, 100, 12288, 5, 40, 45.2'; echo '1, NVIDIA Test, 100, 12288, 5, 40, [N/A]';; *) echo 'Treiber CUDA Version: 13.0';; esac",
|
||||
);
|
||||
let (gpus, status) = read_gpus(&pfad).await;
|
||||
assert_eq!(status, GpuStatus::Ok);
|
||||
assert_eq!(gpus.len(), 2);
|
||||
assert_eq!(gpus[0].power_w, Some(45.2));
|
||||
assert_eq!(gpus[1].power_w, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn alter_treiber_ohne_power_draw_faellt_auf_die_kurze_abfrage_zurueck() {
|
||||
let pfad = attrappe(
|
||||
"alt",
|
||||
"case \"$*\" in *power.draw*) exit 2;; *query-gpu*) echo '0, NVIDIA Alt, 100, 12288, 5, 40';; esac",
|
||||
);
|
||||
let (gpus, status) = read_gpus(&pfad).await;
|
||||
assert_eq!(status, GpuStatus::Ok);
|
||||
assert_eq!(gpus.len(), 1);
|
||||
assert_eq!(gpus[0].power_w, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user