Skip to content

Unify BSD OSThread parsing into a shared BsdOSThread base - #3374

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-osthread-dedup
Jun 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-osthread-dedup

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Builds on the BSD OSProcess dedup (#3370/#3372) by applying the same pattern one level down to the BSD OSThread implementations. The FreeBSD, OpenBSD, DragonFly, and NetBSD OSThread classes 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.

  • A single BsdPsThreadKeyword master enum (union of all BSD ps thread columns).
  • A BsdOSThread base holding the field storage, the trivial accessors, the single-thread refresh, and one shared updateAttributes(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+time vs cputime vs time), thread address (tdaddr or none), and the missing elapsed column on DragonFly. State mapping reuses BsdOSProcess.getStateFromOutput.
  • Each platform's OSThread declares its ordered PS_THREAD_KEYWORDS subset (the row-complete sentinel is the last column) plus 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 (two constructors + two hooks). Net −245 lines. Behavior is unchanged — the per-platform column lists and ps invocations are identical to before.

The thread column constants live on the OSThread class (not the OSProcess base) to avoid a static-import name collision between BsdPsKeyword and BsdPsThreadKeyword, which share many constant names.

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

  • Refactor
    • Unified thread management logic across BSD operating system implementations for improved code maintainability and consistency.

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).
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR consolidates BSD thread parsing across four BSD platforms by introducing a unified BsdOSThread base class and BsdPsThreadKeyword enum, then refactoring each platform-specific thread class to extend the base and delegate attribute parsing to shared logic.

Changes

BSD thread parsing consolidation

Layer / File(s) Summary
BSD thread keyword enum and base class
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdPsThreadKeyword.java, oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java, config/checkstyle-suppressions.xml
BsdPsThreadKeyword enum defines all BSD ps thread column identifiers. BsdOSThread abstract class centralizes thread attribute fields, public getters, and attribute update logic that parses ps output via a protected map-based updater and executes subclass-provided ps commands to refresh thread rows. Checkstyle suppression is broadened to cover the new base class.
DragonFlyBSD thread implementation and integration
oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.java, oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
DragonFlyBsdOSThread extends BsdOSThread and provides DragonFly-specific thread column constants (PS_THREAD_KEYWORDS, PS_THREAD_COLUMNS) via override methods. DragonFlyBsdOSProcess.getThreadDetails() is updated to use the thread class's column definitions and BsdPsThreadKeyword for parsing.
FreeBSD thread implementation and integration
oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.java, oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
FreeBsdOSThread extends BsdOSThread with FreeBSD-specific column configuration. FreeBsdOSProcess.getThreadDetails() is refactored to construct the ps command from and parse output using the thread class's PS_THREAD_COLUMNS and BsdPsThreadKeyword-based mapping.
NetBSD thread implementation and integration
oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.java, oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java
NetBsdOSThread extends BsdOSThread and provides NetBSD-specific thread column definitions. NetBsdOSProcess.getThreadDetails() uses the thread class's keyword/column constants for ps command construction and per-thread parsing.
OpenBSD thread implementation and integration
oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSThread.java, oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java
OpenBsdOSThread extends BsdOSThread with OpenBSD-specific thread column configuration. OpenBsdOSProcess.getThreadDetails() is updated to use BsdPsThreadKeyword and the thread class's column definitions instead of process-local enums.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • oshi/oshi#3316: Earlier FreeBSD thread column setup changes that establish the foundation for this broader BSD thread parsing consolidation across all BSD platforms.

Poem

🐰 BSD threads now share a burrow—
One warren for parsing ps rows!
From four platforms, logic flows
Into shared code that gently grows.
Duplication melts away today,
As BsdOSThread shows the way! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.11% 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 accurately summarizes the main objective: unifying BSD OSThread parsing logic across multiple platform implementations into a shared BsdOSThread base class, which is the core purpose of this refactoring.
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 78.12500% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.01%. Comparing base (68e6811) to head (7a92594).

Files with missing lines Patch % Lines
.../oshi/software/common/os/unix/bsd/BsdOSThread.java 70.42% 9 Missing and 12 partials ⚠️
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     
Flag Coverage Δ
freebsd 26.26% <61.45%> (+0.06%) ⬆️
linux 30.01% <0.00%> (+0.03%) ⬆️
macos 30.70% <0.00%> (+0.03%) ⬆️
openbsd 25.68% <58.33%> (+0.04%) ⬆️
solaris 27.11% <0.00%> (+0.01%) ⬆️
windows 38.55% <0.00%> (+0.01%) ⬆️

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-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java (1)

131-131: 💤 Low value

Potential NPE or StringIndexOutOfBoundsException on STATE lookup.

If threadMap.get(BsdPsThreadKeyword.STATE) returns null (e.g., malformed ps output missing the STATE column despite row-completeness passing) or an empty string, calling .charAt(0) will throw. While valid ps output 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

📥 Commits

Reviewing files that changed from the base of the PR and between 68e6811 and 7a92594.

📒 Files selected for processing (11)
  • config/checkstyle-suppressions.xml
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdPsThreadKeyword.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdOSProcess.java
  • oshi-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.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.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/FreeBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSThread.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
  • oshi-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!

@dbwiddis
dbwiddis merged commit 5f860c2 into oshi:master Jun 12, 2026
22 checks passed
@dbwiddis
dbwiddis deleted the bsd-osthread-dedup branch June 12, 2026 01:07
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