Repository navigation
Split LinuxGraphicsCard, LinuxGpuStats, and NvmlUtil across modules - #3139
Conversation
Move native-free logic for LinuxGraphicsCard and LinuxGpuStats to oshi-common as abstract classes. Create JNA subclasses in oshi-core and FFM subclasses in oshi-core-java25. Rename NvmlUtil to NvmlUtilJNA in oshi-core and create NvmlUtilFFM in oshi-core-java25 with new NvmlFunctions FFM bindings. LinuxGraphicsCard uses a factory Function<Attrs, GraphicsCard> parameter so JNA/FFM subclasses can provide their concrete type from static getGraphicsCards() methods. LinuxGpuStats defines abstract nvml*() methods that JNA/FFM subclasses implement by delegating to NvmlUtilJNA/NvmlUtilFFM respectively.
📝 WalkthroughWalkthroughThis PR refactors Linux GPU component handling by extracting Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3139 +/- ##
============================================
+ Coverage 61.39% 62.21% +0.81%
Complexity 12 12
============================================
Files 39 39
Lines 2329 2329
Branches 407 407
============================================
+ Hits 1430 1449 +19
+ Misses 783 770 -13
+ Partials 116 110 -6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.java`:
- Around line 36-58: The overrides nvmlGetVramUsed, nvmlGetCoreClockMhz, and
nvmlGetMemoryClockMhz currently pass through NvmlUtilFFM's -1L failure sentinel,
violating LinuxGpuStats' contract that unavailable values must be 0; update each
of those methods to call the corresponding NvmlUtilFFM.get... method
(nvmlGetVramUsed, nvmlGetCoreClockMhz, nvmlGetMemoryClockMhz) and return 0 if
the result is negative (e.g., return Math.max(0L, result) or equivalent) so
impossible negative values are clamped to 0.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ed9e3aae-2766-4c22-9a23-3f4e1f776af5
📒 Files selected for processing (14)
CHANGELOG.mdoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.javaoshi-core-java25/src/main/java/oshi/ffm/common/NvmlFunctions.javaoshi-core-java25/src/main/java/oshi/ffm/common/package-info.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.javaoshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGpuStats.javaoshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.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). (10)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 25, windows-latest
- GitHub Check: Test JDK 11, windows-latest
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: Test JDK 25, macos-latest
- GitHub Check: Test JDK 25, ubuntu-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (9)
📚 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.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardJNA.javaoshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGpuStats.java
📚 Learning: 2026-04-05T16:15:06.124Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java:95-97
Timestamp: 2026-04-05T16:15:06.124Z
Learning: In `LinuxLogicalVolumeGroupJNA.getLogicalVolumeGroups()` (oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java), `physicalVolumesMap.get(e.getKey())` in the final stream mapping cannot return null: both `logicalVolumesMap` and `physicalVolumesMap` are populated via `computeIfAbsent` inside the same conditional block (`!Util.isBlank(vgName) && !Util.isBlank(lvName)`), so every VG key in `logicalVolumesMap` is guaranteed to have a corresponding entry in `physicalVolumesMap`. Do not flag this `.get()` call as a potential null-return issue. The FFM version uses `getOrDefault` as extra defensiveness, not because null is possible here.
Applied to files:
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java
📚 Learning: 2026-03-29T20:01:06.452Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:06.452Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.
Applied to files:
oshi-core-java25/src/main/java/oshi/ffm/common/package-info.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.javaoshi-core-java25/src/main/java/oshi/ffm/common/NvmlFunctions.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
📚 Learning: 2026-04-06T04:45:51.516Z
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:51.516Z
Learning: In the oshi/oshi project, the public API is strictly limited to interfaces and classes in the `oshi`, `oshi.hardware`, and `oshi.software.os` packages. All platform-specific code under subpackages like `oshi.software.os.linux`, `oshi.hardware.platform.linux`, etc., is considered implementation detail — not public API. Do not flag changes to platform-specific classes (e.g., making `LinuxOperatingSystem` abstract, narrowing constructor visibility, adding abstract methods) as public API breaks.
Applied to files:
oshi-core-java25/src/main/java/oshi/ffm/common/package-info.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaCHANGELOG.mdoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
📚 Learning: 2026-04-03T02:07:47.621Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3110
File: oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java:119-121
Timestamp: 2026-04-03T02:07:47.621Z
Learning: In OSHI’s Java FFM/API implementation classes (e.g., *FFM.java) that implement/override `updateAttributes()`, preserve the intentional failure-handling pattern: catch `Throwable` and return `false` without logging. `updateAttributes()` is a public API whose contract is to signal failure via the boolean return value, and callers rely on that return value (so emitting logs from this path is not desired and should not be introduced during review unless the method contract changes).
Applied to files:
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.javaoshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.
Applied to files:
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.
Applied to files:
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.javaoshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.java
📚 Learning: 2026-04-05T05:40:45.797Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:45.797Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
Applied to files:
oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardJNA.javaoshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java
📚 Learning: 2026-03-29T20:03:00.830Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java25/src/main/java/oshi/util/platform/windows/PdhUtilFFM.java:87-89
Timestamp: 2026-03-29T20:03:00.830Z
Learning: In the OSHI FFM codebase (oshi-core-java25), all PDH/Kernel32/COM wrapper methods in classes like PdhFFM, Kernel32FFM, etc. declare `throws Throwable` solely as a compiler artifact of `MethodHandle.invokeExact`. They are native calls and do not throw Java checked exceptions in practice. The enclosing `getOpenFileDescriptors()` method in PdhUtilFFM already has an outer `catch (Throwable t)` that covers the entire method body including any `finally` blocks, so no exception-masking concern exists for cleanup calls like `PdhCloseQuery` in `finally`.
Applied to files:
oshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.javaoshi-core-java25/src/main/java/oshi/ffm/common/NvmlFunctions.java
🔇 Additional comments (18)
CHANGELOG.md (1)
15-15: LGTM!The changelog entry accurately describes the PR scope and follows the existing format conventions.
oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java (2)
42-44: LGTM!The rename from
NvmlUtiltoNvmlUtilJNAis correct and consistent with the project's naming pattern for JNA-specific implementations. The logger is properly updated to reference the new class name.
75-76: LGTM!Private constructor renamed correctly to match the new class name.
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGpuStats.java (3)
26-26: LGTM!Import correctly updated to use
NvmlUtilJNAfor Windows platform which uses JNA bindings.
198-198: LGTM!All NVML metric method calls correctly updated to use
NvmlUtilJNA. The method signatures match the utility class API.Also applies to: 218-218, 242-242, 263-263, 284-284
334-344: LGTM!Device discovery methods (
isAvailable,findDevice,findDeviceByName) correctly updated to useNvmlUtilJNA. The caching logic infindNvmlDevice()is preserved.oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java (4)
5-5: LGTM!The refactoring to an abstract base class in
oshi-commonis well-structured:
- Package move to
oshi.hardware.common.platform.linuxcorrectly places native-free code in the common module- Class made
public abstractwithprotectedconstructor appropriately enables subclassing by JNA/FFM implementationsAlso applies to: 29-29, 41-56
76-149: LGTM!The abstract NVML hook methods provide a clean contract for JNA/FFM subclasses:
- Availability check:
nvmlIsAvailable()- Device resolution:
nvmlFindDevice(),nvmlFindDeviceByName()- Metric retrieval: 6 methods for VRAM, temperature, power, clocks, and fan speed
The Javadoc clearly documents each method's purpose and return value semantics.
155-180: LGTM!The
findNvmlDevice()caching implementation correctly uses:
nullto indicate "not yet resolved"- Empty string to indicate "resolved but unavailable"
This pattern is consistent with
WindowsGpuStats.cachedNvmlDeviceand avoids repeated NVML lookups.
223-241: LGTM!The metric methods correctly delegate to abstract
nvml*hooks while preserving the existing sysfs/hwmon/DRM fallback logic for non-NVIDIA GPUs.Also applies to: 249-266, 268-285, 287-315, 317-340, 342-364
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsJNA.java (1)
1-65: LGTM!Clean implementation of the JNA-based Linux GPU stats:
- Properly extends
LinuxGpuStatsabstract base class- All 9 abstract NVML methods correctly delegate to
NvmlUtilJNA- Thread-safe annotation present
- Package-private visibility appropriate for platform implementation detail
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java (1)
62-64: LGTM!The JNA hardware abstraction layer correctly delegates to
LinuxGraphicsCardJNA.getGraphicsCards(), which internally uses theLinuxGpuStatsJNAimplementation for NVML operations.oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java (1)
57-59: LGTM!The FFM hardware abstraction layer correctly delegates to
LinuxGraphicsCardFFM.getGraphicsCards(), which internally uses theLinuxGpuStatsFFMimplementation for NVML operations via FFM bindings.oshi-core-java25/src/main/java/oshi/ffm/common/package-info.java (1)
1-8: LGTM!Standard package-info.java with appropriate copyright header and documentation describing the package's purpose for FFM bindings.
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardJNA.java (1)
25-38: Clean JNA adapter split.Keeping discovery in the common class and limiting this class to JNA-specific stats-session creation makes the module boundary much clearer.
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGraphicsCardFFM.java (1)
25-38: Clean FFM adapter split.This is a good counterpart to the JNA path: common discovery stays shared, while the FFM module only supplies the concrete stats implementation.
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java (1)
87-157: Nice extraction of the common discovery path.The immutable
Attrscarrier plus factory injection keeps the parsing code native-free while still letting JNA and FFM supply their own concreteGraphicsCard/stats behavior.oshi-core-java25/src/main/java/oshi/ffm/common/NvmlFunctions.java (1)
80-140: Bootstrap looks solid.The binding init fails closed via
AVAILABLEand doesn't expose half-initialized handles when NVML isn't present.
Move native-free logic for
LinuxGraphicsCardandLinuxGpuStatsto oshi-common as abstract classes, with JNA subclasses in oshi-core and FFM subclasses in oshi-core-java25.NvmlUtil split
NvmlUtil→NvmlUtilJNAin oshi-coreNvmlUtilFFMin oshi-core-java25 with identical public APINvmlFunctionsFFM bindings for the NVML library (lazy-loaded, struct layouts, method handles)LinuxGpuStats split
LinuxGpuStatsin oshi-commonnvml*()methods for NVML operationsLinuxGpuStatsJNAdelegates toNvmlUtilJNALinuxGpuStatsFFMdelegates toNvmlUtilFFMLinuxGraphicsCard split
public abstractclassAttrsinner class (JDK 8 compatible) to bundle graphics card attributesgetGraphicsCards()withFunction<Attrs, GraphicsCard>factory so JNA/FFM subclasses provide their concrete typeLinuxGraphicsCardJNAandLinuxGraphicsCardFFMoverridecreateStatsSession()to return their respectiveLinuxGpuStatssubclassOther updates
LinuxHardwareAbstractionLayerJNA→ usesLinuxGraphicsCardJNALinuxHardwareAbstractionLayerFFM→ usesLinuxGraphicsCardFFMWindowsGpuStats→ usesNvmlUtilJNASummary by CodeRabbit