Skip to content

Extract a shared BsdOSProcess base to de-duplicate the BSD OSProcess classes - #3370

Merged
dbwiddis merged 5 commits into
oshi:masterfrom
dbwiddis:bsd-osprocess-common-base
Jun 11, 2026
Merged

dbwiddis merged 5 commits into
oshi:masterfrom
dbwiddis:bsd-osprocess-common-base

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 11, 2026 •

Copy link
Copy Markdown
Member

Eliminates the largest remaining duplication cluster in the codebase. A SonarCloud duplication query showed the BSD OSProcess classes dominating the top of the list: when FreeBSD/OpenBSD/DragonFly were FFM-ported they were given near-empty bases, leaving ~450 lines of process logic duplicated in each JNA and FFM concrete — and again across the platforms (DragonFly is a FreeBSD fork). This applies the same base-extraction approach used for the Solaris series (#3367/#3368/#3369), but shared across the whole BSD family.

What changed

  • New abstract oshi.software.common.os.unix.bsd.BsdOSProcess holds the cross-BSD-identical, non-native logic: the field storage and all trivial accessors, the command-line/argument/environment/bitness memoization, the ps state mapping, the cpuset affinity lookup, and the /proc/<pid>/limits fallback.
  • The per-platform bases (FreeBsdOSProcess, OpenBsdOSProcess, DragonFlyBsdOSProcess) now extend it and keep only their PsThreadColumns enum; the JNA/FFM concretes keep only the platform-specific work — the native sysctl argument/environment/bitness reads, getrlimit, procstat/fstat cwd/open-file lookups, ps thread enumeration, and the ps attribute parsing.
  • NetBsdOSProcess (a single no-native class) folds in too: its argument/environment/bitness hooks are pure Java, and it overrides getAffinityMask for its schedctl-based lookup.

The JNA/FFM concretes drop from ~458 to ~200-250 lines each; NetBSD from 402 to 214. The shared updateAttributes is left per-platform because it reads each OS's own PsKeywords enum (which lives in the operating-system classes, not shared).

Incidental fix

Removes a dead, unreachable this.threadCount = 1; in the OpenBSD JNA updateThreadCount() that unconditionally overwrote the computed ps thread count.

Validation

Full clean reactor build plus spotless/checkstyle/forbiddenapis/javadoc pass locally. The Unix CI (FreeBSD, OpenBSD, DragonFly, NetBSD, and OpenIndiana VMs) was run on the branch and is green, so the per-platform behavior is verified unchanged.

Summary by CodeRabbit

  • Refactor
    • Consolidated common BSD operating system process handling logic into a shared abstract base class, enabling code reuse across FreeBSD, OpenBSD, NetBSD, and DragonFly BSD implementations while maintaining existing functionality.

dbwiddis added 5 commits June 10, 2026 21:47
The FreeBSD/OpenBSD/DragonFly/NetBSD OSProcess classes duplicate a large
amount of identical logic. Introduce an abstract BsdOSProcess base in
oshi-common holding the field storage, all trivial accessors, the
command-line/argument/environment/bitness memoization, the ps state
mapping, the cpuset affinity lookup, and the /proc/<pid>/limits fallback.
Per-platform subclasses provide the ps attribute query, thread enumeration,
cwd/open-file lookups, and the native argument/environment/file-limit reads.
FreeBsdOSProcess extends BsdOSProcess; the JNA and FFM concretes drop the
inherited fields/getters/suppliers/affinity/limits and keep only the
sysctl arg/env/bitness reads, getrlimit, procstat cwd/open-files, ps thread
enumeration, and the ps attribute parsing.
DragonFlyBsdOSProcess extends BsdOSProcess; concretes keep the DragonFly
specifics (/proc/<pid>/cmdline args, System.getenv, sysctl bitness,
/proc/<pid>/status start time, group=user).
OpenBsdOSProcess extends BsdOSProcess; concretes keep the OpenBSD specifics
(sysctl pointer-list arg/env parsing, fixed bitness, fstat cwd/open-files).
Also removes a dead, unreachable threadCount=1 assignment in the JNA
updateThreadCount that overwrote the computed value.
NetBsdOSProcess (single no-native class) extends BsdOSProcess, implementing
the arg/env/bitness hooks in pure Java and overriding getAffinityMask for
its schedctl-based lookup.
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR consolidates common BSD process attribute storage and accessor logic by introducing a shared BsdOSProcess base class and refactoring four BSD-family OSProcess implementations (DragonFly, FreeBSD, NetBSD, OpenBSD) across three technology modules (common, FFM, JNA) to extend and delegate to this new base, removing duplicated getter overrides and state-mapping logic.

Changes

BSD Process Hierarchy Consolidation

Layer / File(s) Summary
BsdOSProcess base class definition and attribute storage
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
Introduces the new abstract BsdOSProcess class extending AbstractOSProcess, with protected fields for process name, path, user, group, state, parent/thread count, priority, memory/timing statistics, I/O counters, and fault/context-switch counts. Implements constructor wiring and memoized supplier initialization for command line, arguments, and environment variables with lazy evaluation.
BsdOSProcess public getters and static helper methods
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
Provides public getter overrides for all stored attributes, implements getAffinityMask() by executing cpuset -gp <pid> and parsing the returned bitmask, adds static getStateFromOutput(char) to map single-character ps state codes to OSProcess.State enums, and implements getProcessOpenFileLimit(...) to read /proc/<pid>/limits for file descriptor limits with error handling.
Module export, package documentation, and checkstyle configuration
oshi-common/src/main/java/module-info.java, oshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.java, config/checkstyle-suppressions.xml
Exports the new oshi.software.common.os.unix.bsd package from the module descriptor, adds package-level Javadoc with MIT copyright header, and suppresses Checkstyle VisibilityModifier violations for BsdOSProcess.java with an explanatory comment about protected field accessors.
Common module BSD variant implementations refactored to extend BsdOSProcess
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java, oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java, oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java, oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java
Updates all four BSD-family implementations to extend BsdOSProcess instead of AbstractOSProcess, removes duplicate public getter overrides from NetBsdOSProcess, replaces local state-character-to-State switch statements with calls to getStateFromOutput(), and introduces protected queryArguments(), queryEnvironmentVariables(), and queryBitness() overrides for platform-specific queries.
FFM implementations refactored to delegate process attribute access to BsdOSProcess
oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.java, oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java, oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.java
Removes memoized supplier fields and large blocks of public getter overrides from all three FFM variants, converts private query methods to protected overrides, replaces inline state-mapping switches with getStateFromOutput() calls, and eliminates imports related to memoization and unused state constants.
JNA implementations refactored to delegate process attribute access to BsdOSProcess
oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java, oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.java, oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.java
Converts private query methods (queryArguments, queryEnvironmentVariables, queryBitness) to protected overrides across all JNA variants, removes large blocks of duplicated public getter implementations (identity, state, scheduling, memory, timing, I/O counters, faults, context switches), eliminates the /proc/<pid>/limits helper from DragonFlyBsdOSProcessJNA, and cleans up imports by removing memoization and unused state references.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • oshi/oshi#3363: Refactors OpenBsdOSProcessJNA by updating type names and command-argument handling; the main PR further refactors the same class to delegate attribute access to the new BSD superclass.
  • oshi/oshi#3335: Modifies OpenBsdOSProcessFFM to wire shared superclass logic for state mapping and getter delegation; the main PR introduces the shared BsdOSProcess base that this pattern now inherits from.
  • oshi/oshi#3310: Touches NetBsdOSProcess with attribute getter and state-mapping refactors; the main PR extends those same refactors by moving the logic into a shared BSD base class.

Poem

🐰 BSD branches unified in one tree,
Protected fields where all can be,
State maps shared, no duplication's fee,
Query methods dance in harmony—
One base, four flavors, wild and free! 🌳

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.63% 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 clearly and specifically describes the main change: extraction of a shared BsdOSProcess base class to eliminate duplication across BSD OSProcess 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 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.33333% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.15%. Comparing base (67a4a27) to head (2904e9d).

Files with missing lines Patch % Lines
...oshi/software/common/os/unix/bsd/BsdOSProcess.java 75.38% 13 Missing and 3 partials ⚠️
.../software/os/unix/openbsd/OpenBsdOSProcessFFM.java 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3370      +/-   ##
============================================
+ Coverage     75.13%   75.15%   +0.02%     
  Complexity       76       76              
============================================
  Files           564      565       +1     
  Lines         22374    22192     -182     
  Branches       3681     3662      -19     
============================================
- Hits          16810    16678     -132     
+ Misses         3498     3451      -47     
+ Partials       2066     2063       -3     
Flag Coverage Δ
freebsd 26.22% <73.33%> (+<0.01%) ⬆️
linux 29.85% <0.00%> (+0.24%) ⬆️
macos 30.54% <0.00%> (+0.24%) ⬆️
openbsd 25.69% <61.33%> (+0.06%) ⬆️
solaris 26.98% <0.00%> (+0.22%) ⬆️
windows 38.33% <0.00%> (+0.27%) ⬆️

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.java (1)

115-149: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard the ARGMAX == 0 fallback here too.

The static initializer already degrades to ARGMAX = 0 when kern.argmax fails, but this method still allocates new Memory(ARGMAX). That turns the intended empty-map fallback into a runtime exception the first time getEnvironmentVariables() is queried.

Suggested fix
 `@Override`
 protected Map<String, String> queryEnvironmentVariables() {
+    if (ARGMAX <= 0) {
+        return Collections.emptyMap();
+    }
     // Get environment variables via sysctl(3)
     int[] mib = new int[4];
     mib[0] = 1; // CTL_KERN
     mib[1] = 55; // KERN_PROC_ARGS
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.java`
around lines 115 - 149, In queryEnvironmentVariables(), guard against ARGMAX
being 0 (or <= 0) before allocating Memory and CloseableSizeTByReference: if
ARGMAX is 0 return Collections.emptyMap() (or skip the sysctl path) to avoid new
Memory(ARGMAX) throwing; update the start of the method (before "try (Memory m =
new Memory(ARGMAX)...") to early-return when ARGMAX <= 0, referencing ARGMAX,
the queryEnvironmentVariables method, and the existing try-with-resources that
creates Memory and CloseableSizeTByReference.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.java`:
- Around line 115-149: In queryEnvironmentVariables(), guard against ARGMAX
being 0 (or <= 0) before allocating Memory and CloseableSizeTByReference: if
ARGMAX is 0 return Collections.emptyMap() (or skip the sysctl path) to avoid new
Memory(ARGMAX) throwing; update the start of the method (before "try (Memory m =
new Memory(ARGMAX)...") to early-return when ARGMAX <= 0, referencing ARGMAX,
the queryEnvironmentVariables method, and the existing try-with-resources that
creates Memory and CloseableSizeTByReference.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 08a2fb0e-a017-4e4c-9e85-1ac83c5d433e

📥 Commits

Reviewing files that changed from the base of the PR and between 67a4a27 and 2904e9d.

📒 Files selected for processing (14)
  • config/checkstyle-suppressions.xml
  • oshi-common/src/main/java/module-info.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.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). (14)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • 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/module-info.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.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/unix/openbsd/OpenBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java
🔇 Additional comments (14)
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java (1)

11-13: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java (1)

11-13: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java (1)

22-22: LGTM!

Also applies to: 35-35, 61-61, 71-71, 112-115, 176-176

oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java (1)

11-13: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java (1)

39-301: LGTM!

oshi-common/src/main/java/module-info.java (1)

50-50: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.java (1)

1-8: LGTM!

config/checkstyle-suppressions.xml (1)

65-68: LGTM!

oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.java (1)

55-63: LGTM!

Also applies to: 65-75, 113-135, 166-193

oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java (1)

65-109: LGTM!

Also applies to: 150-168, 199-225

oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.java (1)

66-141: LGTM!

Also applies to: 208-231

oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java (1)

51-59: LGTM!

Also applies to: 61-69, 103-124, 155-182

oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.java (1)

36-38: LGTM!

Also applies to: 54-78, 80-104, 138-159, 190-216

oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessJNA.java (1)

73-113: LGTM!

Also applies to: 215-243

@dbwiddis
dbwiddis merged commit bddf85f into oshi:master Jun 11, 2026
22 checks passed
@dbwiddis
dbwiddis deleted the bsd-osprocess-common-base branch June 11, 2026 05:21
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