Skip to content

Make the GPU classes' nullability contracts honest - #3621

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:jspecify-gpu
Aug 15, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:jspecify-gpu

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Aug 15, 2026 •

Copy link
Copy Markdown
Member

Fourth group of the backlog #3618 left at WARN, following #3619 and #3620. Takes oshi-common from 172 warnings to 158 and clears the GPU classes. The reactor drops 568 to 554 with nothing newly surfaced.

No behavior changes.

Two of these describe themselves

LinuxGpuStats caches the NVML device id in a field carrying this comment:

// Cached NVML device id; null means not yet resolved, empty string means unavailable
private String nvmlDeviceId;

Three states in a type that admitted two. WindowsGpuStats keeps the same cache, plus a previous-ticks snapshot that is null until the first sample.

findDrmInfo documents @param pciSlot the PCI slot address ... or null to use first-match on both overloads, and is handed to getGraphicsCardsFrom* as a Function whose input the callers may genuinely not know — lspci does not always report a slot.

The rest follow the same pattern: MacGpuStats.sampler, which is null when the IOReport subscription could not be created — the constructor already checks for that and logs it — and DxgiUtil.findMatch with its JNA forwarder, whose javadoc already said "or null".

Scope note

LinuxGraphicsCard and LinuxGraphicsCardNF are in this group rather than with the peripherals later on. They share findDrmInfo and the DRM lookup Function with the GpuStats classes, so splitting them would mean deciding one contract in two separate reviews — the same fragmentation avoided by grouping these changes by subsystem rather than by operating system.

Verification

Full gate green locally on macOS/JDK 25: ./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadoc, 1561 tests passing, plus ./mvnw -Perror-prone clean install -DskipTests building successfully across the reactor.

Two assertions were added in DxgiUtilTest, at the two places that dereference the now-nullable findMatch result. The tests that assert it returns null are untouched.

Summary by CodeRabbit

  • Bug Fixes

    • Improved clarity around GPU hardware detection when information is unavailable across Windows, Linux, and macOS.
    • Preserved existing GPU matching and statistics behavior.
  • Tests

    • Strengthened GPU matching tests to verify successful results before checking their details.

Fourth group of the backlog #3618 left at WARN. Takes oshi-common from 172
warnings to 158 and clears the GPU classes; the reactor drops 568 to 554 with
nothing newly surfaced.

Two of these describe themselves. LinuxGpuStats caches the NVML device id in a
field commented "null means not yet resolved, empty string means unavailable",
which is three states in a type that admitted two. findDrmInfo documents
"the PCI slot address, or null to use first-match" on both overloads, and is
passed as a Function whose input the callers may not know.

The rest follow: WindowsGpuStats' two lazily populated caches, MacGpuStats'
sampler, which is null when the IOReport subscription could not be created and
which the constructor already checks and logs, and DxgiUtil.findMatch with its
JNA forwarder.

LinuxGraphicsCard and its native-free subclass are here rather than with the
peripherals group, because they share findDrmInfo and the DRM lookup function
with the GpuStats classes; splitting them would mean deciding one contract in
two places.
@coderabbitai

coderabbitai Bot commented Aug 15, 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 Plus

Run ID: 9111665e-952b-4bc4-bf02-040626564f68

📥 Commits

Reviewing files that changed from the base of the PR and between 6d85042 and e2d9869.

📒 Files selected for processing (8)
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java
  • oshi-core/src/test/java/oshi/util/gpu/DxgiUtilTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Error Prone
🧰 Additional context used
📓 Path-based instructions (4)
**/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/main/java/**/*.java: Before writing any parsing helper, check whether ParseUtil already has one.
Every public and protected type and method needs javadoc.
Do not use fully-qualified class names in
code or in javadoc — including inside {@link ...}. Import the type and link the simple name.

Files:

  • oshi-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
oshi-common/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

oshi-common/src/main/java/**/*.java: oshi-common may not reference com.sun.jna.**, java.lang.foreign.**, or any
System.load/loadLibrary. It must stay pure JDK.

Files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
**/src/test/java/**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Test files are named *Test.java.

Files:

  • oshi-core/src/test/java/oshi/util/gpu/DxgiUtilTest.java
**/src/test/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/test/java/**/*.java: Assert on invariants (non-null, correct type, ordering, a plausible range), not on specific values.
Any oshi-core test that allocates JNA needs
@DisabledIfSystemProperty(named = "os.name", matches = "(?i).*netbsd.*"); the NetBSD CI runner
has no libjnidispatch.

Files:

  • oshi-core/src/test/java/oshi/util/gpu/DxgiUtilTest.java
