Skip to content

Reduce Auxv duplication with functional interface - #3263

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/auxv-functional-interface
May 18, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/auxv-functional-interface

Conversation

@dbwiddis

@dbwiddis dbwiddis commented May 18, 2026 •

Copy link
Copy Markdown
Member

Closes #3262

Introduces a NativeLongReader functional interface in the common Auxv class and moves the buffer-reading loop into Auxv.queryAuxv(NativeLongReader). The JNA and FFM subclasses now each supply their reader as a one-liner delegation.

Changes:

  • Auxv.java (oshi-common): Added NativeLongReader interface and queryAuxv() method
  • AuxvJNA.java: Reduced to single delegation call
  • AuxvFFM.java: Reduced to single delegation call
  • New AuxvTest.java in oshi-common testing the shared implementation

Summary by CodeRabbit

  • New Features

    • Added a Linux auxiliary-vector query capability exposing system auxiliary data and metrics.
  • Refactor

    • Consolidated parsing into a shared helper and updated implementations to delegate to it for consistency and reduced duplication.
  • Tests

    • Added Linux-focused tests to validate retrieval of auxiliary vector entries and key system parameters.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

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: def536a7-eae3-4942-aea0-1420600d2ee7

📥 Commits

Reviewing files that changed from the base of the PR and between 426c370 and ed500a6.

📒 Files selected for processing (4)
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-core-ffm/src/main/java/oshi/driver/linux/proc/AuxvFFM.java
  • oshi-core/src/main/java/oshi/driver/linux/proc/AuxvJNA.java
🚧 Files skipped from review as they are similar to previous changes (4)
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-core-ffm/src/main/java/oshi/driver/linux/proc/AuxvFFM.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java
  • oshi-core/src/main/java/oshi/driver/linux/proc/AuxvJNA.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). (12)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)

📝 Walkthrough

Walkthrough

Consolidates duplicate /proc/self/auxv parsing into a shared Auxv.queryAuxv(NativeLongReader) in oshi-common; adds a Linux JUnit test and updates AuxvJNA and AuxvFFM to delegate to the shared implementation with their platform readers.

Changes

Auxiliary Vector Parsing Consolidation

Layer / File(s) Summary
Shared Auxv contract & implementation
oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java
Adds NativeLongReader functional interface, implements queryAuxv(NativeLongReader) to read /proc/self/auxv into a ByteBuffer, parse native-long key/value pairs until AT_NULL, and return a Map<Integer,Long>. Updates Javadoc and marks Auxv @ThreadSafe.
Auxv parsing validation
oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
Adds a Linux-only JUnit 5 test selecting the platform reader and asserting queryAuxv(...) returns a non-null map containing positive AT_CLKTCK and AT_PAGESZ values.
JNA and FFM drivers delegate to shared Auxv
oshi-core/src/main/java/oshi/driver/linux/proc/AuxvJNA.java, oshi-core-ffm/src/main/java/oshi/driver/linux/proc/AuxvFFM.java
Replace inline parsing loops with single calls to Auxv.queryAuxv(...), passing FileUtilJNA/FileUtilFFM readers; remove now-unused imports and duplicated parsing code.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • oshi/oshi#3135: Earlier work splitting/introducing Auxv variants that this consolidation builds on.
  • oshi/oshi#3136: Uses Auxv parsing results (USER_HZ, PAGE_SIZE); related to how callers consume AT_CLKTCK/AT_PAGESZ.

Poem

🐰 I nibble bytes in the proc-tree night,

Tiny keys and longs in soft moonlight.
JNA and FFM, once scattered apart,
Now hop in a line — one shared beating heart.
Hooray, one query, quick as a cart!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.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 directly and concisely summarizes the main change: introducing a functional interface to reduce code duplication in the Auxv driver implementation across JNA and FFM variants.
Linked Issues check ✅ Passed The implementation fully addresses issue #3262: introduces NativeLongReader functional interface in common Auxv class, moves buffer-reading loop into parameterized queryAuxv() method, and both JNA/FFM subclasses now delegate via single-line calls, eliminating duplication.
Out of Scope Changes check ✅ Passed All changes are directly aligned with the stated objectives: the new Auxv interface and method in oshi-common, test coverage in AuxvTest, and refactored delegations in AuxvJNA and AuxvFFM all serve the core goal of reducing duplication through parameterization.

✏️ 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 May 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.15%. Comparing base (f04f06b) to head (ed500a6).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3263      +/-   ##
============================================
- Coverage     74.26%   74.15%   -0.11%     
  Complexity       73       73              
============================================
  Files           396      397       +1     
  Lines         16096    16091       -5     
  Branches       2589     2587       -2     
============================================
- Hits          11954    11933      -21     
- Misses         2631     2639       +8     
- Partials       1511     1519       +8     
Flag Coverage Δ
linux 38.60% <100.00%> (-0.02%) ⬇️
macos 39.30% <0.00%> (+0.01%) ⬆️
windows 46.98% <0.00%> (-0.09%) ⬇️

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.

@dbwiddis
dbwiddis force-pushed the refactor/auxv-functional-interface branch from 7fa3276 to 426c370 Compare May 18, 2026 18:27

@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 (2)
oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java (1)

19-27: ⚡ Quick win

Gate or adapt this test for native-long width.

Line 24 hardcodes an 8-byte reader, but the class is only Linux-gated. On 32-bit Linux, auxv entries are native-long width and this test can misparse data. Please gate to 64-bit or switch to a width-aware test reader.

