Skip to content

fix(core): establish cpu baseline when possible to improve measurement accuracy#34120

Merged
FrozenPandaz merged 1 commit intomasterfrom
nxc-3723
Jan 19, 2026
Merged

fix(core): establish cpu baseline when possible to improve measurement accuracy#34120
FrozenPandaz merged 1 commit intomasterfrom
nxc-3723

Conversation

@leosvelperez
Copy link
Member

@leosvelperez leosvelperez commented Jan 16, 2026

Current Behavior

New task processes show 0% CPU on their first measurement because no baseline exists. Accurate readings only appear on the second collection cycle (~1s later).

Expected Behavior

New task processes get accurate CPU readings on their first measurement. The collector establishes CPU baselines for newly registered processes ~250ms before collection, giving sysinfo enough time to calculate accurate CPU deltas.

Technical Details: Baselining & Collection Flow

The collection loop runs in 4 phases:

 T=0ms      T=750ms      T=1000ms    T=1750ms     T=2000ms  
   |           |             |           |            |
   v           v             v           v            v
Collect → Sleep(750ms) → Baseline → Sleep(250ms) → Collect → ...
  1. Collect: Refresh all processes and gather metrics
  2. Post-collection sleep: Wait until baseline time (interval - 250ms)
  3. Baseline: Bulk CPU refresh for newly registered PIDs (if any)
  4. Pre-collection sleep: Wait 250ms for accurate CPU delta calculation

@leosvelperez leosvelperez self-assigned this Jan 16, 2026
@leosvelperez leosvelperez requested review from a team as code owners January 16, 2026 09:02
@vercel
Copy link

vercel bot commented Jan 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
nx-dev Ready Ready Preview Jan 19, 2026 1:50pm

Request Review

@netlify
Copy link

netlify bot commented Jan 16, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 2b0cfa5
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/696e351ab254bb00087daff6
😎 Deploy Preview https://deploy-preview-34120--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Contributor

nx-cloud bot commented Jan 16, 2026

View your CI Pipeline Execution ↗ for commit 2b0cfa5

Command Status Duration Result
nx affected --targets=lint,test,test-kt,build,e... ✅ Succeeded 11m 14s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 2m 48s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 11s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗
nx-cloud record -- nx format:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-01-19 16:16:30 UTC

Copy link
Contributor

@nx-cloud nx-cloud bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

At least one additional CI pipeline execution has run since the conclusion below was written and it may no longer be applicable.

Nx Cloud is proposing a fix for your failed CI:

We removed the pre-loop baseline logic that was interfering with process initialization. The original code ran a baseline refresh immediately at startup (T=0ms), then performed the first collection only 250ms later, which disrupted normal task process startup and caused the Playwright webserver readiness check to timeout. By letting the first collection happen immediately (as before the PR) and moving baseline establishment to occur after the first collection, we maintain backward compatibility with existing process startup expectations while preserving the PR's intent of accurate CPU measurements through strategic baseline timing.

We could not verify this fix.

diff --git a/packages/nx/src/native/metrics/collector.rs b/packages/nx/src/native/metrics/collector.rs
index 01c5288b44..4d2671092e 100644
--- a/packages/nx/src/native/metrics/collector.rs
+++ b/packages/nx/src/native/metrics/collector.rs
@@ -145,13 +145,7 @@ impl CollectionRunner {
         let interval = Duration::from_millis(self.config.collection_interval_ms);
         // sysinfo's MINIMUM_CPU_UPDATE_INTERVAL + 50ms safety buffer
         let baseline_offset = sysinfo::MINIMUM_CPU_UPDATE_INTERVAL + Duration::from_millis(50);
-        let post_collection_sleep = interval - baseline_offset;
-
-        // First iteration: baseline if needed, then wait before first collection
-        if self.should_collect.load(Ordering::Acquire) && self.run_baseline_if_needed() {
-            // Sleep to allow CPU to be calculated correctly for the baselined processes
-            self.sleep_with_early_exit(baseline_offset);
-        }
+        let post_collection_sleep = interval.saturating_sub(baseline_offset);
 
         while self.should_collect.load(Ordering::Acquire) {
             // Collect current metrics and send to main collector thread
@@ -166,6 +160,7 @@ impl CollectionRunner {
                 break;
             }
 
+            // Establish baselines for newly registered processes if any
             self.run_baseline_if_needed();
             // Sleep until next collection (offset)
             self.sleep_with_early_exit(baseline_offset);

Apply fix via Nx Cloud  Reject fix via Nx Cloud


Or Apply changes locally with:

npx nx-cloud apply-locally rcTg-vkDQ

Apply fix locally with your editor ↗   View interactive diff ↗


🎓 Learn more about Self-Healing CI on nx.dev

@FrozenPandaz FrozenPandaz merged commit 6bb82c0 into master Jan 19, 2026
22 of 23 checks passed
@FrozenPandaz FrozenPandaz deleted the nxc-3723 branch January 19, 2026 16:53
@github-actions
Copy link
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants