Skip to content

Extract IOKit iteration into IOKitProvider callback pattern - #3264

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/iokit-provider
May 18, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/iokit-provider

Conversation

@dbwiddis

@dbwiddis dbwiddis commented May 18, 2026 •

Copy link
Copy Markdown
Member

Closes #3260

Introduces an IOKitProvider interface in oshi-common that abstracts IOKit registry operations, with JNA and FFM singleton implementations. Duplicated IOKit iteration logic is moved from subclasses into the common base classes.

New files:

  • IOKitProvider.java — interface with withMatchingService, forEachMatchingService, forEachMatchingServiceUntil, and RegistryEntry
  • IOKitProviderJNA.java / IOKitProviderFFM.java — implementations

Refactored base classes (logic moved in):

  • MacCentralProcessor — platformExpert(), queryCompatibleStrings(), calculateNominalFrequencies()
  • MacBaseboard — queryPlatform()
  • MacComputerSystem — platformExpert()

Simplified subclasses (~330 lines removed):

  • MacCentralProcessorJNA/FFM — removed IOKit methods, just implement ioKitProvider()
  • MacBaseboardJNA/FFM — reduced to ioKitProvider() only
  • MacComputerSystemJNA/FFM — reduced to ioKitProvider() + factory methods

Classes using getChildIterator (MacFirmware, MacUsbDevice) are not addressed here as they require a more complex lifecycle pattern.

Summary by CodeRabbit

  • Refactor
    • Unified macOS hardware access behind a new provider abstraction to reduce duplication and simplify platform implementations.
  • Bug Fixes
    • Improved extraction and normalization of Mac manufacturer, model, serial and UUID with sensible defaults (e.g., "Apple Inc.", "Unknown") when values are missing or blank.
  • Tests
    • Tests updated to avoid invoking native provider behavior during unit runs.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f8bc173d-f55b-4e64-9273-e7f0e8d496aa

📥 Commits

Reviewing files that changed from the base of the PR and between 4e10c91 and 0ad54d8.

📒 Files selected for processing (13)
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/IOKitProvider.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacBaseboard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacComputerSystem.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/mac/MacCentralProcessorTest.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/IOKitProviderFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacBaseboardFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacComputerSystemFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/IOKitProviderJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacBaseboardJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacComputerSystemJNA.java
🚧 Files skipped from review as they are similar to previous changes (11)
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/IOKitProvider.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacBaseboardJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/IOKitProviderJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacComputerSystem.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacBaseboard.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacBaseboardFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacComputerSystemJNA.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/IOKitProviderFFM.java
📜 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). (15)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 11, ubuntu-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (2)
oshi-common/src/test/java/oshi/hardware/common/platform/mac/MacCentralProcessorTest.java (1)

86-89: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacComputerSystemFFM.java (1)

10-10: LGTM!

Also applies to: 14-15, 30-31


📝 Walkthrough

Walkthrough

Adds an IOKitProvider interface and RegistryEntry contract, implements JNA and FFM provider singletons, updates common Mac base classes to use ioKitProvider() for platform/CPU discovery, and migrates JNA/FFM mac subclasses to return the corresponding provider instances.

Changes

IOKit Provider Architecture and Base Class Refactoring

Layer / File(s) Summary
IOKitProvider abstraction
oshi-common/src/main/java/oshi/hardware/common/platform/mac/IOKitProvider.java
New interface defines service lookup/iteration contract with nested RegistryEntry for property access; three methods: single extraction, full iteration, and early-terminating iteration.
MacBaseboard base class implementation
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacBaseboard.java
Adds abstract ioKitProvider() hook and implements queryPlatform() to extract baseboard data (manufacturer/model/version/serial) from IOPlatformExpertDevice with byte-to-UTF-8 conversion and null/blank normalization.
MacCentralProcessor base class implementation
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java
Adds CPU_N regex and concrete platformExpert(), queryCompatibleStrings(), and calculateNominalFrequencies() implementations using ioKitProvider() to query IOPlatformDevice and AppleARMIODevice for processor compatibility and frequency data.
MacComputerSystem base class implementation
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacComputerSystem.java
Adds abstract ioKitProvider() hook and implements platformExpert() to extract system identifiers (manufacturer/model/serial/UUID) from IOPlatformExpertDevice with property decoding and fallback defaults.

JNA IOKit Provider Implementation and Subclass Migration

Layer / File(s) Summary
IOKitProviderJNA implementation
oshi-core/src/main/java/oshi/hardware/platform/mac/IOKitProviderJNA.java
New JNA-backed IOKitProvider singleton with withMatchingService (single lookup + cleanup), forEachMatchingService (delegation), and forEachMatchingServiceUntil (iterator-based traversal with try/finally resource release); wraps JNA IORegistryEntry via JnaRegistryEntry adapter.
JNA subclass refactoring
oshi-core/src/main/java/oshi/hardware/platform/mac/MacBaseboardJNA.java, MacCentralProcessorJNA.java, MacComputerSystemJNA.java
Each refactored to override ioKitProvider() returning IOKitProviderJNA.INSTANCE, removing previous direct IOKit implementations and related imports; platform expert/CPU logic now delegated to base classes.

