Skip to content

Extract OpenBSD/NetBSD CentralProcessor common bases (BSD dedup) - #3387

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:openbsd-netbsd-cp-common-base
Jun 13, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:openbsd-netbsd-cp-common-base

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add a cross-BSD BsdCentralProcessor base (oshi.hardware.common.platform.unix) holding the shared dmesg-based processor/cache enumeration (initProcessorCounts, parseCacheStr) and the vmstat-backed context-switch/interrupt counters, with an abstract int sysctl(name, def) hook and an abstract queryVmStats()
  • Add an OpenBsdCentralProcessor base extending it with the shared processor identification, frequency, and OpenBSD vmstat parsing; abstract sysctl overloads let the JNA and FFM subclasses supply their backend
  • OpenBsdCentralProcessorJNA/FFM now contain only sysctl delegation plus the native CPU-tick readers and getloadavg; NetBsdCentralProcessor extends the cross-BSD base and keeps its command/text-based identification, ticks, and load average
  • Eliminates ~180 duplicated lines across the three implementations
  • Fixes a pre-existing NetBSD bug: initProcessorCounts split the dmesg cache line on regex group(1) (the cpu number) instead of group(2) (the cache description), so dmesg-derived cache detection never produced any caches; the shared base uses the correct group(2)

Test plan

  • All pre-commit checks pass locally (spotless, checkstyle, forbiddenapis, javadoc) plus a clean full-reactor build
  • Unix CI green: OpenBSD, NetBSD, FreeBSD, DragonFly BSD, OpenIndiana

Summary by CodeRabbit

  • Refactor
    • Consolidated BSD processor logic into a shared base to unify OpenBSD/NetBSD behavior.
    • Moved platform-specific sysctl/back-end pieces into small adapters, simplifying implementations.
    • Improved CPU detection and reporting: topology, cache discovery, frequency, and vmstat-derived counters (context switches, interrupts) are now handled consistently.

The OpenBSD JNA and FFM CentralProcessors were identical except for the
sysctl backend and the native CPU-tick/load-average calls, and the
dmesg-based topology/cache parsing was duplicated again in NetBSD.

Add a cross-BSD BsdCentralProcessor base (oshi.hardware.common.platform.unix)
holding the shared dmesg processor/cache enumeration, vmstat-backed context
switch/interrupt counters, and an abstract int sysctl(name, def) hook plus an
abstract queryVmStats(). NetBsdCentralProcessor and a new OpenBsdCentralProcessor
extend it; OpenBsdCentralProcessor adds the shared processor identification,
frequency, and OpenBSD vmstat parsing with abstract sysctl hooks, leaving only
the native tick readers and getloadavg in the JNA and FFM subclasses.

This fixes a pre-existing NetBSD bug: its initProcessorCounts split the dmesg
cache line on regex group(1) (the cpu number) instead of group(2) (the cache
description), so dmesg-derived cache detection never produced any caches; the
shared base uses the correct group(2).
@coderabbitai

coderabbitai Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6c8cec2d-4333-4929-8497-94e056304e34

📥 Commits

Reviewing files that changed from the base of the PR and between c3c98c7 and 271437f.

📒 Files selected for processing (1)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
📜 Recent 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). (18)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Analyze (java)

📝 Walkthrough

Walkthrough

This PR extracts shared BSD dmesg-based CPU topology, cache, and VM-stat logic into a new BsdCentralProcessor base, adds an OpenBsdCentralProcessor for OpenBSD sysctl-based queries, and refactors NetBSD/OpenBSD adapters to extend the new hierarchy and provide platform sysctl bridges.

Changes

BSD Central Processor Hierarchy Refactoring

Layer / File(s) Summary
Shared BSD CPU topology and cache parsing
oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdCentralProcessor.java
New BsdCentralProcessor abstract class memoizes VM statistics and centralizes dmesg parsing to extract CPU core/package counts, build logical and optional physical processors, parse cache descriptors into ProcessorCache objects via parseCacheStr, and collect feature flags. Implements queryContextSwitches() and queryInterrupts() from memoized VM stats and declares sysctl and queryVmStats hooks.
OpenBSD-specific CPU identification and frequency queries
oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
New OpenBsdCentralProcessor abstract class extends BsdCentralProcessor and implements OpenBSD-specific overrides: queryProcessorId() reads CPU strings and CPUID from sysctl and parses family/model/stepping; queryCurrentFreq() reads HW_CPUSPEED and converts to Hz; queryVmStats() executes vmstat -s and returns context-switch and interrupt counters. Declares abstract sysctl read methods for subclasses.
NetBSD platform adapter
oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
Refactors to extend BsdCentralProcessor. Adds sysctl(String name, int def) override delegating to NetBsdSysctlUtil and changes queryVmStats visibility from private static to protected instance to integrate with the base memoization.
OpenBSD platform adapters
oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorFFM.java, oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorJNA.java
Refactor both classes to extend OpenBsdCentralProcessor. Each provides sysctl method overloads (String/int, String/String, int[]/String, int[]/int) delegating to their platform sysctl utilities, removing duplicated CPU identification, topology parsing, and vmstat logic and leaving thin adapter implementations.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • oshi/oshi#3334: Introduces OpenBsdSysctlUtilFFM, which OpenBsdCentralProcessorFFM now delegates its sysctl overloads to in this PR.
  • oshi/oshi#3307: Earlier NetBSD CPU implementation with dmesg/vmstat/sysctl logic that this PR refactors to extend the shared BsdCentralProcessor base.
  • oshi/oshi#3310: Related changes to NetBsdSysctlUtil and NetBSD backend adjustments that align with the platform adapter refactors here.

