perf(agent): windows/mac cpu collection tweak; add optional normalization; silence linux dead_code

This commit is contained in:
jasonwitty 2025-08-25 22:59:07 -07:00
parent 00d5777d05
commit 66270c16b7

View File

@ -16,6 +16,18 @@ use std::time::{Duration, Instant};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate}; use sysinfo::{ProcessRefreshKind, ProcessesToUpdate};
use tracing::warn; use tracing::warn;
// Optional normalization: divide per-process cpu_usage by logical core count so a fully
// saturated multi-core process reports near 100% instead of N*100%. Enable via
// SOCKTOP_AGENT_NORMALIZE_CPU=1. Default keeps raw sysinfo semantics.
#[cfg(not(target_os = "linux"))]
fn normalize_cpu_enabled() -> bool {
static ON: OnceCell<bool> = OnceCell::new();
*ON.get_or_init(|| {
std::env::var("SOCKTOP_AGENT_NORMALIZE_CPU")
.map(|v| v != "0")
.unwrap_or(false)
})
}
// Runtime toggles (read once) // Runtime toggles (read once)
fn gpu_enabled() -> bool { fn gpu_enabled() -> bool {
static ON: OnceCell<bool> = OnceCell::new(); static ON: OnceCell<bool> = OnceCell::new();
@ -430,7 +442,8 @@ pub async fn collect_processes_all(state: &AppState) -> ProcessesPayload {
// Second refresh: only CPU counters (lighter than full everything) to reduce overhead. // Second refresh: only CPU counters (lighter than full everything) to reduce overhead.
let (total_count, procs) = { let (total_count, procs) = {
let mut sys = state.sys.lock().await; let mut sys = state.sys.lock().await;
let cpu_only = ProcessRefreshKind::new().with_cpu().without_tasks(); // Build a lightweight refresh kind: only CPU times.
let cpu_only = ProcessRefreshKind::nothing().with_cpu();
sys.refresh_processes_specifics(ProcessesToUpdate::All, false, cpu_only); sys.refresh_processes_specifics(ProcessesToUpdate::All, false, cpu_only);
let total_count = sys.processes().len(); let total_count = sys.processes().len();
let norm = normalize_cpu_enabled(); let norm = normalize_cpu_enabled();