FFM IOKit Provider Implementation and Subclass Migration

Layer / File(s) Summary
IOKitProviderFFM implementation
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/IOKitProviderFFM.java
New FFM-backed IOKitProvider singleton with withMatchingService (single lookup + cleanup), forEachMatchingService (delegation), and forEachMatchingServiceUntil (iterator-based traversal with try/finally resource release); wraps FFM IORegistryEntry via FfmRegistryEntry adapter.
FFM subclass refactoring
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacBaseboardFFM.java, MacCentralProcessorFFM.java, MacComputerSystemFFM.java
Each refactored to override ioKitProvider() returning IOKitProviderFFM.INSTANCE, removing previous direct IOKit implementations and related imports; platform expert/CPU logic now delegated to base classes.

Sequence Diagram

sequenceDiagram
  participant Base as MacCentralProcessor (base)
  participant ProviderHook as ioKitProvider()
  participant ProviderImpl as IOKitProviderJNA/IOKitProviderFFM
  participant Registry as IORegistryEntry

  Base->>ProviderHook: call
  ProviderHook-->>Base: provider instance
  Base->>ProviderImpl: withMatchingService("IOPlatformExpertDevice", extractor)
  ProviderImpl->>Registry: lookup service
  Registry-->>ProviderImpl: IORegistryEntry
  ProviderImpl->>ProviderImpl: wrap -> RegistryEntry
  ProviderImpl-->>Base: apply extractor result
  Base->>Base: decode properties (UTF-8), normalize
  ProviderImpl->>Registry: release entry (finally)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

  • oshi/oshi#3146: Modifies shared macOS hardware superclasses; related to how platform data is supplied and structured.
  • oshi/oshi#3149: Centralizes Mac CPU identification/frequency logic in base classes; similar refactor area.
  • oshi/oshi#3109: Earlier FFM migration work touching Mac FFM classes; related to provider/FFM integration.

Poem

🐰 From IOKit groves I hop and write,

One provider here to share the light,
JNA and FFM join the tune,
Base classes hum a single rune,
My carrot-powered tests sleep tight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 29.41% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main refactoring: extracting IOKit iteration logic into a reusable IOKitProvider callback pattern interface, which is the core change across all modified files.
Linked Issues check ✅ Passed The PR fully addresses issue #3260: introduces IOKitProvider abstraction [#3260], centralizes IOKit iteration in base classes, provides JNA/FFM implementations, and removes ~300 lines of duplicated code.
Out of Scope Changes check ✅ Passed All changes directly support the stated objective of extracting IOKit iteration into callback pattern; no unrelated refactoring or scope creep detected across the modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

Introduce an IOKitProvider interface in oshi-common that abstracts IOKit
registry operations (getMatchingService, iteration, property access).
JNA and FFM each provide a singleton implementation.

Move duplicated IOKit logic from MacCentralProcessor, MacBaseboard, and
MacComputerSystem subclasses into their common base classes, which now
use the provider. This eliminates ~330 lines of duplicated iteration
code across the JNA/FFM pairs.
@dbwiddis
dbwiddis force-pushed the refactor/iokit-provider branch from 4e10c91 to 0ad54d8 Compare May 18, 2026 19:12
@codecov

codecov Bot commented May 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.08547% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.25%. Comparing base (d2746f2) to head (0ad54d8).

Files with missing lines Patch % Lines
...dware/common/platform/mac/MacCentralProcessor.java 65.38% 4 Missing and 5 partials ⚠️
...shi/hardware/common/platform/mac/MacBaseboard.java 52.94% 1 Missing and 7 partials ⚠️
...ardware/common/platform/mac/MacComputerSystem.java 50.00% 1 Missing and 7 partials ⚠️
...a/oshi/hardware/platform/mac/IOKitProviderFFM.java 80.76% 2 Missing and 3 partials ⚠️
...a/oshi/hardware/platform/mac/IOKitProviderJNA.java 80.76% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3264      +/-   ##
============================================
+ Coverage     74.19%   74.25%   +0.05%     
  Complexity       73       73              
============================================
  Files           397      399       +2     
  Lines         16091    16029      -62     
  Branches       2587     2555      -32     
============================================
- Hits          11939    11902      -37     
+ Misses         2636     2633       -3     
+ Partials       1516     1494      -22     
Flag Coverage Δ
linux 38.76% <0.85%> (+0.15%) ⬆️
macos 39.28% <70.08%> (-0.03%) ⬇️
windows 47.15% <0.85%> (+0.14%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

@dbwiddis
dbwiddis merged commit fdd3efc into oshi:master May 18, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the refactor/iokit-provider branch May 18, 2026 20:00
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.

Reduce duplication: extract IOKit iteration into template/callback pattern

1 participant