perf(agent,non-linux): enable CPU core normalization by default; tighter scaling threshold

This commit is contained in:
jasonwitty 2025-08-25 23:50:26 -07:00
parent e2dc5e8ac9
commit c9ebea92f5

View File

@ -17,15 +17,15 @@ use sysinfo::{ProcessRefreshKind, ProcessesToUpdate};
use tracing::warn; use tracing::warn;
// Optional normalization: divide per-process cpu_usage by logical core count so a fully // 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 // saturated multi-core process reports near 100% instead of N*100%. Enabled by default on
// SOCKTOP_AGENT_NORMALIZE_CPU=1. Default keeps raw sysinfo semantics. // non-Linux (macOS/Windows) to counter per-core summing; disable with SOCKTOP_AGENT_NORMALIZE_CPU=0.
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
fn normalize_cpu_enabled() -> bool { fn normalize_cpu_enabled() -> bool {
static ON: OnceCell<bool> = OnceCell::new(); static ON: OnceCell<bool> = OnceCell::new();
*ON.get_or_init(|| { *ON.get_or_init(|| {
std::env::var("SOCKTOP_AGENT_NORMALIZE_CPU") std::env::var("SOCKTOP_AGENT_NORMALIZE_CPU")
.map(|v| v != "0") .map(|v| v != "0")
.unwrap_or(false) .unwrap_or(true)
}) })
} }
// Runtime toggles (read once) // Runtime toggles (read once)
@ -476,8 +476,8 @@ pub async fn collect_processes_all(state: &AppState) -> ProcessesPayload {
} }
}) })
.collect(); .collect();
// Automatic scaling (enabled by default): if sum of per-process CPU exceeds global // Automatic scaling (enabled by default): if sum of per-process CPU exceeds global
// CPU by >25%, scale all process CPU values proportionally so the sum matches global. // CPU by >5%, scale all process CPU values proportionally so the sum matches global.
if std::env::var("SOCKTOP_AGENT_SCALE_PROC_CPU") if std::env::var("SOCKTOP_AGENT_SCALE_PROC_CPU")
.map(|v| v != "0") .map(|v| v != "0")
.unwrap_or(true) .unwrap_or(true)
@ -486,7 +486,8 @@ pub async fn collect_processes_all(state: &AppState) -> ProcessesPayload {
let global = sys.global_cpu_usage(); let global = sys.global_cpu_usage();
if sum > 0.0 && global > 0.0 { if sum > 0.0 && global > 0.0 {
let scale = global / sum; let scale = global / sum;
if scale < 0.75 { // only scale if we're at least 25% over if scale < 0.95 {
// only scale if we're at least 5% over
for p in &mut list { for p in &mut list {
p.cpu_usage = (p.cpu_usage * scale).clamp(0.0, 100.0); p.cpu_usage = (p.cpu_usage * scale).clamp(0.0, 100.0);
} }