Skip to content

FreeBSD Port: FFM libc bindings + sysctl util + WhoFFM driver - #3319

Merged
dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:freebsd-ffm
Jun 3, 2026
Merged

dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:freebsd-ffm

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Phase 3 of the FreeBSD FFM port (tracking #3315). Bottom-up layer:
introduce the lowest FFM bindings (sysctlbyname, getloadavg,
utmpx family) and the first sysctl utility and session driver
built on them. Lays the foundation Phase 4+ concretes need without
exposing any half-built SystemInfo surface — FFM SystemInfo still
reports FreeBSD as unsupported until Phase 6 wires the HAL/OS in.

Per-commit walk

  1. Phase 3 commit 1 — oshi.ffm.unix.freebsd.FreeBsdLibcFunctions
    with errno-captured sysctlbyname + getloadavg. New
    oshi.ffm.util.platform.unix.freebsd.BsdSysctlUtilFFM mirrors
    oshi.util.platform.unix.freebsd.BsdSysctlUtil 1:1
    (int/long/String/struct/raw-buffer variants), so per-class FFM
    concretes can be dropped in without changing call sites.
    module-info exports the new util package alongside
    oshi.ffm.util.platform.{mac,windows}.
  2. Phase 3 commit 2 — Adds the utmpx surface to
    FreeBsdLibcFunctions: setutxent/getutxent/endutxent,
    a 276-byte UTMPX_LAYOUT matching FreeBSD's struct utmpx
    (short ut_type + Timeval ut_tv + ut_id[8] + ut_pid +
    ut_user[32] + ut_line[16] + ut_host[128] + __ut_spare[64]),
    plus primitive/string accessors. New
    oshi.driver.unix.freebsd.WhoFFM mirrors the JNA Who driver
    one-for-one — walks getutxent, filters USER_PROCESS /
    LOGIN_PROCESS, falls back to
    oshi.util.driver.unix.Who.queryWho() on errors, same as the
    oshi.driver.{mac,linux}.WhoFFM siblings.
  3. Phase 3 commit 3 — Test alignment with the established Mac
    pattern. Adds BsdSysctlUtilTest in oshi-core mirroring
    oshi.util.platform.mac.SysctlUtilTest (smoke-tests
    default-on-failure paths for int/long/String) — this closes a
    pre-existing JNA-side coverage gap where the Mac mirror had a test
    but the FreeBSD util didn't. Adds BsdSysctlUtilFFMTest in
    oshi-core-ffm following the same shape; add-opens the new test
    package in module-info.test.

Intentionally deferred

  • FFM SystemInfo factory wiring — kept for Phase 6 so
    isAvailable() doesn't return true on FreeBSD while concretes are
    still being built. NativeComparisonTest stays gated to
    {LINUX, MAC, WINDOWS}.
  • Other libc bindings (sysctl int-MIB form, getpid,
    getrlimit, thr_self) — wait for their first FFM consumer
    in Phase 5 rather than landing untested-on-FreeBSD.
  • WhoFFM and getloadavg dedicated tests — both will be
    covered transitively in Phase 6 via NativeComparisonTest.sessions()
    and .processorLoadAverage() once HAL/OS wiring lands. No
    comparison-test scaffolding committed.

Test plan

  • ./mvnw clean test -Paggregate-coverage locally (Mac, JDK 25)
  • ./mvnw spotless:check checkstyle:check clean
  • Temporary FreeBSD CI green on all 3 commits in succession
  • BsdSysctlUtilTest (oshi-core, 3 tests) and
    BsdSysctlUtilFFMTest (oshi-core-ffm, 3 tests) both run and
    pass on the FreeBSD VM
  • PR CI: existing testfreebsd job in pr.yaml will exercise
    the new tests under flags: freebsd

Tracks #3315.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Improved FreeBSD support: native FFM-based bindings for system calls and sysctl access.
    • Publicly exposed FreeBSD utilities for broader use.
    • Added FreeBSD user-session querying using the new native bindings.
  • Tests

    • Added FreeBSD-targeted tests ensuring sysctl lookups return sensible defaults and validating session-query behavior.

dbwiddis and others added 2 commits June 2, 2026 17:17
Lowest-level FFM bindings for the FreeBSD port: sysctlbyname and
getloadavg in FreeBsdLibcFunctions, and a BsdSysctlUtilFFM that mirrors
the JNA BsdSysctlUtil API one-for-one so per-class FFM concretes can be
dropped in without changing call sites.

Bottom-up ordering: bind libc and the sysctl util first, then build up
FFM concretes against them (Phase 4), defer SystemInfo factory wiring
to Phase 6 so we never expose a half-built "available" state.

Module-info exports the new oshi.ffm.util.platform.unix.freebsd package
to match the established oshi.ffm.util.platform.{mac,windows} pattern.

Parity test (oshi-benchmark BsdSysctlComparisonTest, @EnabledOnOs FREEBSD)
runs each BsdSysctlUtil method (int, long, String, struct, raw buffer,
unknown-MIB) through both implementations and asserts equality. Skipped
on Mac/Linux/Windows; exercised on the FreeBSD CI workflow.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Continues bottom-up Phase 3 work without leaving testable surface on
the floor:

* Extends FreeBsdLibcFunctions with the utmpx surface — setutxent /
  getutxent / endutxent MethodHandles plus UTMPX_LAYOUT matching
  FreeBSD's struct utmpx (276 bytes: short ut_type + Timeval{tv_sec,
  tv_usec} + ut_id[8] + ut_pid + ut_user[32] + ut_line[16] +
  ut_host[128] + __ut_spare[64]) and primitive/string accessors that
  mirror the Linux pattern.

