Skip to content

Extract parsing logic from lspci/lshw commands - #3202

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:3189-extract-lspci-lshw-parsing
Apr 28, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:3189-extract-lspci-lshw-parsing

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 28, 2026 •

Copy link
Copy Markdown
Member

Resolves part of #3189.

Separates command execution from output parsing in 4 methods across 2 classes:

Refactored methods:

  • LinuxGraphicsCard.getGraphicsCardsFromLspci() — lspci -vnnm
  • LinuxGraphicsCard.queryLspciMemorySize() — lspci -v -s
  • Lshw static initializer → new parseSystemInfo(List<String>) — lshw -C system
  • Lshw.queryCpuCapacity() — lshw -class processor

Tests added to LinuxGraphicsCardTest:

  • Single VGA card, two cards, 3D controller parsing from lspci -vnnm
  • Prefetchable memory size parsing, no-prefetchable, empty input

Tests added to LshwTest:

  • System info parsing (model, serial, UUID), partial/empty input
  • CPU capacity parsing, missing capacity line, empty input

No public API changes. No new dependencies.

Summary by CodeRabbit

  • Refactor

    • Restructured graphics card detection processing to improve code modularity and testability
    • Enhanced system information extraction with input-driven parsing mechanisms
  • Tests

    • Added comprehensive test coverage for graphics card detection and parsing scenarios
    • Introduced unit tests for system information parsing with edge cases and various input conditions

Separate command execution from output parsing in 4 methods across 2
classes, enabling deterministic testing with fixture data:

- LinuxGraphicsCard.getGraphicsCardsFromLspci() — lspci -vnnm
- LinuxGraphicsCard.queryLspciMemorySize() — lspci -v -s
- Lshw static initializer (parseSystemInfo) — lshw -C system
- Lshw.queryCpuCapacity() — lshw -class processor

Each method now has a package-private overload accepting List<String>,
with the public/private method delegating after running the command.
No public API changes.

Added fixture-based tests covering lspci VGA/3D controller parsing,
prefetchable memory size parsing, lshw system info and CPU capacity.
@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9189185a-6717-4338-b21c-f55c6c7597b2

📥 Commits

Reviewing files that changed from the base of the PR and between f09ca97 and fdca88d.

📒 Files selected for processing (4)
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/util/driver/linux/Lshw.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxGraphicsCardTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/LshwTest.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). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
🧠 Learnings (7)
📚 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-common/src/main/java/oshi/util/driver/linux/Lshw.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/util/driver/linux/Lshw.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
📚 Learning: 2026-04-19T01:28:47.155Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3174
File: oshi-benchmark/src/test/java/oshi/comparison/WmiComparisonTest.java:237-257
Timestamp: 2026-04-19T01:28:47.155Z
Learning: In `oshi-benchmark/src/test/java/oshi/comparison/WmiComparisonTest.java`, the WMI comparison tests (e.g., `testProcessor`) intentionally validate only index 0 and a few representative fields per WMI data type (String, uint16, uint32, uint64). Exhaustive row-by-row comparison is deliberately avoided for numerically heavy multi-row tables. The design rationale is: matching row counts + a few selected fields covering each data type is sufficient to verify JNA vs FFM driver parity. Do not suggest looping over all rows for these tests.

Applied to files:

  • oshi-common/src/test/java/oshi/util/driver/linux/LshwTest.java
📚 Learning: 2026-04-18T03:18:06.205Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3170
File: oshi-benchmark/src/test/java/oshi/comparison/PerfmonComparisonTest.java:177-183
Timestamp: 2026-04-18T03:18:06.205Z
Learning: In `oshi-benchmark/src/test/java/oshi/comparison/PerfmonComparisonTest.java`, GPU engine PDH instance counts (from `GpuInformationJNA.queryGpuEngineCounters()` / `GpuInformationFFM.queryGpuEngineCounters()`) are highly volatile — counts can differ dramatically (e.g., 53 vs 40) even between queries taken milliseconds apart. Do not assert instance count equality or exact instance list matching between JNA and FFM results. The only reliable assertion is that both are empty or both are non-empty (`assertThat(ffm.getA().isEmpty()).isEqualTo(jna.getA().isEmpty())`).

Applied to files:

  • oshi-common/src/test/java/oshi/util/driver/linux/LshwTest.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxGraphicsCardTest.java
📚 Learning: 2026-04-11T05:51:52.684Z
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:52.684Z
Learning: In OSHI's `LinuxGpuStats` (oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java), the abstract `nvml*` methods (nvmlGetVramUsed, nvmlGetCoreClockMhz, nvmlGetMemoryClockMhz, nvmlGetTemperature, nvmlGetPowerDraw, nvmlGetFanSpeedPercent) are contractually expected to return `-1` (long or double) on failure. The base class applies `if (val >= 0)` guards before using any NVML result and falls through to sysfs/hwmon paths when negative. Subclasses (LinuxGpuStatsJNA, LinuxGpuStatsFFM) correctly pass through the -1 sentinel unchanged — do not flag this as a missing clamp or contract violation.

