Skip to content

Consolidate Linux UsbDevice udev logic into a shared tree builder - #3448

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-linux-usb-udev
Jul 11, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-linux-usb-udev

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of the ongoing cross-OS consistency/dedup audit. The three Linux USB backends (JNA libudev, FFM libudev, and the native-free sysfs reader) each carried a full copy of the same map-building, parent/child hub-graph, sorting, and recursive device-tree construction — differing only in how they enumerated devices from the OS.

This splits at the carrier boundary: each backend enumerates once and returns a list of raw attributes; a single shared builder assembles the tree.

Changes

  • New UdevUsbDevice (oshi-common) — immutable carrier for one device's raw attributes: syspath, product, manufacturer, vendorId, productId, serial, parentSyspath (null parent = root controller). Any field may be null if the backend didn't report it.
  • LinuxUsbDevice — now final; holds the single copy of getUsbDevices(List<UdevUsbDevice>) (lookup maps + hub graph + sort + recursive tree). getDeviceAndChildren kept package-visible for its existing unit test.
  • Backends reduced to pure enumeration, each returning List<UdevUsbDevice>:
    • LinuxUsbDeviceJNA — JNA libudev (unref() finally chain)
    • LinuxUsbDeviceFFM — FFM libudev (callInArenaOrDefault + NativeHandle)
    • LinuxUsbDeviceNF — sysfs /sys/bus/usb/devices/ reader
  • All 3 Linux HALs createUsbDevices() → LinuxUsbDevice.getUsbDevices(XxxDriver.queryUsbDevices()).

Net −82 LOC of hand-written logic (three copies collapsed to one).

Behavior

No user-facing change: same device tree, same map fallbacks, same sort order. LinuxUsbDeviceNFTest now routes its sysfs fixtures through getUsbDevices so it keeps asserting on the built tree; LinuxUsbDeviceTest still unit-tests getDeviceAndChildren directly.

Testing

  • spotless / checkstyle / forbiddenapis / javadoc: clean
  • Clean full-reactor install with tests: BUILD SUCCESS, 0 tests failed
  • Linux-only NF/tree tests validated by CI on this PR

Summary by CodeRabbit

  • New Features

    • Improved Linux USB device detection across native-free, FFM, and JNA implementations.
    • USB devices now expose more consistent product, manufacturer, vendor, product ID, serial number, and hierarchy information.
    • Added a shared representation for raw USB device details.
  • Bug Fixes

    • Improved construction and ordering of nested USB device trees.
    • Empty USB attributes are handled consistently.

The JNA, FFM, and native-free (sysfs) Linux USB backends each carried a
full copy of the map-building, parent/child hub graph, sorting, and
recursive tree-construction logic, differing only in how they enumerated
devices. Split that boundary cleanly:

- New UdevUsbDevice carrier holds the raw per-device attributes a backend
  extracts (syspath, product, manufacturer, vendorId, productId, serial,
  parentSyspath; null parent = root controller).
- Each backend now only enumerates once and returns List<UdevUsbDevice>:
  LinuxUsbDeviceJNA (JNA libudev), LinuxUsbDeviceFFM (FFM libudev), and
  LinuxUsbDeviceNF (sysfs reader).
- LinuxUsbDevice.getUsbDevices(List<UdevUsbDevice>) holds the single copy
  of the lookup-map/hub-graph/sort/tree logic; the three HALs call it with
  their backend's carrier list.

No user-facing change: same device tree, same map fallbacks, same sort
order. LinuxUsbDeviceNFTest routes through getUsbDevices to keep asserting
on the built tree; LinuxUsbDeviceTest still unit-tests getDeviceAndChildren.
@coderabbitai

coderabbitai Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

USB enumeration now returns raw UdevUsbDevice records from each Linux backend. LinuxUsbDevice centrally converts those records into hierarchical controller and child-device trees, with native-free, JNA, and FFM callers updated accordingly.

Changes

Linux USB tree construction

Layer / File(s) Summary
Common USB record and tree construction
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java, oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java
Adds immutable raw USB attributes and centralizes syspath-based parent-child tree construction in a final LinuxUsbDevice API.

Native-free enumeration

Layer / File(s) Summary
Native-free raw USB querying
oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java, oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.java, oshi-common/src/test/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNFTest.java
Reads sysfs attributes into UdevUsbDevice records, delegates tree construction to LinuxUsbDevice, and updates tests to use the two-step flow.

JNA and FFM enumeration