* Adds oshi.driver.unix.freebsd.WhoFFM mirroring the JNA Who driver
  one-for-one — walks getutxent, filters USER_PROCESS / LOGIN_PROCESS,
  returns List<OSSession>. Falls back to the oshi.util.driver.unix.Who
  command-line implementation on invalid sessions or FFM errors, same
  as the mac/linux WhoFFM siblings.

* Adds two new FreeBSD-only parity tests:
  - WhoComparisonTest — asserts JNA Who.queryUtxent() and FFM
    WhoFFM.queryUtxent() return identical session sets (user, device,
    host, loginTime) on FreeBSD.
  - FreeBsdLibcComparisonTest.getloadavg — closes out the gap where
    Phase 3 commit 1 bound getloadavg but never tested it.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 3, 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: 85ca36b3-26d2-48dd-8786-434b68795931

📥 Commits

Reviewing files that changed from the base of the PR and between c65c2f2 and c398f6c.

📒 Files selected for processing (5)
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFM.java
  • oshi-core-ffm/src/test/java/module-info.test
  • oshi-core-ffm/src/test/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFMTest.java
  • oshi-core/src/test/java/oshi/util/platform/unix/freebsd/BsdSysctlUtilTest.java
🚧 Files skipped from review as they are similar to previous changes (5)
  • oshi-core-ffm/src/test/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFMTest.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
  • oshi-core/src/test/java/oshi/util/platform/unix/freebsd/BsdSysctlUtilTest.java
  • oshi-core-ffm/src/test/java/module-info.test
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFM.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). (14)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • 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 26, ubuntu-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)

📝 Walkthrough

Walkthrough

This PR adds FreeBSD FFM bindings (sysctlbyname, getloadavg, utmpx API), a BsdSysctlUtilFFM sysctl wrapper, an FFM-backed utmpx-based session query (WhoFFM), module export and test wiring, and FreeBSD-gated tests verifying default-fallback behavior.

Changes

FreeBSD FFM System Integration

Layer / File(s) Summary
FFM bindings and libc memory contracts
oshi-core-ffm/src/main/java/oshi/ffm/unix/freebsd/FreeBsdLibcFunctions.java, oshi-core-ffm/src/main/java/oshi/ffm/unix/freebsd/package-info.java
Defines FFM memory layouts, VarHandles, and MethodHandles for FreeBSD libc: sysctlbyname, getloadavg, and the utmpx iteration API (setutxent, getutxent, endutxent). Public wrappers invoke native functions with errno capture; helper accessors read utmpx fields and normalize pointers.
Sysctl wrapper utilities
oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFM.java, oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/freebsd/package-info.java, oshi-core-ffm/src/main/java/module-info.java
Implements BsdSysctlUtilFFM overloads to query sysctl by name as int, long, String, struct, or buffer. Each allocates within a confined arena, invokes native sysctlbyname, logs failures with errno, returns provided defaults on error, and the module now exports the util package.
User session query using FFM
oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
Iterates FreeBSD utmpx records via getutxent(), filters for USER_PROCESS and LOGIN_PROCESS, extracts user/host/login fields, validates sessions, and constructs OSSession objects. Falls back to Who.queryWho() on invalid session or exception.
Module test access and tests
oshi-core-ffm/src/test/java/module-info.test, oshi-core-ffm/src/test/java/oshi/ffm/util/platform/unix/freebsd/BsdSysctlUtilFFMTest.java, oshi-core/src/test/java/oshi/util/platform/unix/freebsd/BsdSysctlUtilTest.java
Opens the new FreeBSD utility package to the test framework and adds FreeBSD-gated tests that verify sysctl overloads return provided defaults when querying invalid sysctl names across String, int, and long variants.

