Repository navigation
Extract a shared BsdOSProcess base to de-duplicate the BSD OSProcess classes - #3370
Conversation
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.
📝 WalkthroughWalkthroughThis PR consolidates common BSD process attribute storage and accessor logic by introducing a shared ChangesBSD Process Hierarchy Consolidation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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 winGuard the
ARGMAX == 0fallback here too.The static initializer already degrades to
ARGMAX = 0whenkern.argmaxfails, but this method still allocatesnew Memory(ARGMAX). That turns the intended empty-map fallback into a runtime exception the first timegetEnvironmentVariables()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
📒 Files selected for processing (14)
config/checkstyle-suppressions.xmloshi-common/src/main/java/module-info.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.javaoshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.javaoshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.javaoshi-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.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/package-info.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.javaoshi-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.javaoshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.javaoshi-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
Eliminates the largest remaining duplication cluster in the codebase. A SonarCloud duplication query showed the BSD
OSProcessclasses 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
oshi.software.common.os.unix.bsd.BsdOSProcessholds the cross-BSD-identical, non-native logic: the field storage and all trivial accessors, the command-line/argument/environment/bitness memoization, thepsstate mapping, thecpusetaffinity lookup, and the/proc/<pid>/limitsfallback.FreeBsdOSProcess,OpenBsdOSProcess,DragonFlyBsdOSProcess) now extend it and keep only theirPsThreadColumnsenum; the JNA/FFM concretes keep only the platform-specific work — the nativesysctlargument/environment/bitness reads,getrlimit,procstat/fstatcwd/open-file lookups,psthread enumeration, and thepsattribute parsing.NetBsdOSProcess(a single no-native class) folds in too: its argument/environment/bitness hooks are pure Java, and it overridesgetAffinityMaskfor itsschedctl-based lookup.The JNA/FFM concretes drop from ~458 to ~200-250 lines each; NetBSD from 402 to 214. The shared
updateAttributesis left per-platform because it reads each OS's ownPsKeywordsenum (which lives in the operating-system classes, not shared).Incidental fix
Removes a dead, unreachable
this.threadCount = 1;in the OpenBSD JNAupdateThreadCount()that unconditionally overwrote the computedpsthread 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