fix(powermetrics): reject unsupported Macs and report 0 W when no samples - #1345
davidberenstein1957 wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1345 +/- ##
==========================================
+ Coverage 91.70% 91.71% +0.01%
==========================================
Files 49 49
Lines 5157 5156 -1
==========================================
Hits 4729 4729
+ Misses 428 427 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
5247f57 to
f67379e
Compare
…ples Two failure modes in the powermetrics backend produced silently wrong totals. `_setup_cli` returned without setting `_cli` and without raising on an Intel Mac, so `ApplePowermetrics()` succeeded there and `ResourceTracker` could register an `AppleSiliconChip` on hardware that has none. It also crashed with `AttributeError` when `detect_cpu_model()` returned None. Both paths now raise `SystemError`, the argv uses the validated `self._cli`, and a stray empty argument is dropped. Averaging an empty sample list returned NaN (numpy) or raised (`statistics.fmean`), and the NaN poisoned every downstream total. Report 0 W instead. This also drops the numpy dependency from this module. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
f67379e to
c583b1d
Compare
Verdict: 🔧 Request changes (rebase needed)Both fixes are real:
Making Must fix:
Nits:
|
…e is_mac_arm Keep master's Popen timeout and early return in get_details, rewrite the clean-command test against Popen, warn once when no CPU samples are logged, use is_mac_arm for the Apple Silicon check and drop the now-dead Intel Mac powermetrics branch in resource_tracker. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Made the changes in d537043: merged master (kept the Popen timeout and early return, rewrote the command test against Popen), added a one-time warning for empty CPU samples, switched to |
Description
Two fixes to
codecarbon/core/powermetrics.py. First,get_details()now guards against an empty match list before averaging, so it reports0.0W instead of NaN whenpowermetricsproduced noCPU Power:/GPU Power:lines, and logs a warning pointing to the sudoers setup docs the first time there are no CPU samples; the previously-duplicated CPU and GPU branches are now one loop. Second,_setup_cli()now raisesSystemErrorunless the CPU is Apple Silicon (the sameis_mac_armcheck used byresource_tracker), so an Intel Mac without Power Gadget falls back to TDP / cpu_load instead of selecting powermetrics, since Intel powermetrics output has noCPU Power: X mWline. The now-unreachable Intel-Mac powermetrics branch inresource_tracker._try_platform_cpu_backendis removed, and the command now usesself._cliand drops a stray""argument. Master'sPopentimeout and early-return inget_detailsare kept.Related Issue
Fixes #1306
Motivation and Context
np.mean([])returnsnanand only emits aRuntimeWarning, so the surroundingexcept Exceptionnever caught it. The NaN flowed into the tracker accumulators and, since NaN is absorbing under addition, madeenergy_consumed,emissionsandemissions_rateNaN for the rest of the run, silently written toemissions.csvand POSTed to the API.How Has This Been Tested?
New tests in
tests/test_powermetrics.pycover empty logs (0 W, warning logged once), CPU-only logs, Intel / unknown CPU rejection, Apple Silicon setup, and the command built forPopen.test_resource_tracker.pynow asserts that an Intel Mac does not use powermetrics. Fulltest-packagepasses.Screenshots (if appropriate):
N/A
Types of changes
AI Usage Disclosure
Checklist: