Skip to content

Refactor FFM PDH utility and remove dead constants - #3165

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:simplify-perfdata-util
Apr 17, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:simplify-perfdata-util

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Rename PdhUtilFFM to PerfDataUtilFFM, paralleling PerfDataUtil on the JNA side. Generalize the hardcoded handle count query into a reusable queryCounter(object, instance, counter) method. Update the caller in WindowsFileSystemFFM to use constants from PerfmonConstants and HandleCountProperty instead of hardcoded strings.

Remove unused delegated constants from PerfCounterQuery that were left over from the perfmon enum refactoring to oshi-common.

Changes

  • PdhUtilFFM → PerfDataUtilFFM: Renamed and generalized getOpenFileDescriptors() into queryCounter(String object, String instance, String counter) that builds the counter path from parameters and returns the value (or -1 on failure). Fixed resource safety: query handle is null-checked in finally to handle the case where queryPtr.get() throws after PdhOpenQuery succeeds.
  • WindowsFileSystemFFM: Updated to call PerfDataUtilFFM.queryCounter(PROCESS, TOTAL_INSTANCE, HandleCountProperty.HANDLECOUNT.getCounter()) using shared constants.
  • PerfCounterQuery: Removed 5 dead delegated constants (TOTAL_INSTANCE, TOTAL_OR_IDLE_INSTANCES, TOTAL_INSTANCES, NOT_TOTAL_INSTANCE, NOT_TOTAL_INSTANCES) that just pointed to PerfmonConstants with no remaining callers.

Summary by CodeRabbit

  • Refactor
    • Optimized Windows file descriptor tracking through improved performance counter query mechanisms.

Rename PdhUtilFFM to PerfDataUtilFFM, paralleling PerfDataUtil on the
JNA side. Generalize the hardcoded handle count query into a reusable
queryCounter(object, instance, counter) method. Update the caller in
WindowsFileSystemFFM to use constants from PerfmonConstants and
HandleCountProperty instead of hardcoded strings.

Remove unused delegated constants from PerfCounterQuery that were
left over from the perfmon enum refactoring to oshi-common.
@coderabbitai

coderabbitai Bot commented Apr 17, 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: 821f7d40-300a-4d62-b822-b7abaace9561

📥 Commits

Reviewing files that changed from the base of the PR and between fa2cebe and f9ea8af.

📒 Files selected for processing (3)
  • oshi-core-java25/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java
  • oshi-core-java25/src/main/java/oshi/util/platform/windows/PerfDataUtilFFM.java
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQuery.java
💤 Files with no reviewable changes (1)
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQuery.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). (9)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
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`.
📚 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/software/os/windows/WindowsFileSystemFFM.java
  • oshi-core-java25/src/main/java/oshi/util/platform/windows/PerfDataUtilFFM.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/software/os/windows/WindowsFileSystemFFM.java
  • oshi-core-java25/src/main/java/oshi/util/platform/windows/PerfDataUtilFFM.java
🔇 Additional comments (2)
oshi-core-java25/src/main/java/oshi/util/platform/windows/PerfDataUtilFFM.java (1)

44-83: Clean generalization; lifecycle and logging look correct.

The path construction, null-guarded PdhCloseQuery in finally, and SLF4J format (2 placeholders + trailing Throwable for stacktrace) are all well-formed. Returning -1 on failure is consistent with the prior FFM contract.

One minor note (non-blocking): queryPtr.get(ADDRESS, 0) could in principle yield MemorySegment.NULL (not Java null), but since PdhOpenQuery success guarantees a valid handle, the current guard is sufficient in practice.

oshi-core-java25/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java (1)

307-310: LGTM — semantics match the JNA implementation.

Querying \Process(_Total)\Handle Count via PDH returns the system-wide handle total, equivalent to summing per-process HandleCount values as the JNA WindowsFileSystem.getOpenFileDescriptors() does. Note a minor behavioral divergence: on PDH failure this returns -1 whereas the JNA variant returns 0; this matches the prior FFM behavior and is a reasonable "unknown" sentinel, but worth being aware of for consumers expecting the JNA semantics.


📝 Walkthrough

Walkthrough

This refactor consolidates Windows performance counter querying logic by replacing the specialized PdhUtilFFM.getOpenFileDescriptors() method with a generic PerfDataUtilFFM.queryCounter() method. Duplicate constants from PerfCounterQuery are removed as they delegate to PerfmonConstants.

Changes

Cohort / File(s) Summary
PDH Counter Query Refactor
oshi-core-java25/src/main/java/oshi/util/platform/windows/PerfDataUtilFFM.java
Class renamed from PdhUtilFFM to PerfDataUtilFFM. Removed getOpenFileDescriptors() and added generic queryCounter(String object, String instance, String counter) method that constructs PDH counter paths, executes queries, and returns formatted counter values or -1 on error.
Counter Query Integration
oshi-core-java25/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java
Updated getOpenFileDescriptors() to invoke PerfDataUtilFFM.queryCounter() with PROCESS, TOTAL_INSTANCE, and HandleCountProperty.HANDLECOUNT instead of delegating to the removed PdhUtilFFM method.
Duplicate Constant Removal
oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQuery.java
Removed five public static constants (TOTAL_INSTANCE, TOTAL_OR_IDLE_INSTANCES, TOTAL_INSTANCES, NOT_TOTAL_INSTANCE, NOT_TOTAL_INSTANCES) that previously delegated to PerfmonConstants.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Poem

🐰 A counter query, once specialized and long,

Now generic and strong, queryCounter sings the song—

No more scattered constants, duplicates removed with care,

PDH paths dance through the refactored air! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the two main objectives: refactoring the FFM PDH utility class and removing dead constants, which matches the substantive changes across the three files.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ 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 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.11%. Comparing base (b56d9af) to head (f9ea8af).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...va/oshi/util/platform/windows/PerfDataUtilFFM.java 80.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3165      +/-   ##
==========================================
- Coverage   58.14%   58.11%   -0.03%     
==========================================
  Files         324      324              
  Lines       17375    17382       +7     
  Branches     3066     3068       +2     
==========================================
- Hits        10102    10101       -1     
- Misses       5788     5805      +17     
+ Partials     1485     1476       -9     
Flag Coverage Δ
linux 26.52% <0.00%> (-0.07%) ⬇️
macos 29.81% <0.00%> (-0.03%) ⬇️
windows 32.14% <81.25%> (+0.03%) ⬆️

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