Example gating approach
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.EnabledOnOs;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
 import org.junit.jupiter.api.condition.OS;
@@
 `@EnabledOnOs`(OS.LINUX)
+@EnabledIfSystemProperty(named = "sun.arch.data.model", matches = "64")
 class AuxvTest {
🤖 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/test/java/oshi/util/driver/linux/proc/AuxvTest.java` around
lines 19 - 27, The test AuxvTest currently calls
Auxv.queryAuxv(FileUtil::readLongFromBuffer) which assumes native longs are 8
bytes; on 32-bit Linux this will misparse auxv. Update the test to either gate
it to 64-bit JVMs (e.g. only run when Native long size == 8) or pass a
width-aware reader to Auxv.queryAuxv that reads native-long-sized values based
on Platform.nativePointerSize (or equivalent); adjust assertions using
Auxv.AT_CLKTCK to use the parsed map from the corrected reader and ensure the
test only asserts greaterThan(0L) when running with a 64-bit native-long reader.
oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java (1)

60-65: ⚡ Quick win

Add a defensive guard for truncated auxv buffers.

queryAuxv assumes AT_NULL is always reachable. If the buffer is truncated/corrupt, Line 61 or Line 63 can throw during read and fail the call. Consider handling underflow and returning parsed entries safely.

Proposed patch
+import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
@@
-        int key;
-        do {
-            key = (int) reader.read(buff);
-            if (key != AT_NULL) {
-                auxvMap.put(key, reader.read(buff));
-            }
-        } while (key != AT_NULL);
+        try {
+            int key;
+            do {
+                key = (int) reader.read(buff);
+                if (key != AT_NULL) {
+                    auxvMap.put(key, reader.read(buff));
+                }
+            } while (key != AT_NULL);
+        } catch (BufferUnderflowException e) {
+            // Return entries parsed so far if auxv is truncated/malformed.
+        }
         return auxvMap;
🤖 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/util/driver/linux/proc/Auxv.java` around lines
60 - 65, The loop in Auxv.queryAuxv assumes AT_NULL will be read and performs
two unguarded reads (for key and value) which can throw or underflow on
truncated/corrupt buffers; modify the do/while to check that the reader has
enough remaining bytes before each reader.read(buff) (or verify reader.read
returns the expected number of bytes) and break out gracefully if there isn’t
enough data, ensuring auxvMap is returned with the entries parsed so far; update
the block around the key read and the subsequent value read (the variables key
and the auxvMap.put(...) call) to perform these bounds/return-value checks and
avoid throwing on truncated buffers.
🤖 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/util/driver/linux/proc/Auxv.java`:
- Around line 60-65: The loop in Auxv.queryAuxv assumes AT_NULL will be read and
performs two unguarded reads (for key and value) which can throw or underflow on
truncated/corrupt buffers; modify the do/while to check that the reader has
enough remaining bytes before each reader.read(buff) (or verify reader.read
returns the expected number of bytes) and break out gracefully if there isn’t
enough data, ensuring auxvMap is returned with the entries parsed so far; update
the block around the key read and the subsequent value read (the variables key
and the auxvMap.put(...) call) to perform these bounds/return-value checks and
avoid throwing on truncated buffers.

In `@oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java`:
- Around line 19-27: The test AuxvTest currently calls
Auxv.queryAuxv(FileUtil::readLongFromBuffer) which assumes native longs are 8
bytes; on 32-bit Linux this will misparse auxv. Update the test to either gate
it to 64-bit JVMs (e.g. only run when Native long size == 8) or pass a
width-aware reader to Auxv.queryAuxv that reads native-long-sized values based
on Platform.nativePointerSize (or equivalent); adjust assertions using
Auxv.AT_CLKTCK to use the parsed map from the corrected reader and ensure the
test only asserts greaterThan(0L) when running with a 64-bit native-long reader.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 690ba4a7-cb1e-471a-8085-452adfc95236

📥 Commits

Reviewing files that changed from the base of the PR and between f04f06b and 426c370.

📒 Files selected for processing (4)
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-core-ffm/src/main/java/oshi/driver/linux/proc/AuxvFFM.java
  • oshi-core/src/main/java/oshi/driver/linux/proc/AuxvJNA.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). (4)
  • GitHub Check: Analyze (java)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Codacy Static Code Analysis
🧰 Additional context used
🧠 Learnings (1)
📚 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/util/driver/linux/proc/Auxv.java
🔇 Additional comments (3)
oshi-common/src/main/java/oshi/util/driver/linux/proc/Auxv.java (1)

16-47: LGTM!

oshi-core/src/main/java/oshi/driver/linux/proc/AuxvJNA.java (1)

29-29: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/linux/proc/AuxvFFM.java (1)

29-29: LGTM!

Move the auxv buffer-reading logic into a shared queryAuxv(NativeLongReader)
method in oshi-common. AuxvJNA and AuxvFFM now delegate to it, each supplying
their platform-specific native-long reader as a lambda/method reference.
@dbwiddis
dbwiddis force-pushed the refactor/auxv-functional-interface branch from 426c370 to ed500a6 Compare May 18, 2026 18:46
@dbwiddis
dbwiddis merged commit d2746f2 into oshi:master May 18, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the refactor/auxv-functional-interface branch May 18, 2026 18:57
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.

Reduce duplication: merge trivial drivers like Auxv into single parameterized implementation

1 participant