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,10 +153,15 @@ pub async fn collect_fast_metrics(state: &AppState) -> Metrics {
.collect()
};
// GPUs: refresh only when cache is stale
let gpus = if cached_gpus().is_some() {
// GPUs: if we already determined none exist, short-circuit (no repeated probing)
let gpus = if gpu_enabled() {
if state.gpu_checked.load(std::sync::atomic::Ordering::Acquire)
&& !state.gpu_present.load(std::sync::atomic::Ordering::Relaxed)
{
None
} else if cached_gpus().is_some() {
cached_gpus()
} else if gpu_enabled() {
} else {
let v = match collect_all_gpus() {
Ok(v) if !v.is_empty() => Some(v),
Ok(_) => None,
@ -165,8 +170,24 @@ pub async fn collect_fast_metrics(state: &AppState) -> Metrics {
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());
v
}
} else {
None
};

View File

@ -2,7 +2,7 @@
#[cfg(target_os = "linux")]
use std::collections::HashMap;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::Arc;
use sysinfo::{Components, Disks, Networks, System};
use tokio::sync::Mutex;
@ -34,6 +34,9 @@ pub struct AppState {
pub client_count: Arc<AtomicUsize>,
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 {
@ -54,6 +57,8 @@ impl AppState {
auth_token: std::env::var("SOCKTOP_TOKEN")
.ok()
.filter(|s| !s.is_empty()),
gpu_checked: Arc::new(AtomicBool::new(false)),
gpu_present: Arc::new(AtomicBool::new(false)),
}
}
}