diff --git a/WatcherAgent/src/hardware/gpu.rs b/WatcherAgent/src/hardware/gpu.rs index f11db0b..5fbda4e 100644 --- a/WatcherAgent/src/hardware/gpu.rs +++ b/WatcherAgent/src/hardware/gpu.rs @@ -12,20 +12,21 @@ pub struct GpuInfo { } pub async fn get_gpu_info() -> Result> { - let nvml = Nvml::init()?; - let device = nvml.device_by_index(0)?; + //let nvml = Nvml::init()?; + //let device = nvml.device_by_index(0)?; - let (used, total) = get_gpu_vram_usage(&device)?; + //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: get_gpu_load(&device).ok(), - current_temp: get_gpu_temp(&device).ok(), - vram_total: Some(total), - vram_used: Some(used), + current_load: Some(gpu_load), + current_temp: Some(gpu_temp), + vram_total: Some(vram_total), + vram_used: Some(vram_used), }) } - +/* pub fn get_gpu_load(device: &nvml_wrapper::Device) -> Result> { Ok(device.utilization_rates().unwrap().gpu as f64) } @@ -39,6 +40,33 @@ pub fn get_gpu_temp(device: &nvml_wrapper::Device) -> Result pub fn get_gpu_vram_usage(device: &nvml_wrapper::Device) -> Result<(f64, f64), Box> { 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> { + 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) + } else { + return Err(anyhow::anyhow!("Failed to initialize NVML").into()); + }; + + Ok(( + gpu_temp as f64, + gpu_load as f64, + vram_used as f64, + vram_total as f64, + )) } fn detect_gpu_name() -> String {