Skip to content

Extract FreeBsdCentralProcessor common base (JNA/FFM dedup) - #3380

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:extract-freebsd-centralprocessor-common
Jun 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:extract-freebsd-centralprocessor-common

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Move shared processor logic (queryProcessorId, initProcessorCounts, topology parsing, cache info from lscpu, frequency queries, getProcessorIDfromDmiDecode) into the FreeBsdCentralProcessor superclass
  • Abstract sysctlString/sysctlLong methods let JNA and FFM subclasses provide their sysctl backend
  • JNA/FFM subclasses now only contain native-specific code: CPU load ticks, load average, context switches, and interrupts
  • Eliminates ~189 duplicated lines between the two implementations

Test plan

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

Summary by CodeRabbit

  • Improvements
    • Expanded and unified FreeBSD CPU detection for more accurate processor identification and reporting.
    • Improved frequency querying with stronger fallbacks for current and max clock speeds.
    • Enhanced CPU topology and cache discovery for more reliable logical/physical core and cache size reporting across FreeBSD builds.

@coderabbitai

coderabbitai Bot commented Jun 12, 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: ac069087-d376-4c98-ab0b-dd788dbb0ebd

📥 Commits

Reviewing files that changed from the base of the PR and between 21daff1 and 722e144.

📒 Files selected for processing (3)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorJNA.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorJNA.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). (15)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Analyze (java)
🔇 Additional comments (2)
oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorFFM.java (2)

42-50: LGTM!


52-120: LGTM!


📝 Walkthrough

Walkthrough

This PR refactors FreeBSD CPU processor support by centralizing shared parsing and query logic into the base FreeBsdCentralProcessor class and removing duplicate implementations from the FFM and JNA subclasses. The base class gains processor ID discovery from dmesg, topology/cache parsing from sysctl and lscpu, and frequency queries, while subclasses now only implement platform-specific sysctl accessors that delegate to their respective utility classes.

Changes

FreeBSD CPU Implementation Refactoring

Layer / File(s) Summary
Base class structure with abstract sysctl accessors
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
FreBsdCentralProcessor class header, CPUMASK regex pattern, and abstract sysctl accessor method signatures for string and long value reads.
Processor identification via dmesg and dmidecode
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
queryProcessorId() parses /var/run/dmesg.boot for vendor/model/stepping and feature flags, determines 64-bit capability, derives frequency from hw.clockrate, and implements dmidecode-based processor ID lookup with hex formatting fallback.
Processor topology and cache initialization
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
initProcessorCounts() orchestrates topology parsing from kern.sched.topology_spec (including CSV hex mask support) and logical/physical mapping via matchBitmasks()/getMatchingBitmask(); getCacheInfoFromLscpu() extracts L1d/L1i/L2/L3 cache sizes.
CPU frequency and max frequency queries
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
queryCurrentFreq() reads dev.cpu.0.freq (MHz→Hz) with fallback to machdep.tsc_freq; queryMaxFreq() parses dev.cpu.0.freq_levels pairs to select max MHz (→Hz) with fallback.
FreeBsdCentralProcessorFFM refactoring: remove implementations, delegate sysctl
oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorFFM.java
FFM subclass refactored to remove CPUMASK constant, CPU parsing/topology/cache/frequency implementations, and getProcessorIDfromDmiDecode helper; new sysctlString and sysctlLong overrides delegate to BsdSysctlUtilFFM.
FreeBsdCentralProcessorJNA refactoring: remove implementations, delegate sysctl
oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorJNA.java
JNA subclass refactored to remove CPUMASK constant, CPU parsing/topology/cache/frequency implementations, and getProcessorIDfromDmiDecode helper; new sysctlString and sysctlLong overrides delegate to BsdSysctlUtil.

Sequence Diagram

