fix: measure process CPU concurrently with system CPU

Previously system CPU was measured first (1s window), then processes
were sampled 1+ second later. Short-lived spikes would appear in system
CPU but not in the process list because the offending process had already
finished. Now both run in parallel so their windows overlap — a process
that caused a spike during the CPU measurement window will be captured.
This commit is contained in:
Joel Male
2026-02-25 21:52:00 +10:00
parent b9b31414b0
commit ef6fa1a07f
+32 -10
View File
@@ -15,6 +15,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"time" "time"
@@ -908,12 +909,34 @@ func collectMetrics(config *Config) (*Metrics, error) {
} }
} }
// CPU — single sample with percpu=true, then average for the total. // CPU and process collection run concurrently so their measurement
// Calling cpu.Percent twice with separate 1s windows can produce // windows overlap. Previously, processes were sampled 1+ second after
// wildly inconsistent totals vs per-core values. // the CPU window ended, causing spikes to appear in system CPU but not
cpuPerCore, err := cpu.Percent(time.Second, true) // in the process list (the offending process had already finished).
if err != nil { var (
return nil, fmt.Errorf("failed to get CPU percent: %w", err) cpuPerCore []float64
cpuErr error
processes []ProcessInfo
processErr error
)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
cpuPerCore, cpuErr = cpu.Percent(time.Second, true)
}()
go func() {
defer wg.Done()
processes, processErr = collectTopProcesses()
}()
wg.Wait()
if cpuErr != nil {
return nil, fmt.Errorf("failed to get CPU percent: %w", cpuErr)
} }
metrics.CPU.PerCore = cpuPerCore metrics.CPU.PerCore = cpuPerCore
if len(cpuPerCore) > 0 { if len(cpuPerCore) > 0 {
@@ -1052,10 +1075,9 @@ func collectMetrics(config *Config) (*Metrics, error) {
prevNet = current prevNet = current
} }
// Top processes // Top processes (collected concurrently with CPU above)
processes, err := collectTopProcesses() if processErr != nil {
if err != nil { log.Printf("Warning: Failed to collect process info: %v", processErr)
log.Printf("Warning: Failed to collect process info: %v", err)
metrics.Processes = []ProcessInfo{} metrics.Processes = []ProcessInfo{}
} else { } else {
metrics.Processes = processes metrics.Processes = processes