Applied to files:

  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxGraphicsCardTest.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
📚 Learning: 2026-04-14T23:28:18.252Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3160
File: oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxSensorsTest.java:34-40
Timestamp: 2026-04-14T23:28:18.252Z
Learning: In oshi/oshi, `vcgencmd` (used in `LinuxSensors.queryCpuTemperatureFromVcGenCmd()` and related methods) is a Raspberry Pi-specific binary. It will never be present on standard x86/amd64 CI runners or developer machines. Tests that pass `isPi=true` to `LinuxSensors` and assert 0/empty returns are deterministic in practice; do not flag them as potentially flaky due to `vcgencmd` availability.

Applied to files:

  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxGraphicsCardTest.java
📚 Learning: 2026-04-06T22:30:07.083Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:215-234
Timestamp: 2026-04-06T22:30:07.083Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`, the `found` flag in `getControllerIdByLocation` is intentionally set to `true` as soon as either `vendor-id` or `device-id` is found in a matching service's parent. This is a deliberate design choice: a partial match (one of the two IDs) is preferred over continuing to iterate, because iterating further risks pairing IDs from two different and potentially mismatched services. Do not flag this as a bug or suggest splitting into separate `foundVendor`/`foundProduct` flags.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
🔇 Additional comments (4)
oshi-common/src/test/java/oshi/util/driver/linux/LshwTest.java (1)

30-94: Good split between fixture tests and live smoke checks.

The parser assertions are deterministic, and keeping the command-backed checks inside the Linux-only nested class keeps the new unit-style coverage clean.

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

189-269: Solid coverage for the new parsing paths.

The stubbed lookups keep the lspci cases deterministic, and the existing temp-dir tests still cover the sysfs helpers without pulling in live command execution.

oshi-common/src/main/java/oshi/util/driver/linux/Lshw.java (1)

26-31: The static initializer handles missing lshw gracefully. When ExecutingCommand.runPrivilegedNative() fails (including when lshw is unavailable), it returns an empty list. The parseSystemInfo() method iterates over an empty list, skips all parsing logic, and returns a triplet of nulls. The static fields MODEL, SERIAL, and UUID are then initialized to null, which matches the documented behavior ("null for missing values"). No changes needed.

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

175-236: The drmLookup contract is null-safe by design.

The asymmetry you noticed—vramLookup has a null guard but drmLookup doesn't—is intentional. The findDrmInfo(String pciSlot) method is explicitly documented to accept null (javadoc: "or {@code null} to use first-match") and handles it safely with a fallback mechanism. It always returns a Triplet<String, String, String> (never null), even when pciSlot is null. No NPE risk here; this is the intended API contract.


📝 Walkthrough

Walkthrough

Refactored Linux graphics card and system info discovery to decouple command execution from parsing. Added package-private overloads accepting precomputed output and injected lookup functions, enabling testability without system calls. Existing public methods delegate to new parsing logic.

Changes

Cohort / File(s) Summary
Source Refactoring
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java, oshi-common/src/main/java/oshi/util/driver/linux/Lshw.java
Added new package-private overloads that accept precomputed command output (List<String>), enabling injection of lookup functions and test-friendly parsing. Original command-executing methods now delegate to these new helpers.
Test Coverage
oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxGraphicsCardTest.java, oshi-common/src/test/java/oshi/util/driver/linux/LshwTest.java
Comprehensive test fixtures for parsing logic, covering single/multiple cards, empty input, prefetchable memory summation, and CPU capacity extraction. Validators added for partial/missing fields and cross-platform test isolation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Poem

🔧 Lines of lspci now dance with grace,
No command calls clog the parsing space—
Tests rejoice, injection flows so free,
Where output meets logic, decoupled, clean, and glee! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.36% 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 summarizes the main refactoring: extracting parsing logic from command execution across lspci and lshw methods.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.58824% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.61%. Comparing base (f09ca97) to head (fdca88d).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...dware/common/platform/linux/LinuxGraphicsCard.java 37.50% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3202      +/-   ##
==========================================
+ Coverage   58.39%   58.61%   +0.22%     
==========================================
  Files         421      421              
  Lines       20351    20355       +4     
  Branches     3576     3576              
==========================================
+ Hits        11883    11932      +49     
+ Misses       6716     6674      -42     
+ Partials     1752     1749       -3     
Flag Coverage Δ
linux 24.56% <70.58%> (+0.20%) ⬆️
macos 27.84% <58.82%> (+0.33%) ⬆️
windows 36.91% <58.82%> (+0.35%) ⬆️

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.

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