Repository navigation
Add Apple Silicon SMC sensor support - #3091
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
🔇 Additional comments (9)
📝 WalkthroughWalkthroughAdds Apple Silicon SMC sensor support: new Apple Silicon SMC key constants and a multi-key float lookup in SmcUtil, MacSensors updated to prefer Apple Silicon keys with fallbacks and improved null/resource handling, plus a SmcDump demo and a CHANGELOG entry. Changes
Sequence Diagram(s)sequenceDiagram
participant MacSensors
participant SmcUtil
participant SMC as "IOKit / SMC"
MacSensors->>SmcUtil: smcOpen()
SmcUtil->>SMC: open connection
SmcUtil-->>MacSensors: IOConnect (or null)
MacSensors->>SmcUtil: smcGetFirstFloat(conn, AS_keys..., Intel_key)
SmcUtil->>SMC: smcReadKey / smcGetFloat (iterate keys)
SMC-->>SmcUtil: float value (or 0)
SmcUtil-->>MacSensors: first positive float (or 0)
MacSensors->>SmcUtil: smcClose(conn)
SmcUtil->>SMC: close connection
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3091 +/- ##
==========================================
+ Coverage 69.38% 69.98% +0.59%
==========================================
Files 35 35
Lines 1839 1839
Branches 297 297
==========================================
+ Hits 1276 1287 +11
+ Misses 467 453 -14
- Partials 96 99 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
oshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.java (1)
59-62: Avoid exposing mutable key arrays as public constants.
public static final String[]is still caller-mutable, so external code can change the fallback order or corrupt these defaults for the entire process. Prefer private arrays plus cloning accessors, or an immutable collection type.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@oshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.java` around lines 59 - 62, The public mutable arrays SMC_KEYS_CPU_TEMP_AS and SMC_KEYS_GPU_TEMP_AS (and the exposed SMC_KEY_CPU_VOLTAGE_AS if you want consistency) must be made non-mutable: change them to private (e.g., private static final String[] SMC_KEYS_CPU_TEMP_AS and SMC_KEYS_GPU_TEMP_AS) and add public accessor methods in SmcUtil like getSmcKeysCpuTempAs() / getSmcKeysGpuTempAs() that return a defensive copy (clone) or return an unmodifiable List constructed from List.of(...) to prevent callers from mutating the originals; keep the single scalar SMC_KEY_CPU_VOLTAGE_AS as-is or provide a getter for consistency.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacSensors.java`:
- Around line 48-59: queryFanSpeeds calls SmcUtil.smcOpen() which may return
null but then immediately passes conn into
SmcUtil.smcGetLong/smcGetFloat/smcClose causing NPEs; update queryFanSpeeds to
check if conn is null right after SmcUtil.smcOpen() and if so return a
zero-length int[] (or an array of zeros sized by this.numFans if you prefer),
otherwise proceed to call smcGetLong/smcGetFloat and only call
SmcUtil.smcClose(conn) when conn is non-null; reference the method
queryFanSpeeds and the SmcUtil methods smcOpen, smcGetLong, smcGetFloat, and
smcClose when making the change.
---
Nitpick comments:
In `@oshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.java`:
- Around line 59-62: The public mutable arrays SMC_KEYS_CPU_TEMP_AS and
SMC_KEYS_GPU_TEMP_AS (and the exposed SMC_KEY_CPU_VOLTAGE_AS if you want
consistency) must be made non-mutable: change them to private (e.g., private
static final String[] SMC_KEYS_CPU_TEMP_AS and SMC_KEYS_GPU_TEMP_AS) and add
public accessor methods in SmcUtil like getSmcKeysCpuTempAs() /
getSmcKeysGpuTempAs() that return a defensive copy (clone) or return an
unmodifiable List constructed from List.of(...) to prevent callers from mutating
the originals; keep the single scalar SMC_KEY_CPU_VOLTAGE_AS as-is or provide a
getter for consistency.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5e72f0f5-478c-47fa-8d66-d0b16520b934
📒 Files selected for processing (4)
CHANGELOG.mdoshi-core/src/main/java/oshi/hardware/platform/mac/MacSensors.javaoshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.javaoshi-demo/src/main/java/oshi/demo/SmcDump.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: Test JDK 25, ubuntu-latest
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 25, macos-latest
- GitHub Check: Analyze (java)
🔇 Additional comments (3)
oshi-core/src/main/java/oshi/util/platform/mac/SmcUtil.java (1)
135-149: Nice fallback helper extraction.Centralizing the “first positive key wins” logic here keeps the Apple Silicon/Intel probe order in one place and avoids duplicating native-read branching in callers.
oshi-core/src/main/java/oshi/hardware/platform/mac/MacSensors.java (1)
31-44: The temperature and voltage paths are much safer now.The early null return plus
finally-based close handles both open failures and fallback branches without leaking the native connection.Also applies to: 63-78
oshi-demo/src/main/java/oshi/demo/SmcDump.java (1)
33-63: Useful native-debugging demo.The null-guarded open,
finallyclose, and try-with-resources around the native structs make this safe enough for a diagnostic utility.Also applies to: 66-85
Adds Apple Silicon (M-series) SMC key support for CPU temperature and voltage
sensors in
SmcUtilandMacSensors, with probe-first fallback to Intel keysfor backward compatibility. Also fixes a null-pointer risk in
MacSensorswhensmcOpen()fails.Phase 1 of #3090 (comment)
Changes
SmcUtil.javaSMC_KEYS_CPU_TEMP_AS— ordered array of Apple Silicon CPU dietemperature keys (
Tp09,Tp0T,Tp01,Tp05,Tp0D), derived from liveSMC enumeration on Apple Silicon hardware
SMC_KEYS_GPU_TEMP_AS— ordered array of Apple Silicon GPU temperaturekeys (
Tg05,Tg0D,Tg0f,Tg0j) for future useSMC_KEY_CPU_VOLTAGE_AS— Apple Silicon CPU core voltage key (VP0C),type
flt, already in volts (no scaling required)smcGetFirstFloat(IOConnect conn, String... keys)— iterates a list ofkeys and returns the first value
> 0, or0if all failMacSensors.javaqueryCpuTemperature(): wrapped intry/finallywith null-guard onconnection; probes Apple Silicon keys first via
smcGetFirstFloat, falls backto Intel
TC0PqueryCpuVoltage(): wrapped intry/finallywith null-guard on connection;probes Apple Silicon
VP0Cfirst (no/1000scaling), falls back to IntelVC0C / 1000queryFanSpeeds(): unchanged —FNumandF%dAckeys work correctly onboth architectures; zero fan speeds on fanless Apple Silicon models are
accurate
SmcUtil.SMC_KEY_*qualified references with explicit staticimports
Motivation
Apple Silicon Macs use entirely different SMC key namespaces from Intel Macs.
The existing code only knew Intel keys (
TC0P,VC0C), causing CPU temperatureand voltage to report
0on all M-series hardware. As Apple Silicon is now themajority of active Mac hardware, AS keys are probed first with Intel keys as the
fallback.
Testing
Verified on Apple Silicon (M-series) hardware:
0.0°C65.8°C[0, 0][0, 0]0.0V0.997VFan speeds correctly report
[0, 0]on fanless Apple Silicon models. Intelbehaviour is unchanged — the Intel key paths are still exercised when AS keys
return
0.Summary by CodeRabbit
New Features
Bug Fixes