Layer / File(s) Summary
Native backend querying and wiring
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java, oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java, oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java, oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java
Changes libudev backends to return raw records and routes both backend results through the common tree builder.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • oshi/oshi#3238: Adds native-free Linux USB wiring related to the refactored query-and-build flow.

Poem

A rabbit hops through USB trees,
Where raw device leaves rustle in the breeze.
Controllers gather, children align,
JNA, FFM, and sysfs combine.
One builder shapes the branchwork bright—
Hop, hop, the devices fit just right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 46.67% 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: consolidating Linux USB udev logic into a shared tree builder.
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.
✨ 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.

@codecov

codecov Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.93939% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.48%. Comparing base (93fce6a) to head (b40da84).

Files with missing lines Patch % Lines
...hardware/common/platform/linux/LinuxUsbDevice.java 93.10% 0 Missing and 2 partials ⚠️
...on/platform/linux/nativefree/LinuxUsbDeviceNF.java 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3448      +/-   ##
============================================
+ Coverage     74.46%   74.48%   +0.01%     
  Complexity       76       76              
============================================
  Files           582      583       +1     
  Lines         21913    21934      +21     
  Branches       3669     3671       +2     
============================================
+ Hits          16318    16338      +20     
- Misses         3558     3559       +1     
  Partials       2037     2037              
Flag Coverage Δ
freebsd 26.85% <0.00%> (-0.05%) ⬇️
linux 31.48% <93.93%> (+0.06%) ⬆️
macos 31.80% <0.00%> (-0.07%) ⬇️
openbsd 26.33% <0.00%> (-0.03%) ⬇️
solaris 28.40% <0.00%> (-0.01%) ⬇️
windows 39.81% <0.00%> (-0.02%) ⬇️

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/hardware/common/platform/linux/UdevUsbDevice.java (1)

16-97: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider converting UdevUsbDevice to a record. It’s a pure immutable carrier, and the existing call sites in this PR can switch to record accessors with minimal churn.

🤖 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/hardware/common/platform/linux/UdevUsbDevice.java`
around lines 16 - 97, Convert UdevUsbDevice from an immutable class to a Java
record using its seven existing components: syspath, product, manufacturer,
vendorId, productId, serial, and parentSyspath. Remove the explicit fields,
constructor, and get* accessors, then update all call sites to use record
accessors such as syspath() and parentSyspath().
🤖 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/hardware/common/platform/linux/UdevUsbDevice.java`:
- Around line 16-97: Convert UdevUsbDevice from an immutable class to a Java
record using its seven existing components: syspath, product, manufacturer,
vendorId, productId, serial, and parentSyspath. Remove the explicit fields,
constructor, and get* accessors, then update all call sites to use record
accessors such as syspath() and parentSyspath().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 63529975-66b1-41f3-ad0b-ecc66f519445

📥 Commits

Reviewing files that changed from the base of the PR and between 93fce6a and b40da84.

📒 Files selected for processing (9)
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNFTest.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📜 Review details
⏰ Context from checks skipped due to timeout. (15)
  • GitHub Check: CodeRabbit
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (4)
📚 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/linux/nativefree/LinuxHardwareAbstractionLayerNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T15:37:16.042Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java:59-65
Timestamp: 2026-04-05T15:37:16.042Z
Learning: For OSHI Linux USB device implementations, the behavior of `getUsbDevices(boolean tree)` with `tree=false` is intentional: the returned flat list includes controller nodes (it is not meant to exclude them). In code review, do not flag the implementation for including controllers in flat mode; instead, only flag/update documentation/JavaDoc that incorrectly claims controllers are excluded (e.g., Javadoc text like "returns a flat list excluding controllers" should be corrected).

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
🔇 Additional comments (8)
oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java (1)

21-21: LGTM!

Also applies to: 74-74

oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java (1)

21-27: LGTM!

Also applies to: 43-52, 63-76, 89-89

oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java (1)

21-21: LGTM!

Also applies to: 59-59

oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java (1)

2-10: LGTM!

Also applies to: 19-77, 79-95

oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java (1)

7-82: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.java (1)

20-20: LGTM!

Also applies to: 57-57

oshi-common/src/test/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNFTest.java (1)

24-41: LGTM!

Also applies to: 50-51, 67-68, 96-97, 127-128

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (1)

21-27: LGTM!

Also applies to: 43-56, 68-68, 85-97

@dbwiddis
dbwiddis merged commit ee5c0ca into oshi:master Jul 11, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the dedup-linux-usb-udev branch July 11, 2026 03:44
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