added graceful fallback if nvml doesnt hit for the name

This commit is contained in:
2025-08-09 18:58:32 +02:00
parent 41b56d165e
commit f997ba385b

View File

@@ -12,56 +12,52 @@ pub struct GpuInfo {
}
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
//let nvml = Nvml::init()?;
//let device = nvml.device_by_index(0)?;
//let (used, total) = get_gpu_vram_usage(&device)?;
let (gpu_temp, gpu_load, vram_used, vram_total) = get_gpu_metrics()?;
let gpu_name = detect_gpu_name();
Ok(GpuInfo {
name: Some(gpu_name),
current_load: Some(gpu_load),
current_temp: Some(gpu_temp),
vram_total: Some(vram_total),
vram_used: Some(vram_used),
})
match get_gpu_metrics() {
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
let gpu_name = detect_gpu_name();
Ok(GpuInfo {
name: Some(gpu_name),
current_load: Some(gpu_load),
current_temp: Some(gpu_temp),
vram_total: Some(vram_total),
vram_used: Some(vram_used),
})
}
Err(e) => {
// Graceful fallback: log error, return empty/None values
eprintln!("GPU info unavailable: {e}");
Ok(GpuInfo {
name: Some(detect_gpu_name()),
current_load: None,
current_temp: None,
vram_total: None,
vram_used: None,
})
}
}
}
/*
pub fn get_gpu_load(device: &nvml_wrapper::Device) -> Result<f64, Box<dyn Error>> {
Ok(device.utilization_rates().unwrap().gpu as f64)
}
pub fn get_gpu_temp(device: &nvml_wrapper::Device) -> Result<f64, Box<dyn Error>> {
Ok(device
.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu)
.unwrap() as f64)
}
pub fn get_gpu_vram_usage(device: &nvml_wrapper::Device) -> Result<(f64, f64), Box<dyn Error>> {
let mem_info = device.memory_info().unwrap();
Ok((mem_info.used as f64, mem_info.total as f64))
}*/
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
let nvml = Nvml::init()?;
let (gpu_temp, gpu_load, vram_used, vram_total) = if let nvml = nvml {
let device = nvml.device_by_index(0).ok().unwrap();
let temp = device
.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu)
.unwrap_or(0) as f64;
let load = device
.utilization_rates()
.map(|u| u.gpu as f64)
.unwrap_or(0.0);
let mem = device.memory_info().ok();
let used = mem.clone().map(|m| (m.used as f64)).unwrap_or(0.0); // B
let total = mem.map(|m| (m.total as f64)).unwrap_or(0.0); // B
(temp, load, used, total)
let nvml = Nvml::init();
if let Ok(nvml) = nvml {
if let Ok(device) = nvml.device_by_index(0) {
let temp = device
.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu)
.unwrap_or(0) as f64;
let load = device
.utilization_rates()
.map(|u| u.gpu as f64)
.unwrap_or(0.0);
let mem = device.memory_info().ok();
let used = mem.clone().map(|m| m.used as f64).unwrap_or(0.0);
let total = mem.map(|m| m.total as f64).unwrap_or(0.0);
Ok((temp, load, used, total))
} else {
Err(anyhow::anyhow!("No NVIDIA GPU found").into())
}
} else {
return Err(anyhow::anyhow!("Failed to initialize NVML").into());
};
Ok((gpu_temp, gpu_load, vram_used, vram_total))
Err(anyhow::anyhow!("Failed to initialize NVML").into())
}
}
fn detect_gpu_name() -> String {
@@ -83,13 +79,10 @@ fn fallback_gpu_name() -> Option<String> {
.args(&["-C", "display"])
.output()
.ok()?;
Some(
String::from_utf8_lossy(&output.stdout)
.lines()
.find(|l| l.contains("product:"))
.map(|l| l.trim().replace("product:", "").trim().to_string())
.unwrap_or("Unknown GPU".to_string()),
)
String::from_utf8_lossy(&output.stdout)
.lines()
.find(|l| l.contains("product:"))
.map(|l| l.trim().replace("product:", "").trim().to_string())
}
#[cfg(target_os = "windows")]
@@ -98,12 +91,15 @@ fn fallback_gpu_name() -> Option<String> {
.args(["path", "win32_VideoController", "get", "name"])
.output()
.ok()?;
Some(
String::from_utf8_lossy(&output.stdout)
.lines()
.nth(1)
.map(|s| s.trim().to_string())
.unwrap_or("Unknown GPU".to_string()),
)
String::from_utf8_lossy(&output.stdout)
.lines()
.skip(1) // Skip header
.find(|s| !s.trim().is_empty())
.map(|s| s.trim().to_string())
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
{
None
}
}