Skip to content

Extract MacOSProcess common base (JNA/FFM dedup) - #3376

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-osprocess-base
Jun 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-osprocess-base

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Extends the OSProcess deduplication to macOS (after the BSD #3370/#3372 and Solaris #3367 work). The JNA and FFM macOS OSProcess implementations duplicated their field storage, all the trivial accessors, the command-line/argument/environment memoization, thread enumeration, the affinity mask, and the pbi_status state mapping (~243 duplicated lines each per Sonar — the #2/#3 files on the project).

This extracts an abstract MacOSProcess base in oshi-common holding that shared surface, with the macOS state mapping pulled into a shared stateFromStatus() helper. The native attribute reads stay in the JNA and FFM subclasses behind hooks:

  • updateAttributes() — proc_pidinfo (PROC_PIDTASKALLINFO / vnode / rusage)
  • queryArgsAndEnvironment() — KERN_PROCARGS2 sysctl
  • getSoftOpenFileLimit() / getHardOpenFileLimit() — getrlimit
  • queryLogicalProcessorCount() — hw.logicalcpu sysctl (for getAffinityMask)

The os field is typed as the MacOperatingSystem base, so the FFM subclass constructor widens its parameter to match (the OS classes pass this, which already IS-A MacOperatingSystem, so no call-site change is needed).

MacOSThread (single common class) and MacOperatingSystem (base + JNA/FFM) already followed this pattern; MacOSProcess was the last Mac class lacking a common base. The subclasses keep their (genuinely divergent) native struct reads. Net −190 lines.

Testing

Validated on macOS by the full test suite including NativeComparisonTest, which asserts JNA and FFM produce identical results — the strongest parity check available. Local spotless / checkstyle / forbidden-APIs / javadoc and a clean full-reactor build also pass. (A checkstyle VisibilityModifier suppression was extended to cover MacOSProcess, consistent with the existing BsdOSProcess/BsdOSThread shared-base entries.)

Summary by CodeRabbit

  • Refactor
    • Consolidated macOS process handling logic into a shared base class, improving code organization and reducing duplication across different implementation backends.

The JNA and FFM macOS OSProcess implementations duplicated their field storage,
all the trivial accessors, the command-line/argument/environment memoization,
thread enumeration, the affinity mask, and the pbi_status state mapping (~243
duplicated lines per Sonar).

Mirroring the BSD/Solaris OSProcess pattern, extract an abstract MacOSProcess
base in oshi-common holding that shared surface, with the macOS state mapping
pulled into a shared stateFromStatus() helper. The native attribute reads stay
in the JNA and FFM subclasses behind hooks: updateAttributes() (proc_pidinfo),
queryArgsAndEnvironment() (KERN_PROCARGS2 sysctl), getSoft/HardOpenFileLimit()
(getrlimit), and queryLogicalProcessorCount() (hw.logicalcpu sysctl).

The os field is typed as the MacOperatingSystem base, so the FFM subclass
constructor widens its parameter accordingly (the OS classes pass 'this', which
already IS-A MacOperatingSystem). Validated by the macOS NativeComparisonTest
(JNA==FFM parity).
@coderabbitai

coderabbitai Bot commented Jun 12, 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

Run ID: 4e2ff3e7-4dbd-4bb6-89f8-bf2a4de5102d

📥 Commits

Reviewing files that changed from the base of the PR and between 0154a72 and 8c27b1f.

📒 Files selected for processing (4)
  • config/checkstyle-suppressions.xml
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.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). (14)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
📚 Learning: 2026-04-06T04:45:47.644Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).

Applied to files:

  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
🔇 Additional comments (17)
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java (6)

1-54: LGTM!


56-97: LGTM!


99-167: LGTM!


179-269: LGTM!


271-309: LGTM!


168-177: Fix: MacOSThread constructor signature matches, but queryTaskThreads() nullability needs to be checked.

MacOSThread in oshi.software.common.os.mac has a constructor MacOSThread(int pid, int threadId, State state, long kernelTime, long userTime, long startTime, long upTime, int priority), which matches the argument types/order used in MacOSProcess#getThreadDetails().

However, the earlier check didn’t retrieve ThreadInfo.queryTaskThreads() (method signature/return type not found from the attempted pattern), so whether it can return null (and thus make .stream() throw an NPE) is still unknown. Remove/adjust the NPE concern only after confirming the queryTaskThreads() contract (null vs empty collection).

oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java (5)

85-86: LGTM!

Also applies to: 91-94


126-128: LGTM!


131-132: LGTM!


201-203: LGTM!


270-270: LGTM!

oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java (5)

35-35: LGTM!

Also applies to: 42-45


78-79: LGTM!


83-84: LGTM!


153-156: LGTM!


204-204: LGTM!

config/checkstyle-suppressions.xml (1)

65-68: LGTM!


📝 Walkthrough

Walkthrough

This PR extracts shared macOS process logic into a new abstract base class, reducing duplication across JNA and FFM backends. Both implementations now inherit common behavior for state mapping, thread details, memory metrics, and counter tracking, while retaining backend-specific queries for arguments and logical processor count.

Changes

macOS Process Abstraction Extraction

Layer / File(s) Summary
MacOSProcess abstract base class definition
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
New abstract base class provides shared macOS process behavior: state mapping from pbi_status codes via stateFromStatus(), memoized command-line and args/environment retrieval, and implementations for all public AbstractOSProcess getters (identity, memory, timing, I/O, counters, thread details). Declares abstract methods queryArgsAndEnvironment() and queryLogicalProcessorCount() for backend-specific implementations.
MacOSProcessFFM refactoring to extend MacOSProcess
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
Rebases from AbstractOSProcess to MacOSProcess, changes constructor to accept MacOperatingSystem, removes State constant imports and duplicated getter overrides, implements queryLogicalProcessorCount() via sysctl, and delegates state mapping to stateFromStatus().
MacOSProcessJNA refactoring to extend MacOSProcess
oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
Rebases from AbstractOSProcess to MacOSProcess, removes duplicated getter overrides, implements queryLogicalProcessorCount() via sysctl, and delegates state mapping to stateFromStatus(). Retains sysctl-based queryArgsAndEnvironment() implementation.
Checkstyle suppression configuration update
config/checkstyle-suppressions.xml
Expands VisibilityModifier suppression pattern to include MacOSProcess alongside BSD-family base classes, documenting that shared base classes cannot make protected fields private due to subclass and native code updates.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • oshi/oshi#3370: Adjusts the same config/checkstyle-suppressions.xml visibility suppression around the BSD-family process base BsdOSProcess.
  • oshi/oshi#3144: Moves ThreadInfo and MacOSThread into oshi-common, which underpins the new MacOSProcess base class getThreadDetails() implementation.
  • oshi/oshi#3150: Refactors MacOSProcessFFM to inherit common process logic via the new MacOSProcess base class and fixes group-name resolution.

Poem

🐰 A rabbit hops through code so neat,
Extracting logic, making it sweet!
One base class for all macOS ways,
Less duplication, clearer days! 🌱

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.12% 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 'Extract MacOSProcess common base (JNA/FFM dedup)' clearly and specifically describes the main change: extracting a shared MacOSProcess base class to eliminate duplication between JNA and FFM implementations.
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.

✏️ 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.

@codecov

codecov Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.01%. Comparing base (0154a72) to head (8c27b1f).

Files with missing lines Patch % Lines
...java/oshi/software/common/os/mac/MacOSProcess.java 86.20% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3376      +/-   ##
============================================
- Coverage     75.04%   75.01%   -0.04%     
  Complexity       76       76              
============================================
  Files           570      571       +1     
  Lines         21948    21893      -55     
  Branches       3640     3637       -3     
============================================
- Hits          16471    16422      -49     
+ Misses         3425     3412      -13     
- Partials       2052     2059       +7     
Flag Coverage Δ
freebsd 26.28% <0.00%> (+0.07%) ⬆️
linux 30.25% <0.00%> (+0.07%) ⬆️
macos 30.80% <87.50%> (-0.08%) ⬇️
openbsd 25.68% <0.00%> (+0.06%) ⬆️
solaris 27.33% <0.00%> (+0.05%) ⬆️
windows 38.84% <0.00%> (+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.

@dbwiddis
dbwiddis merged commit 054d5ab into oshi:master Jun 12, 2026
22 checks passed
@dbwiddis
dbwiddis deleted the mac-osprocess-base branch June 12, 2026 02:46
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