Skip to content

Finish the two findings the previous round did not clear - #3639

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:codeql-gpustats
Aug 15, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:codeql-gpustats

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Aug 15, 2026 •

Copy link
Copy Markdown
Member

Both of these survived #3638. Re-reading them once the Sonar and Code Quality scans refreshed on
current master showed why — and in one case the reason was my own mistake.

WindowsGpuStats — I fixed the wrong field

The java/safe-publication finding was on cachedAdlIndex, not the adjacent cachedNvmlDevice I
made volatile in #3638. I inferred which field the reported line number held rather than counting it,
so the finding just moved down one line with my change.

cachedAdlIndex and prevUtilTicks are both written after construction on a @ThreadSafe class, and
are volatile now. closed is not, and does not need to be — its only accessors are synchronized,
which is why CodeQL never flagged it.

Making prevUtilTicks volatile also required fixing how it is read. The old code tested it for null
and then dereferenced it — two reads of a field another thread can write in between:

if (prevUtilTicks != null) {
    long dActive = curr.getActiveTicks() - prevUtilTicks.getActiveTicks();   // second read

It now reads once into a local, which removes the race as well as the visibility gap.

NetBsdCentralProcessor — the regex was unnecessary, not just unlucky

The possessive quantifiers from #3638 did not satisfy java:S8786; Sonar still reported
backtracking on a pattern that by definition does none. Rather than argue with the rule, I looked at
the call site again:

String[] kv = KEY_VALUE.split(pair.trim(), -1);
if (kv.length == 2) {
    long val = ParseUtil.parseLongOrDefault(kv[1].trim(), 0L);
    switch (kv[0].trim()) {

Both halves are trimmed immediately afterwards, so matching the surrounding whitespace in the pattern
achieved nothing the trim did not. Splitting on a plain "=" is equivalent, takes the JDK's
single-character fast path rather than the regex engine, and leaves nothing for the rule to report.
The Pattern constant and its import are gone.

parseCpTime's tests cover real sysctl output — user = 2930, nice = 42, sys = 1334, ... with
asserted tick values — and are unchanged.

After this

Sonar should hold at the 5 known false positives; Code Quality at the 2 informational findings you
have been dismissing (ParseUtil's overload pair and the deliberately unread Udev probe).

Full gate on macOS: spotless:apply spotless:check checkstyle:check install forbiddenapis:check javadoc:javadoc plus -Perror-prone clean install — all modules green, 1326 oshi-common tests, 0
failures, NullAway zero at ERROR.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when reading CPU timing information on NetBSD systems.
    • Improved consistency of GPU utilization reporting on Windows, especially during concurrent updates or invalid measurement intervals.

Both survived #3638, and re-reading them after the scans refreshed showed why.

WindowsGpuStats: the safe-publication finding was on cachedAdlIndex, not the
adjacent cachedNvmlDevice I made volatile. I inferred which field the reported
line held instead of counting, and fixed the wrong one; the finding simply moved
down with it. cachedAdlIndex and prevUtilTicks are both written after
construction on a @threadsafe class and are volatile now. closed is not, and
does not need to be: its only accessors are synchronized.

Making prevUtilTicks volatile also has to fix how it is read. The old code
tested it for null and then dereferenced it, two reads of a field another thread
can write between; it now reads once into a local, which removes the race as
well as the visibility gap.

NetBsdCentralProcessor: the possessive quantifiers did not satisfy S8786 - Sonar
still reported backtracking on a pattern that by definition does none. Rather
than argue with it, the pattern turned out to be unnecessary. The call site
trims both halves of the split immediately afterwards, so matching the
surrounding whitespace achieved nothing that the trim did not; splitting on a
plain "=" is equivalent, takes the JDK's single-character fast path rather than
the regex engine, and leaves nothing for the rule to report.
parseCpTime's tests cover real sysctl output and are unchanged.
@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: affad876-2cd4-44ab-a8cf-ecfe072e0a3d

📥 Commits

Reviewing files that changed from the base of the PR and between 47a09d5 and 0eb5f0d.

📒 Files selected for processing (2)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (12)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Error Prone
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Analyze (java)
🧰 Additional context used
📓 Path-based instructions (3)
**/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/main/java/**/*.java: Before writing any parsing helper, check whether ParseUtil already has one.
Do not use fully-qualified class names in
code or in javadoc — including inside {@link ...}. Import the type and link the simple name.
Every public and protected type and method needs javadoc.
New files get a current-year-only
header (Copyright 2026 The OSHI Project Contributors / SPDX-License-Identifier: MIT)
new classes are
expected to declare one
Do not reach for @Nullable to make a null return legal: the convention is a sentinel, not a
null

System calls are expensive, and callers poll. Values that are constant (CPU model,
serial number) or expensive-but-slow-changing are wrapped in
oshi.util.Memoizer.memoize(supplier, ...)

Files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.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/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
**/*.{java,xml,md}

📄 CodeRabbit inference engine (AGENTS.md)

Do not commit generated output, target/, or IDE files.

Files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
🧠 Learnings (7)
📚 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/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.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/netbsd/NetBsdCentralProcessor.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/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.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-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.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-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
📚 Learning: 2026-08-15T14:25:48.170Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3625
File: oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.java:65-69
Timestamp: 2026-08-15T14:25:48.170Z
Learning: In OSHI Java code, treat `oshi.util.Util.isBlank(String)` as equivalent to `value == null || value.isEmpty()`: it does not consider whitespace-only strings blank. Do not report a behavior change when code replaces `!Util.isBlank(value)` with the equivalent `value != null && !value.isEmpty()` condition.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
📚 Learning: 2026-08-15T15:43:29.296Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3628
File: oshi-core-ffm/src/main/java/oshi/driver/windows/registry/HkeyUserDataFFM.java:108-108
Timestamp: 2026-08-15T15:43:29.296Z
Learning: In the OSHI Java codebase, use the sentinel-over-null convention at public API boundaries. Private helpers may return an explicitly JSpecify-annotated nullable value when the caller handles the missing result clearly and locally; callers must check for null before accessing the result. For example, methods such as lookupAccountBySid(String) may return String Nullable [] when queryUserSessions() performs the required null check.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
🔇 Additional comments (3)
oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java (1)

8-8: LGTM!

Also applies to: 25-25, 187-188

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

45-46: LGTM!


277-280: LGTM!


📝 Walkthrough

Walkthrough

The PR simplifies NetBSD CPU time key/value parsing and updates Windows GPU statistics for cross-thread field visibility. GPU utilization now snapshots previous ticks locally before calculating deltas.

Changes

NetBSD CPU time parsing

Layer / File(s) Summary
Direct key/value parsing
oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdCentralProcessor.java
CPU time parsing splits pairs directly on =. Trimming and numeric parsing remain unchanged. Regex support is removed.

Windows GPU state visibility

Layer / File(s) Summary
GPU state visibility and snapshot
oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsGpuStats.java
cachedAdlIndex and prevUtilTicks are now volatile. getGpuUtilization() reads prevUtilTicks into a local variable before delta calculations.

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

Merge Risk: ⚪ Minimal · up to 0eb5f

The PR makes localized fixes to GPU statistics publication and NetBSD key-value parsing; no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

Poem

A rabbit hops through CPU time,
Splitting pairs on equals fine.
GPU ticks stay fresh and bright,
Volatile fields share their sight.
Small changes keep the counters right! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately identifies that the pull request resolves two remaining findings, but it does not name the affected components or changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 Aug 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.93%. Comparing base (47a09d5) to head (0eb5f0d).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3639      +/-   ##
============================================
- Coverage     79.98%   79.93%   -0.06%     
+ Complexity     4109      203    -3906     
============================================
  Files           697      697              
  Lines         24292    24291       -1     
  Branches       4012     4012              
============================================
- Hits          19430    19416      -14     
- Misses         2724     2736      +12     
- Partials       2138     2139       +1     
Flag Coverage Δ
freebsd 33.51% <100.00%> (-0.02%) ⬇️
linux 37.70% <100.00%> (+<0.01%) ⬆️
macos 38.54% <100.00%> (-0.05%) ⬇️
openbsd 32.70% <100.00%> (-0.01%) ⬇️
solaris 34.21% <100.00%> (-0.01%) ⬇️
windows 45.28% <100.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.

@dbwiddis
dbwiddis merged commit c765385 into oshi:master Aug 15, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the codeql-gpustats branch August 15, 2026 20:45
dbwiddis added a commit that referenced this pull request Aug 15, 2026
Making prevUtilTicks volatile in #3639 traded a CodeQL safe-publication finding
for a Sonar one: S3077 objects that volatile alone does not make a non-primitive
field thread-safe. That is true in general and not here. GpuTicks is @immutable,
a final class whose two long fields are final, so a reader either sees the old
reference or a fully constructed new one; there is no partially published state
for volatile to fail to protect.

Suppressed the way SmcKeyCache's volatile list already is, with the same reason.
dbwiddis added a commit that referenced this pull request Aug 16, 2026
Everything sat under "Bug Fixes and Improvements", which put the release's
headline change in the wrong place and left one entry carrying three
different kinds of change at once.

New Features: the JSpecify adoption, rewritten to lead with what a consumer
gets rather than what the sweep did to the build, and ParseUtil's new
getStringValueOrEmpty, which was a clause in the middle of that paragraph.

Behavior Changes: the null-to-sentinel list, promoted out of its sub-bullet.
Someone whose code tests those getters for null now has dead branches, and
that was the least visible thing in the section. #3616 moves here from Bug
Fixes, being the same kind of change.

Removals: #3608's 14 WMI forwarding shims, Util.isBlankOrUnknown and
ParseUtil.slash, with a note that they sit outside the semver-guaranteed API
and are listed only because removing them can break a compile.

Bug Fixes and Improvements keeps #3589 and #3611/#3612, gains the hardening
work split out of the JSpecify entry, and gains #3639, which had no entry at
all: the Windows GPU utilization path tested its cached tick sample for null
and then dereferenced it in a second read, so a concurrent poll could throw.

Dependency bumps get no entries; none changed anything user-visible.
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