Skip to content

Share the SMC key resolution between the two backends - #3551

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:smc-dedup
Jul 30, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:smc-dedup

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

Cleaning up after ourselves. The sensor work in #3531, #3535, #3536 and #3545 grew SmcUtil and SmcUtilFFM in 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:

block contents
SmcUtil 338-398 getGpuTemperatureKeys + getFanSpeedKeys
SmcUtil 413-459 discoverFanSpeedKeys tail + getCpuVoltageKeys
SmcUtil 470-487 smcGetFirstVoltage

Both 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 S3077 suppressions.

What changed

SmcKeyCache in oshi-common holds that resolution once. Each backend now supplies only the discovery:

public static List<String> getGpuTemperatureKeys() {
    return GPU_KEYS.get(SmcUtil::discoverGpuTemperatureKeys);
}

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 S3077 suppressions are gone with the fields.

SmcKeyIndex.firstPlausible, symmetric with the maxPlausible already there, absorbs the two first-plausible scans. smcGetFirstTemperature and smcGetFirstVoltage differed 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.

before after
SmcUtil 596 518
SmcUtilFFM 622 545

No change to the public surface of either.

A suppression cleanup that fell out

Adding a test to oshi.util.common.platform.mac failed at runtime: the package has no opens line in module-info.test. That is precisely why the two existing tests there were public and carried S5786 suppressions asserting JPMS required it — a claim I wrote in #3540 and had to correct once already in d89f6b6fb0.

Rather than add a third suppression, I added the missing opens line. With the package opened, public really is redundant, so SmcKeyIndexTest and SmcSensorValuesTest are 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: firstPlausible returns the first rather than the best (what distinguishes it from maxPlausible), skips a leading sentinel, handles nothing-usable and empty lists, and agrees with maxPlausible on a single key.

Full gate green plus a clean full-reactor install, 0 tests failed. Validated live on an M2 Max: NativeComparisonTest passes including sensors() and gpuTemperatureKeys(), so the two backends still agree on the discovered key lists, and the Mac-only MacSensorsPlausibilityTest/FFMTest pairs pass all 18 tests each.

One observation, unrelated and pre-existing: testAppleSiliconVoltageKeyReadsPlausibly skips on this M2 Max with "VP0C unavailable (Intel)". I confirmed it does the same on unmodified master, 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

  • Bug Fixes
    • Improved reliability when discovering and caching Mac SMC temperature and fan-speed sensors.
    • Added safer fallback behavior when sensor discovery is incomplete or unavailable.
    • Sensor readings now skip implausible values and use the first valid reading.
  • Tests
    • Expanded coverage for sensor discovery, cache/retry behavior, fallback handling, and plausible readings.
    • Updated Mac plausibility tests to better handle chip-specific voltage-key availability.

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.
@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 10ef6d81-241e-40c5-b494-063146e5af81

📥 Commits

Reviewing files that changed from the base of the PR and between 6ebca0c and 2b3808a.

📒 Files selected for processing (2)
  • oshi-core-ffm/src/test/java/oshi/ffm/MacSensorsPlausibilityFFMTest.java
  • oshi-core/src/test/java/oshi/util/platform/mac/MacSensorsPlausibilityTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (8)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
🔇 Additional comments (2)
oshi-core-ffm/src/test/java/oshi/ffm/MacSensorsPlausibilityFFMTest.java (1)

272-276: LGTM!

oshi-core/src/test/java/oshi/util/platform/mac/MacSensorsPlausibilityTest.java (1)

274-278: LGTM!


📝 Walkthrough

Walkthrough

The 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.

Changes

SMC key resolution

Layer / File(s) Summary
Shared cache and value-selection helpers
oshi-common/src/main/java/oshi/util/common/platform/mac/SmcKeyCache.java, oshi-common/src/main/java/oshi/util/common/platform/mac/SmcKeyIndex.java
Adds lazy key caching with configured-key, discovery, and fallback handling, plus ordered first-plausible value selection.
SMC utility integration
oshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.java, oshi-core-ffm/src/main/java/oshi/ffm/util/platform/mac/SmcUtilFFM.java
Uses shared caches for GPU and fan keys and delegates temperature and voltage selection to SmcKeyIndex.firstPlausible.
Resolution tests and test-module access
oshi-common/src/test/java/oshi/util/common/platform/mac/*, oshi-common/src/test/java/module-info.test, oshi-core/src/test/java/oshi/util/platform/mac/MacSensorsPlausibilityTest.java, oshi-core-ffm/src/test/java/oshi/ffm/MacSensorsPlausibilityFFMTest.java
Adds cache behavior coverage, changes test visibility to package-private, opens the package for testing, and updates VP0C test wording.

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
Loading

Possibly related PRs

  • oshi/oshi#3545: Wires the same SMC caching and plausibility helpers into related macOS sensor paths.
  • oshi/oshi#3540: Changes related SmcKeyIndex configuration parsing and key-discovery behavior.
  • oshi/oshi#3109: Introduces the FFM SMC reading path refactored here.

Poem

A rabbit cached keys in an SMC den,
Then found plausible readings again and again.
Temperatures hopped, voltages shone,
Discovery worked—or fallback was known.
“Neat shared helpers!” I twitched with delight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: shared SMC key resolution across the two backends.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.42857% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.11%. Comparing base (96c7f14) to head (2b3808a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ava/oshi/util/common/platform/mac/SmcKeyCache.java 92.59% 1 Missing and 1 partial ⚠️
...ava/oshi/util/common/platform/mac/SmcKeyIndex.java 87.50% 0 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
linux 36.47% <88.57%> (+0.07%) ⬆️
macos 37.37% <91.42%> (+0.02%) ⬆️
windows 44.39% <88.57%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

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.
@dbwiddis
dbwiddis merged commit c7bb35b into oshi:master Jul 30, 2026
16 checks passed
@dbwiddis
dbwiddis deleted the smc-dedup branch July 30, 2026 02:39
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.

1 participant