"🐰 I parsed dmesg with delight,
caches, cores, and flags in sight,
one base to share the load,
adapters bridge the sysctl road,
hop—code tidy, systems bright!"

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.92% 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 reflects the main objective of extracting common OpenBSD/NetBSD processor logic into shared base classes to reduce duplication across BSD 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 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.27451% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.83%. Comparing base (34e28e1) to head (271437f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ware/common/platform/unix/BsdCentralProcessor.java 80.00% 5 Missing and 6 partials ⚠️
...platform/unix/openbsd/OpenBsdCentralProcessor.java 91.89% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3387      +/-   ##
============================================
- Coverage     68.93%   68.83%   -0.11%     
  Complexity       76       76              
============================================
  Files           574      576       +2     
  Lines         21622    21541      -81     
  Branches       3580     3560      -20     
============================================
- Hits          14906    14827      -79     
+ Misses         4888     4884       -4     
- Partials       1828     1830       +2     
Flag Coverage Δ
freebsd 26.24% <0.00%> (+0.10%) ⬆️
linux 30.76% <0.00%> (+0.13%) ⬆️
macos 31.32% <0.00%> (+0.11%) ⬆️
openbsd 25.66% <86.27%> (-0.22%) ⬇️
windows 39.48% <0.00%> (+0.09%) ⬆️

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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In
`@oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java`:
- Around line 104-109: The loop in OpenBsdCentralProcessor (where
contextSwitches and interrupts are set) currently uses
ParseUtil.getFirstIntValue(line) which can truncate large counters; update this
to use ParseUtil.parseLongOrDefault(line, 0L) (or the equivalent long-parsing
helper used in NetBsdCentralProcessor.queryVmStats) and ensure the
contextSwitches and interrupts variables are long-typed (or cast/assigned
safely) so large values are preserved; modify the assignments in the same loop
that checks line.endsWith("cpu context switches") and
line.endsWith("interrupts") to use the long parser for consistency with
NetBsdCentralProcessor.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 18f9a08c-1fff-43b2-b5db-f6498e8f0444

📥 Commits

Reviewing files that changed from the base of the PR and between 34e28e1 and c3c98c7.

📒 Files selected for processing (5)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorJNA.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: JMH Benchmarks, macos-latest
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (3)
📚 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/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdCentralProcessor.java
📚 Learning: 2026-04-12T19:34:56.879Z
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:34:56.879Z
Learning: In OSHI’s Java platform implementations, `PhysicalProcessor.getEfficiency()` / `EfficiencyClass` follow the same convention as Windows `PROCESSOR_RELATIONSHIP.EfficiencyClass`: a *higher* numeric efficiency class value represents *greater performance* and *less efficiency* (typically P-cores higher than E-cores). When reviewing, do not treat cases where P-cores are assigned higher efficiency/class values as a bug—this is intentional cross-platform consistency.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdCentralProcessor.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/hardware/platform/unix/openbsd/OpenBsdCentralProcessorJNA.java
🔇 Additional comments (5)
oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdCentralProcessor.java (1)

1-185: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/openbsd/OpenBsdCentralProcessor.java (1)

1-55: LGTM!

Also applies to: 56-79, 81-97

oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java (1)

1-233: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorFFM.java (1)

1-117: LGTM!

oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdCentralProcessorJNA.java (1)

1-158: LGTM!

OpenBsdCentralProcessor.queryVmStats parsed the vmstat context-switch and
interrupt counters with ParseUtil.getFirstIntValue, which returns an int and
truncates values above ~2.1 billion that long-uptime systems routinely exceed.
Use ParseUtil.parseLongOrDefault on the leading token instead, matching
NetBsdCentralProcessor. This was pre-existing in both the JNA and FFM OpenBSD
implementations; consolidation fixes both at once.
@dbwiddis
dbwiddis merged commit a2f2dc4 into oshi:master Jun 13, 2026
21 of 22 checks passed
@dbwiddis
dbwiddis deleted the openbsd-netbsd-cp-common-base branch June 13, 2026 04:05
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