enable GPU polling only when GPU is present

This commit is contained in:
jasonwitty 2025-08-22 09:27:05 -07:00
parent 384953d5d5
commit 3ac03c07ba
2 changed files with 40 additions and 14 deletions

View File

@ -153,20 +153,41 @@ pub async fn collect_fast_metrics(state: &AppState) -> Metrics {
.collect() .collect()
}; };
// GPUs: refresh only when cache is stale // GPUs: if we already determined none exist, short-circuit (no repeated probing)
let gpus = if cached_gpus().is_some() { let gpus = if gpu_enabled() {
cached_gpus() if state.gpu_checked.load(std::sync::atomic::Ordering::Acquire)
} else if gpu_enabled() { && !state.gpu_present.load(std::sync::atomic::Ordering::Relaxed)
let v = match collect_all_gpus() { {
Ok(v) if !v.is_empty() => Some(v), None
Ok(_) => None, } else if cached_gpus().is_some() {
Err(e) => { cached_gpus()
warn!("gpu collection failed: {e}"); } else {
None let v = match collect_all_gpus() {
Ok(v) if !v.is_empty() => Some(v),
Ok(_) => None,
Err(e) => {
warn!("gpu collection failed: {e}");
None
}
};
// First probe records presence; subsequent calls rely on cache flags.
if !state
.gpu_checked
.swap(true, std::sync::atomic::Ordering::AcqRel)
{
if v.is_some() {
state
.gpu_present
.store(true, std::sync::atomic::Ordering::Release);
} else {
state
.gpu_present
.store(false, std::sync::atomic::Ordering::Release);
}
} }
}; set_gpus(v.clone());
set_gpus(v.clone()); v
v }
} else { } else {
None None
}; };

View File

@ -2,7 +2,7 @@
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::AtomicUsize; use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::Arc; use std::sync::Arc;
use sysinfo::{Components, Disks, Networks, System}; use sysinfo::{Components, Disks, Networks, System};
use tokio::sync::Mutex; use tokio::sync::Mutex;
@ -34,6 +34,9 @@ pub struct AppState {
pub client_count: Arc<AtomicUsize>, pub client_count: Arc<AtomicUsize>,
pub auth_token: Option<String>, pub auth_token: Option<String>,
// GPU negative cache (probe once). gpu_checked=true after first attempt; gpu_present reflects result.
pub gpu_checked: Arc<AtomicBool>,
pub gpu_present: Arc<AtomicBool>,
} }
impl AppState { impl AppState {
@ -54,6 +57,8 @@ impl AppState {
auth_token: std::env::var("SOCKTOP_TOKEN") auth_token: std::env::var("SOCKTOP_TOKEN")
.ok() .ok()
.filter(|s| !s.is_empty()), .filter(|s| !s.is_empty()),
gpu_checked: Arc::new(AtomicBool::new(false)),
gpu_present: Arc::new(AtomicBool::new(false)),
} }
} }
} }