Repository navigation
Consolidate Windows CentralProcessor tick query into common base (JNA/FFM dedup) - #3389
Conversation
queryProcessorCpuLoadTicks() was duplicated nearly verbatim between the JNA and FFM subclasses, differing only in the perfmon driver call. Move it, the memoized processorUtilityCounters supplier, and the initial- counter bootstrap into the WindowsCentralProcessor base (which already owns the related utility-counter state and processTickData), behind two thin driver hooks: queryProcessorCapacityCounters() and queryProcessorCounters(). The previously-abstract queryProcessorUtility- Counters() becomes a concrete delegate. Eliminates ~70 duplicated lines; the JNA and FFM subclasses keep only their native-specific code.
📝 WalkthroughWalkthroughThis PR refactors Windows CPU counter querying to centralize memoization logic in the base ChangesWindows CPU Counter Query Architecture Refactoring
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.java (1)
54-64: DeferqueryProcessorCapacityCounters()call during initialization to avoid constructor-time virtual dispatch
WindowsCentralProcessor’s instance initializer callsprocessorUtilityCounters.get()(which invokesqueryProcessorCapacityCounters()virtually) while the object is still being constructed. JNA/FFM overrides are currently pure static delegates, so this is safe today, but it creates a fragile base-class contract: a future backend that touches instance state inqueryProcessorCapacityCounters()could break during partial construction. SincequeryProcessorCpuLoadTicks()directly readsgetInitialUtilityCounters()to populate theinit*lists underUSE_CPU_UTILITY, any refactor should lazily initializeinitialUtilityCountersbefore those reads (e.g., at the start ofqueryProcessorCpuLoadTicks()).♻️ Possible shape
- { - // Store the initial query and start the memoizer expiration - if (USE_CPU_UTILITY) { - setInitialUtilityCounters(processorUtilityCounters.get().getB()); - } - } + private void ensureInitialUtilityCounters() { + if (USE_CPU_UTILITY && getInitialUtilityCounters() == null) { + setInitialUtilityCounters(processorUtilityCounters.get().getB()); + } + }Then call
ensureInitialUtilityCounters()before reading the initial lists inqueryProcessorCpuLoadTicks().🤖 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/hardware/common/platform/windows/WindowsCentralProcessor.java` around lines 54 - 64, The instance initializer currently calls processorUtilityCounters.get() (which invokes queryProcessorCapacityCounters()) during construction; defer that call by removing the get() call from the initializer and instead add a private ensureInitialUtilityCounters() that checks whether getInitialUtilityCounters() is populated and if not invokes processorUtilityCounters.get() and calls setInitialUtilityCounters(...) with the result; then call ensureInitialUtilityCounters() at the start of queryProcessorCpuLoadTicks() (before any reads of the init* lists) so initialUtilityCounters are lazily initialized and no virtual dispatch occurs during object construction. Reference symbols: processorUtilityCounters, queryProcessorCapacityCounters, setInitialUtilityCounters, getInitialUtilityCounters, ensureInitialUtilityCounters (new), and queryProcessorCpuLoadTicks.
🤖 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/hardware/common/platform/windows/WindowsCentralProcessor.java`:
- Around line 54-64: The instance initializer currently calls
processorUtilityCounters.get() (which invokes queryProcessorCapacityCounters())
during construction; defer that call by removing the get() call from the
initializer and instead add a private ensureInitialUtilityCounters() that checks
whether getInitialUtilityCounters() is populated and if not invokes
processorUtilityCounters.get() and calls setInitialUtilityCounters(...) with the
result; then call ensureInitialUtilityCounters() at the start of
queryProcessorCpuLoadTicks() (before any reads of the init* lists) so
initialUtilityCounters are lazily initialized and no virtual dispatch occurs
during object construction. Reference symbols: processorUtilityCounters,
queryProcessorCapacityCounters, setInitialUtilityCounters,
getInitialUtilityCounters, ensureInitialUtilityCounters (new), and
queryProcessorCpuLoadTicks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ce05ae4a-1a98-4d97-a9d9-d6121a8562f9
📒 Files selected for processing (3)
oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.javaoshi-core/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorJNA.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). (18)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 26, windows-latest
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, ubuntu-latest
- GitHub Check: Test JDK 21, ubuntu-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, macos-latest
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2026-04-06T04:45:47.644Z
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:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).
Applied to files:
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorJNA.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/windows/WindowsCentralProcessor.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/windows/WindowsCentralProcessor.java
🔇 Additional comments (3)
oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.java (1)
179-262: LGTM!oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorJNA.java (1)
277-284: LGTM!oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.java (1)
307-315: LGTM!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3389 +/- ##
============================================
- Coverage 75.19% 75.14% -0.05%
Complexity 76 76
============================================
Files 578 578
Lines 21502 21455 -47
Branches 3563 3560 -3
============================================
- Hits 16169 16123 -46
+ Misses 3311 3305 -6
- Partials 2022 2027 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
queryProcessorCpuLoadTicks()was duplicated nearly verbatim betweenWindowsCentralProcessorJNAandWindowsCentralProcessorFFM, differing only in the perfmon driver call.processorUtilityCounterssupplier and the initial-counter bootstrap block — into theWindowsCentralProcessorbase, which already owns the related utility-counter state (initialUtilityCounters,lazilyCalculateMultiplier,processTickData).queryProcessorCapacityCounters()(the memoized utility source) andqueryProcessorCounters()(the non-utility tick source). The previously-abstractqueryProcessorUtilityCounters()becomes a concrete delegate.Test plan
windows-latest (+ JNA==FFM)CI job (cannot be exercised locally on non-Windows)Summary by CodeRabbit
Performance
Refactor