fixed gpu detection - bc unknown lib
This commit is contained in:
@@ -16,8 +16,9 @@ pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
|
||||
let device = nvml.device_by_index(0)?;
|
||||
|
||||
let (used, total) = get_gpu_vram_usage(&device)?;
|
||||
let gpu_name = detect_gpu_name();
|
||||
Ok(GpuInfo {
|
||||
name: device.name().ok(),
|
||||
name: Some(gpu_name),
|
||||
current_load: get_gpu_load(&device).ok(),
|
||||
current_temp: get_gpu_temp(&device).ok(),
|
||||
vram_total: Some(total as f64),
|
||||
@@ -39,3 +40,47 @@ pub fn get_gpu_vram_usage(device: &nvml_wrapper::Device) -> Result<(f64, f64), B
|
||||
let mem_info = device.memory_info().unwrap();
|
||||
Ok((mem_info.used as f64, mem_info.total as f64))
|
||||
}
|
||||
|
||||
fn detect_gpu_name() -> String {
|
||||
try_nvml_gpu_name()
|
||||
.or_else(fallback_gpu_name)
|
||||
.unwrap_or_else(|| "Unknown GPU".to_string())
|
||||
}
|
||||
|
||||
fn try_nvml_gpu_name() -> Option<String> {
|
||||
let nvml = Nvml::init().ok()?;
|
||||
let device = nvml.device_by_index(0).ok()?;
|
||||
device.name().ok().map(|s| s.to_string())
|
||||
}
|
||||
|
||||
fn fallback_gpu_name() -> Option<String> {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let output = std::process::Command::new("lshw")
|
||||
.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()),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let output = std::process::Command::new("wmic")
|
||||
.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()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user