added commentation

This commit is contained in:
2025-10-01 12:07:53 +02:00
parent d994be757e
commit 8c49a63a50
13 changed files with 570 additions and 56 deletions

View File

@@ -2,6 +2,29 @@ use anyhow::Result;
use nvml_wrapper::Nvml;
use std::error::Error;
//! # GPU Hardware Module
//!
//! This module provides GPU information collection for WatcherAgent, including load, temperature, and VRAM statistics.
//!
//! ## Responsibilities
//! - **GPU Detection:** Identifies GPU model and capabilities.
//! - **Metric Collection:** Queries GPU load, temperature, and VRAM usage using NVML (NVIDIA only).
//! - **Error Handling:** Graceful fallback if GPU or NVML is unavailable.
//!
//! ## Units
//! - `current_load`: GPU usage as a percentage (**0.0100.0**)
//! - `current_temp`: GPU temperature in **degrees Celsius (°C)**
//! - `vram_total`: Total VRAM in **megabytes (MB)**
//! - `vram_used`: Used VRAM in **megabytes (MB)**
//!
/// GPU statistics for the host system.
///
/// # Fields
/// - `name`: GPU model name (string)
/// - `current_load`: GPU usage as a percentage (**0.0100.0**)
/// - `current_temp`: GPU temperature in **degrees Celsius (°C)**
/// - `vram_total`: Total VRAM in **megabytes (MB)**
/// - `vram_used`: Used VRAM in **megabytes (MB)**
#[derive(Debug)]
pub struct GpuInfo {
pub name: Option<String>,
@@ -11,6 +34,12 @@ pub struct GpuInfo {
pub vram_used: Option<f64>,
}
/// Collects GPU information (load, temperature, VRAM) using NVML.
///
/// This function attempts to query the first NVIDIA GPU using NVML. If unavailable, it returns a fallback with only the detected GPU name.
///
/// # Returns
/// * `Result<GpuInfo, Box<dyn Error + Send + Sync>>` - GPU statistics or fallback if unavailable.
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
match get_gpu_metrics() {
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
@@ -37,6 +66,10 @@ pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
}
}
/// Queries NVML for GPU metrics: temperature, load, VRAM used/total.
///
/// # Returns
/// * `Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>>` - Tuple of (temperature °C, load %, VRAM used MB, VRAM total MB).
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
let nvml = Nvml::init();
if let Ok(nvml) = nvml {