Discharge rate is reported wrong on a two-battery system because "true 0W" is not allowed by heuristics
Submitted by Tomislav Ivek
Assigned to Richard Hughes @hughsient
Link to original bug (#102273)
Description
Created attachment 133575 allow 0W battery discharge rate to be reported
On battery power but with one out of two batteries unused, the discharge rate of the unused battery is wrongly reported as non-zero, instead of true 0W. This is due to bad heuristics in upower/src/linux/up-device-supply.c, function up_device_supply_calculate_rate(), possible fix attached.
Details:
On my laptop it is by design that one of the two batteries is actively discharging while the other is waiting and not used. Which battery is being used in a given moment is decided by hardware. While disconnected from ac, upower however reports the unused battery's discharge rate to be non-zero even though it is actually 0W.
Steps to reproduce while not on ac power:
$ upower -v UPower client version 0.99.5 UPower daemon version 0.99.5
$ cat /sys/class/power_supply/BAT0/power_now # actively discharging 3580000
$ cat /sys/class/power_supply/BAT1/power_now # not in use, waiting 0
$ upower -d Device: /org/freedesktop/UPower/devices/line_power_AC native-path: AC power supply: yes updated: Čet 17 Kol 2017 13:00:11 (1148 seconds ago) has history: no has statistics: no line-power warning-level: none online: no icon-name: 'ac-adapter-symbolic'
Device: /org/freedesktop/UPower/devices/battery_BAT0 native-path: BAT0 vendor: SANYO model: 01AV419 serial: 795 power supply: yes updated: Čet 17 Kol 2017 13:18:11 (68 seconds ago) has history: yes has statistics: yes battery present: yes rechargeable: yes state: discharging warning-level: none energy: 12,43 Wh energy-empty: 0 Wh energy-full: 22,16 Wh energy-full-design: 24,02 Wh energy-rate: 5,784 W voltage: 11,83 V time to empty: 2,1 hours percentage: 56% capacity: 92,2565% technology: lithium-ion icon-name: 'battery-good-symbolic' History (charge): 1502968691 56,000 discharging History (rate): 1502968691 5,784 discharging
Device: /org/freedesktop/UPower/devices/battery_BAT1 native-path: BAT1 vendor: SANYO model: 01AV422 serial: 723 power supply: yes updated: Čet 17 Kol 2017 13:18:11 (68 seconds ago) has history: yes has statistics: yes battery present: yes rechargeable: yes state: discharging warning-level: none energy: 2,88 Wh energy-empty: 0 Wh energy-full: 22,05 Wh energy-full-design: 24,02 Wh energy-rate: 11,187 W voltage: 11,29 V time to empty: 15,4 minutes percentage: 13% capacity: 91,7985% technology: lithium-ion icon-name: 'battery-low-symbolic'
Device: /org/freedesktop/UPower/devices/DisplayDevice power supply: yes updated: Čet 17 Kol 2017 13:18:11 (68 seconds ago) has history: no has statistics: no battery present: yes state: discharging warning-level: none energy: 15,31 Wh energy-full: 44,21 Wh energy-rate: 16,971 W time to empty: 54,1 minutes percentage: 34,6302% icon-name: 'battery-good-symbolic'
Daemon: daemon-version: 0.99.5 on-battery: yes lid-is-closed: no lid-is-present: yes critical-action: Hibernate
Actual results: upower reports the discharge energy-rate of /org/freedesktop/UPower/devices/battery_BAT1 as non-zero, even though /sys/class/power_supply/BAT0/power_now shows 0. Because of that the total discharge rate and total time to empty are wrong, as verified by powertop and wall clock. This negatively impacts the user experience ("do I have 10 or 30 minutes left?")
Expected results: When disconnected from ac, discharge rate of the unused battery should be reported as true 0W.
Possible fix: The cause can be found in upower/src/linux/up-device-supply.c. The function up_device_supply_calculate_rate() assumes that calculated zero discharge rate must be wrong and keeps reporting the old values, whatever those may be. I am currently testing the attached patch that allows "true" 0W discharge to be reported, so far with success.
Patch 133575, "allow 0W battery discharge rate to be reported":
2ndbat.patch