perf(server): fix N+1 PowerShell process query and reduce sampling frequency - #4829
perf(server): fix N+1 PowerShell process query and reduce sampling frequency#4829UtkarshUsername wants to merge 5 commits into
Conversation
…agnostics The readWindowsProcessRows function spawned powershell.exe with an N+1 WMI query pattern: for each process (via Get-CimInstance Win32_Process), it queried Win32_PerfFormattedData_PerfProc_Process individually using a -IDProcess filter. On machines with 200+ processes, this created 1+N CIM queries per sampling cycle. The old command timed out after 60s on a machine with ~205 processes. Fix: query all performance counter data once into a hashtable (IDProcess -> perf object), then join it with the process list in PowerShell memory. This reduces 1+N queries to exactly 2 queries regardless of process count. Measured improvement (~205 processes): - Old: >60s (timed out) - New: ~2.7s average per query - Speedup: ~22x+ per sampling cycle
The ProcessResourceMonitor was sampling process resources every 5 seconds. Combined with the PowerShell CIM query overhead, this caused excessive CPU usage on Windows. Increasing to 15s reduces sampling frequency by 3x while still providing adequate granularity for the resource history visualization (which aggregates into multi-second buckets). Test: update cpuSecondsApprox expectation from 2 to 6 to match the new interval (the calculation is SAMPLE_INTERVAL_MS / 1000 * cpuPercent/100, so tripling the interval triples the per-sample approximation).
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Approved 2db1250 Performance optimization fixing N+1 PowerShell process query and reducing diagnostic sampling frequency from 5s to 15s. Self-contained changes to monitoring code with corresponding test updates. You can customize Macroscope's approvability policy. Learn more. |
8713172 to
a8e541a
Compare
What Changed
Eliminated two sources of excessive CPU usage in the Windows process resource monitor:
ProcessDiagnostics.ts): The Windows process reader was queryingWin32_PerfFormattedData_PerfProc_Processindividually for each process via an IDProcess filter inside a ForEach-Object loop. Changed to query all perf counter data once into a hashtable, then join with the process list. Reduced from 1+N CIM queries to exactly 2.Why
On Windows machines with 200+ processes, ProcessResourceMonitor was spawning powershell.exe every 5 seconds with an N+1 WMI query pattern. Each cycle issued 1+N CIM queries (e.g., 206 queries for 205 processes). The old command consistently timed out after 60 seconds on a typical development machine, pegging CPU indefinitely.
Checklist
Note
Low Risk
Diagnostics-only changes on Windows sampling path; behavior is coarser metrics (15s) but lower server CPU load, with no auth or data-path impact.
Overview
Reduces Windows process diagnostics overhead by fixing how PowerShell loads CPU perf data and by sampling less often.
Windows process query:
readWindowsProcessRowsno longer runs a filteredWin32_PerfFormattedData_PerfProc_ProcessCIM call inside the per-process loop. It loads all perf rows once into a hashtable keyed byIDProcess, then walksWin32_Processand joins in memory—two CIM queries per cycle instead of 1+N.Resource monitor:
SAMPLE_INTERVAL_MSmoves from 5s to 15s, so the background sampler runs three times less often. Approximate CPU-second rollups use the new interval; tests now expect 6 instead of 2 for the same two-sample scenario.Reviewed by Cursor Bugbot for commit 9d9363e. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Fix N+1 PowerShell process query and increase sampling interval to 15s
readWindowsProcessRowsin ProcessDiagnostics.ts to preload allWin32_PerfFormattedData_PerfProc_Processentries into a hashtable keyed byIDProcess, eliminating a per-process WMI query.SAMPLE_INTERVAL_MSin ProcessResourceMonitor.ts from 5,000 ms to 15,000 ms to reduce polling overhead.Macroscope summarized 9d9363e.