Repository navigation
Correct PowerSupply units on Linux - #3096
Conversation
…ev to the output units specified in the PowerSource interface
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 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). (7)
🔇 Additional comments (3)
📝 WalkthroughWalkthroughIntroduces typed Linux power-supply property handling ( Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.java (1)
236-252:⚠️ Potential issue | 🔴 CriticalPower/current reconciliation zeros valid telemetry.
At Line 250 and Line 251, the
elsebranch clears both values when both are present (or both were already parsed), which drops valid measurements. This should only default to zero when neither signal is available.💡 Proposed fix
- if (psMap.containsKey("POWER_SUPPLY_POWER_NOW")) { - psPowerUsageRate = ParseUtil.parseDoubleOrDefault(psMap.get("POWER_SUPPLY_POWER_NOW"), -1) - / 1000d; - } - - if (psMap.containsKey("POWER_SUPPLY_CURRENT_NOW")) { - psAmperage = ParseUtil.parseDoubleOrDefault(psMap.get("POWER_SUPPLY_CURRENT_NOW"), -1) - / 1000d; - } - - if (psPowerUsageRate < 0 && psAmperage >= 0) { - psPowerUsageRate = psAmperage * psVoltage; - } else if (psPowerUsageRate >= 0 && psAmperage < 0) { - psAmperage = psPowerUsageRate / psVoltage; - } else { - psAmperage = 0; - psPowerUsageRate = 0; - } + double powerNow = psMap.containsKey("POWER_SUPPLY_POWER_NOW") + ? ParseUtil.parseDoubleOrDefault(psMap.get("POWER_SUPPLY_POWER_NOW"), -1) / 1000d + : -1d; + double currentNow = psMap.containsKey("POWER_SUPPLY_CURRENT_NOW") + ? ParseUtil.parseDoubleOrDefault(psMap.get("POWER_SUPPLY_CURRENT_NOW"), -1) / 1000d + : -1d; + + if (powerNow >= 0 && currentNow >= 0) { + psPowerUsageRate = powerNow; + psAmperage = currentNow; + } else if (powerNow >= 0) { + psPowerUsageRate = powerNow; + psAmperage = psPowerUsageRate / psVoltage; + } else if (currentNow >= 0) { + psAmperage = currentNow; + psPowerUsageRate = psAmperage * psVoltage; + } else { + psAmperage = 0; + psPowerUsageRate = 0; + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.java` around lines 236 - 252, The reconciliation logic in LinuxPowerSource that parses psVoltage, psPowerUsageRate and psAmperage currently zeros both values in the final else branch even when valid measurements exist; update the conditional after parsing so that you only default psAmperage = 0 and psPowerUsageRate = 0 when both parsed values are unavailable (e.g., psPowerUsageRate < 0 && psAmperage < 0); otherwise, if one is missing compute it from the other (psPowerUsageRate = psAmperage * psVoltage or psAmperage = psPowerUsageRate / psVoltage) and leave existing parsed values intact. Ensure you modify the block referencing psVoltage, psPowerUsageRate and psAmperage in the LinuxPowerSource class to implement this fix.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.java`:
- Around line 216-227: In LinuxPowerSource, the non-udev parsing mistakenly
assigns POWER_SUPPLY_ENERGY_FULL / _CHARGE_FULL to psCurrentCapacity and
POWER_SUPPLY_ENERGY_FULL_DESIGN / _CHARGE_FULL_DESIGN to psMaxCapacity,
overwriting current and leaving psDesignCapacity wrong; update the assignment so
that POWER_SUPPLY_ENERGY_FULL and POWER_SUPPLY_CHARGE_FULL set psMaxCapacity (or
psCurrentCapacity depending on the intended semantics—use psMaxCapacity for
"full" real capacity) and POWER_SUPPLY_ENERGY_FULL_DESIGN and
POWER_SUPPLY_CHARGE_FULL_DESIGN set psDesignCapacity, ensuring psCurrentCapacity
is set from the correct "remaining" keys (e.g., ENERGY_NOW/CHARGE_NOW) as used
elsewhere in LinuxPowerSource to match udev logic.
- Around line 104-125: The fallback checks are performed after scaling the
parsed values which can turn the -1 sentinel into 0 and skip fallbacks; in
LinuxPowerSource change the logic to first parse raw ints into temporaries using
ParseUtil.parseIntOrDefault(..., -1) (e.g., rawNow, rawFull, rawDesign), test
each raw value for < 0 to decide whether to fall back to the corresponding
CHARGE_* property (and set CapacityUnits when appropriate), and only after
selecting the correct raw value divide by 1000 to assign psCurrentCapacity,
psMaxCapacity, and psDesignCapacity; update uses of device.getPropertyValue and
ParseUtil.parseIntOrDefault accordingly so the fallback behavior works as
intended.
---
Outside diff comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.java`:
- Around line 236-252: The reconciliation logic in LinuxPowerSource that parses
psVoltage, psPowerUsageRate and psAmperage currently zeros both values in the
final else branch even when valid measurements exist; update the conditional
after parsing so that you only default psAmperage = 0 and psPowerUsageRate = 0
when both parsed values are unavailable (e.g., psPowerUsageRate < 0 &&
psAmperage < 0); otherwise, if one is missing compute it from the other
(psPowerUsageRate = psAmperage * psVoltage or psAmperage = psPowerUsageRate /
psVoltage) and leave existing parsed values intact. Ensure you modify the block
referencing psVoltage, psPowerUsageRate and psAmperage in the LinuxPowerSource
class to implement this fix.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6994c12b-73ad-48f8-b1c5-c37cc1361688
📒 Files selected for processing (1)
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.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). (1)
- GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (2)
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxPowerSource.java (2)
129-143: Unit normalization here matches the PowerSource contract.Line 129/Line 130 converts
VOLTAGE_NOWfrom µV to V, and Lines 136/139/142/143 convert current/power from µ-units to milli-units consistently withoshi-core/src/main/java/oshi/hardware/PowerSource.java:91-102.
207-213: Good ENERGY_NOW/CHARGE_NOW scaling and unit tagging in non-udev path.Line 207-213 correctly normalizes to milli-units and updates
CapacityUnitsaccording to source field type.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3096 +/- ##
==========================================
- Coverage 69.49% 69.22% -0.27%
==========================================
Files 35 36 +1
Lines 1839 1875 +36
Branches 297 305 +8
==========================================
+ Hits 1278 1298 +20
- Misses 461 478 +17
+ Partials 100 99 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
dbwiddis
left a comment
There was a problem hiding this comment.
Hey @benplotke thanks for this PR!
This was one of the first classes I wrote a decade ago (yay copyright tombstones) and I'm surprised it took ten years to find this bug! (Technically I left the units "relative" so it wasn't actually a bug, but assigning the correct units is helpful).
There are a few problems with your fix (see below) and some pre-existing bugs we should fix while we're at it.
Also, can you create a changelog entry for this PR?
|
Also seems TEMP is a valid key (tenths of degrees celsius) that we never read/assign. Would be nice to add that. |
|
Hi @dbwiddis, thanks for all the feedback. I am working on the changes you recommended, but I got bogged down trying to setup different platforms to see it working. I hopefully will push the updates tonight. |
|
No worries. Take your time and thanks again for contributing! |
I noticed the units for batteries on Linux were off. I found the incoming units from https://docs.kernel.org/power/power_supply_class.html and added the appropriate conversions to the units specified in the PowerSupply interface.
I checked no tests failed, but I did not add any new tests. Let me know if you want me to add a test or change anything.
Thanks for reviewing my PR!
Summary by CodeRabbit