added graceful fallback if nvml doesnt hit for the name
This commit is contained in:
@@ -12,56 +12,52 @@ pub struct GpuInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
|
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
|
||||||
//let nvml = Nvml::init()?;
|
match get_gpu_metrics() {
|
||||||
//let device = nvml.device_by_index(0)?;
|
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
|
||||||
|
let gpu_name = detect_gpu_name();
|
||||||
//let (used, total) = get_gpu_vram_usage(&device)?;
|
Ok(GpuInfo {
|
||||||
let (gpu_temp, gpu_load, vram_used, vram_total) = get_gpu_metrics()?;
|
name: Some(gpu_name),
|
||||||
let gpu_name = detect_gpu_name();
|
current_load: Some(gpu_load),
|
||||||
Ok(GpuInfo {
|
current_temp: Some(gpu_temp),
|
||||||
name: Some(gpu_name),
|
vram_total: Some(vram_total),
|
||||||
current_load: Some(gpu_load),
|
vram_used: Some(vram_used),
|
||||||
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>> {
|
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
|
||||||
let nvml = Nvml::init()?;
|
let nvml = Nvml::init();
|
||||||
let (gpu_temp, gpu_load, vram_used, vram_total) = if let nvml = nvml {
|
if let Ok(nvml) = nvml {
|
||||||
let device = nvml.device_by_index(0).ok().unwrap();
|
if let Ok(device) = nvml.device_by_index(0) {
|
||||||
let temp = device
|
let temp = device
|
||||||
.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu)
|
.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu)
|
||||||
.unwrap_or(0) as f64;
|
.unwrap_or(0) as f64;
|
||||||
let load = device
|
let load = device
|
||||||
.utilization_rates()
|
.utilization_rates()
|
||||||
.map(|u| u.gpu as f64)
|
.map(|u| u.gpu as f64)
|
||||||
.unwrap_or(0.0);
|
.unwrap_or(0.0);
|
||||||
let mem = device.memory_info().ok();
|
let mem = device.memory_info().ok();
|
||||||
let used = mem.clone().map(|m| (m.used as f64)).unwrap_or(0.0); // B
|
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); // B
|
let total = mem.map(|m| m.total as f64).unwrap_or(0.0);
|
||||||
(temp, load, used, total)
|
Ok((temp, load, used, total))
|
||||||
|
} else {
|
||||||
|
Err(anyhow::anyhow!("No NVIDIA GPU found").into())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!("Failed to initialize NVML").into());
|
Err(anyhow::anyhow!("Failed to initialize NVML").into())
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok((gpu_temp, gpu_load, vram_used, vram_total))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detect_gpu_name() -> String {
|
fn detect_gpu_name() -> String {
|
||||||
@@ -83,13 +79,10 @@ fn fallback_gpu_name() -> Option<String> {
|
|||||||
.args(&["-C", "display"])
|
.args(&["-C", "display"])
|
||||||
.output()
|
.output()
|
||||||
.ok()?;
|
.ok()?;
|
||||||
Some(
|
String::from_utf8_lossy(&output.stdout)
|
||||||
String::from_utf8_lossy(&output.stdout)
|
.lines()
|
||||||
.lines()
|
.find(|l| l.contains("product:"))
|
||||||
.find(|l| l.contains("product:"))
|
.map(|l| l.trim().replace("product:", "").trim().to_string())
|
||||||
.map(|l| l.trim().replace("product:", "").trim().to_string())
|
|
||||||
.unwrap_or("Unknown GPU".to_string()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
@@ -98,12 +91,15 @@ fn fallback_gpu_name() -> Option<String> {
|
|||||||
.args(["path", "win32_VideoController", "get", "name"])
|
.args(["path", "win32_VideoController", "get", "name"])
|
||||||
.output()
|
.output()
|
||||||
.ok()?;
|
.ok()?;
|
||||||
Some(
|
String::from_utf8_lossy(&output.stdout)
|
||||||
String::from_utf8_lossy(&output.stdout)
|
.lines()
|
||||||
.lines()
|
.skip(1) // Skip header
|
||||||
.nth(1)
|
.find(|s| !s.trim().is_empty())
|
||||||
.map(|s| s.trim().to_string())
|
.map(|s| s.trim().to_string())
|
||||||
.unwrap_or("Unknown GPU".to_string()),
|
}
|
||||||
)
|
|
||||||
|
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||||
|
{
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user