🧠 Learnings (5)
📚 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-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
📚 Learning: 2026-07-30T00:35:22.638Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3547
File: oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdFirmware.java:82-82
Timestamp: 2026-07-30T00:35:22.638Z
Learning: In OSHI, treat changes as “API-breaking” only when they affect classes (or relevant API surface) annotated with `PublicAPI`. Public classes/members that are not annotated with `PublicAPI` should be reviewed as internal implementation details, even if they are public (e.g., classes like `oshi.hardware.common.platform.unix.BsdFirmware`). Also, respect intentional visibility decisions used for tests—e.g., methods like `BsdFirmware.parseDmesg(List<String>)` may be intentionally package-private to support tests within the same package; avoid widening visibility or altering signatures unless the related `PublicAPI` contract requires it.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-core/src/test/java/oshi/util/gpu/DxgiUtilTest.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
📚 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/LinuxGraphicsCardNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
  • oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
📚 Learning: 2026-04-11T05:51:49.185Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3139
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.java:36-58
Timestamp: 2026-04-11T05:51:49.185Z
Learning: For OSHI Linux GPU stats implementations (e.g., LinuxGpuStats and its LinuxGpuStatsJNA/LinuxGpuStatsFFM subclasses), treat NVML "nvml*" methods (such as nvmlGetVramUsed, nvmlGetCoreClockMhz, nvmlGetMemoryClockMhz, nvmlGetTemperature, nvmlGetPowerDraw, nvmlGetFanSpeedPercent) as having a documented failure sentinel: they are contractually expected to return -1 (long/double) on failure. The base class/implementations should use guards like `if (val >= 0)` before consuming NVML results and should fall back to sysfs/hwmon paths when the value is negative. In review, do not flag subclass pass-through of the -1 sentinel as a missing clamp/contract violation; only flag issues if the -1 sentinel is altered or NVML failure results are used without the expected `>= 0` guard or fallback behavior.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java
🔇 Additional comments (8)
oshi-common/src/main/java/oshi/driver/common/windows/gpu/DxgiUtil.java (1)

10-10: LGTM!

Also applies to: 40-40

oshi-core/src/main/java/oshi/util/gpu/DxgiUtilJNA.java (1)

9-9: LGTM!

Also applies to: 43-43

oshi-core/src/test/java/oshi/util/gpu/DxgiUtilTest.java (1)

12-12: LGTM!

Also applies to: 39-39, 67-67

oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java (1)

14-14: LGTM!

Also applies to: 69-69, 82-82

oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java (1)

12-12: LGTM!

Also applies to: 44-46, 460-460

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

10-11: LGTM!

Also applies to: 95-103, 158-165

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

13-14: LGTM!

Also applies to: 219-219, 315-315, 400-400, 411-411

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

9-10: LGTM!

Also applies to: 60-66


📝 Walkthrough

Walkthrough

The changes add JSpecify @Nullable annotations to GPU lookup results, nullable inputs, cached state, and sampler contracts across Windows, Linux, and macOS. DXGI tests now assert that successful matches are non-null.

Changes

GPU nullability annotations

Layer / File(s) Summary
DXGI matching contracts
oshi-common/.../DxgiUtil.java, oshi-core/.../DxgiUtilJNA.java, oshi-core/src/test/.../DxgiUtilTest.java
DXGI matching methods declare nullable results. Matching tests assert non-null results before further checks.
Linux GPU lookup contracts
oshi-common/src/main/java/oshi/hardware/common/platform/linux/...
Linux NVML results and DRM PCI slot inputs declare nullable values. Runtime lookup behavior is unchanged.
Platform GPU state contracts
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacGpuStats.java, oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
Mac sampler state and Windows NVML state declare nullable fields and return types.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to e2d98

This PR makes GPU nullability contracts accurate without changing runtime behavior, and the supplied verification is green; no actionable merge-blocking risk remains beyond normal checks and review.

Possibly related PRs

  • oshi/oshi#3543: Introduces the nullable IOReportSampler lifecycle used by MacGpuStats.
  • oshi/oshi#3614: Extends the JSpecify nullability rollout.
  • oshi/oshi#3618: Adds overlapping GPU-related nullability annotations.

Poem

A rabbit checks each nullable call,
With @Nullable labels over all.
DXGI matches stand clear and bright,
Linux and Mac now type things right.
Windows joins the typed-up flight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.71% 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 describes the main change: correcting nullability contracts across GPU classes.
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.

@dbwiddis
dbwiddis merged commit 198dbfa into oshi:master Aug 15, 2026
16 of 17 checks passed
@dbwiddis
dbwiddis deleted the jspecify-gpu branch August 15, 2026 04:40
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