sequenceDiagram
  participant Caller
  participant FreeBsdCentralProcessor
  participant Subclass as FFM/JNA Subclass
  participant BsdSysctlUtil as BsdSysctlUtil(FFM/JNA)

  Caller->>FreeBsdCentralProcessor: queryProcessorId()
  FreeBsdCentralProcessor->>Subclass: sysctlString("hw.model", "")
  Subclass->>BsdSysctlUtil: sysctl(...)
  BsdSysctlUtil-->>Subclass: sysctl result
  Subclass-->>FreeBsdCentralProcessor: sysctl result
  FreeBsdCentralProcessor-->>Caller: ProcessorIdentifier

  Caller->>FreeBsdCentralProcessor: initProcessorCounts()
  FreeBsdCentralProcessor->>Subclass: sysctlString("kern.sched.topology_spec", "")
  Subclass->>BsdSysctlUtil: sysctl(...)
  BsdSysctlUtil-->>Subclass: topology XML/text
  Subclass-->>FreeBsdCentralProcessor: topology XML/text
  FreeBsdCentralProcessor->>FreeBsdCentralProcessor: parseTopology(), matchBitmasks()
  FreeBsdCentralProcessor-->>Caller: Logical/Physical processors, caches

  Caller->>FreeBsdCentralProcessor: queryCurrentFreq()
  FreeBsdCentralProcessor->>Subclass: sysctlLong("dev.cpu.0.freq", 0)
  Subclass->>BsdSysctlUtil: sysctl(...)
  BsdSysctlUtil-->>Subclass: frequency MHz
  Subclass-->>FreeBsdCentralProcessor: frequency MHz
  FreeBsdCentralProcessor-->>Caller: frequency Hz
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • oshi/oshi#3322: Adds FreeBSD CPU parsing logic previously implemented in FFM/JNA subclasses that this PR centralizes into the base class.
  • oshi/oshi#3319: Introduces BsdSysctlUtilFFM and related sysctl delegation used by the FFM subclass changes here.
  • oshi/oshi#3303: DragonFlyBsdCentralProcessor extends FreeBsdCentralProcessor; this PR’s base-class changes affect that implementation.

Poem

🐰 I dug through dmesg and found a name,
Masks and caches played a topology game,
Subclasses now call up through one shared root,
Sysctl whispers answers in a tidy route,
Hopping through bits, I cheer this single truth.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.04% 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 change: extracting shared FreeBsdCentralProcessor logic to eliminate JNA/FFM code duplication.
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 48.64865% with 76 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.02%. Comparing base (f38714a) to head (722e144).

Files with missing lines Patch % Lines
...platform/unix/freebsd/FreeBsdCentralProcessor.java 47.22% 63 Missing and 13 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3380      +/-   ##
============================================
+ Coverage     74.86%   75.02%   +0.15%     
  Complexity       76       76              
============================================
  Files           573      573              
  Lines         21909    21773     -136     
  Branches       3641     3607      -34     
============================================
- Hits          16402    16335      -67     
+ Misses         3455     3394      -61     
+ Partials       2052     2044       -8     
Flag Coverage Δ
freebsd 26.12% <48.64%> (-0.14%) ⬇️
linux 30.42% <0.00%> (+0.17%) ⬆️
macos 30.97% <0.00%> (+0.19%) ⬆️
openbsd 25.84% <0.00%> (+0.15%) ⬆️
solaris 27.50% <0.00%> (+0.17%) ⬆️
windows 39.05% <0.00%> (+0.22%) ⬆️

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/freebsd/FreeBsdCentralProcessor.java`:
- Around line 306-313: In the loop inside FreeBsdCentralProcessor that iterates
ExecutingCommand.runNative("dmidecode -t processor"), guard against
ArrayIndexOutOfBounds when splitting on "ID:" by checking the split result (or
using indexOf/substring) before accessing the second element: when procInfo &&
checkLine.contains(marker) compute the substring after the marker, verify it
exists and .trim() is non-empty, and only then return it; otherwise continue the
loop (or skip empty values) to avoid accessing parts[1] when the marker is at
the end of the line.
🪄 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: 514cd090-def9-4b70-a94a-6549dc473f0a

📥 Commits

Reviewing files that changed from the base of the PR and between f38714a and 21daff1.

📒 Files selected for processing (3)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorJNA.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, macos-latest (+ 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, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ 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/freebsd/FreeBsdCentralProcessor.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/freebsd/FreeBsdCentralProcessor.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/freebsd/FreeBsdCentralProcessorJNA.java
🔇 Additional comments (11)
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java (7)

1-31: LGTM!


33-49: LGTM!


51-99: LGTM!


101-142: LGTM!


144-162: LGTM!


164-261: LGTM!


263-294: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorFFM.java (2)

42-50: LGTM!


52-120: LGTM!

oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdCentralProcessorJNA.java (2)

37-45: LGTM!


47-129: LGTM!

Move shared logic (queryProcessorId, initProcessorCounts, topology
parsing, cache info, frequency queries, dmidecode lookup) into the
FreeBsdCentralProcessor superclass with abstract sysctlString/Long
methods. JNA and FFM subclasses now only contain native-specific
code (cpu ticks, load average, context switches, interrupts),
eliminating ~189 duplicated lines.
@dbwiddis
dbwiddis force-pushed the extract-freebsd-centralprocessor-common branch from 21daff1 to 722e144 Compare June 12, 2026 16:56
@dbwiddis
dbwiddis merged commit c96b9f9 into oshi:master Jun 12, 2026
22 checks passed
@dbwiddis
dbwiddis deleted the extract-freebsd-centralprocessor-common branch June 12, 2026 17:04
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