Repository navigation
Share the SMC key resolution between the two backends - #3551
Conversation
The sensor work in #3531, #3535, #3536 and #3545 grew SmcUtil and SmcUtilFFM in parallel, and Sonar reported 253 duplicated lines between them. All of it was in key-list management rather than the native calls. Both backends had the same three-step resolution twice over, once for GPU temperature and once for fan speeds: a configuration property naming the keys outright, then discovery from the key index, then a fallback that must not be cached so a later call retries. That is four copies of the same double-checked-locking skeleton, four volatile fields and four S3077 suppressions. It is now one SmcKeyCache in oshi-common, so each backend supplies only the discovery, and three of those suppressions are gone. smcGetFirstTemperature and smcGetFirstVoltage were both first-plausible scans over a key list, differing only in how a reading is obtained: the voltage path needs a second read for the key's data type in order to scale it. Moving the scan to SmcKeyIndex.firstPlausible, symmetric with the maxPlausible already there, leaves each backend with just the read. SmcUtil drops from 596 to 518 lines and SmcUtilFFM from 622 to 545, with no change to the public surface of either. Adding a test to oshi.util.common.platform.mac needed an opens line in module-info.test, which the package had never had. That is why the two existing tests there were public and carried S5786 suppressions claiming JPMS required it. With the package opened that is no longer true, so both are now package-private and both suppressions are removed.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📜 Recent review details⏰ Context from checks skipped due to timeout. (8)
🔇 Additional comments (2)
📝 WalkthroughWalkthroughThe PR adds shared SMC key caching and first-plausible reading helpers, integrates them into both macOS SMC utility implementations, and updates related tests and test-module access configuration. ChangesSMC key resolution
Estimated code review effort: 3 (Moderate) | ~30 minutes Sequence Diagram(s)sequenceDiagram
participant SmcUtil
participant SmcKeyCache
participant GlobalConfig
participant SmcKeyIndex
SmcUtil->>SmcKeyCache: get(discovery)
SmcKeyCache->>GlobalConfig: read configured keys
GlobalConfig-->>SmcKeyCache: configured key list
SmcKeyCache-->>SmcUtil: cached, discovered, or fallback keys
SmcUtil->>SmcKeyIndex: firstPlausible(keys, reader, predicate)
SmcKeyIndex-->>SmcUtil: first plausible value or 0d
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3551 +/- ##
============================================
- Coverage 79.22% 79.11% -0.11%
+ Complexity 3759 89 -3670
============================================
Files 705 707 +2
Lines 24216 24267 +51
Branches 4040 4047 +7
============================================
+ Hits 19186 19200 +14
- Misses 2843 2874 +31
- Partials 2187 2193 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The assumption skipped with "VP0C unavailable (Intel)", inferring the chip from the key's absence. That inference is wrong: VP0C reads 0.75 V on an M3 Pro but is absent on an M2 Max, so the test skips on Apple Silicon while reporting Intel, which is what hid that getCpuVoltage returns 0 on that machine. The skip itself is right, since there is nothing to assert when the key does not exist. Only the stated reason changes, to name the absent key rather than guess the hardware. A sweep of all 53 V keys on the M2 Max found no identified core voltage, so 0 may well be the correct answer there; that is worth its own investigation rather than a key added on one machine's evidence.
Cleaning up after ourselves. The sensor work in #3531, #3535, #3536 and #3545 grew
SmcUtilandSmcUtilFFMin parallel, and they had become the 5th-largest duplication cluster in the project at 253 lines — absent from the audit roadmap because it did not exist when the roadmap was written.Where the duplication actually was
Not in the native calls. Sonar pointed at three blocks, all in key-list management:
SmcUtil 338-398getGpuTemperatureKeys+getFanSpeedKeysSmcUtil 413-459discoverFanSpeedKeystail +getCpuVoltageKeysSmcUtil 470-487smcGetFirstVoltageBoth backends had the same three-step resolution twice over — once for GPU temperature, once for fan speeds: a config property naming the keys outright, then discovery from the key index, then a fallback that must not be cached so a later call retries. Four copies of the same double-checked-locking skeleton, four volatile fields, four
S3077suppressions.What changed
SmcKeyCachein oshi-common holds that resolution once. Each backend now supplies only the discovery:The null-versus-empty distinction is preserved and documented on the type: a completed discovery is cached including one that legitimately found nothing, while an incomplete one is not. Three of the four
S3077suppressions are gone with the fields.SmcKeyIndex.firstPlausible, symmetric with themaxPlausiblealready there, absorbs the two first-plausible scans.smcGetFirstTemperatureandsmcGetFirstVoltagediffered only in how a reading is obtained — the voltage path needs a second read for the key's data type in order to scale it — so passing the reader as a lambda leaves each backend with just the read.SmcUtilSmcUtilFFMNo change to the public surface of either.
A suppression cleanup that fell out
Adding a test to
oshi.util.common.platform.macfailed at runtime: the package has noopensline inmodule-info.test. That is precisely why the two existing tests there werepublicand carriedS5786suppressions asserting JPMS required it — a claim I wrote in #3540 and had to correct once already ind89f6b6fb0.Rather than add a third suppression, I added the missing
opensline. With the package opened,publicreally is redundant, soSmcKeyIndexTestandSmcSensorValuesTestare now package-private and both suppressions are removed. Root cause instead of a workaround.Tests
SmcKeyCacheTest(new, hardware-free): a completed discovery is cached and runs once; an incomplete one falls back and is retried, not cached; an empty completed discovery is cached because it is a real answer; configured keys bypass discovery entirely; all-malformed config falls through to discovery rather than being read as an empty key list; the fallback is handed back by identity, which is how callers tell "fell back" from "discovered". Uses test-only property names so it cannot race the parallel suite.SmcKeyIndexTest:firstPlausiblereturns the first rather than the best (what distinguishes it frommaxPlausible), skips a leading sentinel, handles nothing-usable and empty lists, and agrees withmaxPlausibleon a single key.Full gate green plus a clean full-reactor
install, 0 tests failed. Validated live on an M2 Max:NativeComparisonTestpasses includingsensors()andgpuTemperatureKeys(), so the two backends still agree on the discovered key lists, and the Mac-onlyMacSensorsPlausibilityTest/FFMTestpairs pass all 18 tests each.One observation, unrelated and pre-existing:
testAppleSiliconVoltageKeyReadsPlausiblyskips on this M2 Max with "VP0C unavailable (Intel)". I confirmed it does the same on unmodifiedmaster, so it is not from this change — but the message is misleading, since VP0C is the Apple Silicon key and it is simply returning 0 here.No CHANGELOG — no user-observable behavior change.
Summary by CodeRabbit