Sequence Diagram

sequenceDiagram
  participant Caller
  participant BsdSysctlUtilFFM
  participant FreeBsdLibcFunctions
  participant libc
  Caller->>BsdSysctlUtilFFM: sysctl(name, default)
  BsdSysctlUtilFFM->>FreeBsdLibcFunctions: sysctlbyname(callState, name, oldp, oldlenp, ...)
  FreeBsdLibcFunctions->>libc: sysctlbyname
  libc-->>FreeBsdLibcFunctions: return code + errno
  FreeBsdLibcFunctions-->>BsdSysctlUtilFFM: boolean success
  alt success
    BsdSysctlUtilFFM-->>Caller: parsed value
  else failure
    BsdSysctlUtilFFM-->>Caller: provided default
  end
Loading

🎯 3 (Moderate) | ⏱️ ~25 minutes

🐰 In FreeBSD lands where memory flows wild,
FFM bindings tame the system call child,
Utmpx records dance in the light,
Sysctl whispers secrets, all tested right!
Unix traditions meet Java's embrace, ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% 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 clearly and concisely summarizes the main changes: FreeBSD FFM libc bindings, sysctl utilities, and WhoFFM driver implementation.
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 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.04%. Comparing base (6df912b) to head (c398f6c).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...src/main/java/oshi/driver/unix/freebsd/WhoFFM.java 0.00% 20 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3319      +/-   ##
============================================
- Coverage     76.13%   76.04%   -0.10%     
  Complexity       76       76              
============================================
  Files           448      449       +1     
  Lines         17235    17255      +20     
  Branches       2820     2824       +4     
============================================
- Hits          13122    13121       -1     
- Misses         2523     2543      +20     
- Partials       1590     1591       +1     
Flag Coverage Δ
freebsd 31.28% <0.00%> (+0.07%) ⬆️
linux 38.21% <0.00%> (-0.04%) ⬇️
macos 38.51% <0.00%> (-0.06%) ⬇️
windows 45.88% <0.00%> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

Replaces the over-eager comparison tests added in commits 1 and 2 with
the testing pattern OSHI already uses for the same code shape on macOS.

* Removes three oshi-benchmark comparison tests
  (BsdSysctlComparisonTest, WhoComparisonTest, FreeBsdLibcComparisonTest).
  These exercised internals that Phase 6's NativeComparisonTest will
  cover transitively through the HAL/OS public API once SystemInfo
  wires FreeBSD in. WhoFFM is also covered indirectly today by the
  pre-existing oshi.driver.unix.freebsd.WhoTest (JNA smoke pattern).

* Adds BsdSysctlUtilTest in oshi-core mirroring
  oshi.util.platform.mac.SysctlUtilTest — closes a pre-existing JNA-side
  coverage gap where the FreeBSD sysctl util had no dedicated test even
  though the Mac mirror did. Smoke-tests the default-on-failure paths
  for int/long/String variants.

* Adds BsdSysctlUtilFFMTest in oshi-core-ffm following the same shape
  for the FFM util, and add-opens
  oshi.ffm.util.platform.unix.freebsd for JUnit reflection.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@dbwiddis
dbwiddis merged commit fe50ae9 into oshi:master Jun 3, 2026
19 of 20 checks passed
@dbwiddis
dbwiddis deleted the freebsd-ffm branch June 3, 2026 01:37
dbwiddis added a commit that referenced this pull request Jun 4, 2026
Follow-up to the five FreeBSD FFM port PRs (#3316, #3319, #3322,
#3328, #3330) covering loose ends that don't fit any individual port
commit.

JaCoCo agent excludes:
* Remove `**/unix/BsdNetworkIF*` and `**/unix/UnixBaseboard*`. Both
  classes were hoisted to oshi-common during the port (#3316 and
  #3328 respectively) and are now exercised on the permanent
  FreeBSD CI job through `NativeComparisonTest.networkInterfaces()`
  and `ComputerSystem.getBaseboard()`; the excludes were silently
  dropping that coverage from the `freebsd` codecov flag.
* Add `**/FreeBsdPowerSource*` alongside the existing
  `**/{Mac,Windows,Linux}PowerSource*` entries. No battery on the
  FreeBSD CI VM, same hardware-conditional pattern.

Documentation:
* Update the supported-platforms statement in README.md, FAQ.md,
  PERFORMANCE.md, src/site/markdown/index.md, oshi-core-ffm
  module-info Javadoc, oshi-core SystemInfo Javadoc, and oshi-core
  package-info Javadoc from "Windows, macOS, and Linux" to
  "Windows, macOS, Linux, and FreeBSD".

CHANGELOG.md gains a New Features bullet linking all five FreeBSD
FFM port PRs.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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