Repository navigation
Unify BSD OSThread parsing into a shared BsdOSThread base - #3374
Conversation
The FreeBSD, OpenBSD, DragonFly, and NetBSD OSThread implementations each duplicated the same ~175-line body: identical field storage and accessors, an identical ps STATE-char mapping, and a nearly identical updateAttributes(map) that parsed per-platform ps thread columns. Mirror the OSProcess dedup (BsdOSProcess/BsdPsKeyword) one level down: - Add a single BsdPsThreadKeyword master enum (union of all BSD ps thread columns) and a BsdOSThread base holding the field storage, accessors, the shared single-thread refresh, and one shared updateAttributes(map). Per-platform differences (thread-id keyword lwp/tid/lid, name tdname/args, systime vs cputime vs time, thread address, missing elapsed column on DragonFly) are handled by checking which keys are present. State mapping reuses BsdOSProcess.getStateFromOutput. - Each platform's OSThread declares its ordered PS_THREAD_KEYWORDS subset (the row-complete sentinel is the last column) and the ps command; the OSProcess base getThreadDetails references those and parses with the list-driven ParseUtil.stringToEnumMap. The per-platform PsThreadColumns enums are removed. The OSThread subclasses shrink from ~175 lines to ~20 (constructors + two hooks).
📝 WalkthroughWalkthroughThis PR consolidates BSD thread parsing across four BSD platforms by introducing a unified ChangesBSD thread parsing consolidation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 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 #3374 +/- ##
============================================
- Coverage 75.06% 75.01% -0.06%
Complexity 76 76
============================================
Files 566 568 +2
Lines 22094 22071 -23
Branches 3658 3662 +4
============================================
- Hits 16585 16556 -29
+ Misses 3451 3447 -4
- Partials 2058 2068 +10
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.
🧹 Nitpick comments (1)
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java (1)
131-131: 💤 Low valuePotential NPE or StringIndexOutOfBoundsException on STATE lookup.
If
threadMap.get(BsdPsThreadKeyword.STATE)returnsnull(e.g., malformedpsoutput missing the STATE column despite row-completeness passing) or an empty string, calling.charAt(0)will throw. While validpsoutput should always include STATE, adding a defensive check would improve robustness.🛡️ Proposed defensive fix
- this.state = BsdOSProcess.getStateFromOutput(threadMap.get(BsdPsThreadKeyword.STATE).charAt(0)); + String stateStr = threadMap.get(BsdPsThreadKeyword.STATE); + this.state = (stateStr != null && !stateStr.isEmpty()) + ? BsdOSProcess.getStateFromOutput(stateStr.charAt(0)) + : OSProcess.State.OTHER;🤖 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-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java` at line 131, In BsdOSThread, guard the STATE lookup before calling charAt(0): check threadMap.get(BsdPsThreadKeyword.STATE) for null or empty and, if so, supply a safe fallback (e.g., a default state character or call BsdOSProcess.getStateFromOutput with a safe single-char string) instead of invoking .charAt(0) on a null/empty string; update the assignment that calls BsdOSProcess.getStateFromOutput(...) to use this validated/fallback value to avoid NPE or StringIndexOutOfBoundsException.
🤖 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-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java`:
- Line 131: In BsdOSThread, guard the STATE lookup before calling charAt(0):
check threadMap.get(BsdPsThreadKeyword.STATE) for null or empty and, if so,
supply a safe fallback (e.g., a default state character or call
BsdOSProcess.getStateFromOutput with a safe single-char string) instead of
invoking .charAt(0) on a null/empty string; update the assignment that calls
BsdOSProcess.getStateFromOutput(...) to use this validated/fallback value to
avoid NPE or StringIndexOutOfBoundsException.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: d20e14a2-f30d-42ce-b0e3-146d91417a7d
📒 Files selected for processing (11)
config/checkstyle-suppressions.xmloshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdPsThreadKeyword.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSThread.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). (15)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 26, macos-latest
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Analyze (java)
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, windows-latest
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
🧰 Additional context used
🧠 Learnings (1)
📚 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/bsd/BsdPsThreadKeyword.javaoshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.javaoshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSThread.java
🔇 Additional comments (11)
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdPsThreadKeyword.java (1)
1-16: LGTM!config/checkstyle-suppressions.xml (1)
65-68: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java (1)
36-36: LGTM!Also applies to: 91-104
oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java (1)
39-39: LGTM!Also applies to: 71-84
oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java (1)
37-37: LGTM!Also applies to: 79-92
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java (1)
1-42: LGTM!Also applies to: 43-101, 103-119, 128-171, 173-207
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.java (1)
7-62: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.java (1)
7-67: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.java (1)
7-66: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java (1)
38-38: LGTM!Also applies to: 164-174
oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSThread.java (1)
7-65: LGTM!
Summary
Builds on the BSD
OSProcessdedup (#3370/#3372) by applying the same pattern one level down to the BSDOSThreadimplementations. The FreeBSD, OpenBSD, DragonFly, and NetBSDOSThreadclasses each duplicated the same ~175-line body: identical field storage and accessors, an identicalpsSTATE-char mapping, and a nearly identicalupdateAttributes(map)that parsed per-platformpsthread columns.BsdPsThreadKeywordmaster enum (union of all BSDpsthread columns).BsdOSThreadbase holding the field storage, the trivial accessors, the single-thread refresh, and one sharedupdateAttributes(map). Per-platform differences are handled by checking which keys are present: thread-id keyword (lwp/tid/lid), name (tdname/args/absent on DragonFly), kernel/user time (systime+timevscputimevstime), thread address (tdaddror none), and the missing elapsed column on DragonFly. State mapping reusesBsdOSProcess.getStateFromOutput.OSThreaddeclares its orderedPS_THREAD_KEYWORDSsubset (the row-complete sentinel is the last column) plus thepscommand; theOSProcessbasegetThreadDetails()references those and parses with the list-drivenParseUtil.stringToEnumMap. The per-platformPsThreadColumnsenums are removed.The
OSThreadsubclasses shrink from ~175 lines to ~20 (two constructors + two hooks). Net −245 lines. Behavior is unchanged — the per-platform column lists andpsinvocations are identical to before.The thread column constants live on the
OSThreadclass (not theOSProcessbase) to avoid a static-import name collision betweenBsdPsKeywordandBsdPsThreadKeyword, which share many constant names.Testing
All five
unix.yamlVM 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