Skip to content

fix(powermetrics): report 0 W when powermetrics returns no samples - #1345

Open
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/powermetrics-nan-totals
Open

fix(powermetrics): report 0 W when powermetrics returns no samples#1345
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/powermetrics-nan-totals

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What

ApplePowermetrics.get_details() now guards against an empty match list before averaging: if powermetrics produced no CPU Power: / GPU Power: lines it logs a warning and reports 0.0 W instead of nan. The CPU and GPU branches were identical, so they are now one loop.

Why

np.mean([]) returns nan and only emits a RuntimeWarning, so the surrounding except Exception never fired and nothing was logged. The NaN flowed through AppleSiliconChip._get_power into the tracker accumulators, and since NaN is absorbing under addition, one bad sample made energy_consumed, emissions and emissions_rate NaN for the remainder of the run — written to emissions.csv and POSTed to the API as bare NaN.

This is reachable in the field whenever the powermetrics sudoers rule is missing (empty log), and on machines where the cpu_power sampler emits no GPU Power: lines, where it happened on every single call.

Reporting 0 W understates rather than invalidates, and the warning makes the condition visible.

Verification

Two new cases in tests/test_powermetrics.py (test_get_details_without_samples, test_get_details_without_gpu_samples) both fail on master and pass with this change. uv run pytest tests/test_powermetrics.py -q -> 18 passed. The existing test_get_details is unchanged, confirming the non-empty path still behaves identically.

Note: uv run task lint reports pre-existing violations across the repo and uv run task format reformats ~120 unrelated files, so neither was applied wholesale; black --check is clean on the two touched files.

Closes #1306

🤖 Generated with Claude Code

np.mean([]) returns NaN without raising, so an empty powermetrics log
(missing sudoers rule, or a sampler that emits no GPU Power lines) fed
NaN into every tracker accumulator, making energy, emissions and
emissions_rate NaN for the rest of the run.

Closes #1306

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.43%. Comparing base (065d0e6) to head (dd71abc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1345      +/-   ##
==========================================
+ Coverage   91.39%   91.43%   +0.04%     
==========================================
  Files          49       49              
  Lines        5056     5058       +2     
==========================================
+ Hits         4621     4625       +4     
+ Misses        435      433       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty powermetrics output yields NaN power, poisoning all downstream totals on Apple Silicon

1 participant