Skip to content

Extract BSD OperatingSystem common bases (JNA/FFM) - #3375

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-operatingsystem-base
Jun 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-operatingsystem-base

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Completes the BSD OperatingSystem deduplication (after the OSProcess #3372 and OSThread #3374 work). The FreeBSD, OpenBSD, and DragonFly OperatingSystem classes each duplicated their entire body between the JNA and FFM backends — the method signatures were identical; only the native calls and backend factory types differed.

Following the Solaris #3367 template, this extracts an abstract per-platform base in oshi-common holding the shared, non-native logic (ps-based process enumeration, process/thread counts, services, system uptime/boot time). The JNA and FFM subclasses are reduced to the genuinely backend-specific bits: the OSProcess factory (a new createProcess hook), the FileSystem/NetworkParams/InternetProtocolStats factories, getpid/thread-id, the sysctl version query, and who sessions.

Per-platform notes:

  • Boot time: FreeBSD/DragonFly read it from a native sysctl struct, so their base declares an abstract queryBootTime() hook (memoized via getSystemBootTime()); OpenBSD text-parses it, so its base implements queryBootTime() concretely.
  • OpenBSD InternetProtocolStats and NetworkParams are common (non-native) classes, so they move into its base; FreeBSD/DragonFly keep their JNA/FFM factory overrides.
  • DragonFly reuses FreeBSD's filesystem/IP/network impls and keeps its kernel-thread (pid <= 0) filter in the shared enumeration.
  • NetBSD already has a single common OperatingSystem class and is unchanged.

The JNA/FFM subclasses shrink from ~225–238 lines each to ~55–90. Net −287 lines. Behavior is unchanged — the per-platform ps commands and column lists are identical to before.

Cross-platform commonality between the three new bases (e.g. getProcessCount/getServices) remains; collapsing that into a shared BsdOperatingSystem super-base is a possible follow-up but was kept out to keep this change a clean JNA↔FFM dedup mirroring the Solaris template.

Testing

All five unix.yaml VM jobs pass, exercising the refactored code on real FreeBSD, OpenBSD, DragonFly, and NetBSD systems (plus OpenIndiana). Local spotless / checkstyle / forbidden-APIs / javadoc and a clean full-reactor build also pass.

Summary by CodeRabbit

Release Notes

  • Refactor
    • Consolidated BSD operating system implementations for DragonFly BSD, FreeBSD, and OpenBSD. Process enumeration, thread counting, system uptime/boot time queries, and service discovery are now handled through shared base class methods, with platform-specific implementations providing only native-code bindings.

The FreeBSD, OpenBSD, and DragonFly OperatingSystem classes each duplicated
their entire body between the JNA and FFM backends (the method signatures were
identical; only the native calls and backend factory types differed).

Following the Solaris #3367 template, extract an abstract per-platform base in
oshi-common holding the ps-based process enumeration, process/thread counts,
services, and system uptime/boot time. The JNA and FFM subclasses are reduced to
the genuinely backend-specific bits: the OSProcess factory (a new createProcess
hook), the FileSystem/NetworkParams/InternetProtocolStats factories, getpid/
thread-id, the sysctl version query, and who sessions.

Per-platform notes:
- FreeBSD/DragonFly read boot time from a native sysctl struct, so the base
  declares an abstract queryBootTime() hook (memoized); OpenBSD text-parses it,
  so its base implements queryBootTime() concretely.
- OpenBSD's InternetProtocolStats and NetworkParams are common (non-native)
  classes, so they move into the base; FreeBSD/DragonFly keep their JNA/FFM
  factory overrides. DragonFly reuses FreeBSD's filesystem/IP/network impls and
  keeps its kernel-thread (pid <= 0) filter.

NetBSD already has a single common OperatingSystem class and is unchanged. The
JNA/FFM subclasses shrink from ~225-238 lines each to ~55-90.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces three new abstract base classes in oshi-common that consolidate shared BSD operating system behavior (process enumeration, thread metrics, boot-time caching, and service discovery), then refactors six existing JNA/FFM implementations to inherit from these bases and retain only platform-specific overrides.

Changes

BSD Operating System Base Classes

Layer / File(s) Summary
DragonFlyBSD base class
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOperatingSystem.java
DragonFlyBsdOperatingSystem centralizes ps-based process enumeration, process/thread counting, current thread lookup by TID, system uptime from wall-clock minus memoized boot time, service enumeration by merging running child processes with stopped services from /etc/rc.d, and defines abstract hooks for createProcess() and queryBootTime() for subclass-specific implementations.
FreeBSD base class
oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOperatingSystem.java
FreeBsdOperatingSystem implements FreeBSD-specific process discovery via ps command parsing into keyword maps, child/descendant filtering by PID descent, process/thread metrics from ps output, current thread detection by matching TID, system uptime calculation, service enumeration combining running and stopped services from /etc/rc.d, and leaves createProcess() and queryBootTime() abstract for backends.
OpenBSD base class
oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOperatingSystem.java
OpenBsdOperatingSystem provides manufacturer/bitness queries, ps -awwxo command execution with BsdPsKeyword map parsing, child/descendant process filtering, process/thread counting and current thread lookup by TID, system uptime from wall-clock time minus boot time parsed from sysctl kern.boottime, service enumeration from child processes of PID 1 plus stopped services from /etc/rc.d, and abstract createProcess() factory method.

JNA and FFM Platform Refactoring

Layer / File(s) Summary
DragonFlyBSD platform implementations
oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java, oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemFFM.java
Both JNA and FFM classes now extend DragonFlyBsdOperatingSystem instead of AbstractOperatingSystem. Large overrides for process enumeration, thread/process metrics, uptime/boot time, and services are removed. Each class retains only platform-specific hooks: network params, sessions (conditional FFM/JNA behavior), process/thread ID retrieval via native calls, and createProcess()/queryBootTime() overrides using backend-specific bindings.
FreeBSD platform implementations
oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystemJNA.java, oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystemFFM.java
Both JNA and FFM classes change their superclass to FreeBsdOperatingSystem. Method overrides for bitness, process enumeration/querying, process/thread counts, uptime/boot time, and services are deleted. Each class now provides only network params, sessions (with platform-specific fallback), process/thread ID via native methods, and process construction/boot-time overrides delegating to backend-specific sysctl/parsing logic.
OpenBSD platform implementations
oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemJNA.java, oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemFFM.java
Both JNA and FFM classes are refactored to extend OpenBsdOperatingSystem instead of AbstractOperatingSystem. Large blocks of process/thread/uptime/networking logic are removed from both. Each class is reduced to a minimal set of platform-specific overrides: filesystem backend, native process/thread ID retrieval, and createProcess() to construct platform-specific OS process instances.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

  • oshi/oshi#3347: This PR implements the template-method architecture for FreeBSD, OpenBSD, and DragonFlyBSD (shared *OperatingSystem base in oshi-common with JNA/FFM subclasses), which directly addresses the same refactoring requested for NetBSD in issue #3347.

Possibly related PRs

  • oshi/oshi#3372: The new BSD base classes rely on BsdPsKeyword and its shared ps parsing contract introduced in PR #3372.
  • oshi/oshi#3333: OpenBSD OS process/thread base classes (OpenBsdOSProcess, OpenBsdOSThread) moved to oshi-common are core dependencies for the new OpenBsdOperatingSystem abstraction.
  • oshi/oshi#3362: This PR further refactors DragonFlyBsdOperatingSystemFFM that was introduced in PR #3362 to now inherit shared DragonFlyBSD logic from the new DragonFlyBsdOperatingSystem base class.

Poem

🐰 Three BSD branches grew from one tree today,
With shared roots in commons, platforms on display.
Template methods bloom, code duplication fades,
JNA and FFM dance in inheritance's cascades! 🌳✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.38% 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 BSD OperatingSystem common bases (JNA/FFM)' directly and clearly describes the main objective of the PR: extracting shared, non-native logic into abstract per-platform base classes for BSD OperatingSystem implementations to reduce duplication across JNA and FFM backends.
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 84.05797% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.00%. Comparing base (5f860c2) to head (b839d5d).

Files with missing lines Patch % Lines
...common/os/unix/openbsd/OpenBsdOperatingSystem.java 83.82% 6 Missing and 5 partials ⚠️
...common/os/unix/freebsd/FreeBsdOperatingSystem.java 84.48% 6 Missing and 3 partials ⚠️
...are/os/unix/freebsd/FreeBsdOperatingSystemFFM.java 75.00% 0 Missing and 1 partial ⚠️
...are/os/unix/freebsd/FreeBsdOperatingSystemJNA.java 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3375      +/-   ##
============================================
- Coverage     75.04%   75.00%   -0.05%     
  Complexity       76       76              
============================================
  Files           568      570       +2     
  Lines         22071    21948     -123     
  Branches       3662     3640      -22     
============================================
- Hits          16564    16462     -102     
+ Misses         3446     3428      -18     
+ Partials       2061     2058       -3     
Flag Coverage Δ
freebsd 26.21% <39.85%> (-0.06%) ⬇️
linux 30.18% <0.00%> (+0.16%) ⬆️
macos 30.88% <0.00%> (+0.17%) ⬆️
openbsd 25.62% <44.20%> (-0.07%) ⬇️
solaris 27.28% <0.00%> (+0.14%) ⬆️
windows 38.76% <0.00%> (+0.18%) ⬆️

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.

🧹 Nitpick comments (1)
oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java (1)

66-69: 💤 Low value

Minor inconsistency: JNA uses DragonFlyBsdLibc while FFM uses FreeBsdLibcFunctions for getpid().

The JNA backend calls DragonFlyBsdLibc.INSTANCE.getpid() while the FFM backend calls FreeBsdLibcFunctions.getpid(). This works correctly since getpid() is a standard POSIX call, but consider aligning both backends to use the same library pattern for consistency (either both use DragonFlyBsd-specific or both use FreeBsd-shared).

🤖 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/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java`
around lines 66 - 69, The JNA backend in
DragonFlyBsdOperatingSystemJNA.getProcessId() calls
DragonFlyBsdLibc.INSTANCE.getpid(), which is inconsistent with the FFM backend
that uses FreeBsdLibcFunctions.getpid(); change the call in getProcessId() to
use FreeBsdLibcFunctions.getpid() so both backends use the same libc wrapper
naming pattern (or alternatively update the FFM side to DragonFlyBsdLibc—pick
one approach and make both use the same symbol).
🤖 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.

Nitpick comments:
In
`@oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java`:
- Around line 66-69: The JNA backend in
DragonFlyBsdOperatingSystemJNA.getProcessId() calls
DragonFlyBsdLibc.INSTANCE.getpid(), which is inconsistent with the FFM backend
that uses FreeBsdLibcFunctions.getpid(); change the call in getProcessId() to
use FreeBsdLibcFunctions.getpid() so both backends use the same libc wrapper
naming pattern (or alternatively update the FFM side to DragonFlyBsdLibc—pick
one approach and make both use the same symbol).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 82213aeb-0e3e-4daf-b042-3755ded597af

📥 Commits

Reviewing files that changed from the base of the PR and between 5f860c2 and b839d5d.

📒 Files selected for processing (9)
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOperatingSystem.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOperatingSystem.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOperatingSystem.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystemFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemFFM.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystemJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemJNA.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 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: JMH Benchmarks, ubuntu-latest
🧰 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/unix/openbsd/OpenBsdOperatingSystem.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOperatingSystem.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOperatingSystem.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/OpenBsdOperatingSystemJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystemJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java
🔇 Additional comments (30)
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOperatingSystem.java (7)

41-46: LGTM!


48-59: LGTM!


61-104: LGTM!


133-141: LGTM!


143-168: LGTM!


170-185: LGTM!


116-122: getCurrentThread() relies on getThreadId() from the class hierarchy
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOperatingSystem.java calls getThreadId() without declaring it locally; confirm the method is provided via the OperatingSystem API and/or an inherited superclass implementation so this compiles and is consistent with other platform implementations.

oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemFFM.java (4)

42-42: LGTM!

Also applies to: 25-25


67-75: LGTM!


89-92: LGTM!


94-107: LGTM!

oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOperatingSystemJNA.java (3)

33-33: LGTM!

Also applies to: 15-15


77-80: LGTM!


82-92: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOperatingSystem.java (3)

42-184: LGTM!


90-91: No module-boundary issue: FreeBsdOSProcess/FreeBsdOSThread and the referenced constants are in oshi-common.

  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java defines public static final String PS_COMMAND_ARGS and public static final List<BsdPsKeyword> PS_KEYWORDS used at lines 90–91 and 97–98.

  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.java provides the concrete FreeBsdOSThread instantiated at line 120.

      		> Likely an incorrect or invalid review comment.
    

115-121: Resolve getThreadId() inheritance concern in getCurrentThread()
oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOperatingSystem.java calls getThreadId() from getCurrentThread(), and getThreadId() is implemented in the FreeBSD concrete subclasses (FreeBsdOperatingSystemJNA / FreeBsdOperatingSystemFFM) via @Override, so the call is satisfied by the class hierarchy.

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

38-107: LGTM!

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

32-97: LGTM!

oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOperatingSystem.java (7)

1-50: LGTM!


52-68: LGTM!


70-96: LGTM!


144-172: LGTM!


174-209: LGTM!


98-124: Confirm OpenBsdOSProcess static fields are present in oshi-common.

oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java defines public static final String PS_COMMAND_ARGS and public static final List<BsdPsKeyword> PS_KEYWORDS, matching the references in OpenBsdOperatingSystem.


126-142: OpenBsdOSThread exists in oshi-common and matches the expected OpenBSD package. It’s implemented at oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSThread.java under package oshi.software.common.os.unix.openbsd;, so the fallback instantiation in OpenBsdOperatingSystem is valid without an explicit import.

oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemFFM.java (2)

23-23: LGTM!

Also applies to: 28-32


68-71: LGTM!

oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystemJNA.java (2)

17-17: LGTM!

Also applies to: 23-27


59-62: LGTM!

@dbwiddis
dbwiddis merged commit 0154a72 into oshi:master Jun 12, 2026
22 checks passed
@dbwiddis
dbwiddis deleted the bsd-operatingsystem-base branch June 12, 2026 01:44
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