new feature: gpu support
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// gpu.rs
|
||||
use gfxinfo::active_gpu;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct GpuMetrics {
|
||||
pub name: String,
|
||||
pub utilization_gpu_pct: u32, // 0..100
|
||||
pub mem_used_bytes: u64,
|
||||
pub mem_total_bytes: u64,
|
||||
// pub vendor: String,
|
||||
}
|
||||
|
||||
pub fn collect_all_gpus() -> Result<Vec<GpuMetrics>, Box<dyn std::error::Error>> {
|
||||
let gpu = active_gpu()?; // Use ? to unwrap Result
|
||||
let info = gpu.info();
|
||||
|
||||
let metrics = GpuMetrics {
|
||||
name: gpu.model().to_string(),
|
||||
utilization_gpu_pct: info.load_pct() as u32,
|
||||
mem_used_bytes: info.used_vram(),
|
||||
mem_total_bytes: info.total_vram(),
|
||||
// vendor: gpu.vendor().to_string(),
|
||||
};
|
||||
|
||||
Ok(vec![metrics])
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod sampler;
|
||||
mod state;
|
||||
mod types;
|
||||
mod ws;
|
||||
mod gpu;
|
||||
|
||||
use axum::{routing::get, Router};
|
||||
use std::{
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
//! Metrics collection using sysinfo. Keeps sysinfo handles in AppState to
|
||||
//! avoid repeated allocations and allow efficient refreshes.
|
||||
|
||||
|
||||
use crate::gpu::collect_all_gpus;
|
||||
|
||||
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::types::{DiskInfo, Metrics, NetworkInfo, ProcessInfo};
|
||||
|
||||
|
||||
use sysinfo::{Components, System};
|
||||
|
||||
|
||||
pub async fn collect_metrics(state: &AppState) -> Metrics {
|
||||
// System (CPU/mem/proc)
|
||||
let mut sys = state.sys.lock().await;
|
||||
@@ -99,6 +107,11 @@ pub async fn collect_metrics(state: &AppState) -> Metrics {
|
||||
});
|
||||
}
|
||||
|
||||
let gpus = match collect_all_gpus() {
|
||||
Ok(v) if !v.is_empty() => Some(v),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
Metrics {
|
||||
cpu_total: sys.global_cpu_usage(),
|
||||
cpu_per_core: sys.cpus().iter().map(|c| c.cpu_usage()).collect(),
|
||||
@@ -112,6 +125,7 @@ pub async fn collect_metrics(state: &AppState) -> Metrics {
|
||||
disks,
|
||||
networks,
|
||||
top_processes: procs,
|
||||
gpus,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Data types sent to the client over WebSocket.
|
||||
//! Keep this module minimal and stable — it defines the wire format.
|
||||
|
||||
use crate::gpu::GpuMetrics;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
@@ -26,7 +27,7 @@ pub struct NetworkInfo {
|
||||
pub transmitted: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
#[derive(Serialize)]
|
||||
pub struct Metrics {
|
||||
pub cpu_total: f32,
|
||||
pub cpu_per_core: Vec<f32>,
|
||||
@@ -40,4 +41,5 @@ pub struct Metrics {
|
||||
pub disks: Vec<DiskInfo>,
|
||||
pub networks: Vec<NetworkInfo>,
|
||||
pub top_processes: Vec<ProcessInfo>,
|
||||
pub gpus: Option<Vec<GpuMetrics>>, // new
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user