modulized everthing

This commit is contained in:
2025-08-08 18:59:57 +02:00
parent f0b89a9c32
commit abfa0b6fc0
11 changed files with 690 additions and 570 deletions

View File

@@ -0,0 +1,41 @@
use anyhow::Result;
use nvml_wrapper::Nvml;
use std::error::Error;
#[derive(Debug)]
pub struct GpuInfo {
pub name: Option<String>,
pub current_load: Option<f64>,
pub current_temp: Option<f64>,
pub vram_total: Option<f64>,
pub vram_used: Option<f64>,
}
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)?;
Ok(GpuInfo {
name: device.name().ok(),
current_load: get_gpu_load(&device).ok(),
current_temp: get_gpu_temp(&device).ok(),
vram_total: Some(total as f64),
vram_used: Some(used as f64),
})
}
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))
}