Bug 79200

Summary: UPower doesn't detect when battery state changes
Product: upower Reporter: Alexander Mezin <mezin.alexander>
Component: generalAssignee: Richard Hughes <richard>
Status: RESOLVED MOVED QA Contact:
Severity: normal    
Priority: medium CC: bugzilla, dcm-bugzilla, evangelos, freedesktop, IvanMalison
Version: unspecified   
Hardware: All   
OS: Linux (All)   
Whiteboard:
i915 platform: i915 features:

Description Alexander Mezin 2014-05-25 06:58:19 UTC
When I unplug AC, on-battery is still 'false'.
on-battery changes to 'true' only when battery discharge is detected.

upower -d immediately after unplug:

Device: /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               LGC
  model:                AP13B8K
  serial:               16155
  power supply:         yes
  updated:              Вс 25 май 2014 13:50:38 (91 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               fully-charged
    warning-level:       none
    energy:              54,5072 Wh
    energy-empty:        0 Wh
    energy-full:         54,5072 Wh
    energy-full-design:  53,352 Wh
    energy-rate:         0 W
    voltage:             17,008 V
    percentage:          100%
    capacity:            100%
    technology:          lithium-ion
    icon-name:          'battery-full-charged-symbolic'
  History (charge):
    1401000638  100,000 fully-charged

Device: /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Вс 25 май 2014 13:50:38 (91 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/DisplayDevice
  power supply:         yes
  updated:              Чт 01 янв 1970 06:00:00 (1401000729 seconds ago)
  has history:          no
  has statistics:       no
  battery
    present:             yes
    state:               fully-charged
    warning-level:       none
    energy:              54,5072 Wh
    energy-full:         54,5072 Wh
    energy-rate:         0 W
    percentage:          100%
    icon-name:          'battery-full-charged-symbolic'

Daemon:
  daemon-version:  0.99.0
  on-battery:      no
  lid-is-closed:   no
  lid-is-present:  yes
  is-docked:       no
  critical-action: HybridSleep

---------------------------------------------------------------------------

upower -d later:

Device: /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               LGC
  model:                AP13B8K
  serial:               16155
  power supply:         yes
  updated:              Вс 25 май 2014 13:52:10 (8 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              53,352 Wh
    energy-empty:        0 Wh
    energy-full:         54,5072 Wh
    energy-full-design:  53,352 Wh
    energy-rate:         10,6248 W
    voltage:             16,717 V
    time to empty:       5,0 hours
    percentage:          97%
    capacity:            100%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1401000730  97,000  discharging
    1401000638  100,000 fully-charged
  History (rate):
    1401000730  10,625  discharging

Device: /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Вс 25 май 2014 13:50:38 (100 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/DisplayDevice
  power supply:         yes
  updated:              Чт 01 янв 1970 06:00:00 (1401000738 seconds ago)
  has history:          no
  has statistics:       no
  battery
    present:             yes
    state:               discharging
    warning-level:       none
    energy:              53,352 Wh
    energy-full:         54,5072 Wh
    energy-rate:         10,6248 W
    time to empty:       5,0 hours
    percentage:          97%
    icon-name:          'battery-full-symbolic'

Daemon:
  daemon-version:  0.99.0
  on-battery:      yes
  lid-is-closed:   no
  lid-is-present:  yes
  is-docked:       no
  critical-action: HybridSleep
Comment 1 Alexander Mezin 2014-05-26 13:04:58 UTC
When I execute 'acpi -b' after I unplug AC, but before daemon's on-battery property changes, I see "discharging" message. But at that time UPower thinks that battery is "fully charged".
Comment 2 Alexander Mezin 2014-05-29 07:48:54 UTC
The actual problem is UPower doesn't detect when battery state changes.

I unplug AC, and then:
~> cat /sys/class/power_supply/BAT1/status 
Full
~> cat /sys/class/power_supply/BAT1/status 
Discharging
~> cat /sys/class/power_supply/BAT1/status 
Discharging

Battery state changes with slight delay, but UPower doesn't detect it.
Comment 3 Alexander Mezin 2014-05-29 08:02:09 UTC
Calling org.freedesktop.UPower.Device.Refresh through D-bus helps.
Comment 4 Bastien Nocera 2015-04-02 14:26:13 UTC
Does this still happen with 0.99.2? Pretty sure it got fixed.
Comment 5 Alexander Mezin 2015-04-03 07:47:15 UTC
(In reply to Bastien Nocera from comment #4)
> Does this still happen with 0.99.2? Pretty sure it got fixed.

Well... I added a quirk for my laptop in battery driver long time ago. I'll test kernel without the quirk when I have spare time.

The problem was that actual state change happened with a slight delay after notification is sent (buggy firmware, I think).

What changes in upower 0.99.2 could affect it?
Comment 6 Bastien Nocera 2015-04-03 10:21:06 UTC
(In reply to Alexander Mezin from comment #5)
> (In reply to Bastien Nocera from comment #4)
> > Does this still happen with 0.99.2? Pretty sure it got fixed.
> 
> Well... I added a quirk for my laptop in battery driver long time ago.

The quirk is upstream now?

> I'll
> test kernel without the quirk when I have spare time.
> 
> The problem was that actual state change happened with a slight delay after
> notification is sent (buggy firmware, I think).

If there's a slight delay, then it probably wouldn't make a difference. The code was added to update the AC state on battery removal.

> What changes in upower 0.99.2 could affect it?

In the end, none. If the quirk is upstream, that's probably good enough to consider fixed.
Comment 7 Alexander Mezin 2015-04-06 09:39:49 UTC
(In reply to Bastien Nocera from comment #6)
> The quirk is upstream now?
Yes

> If there's a slight delay, then it probably wouldn't make a difference. The
> code was added to update the AC state on battery removal.
Then I don't think anything has changed.

> In the end, none. If the quirk is upstream, that's probably good enough to
> consider fixed.
Yes, you may consider it fixed. But only for single specific laptop model.

Though it could be fixed better way in UPower. By polling with decreasing rate after notifications.
Comment 8 dcm-bugzilla 2015-06-24 04:12:56 UTC
This seems to be a recurring problem with people and it would be great if upower could be fixed rather than just handling it upstream on a laptop-model specific basis - examples of this issue with others:

https://bugs.launchpad.net/ubuntu/+source/upower/+bug/1453691

http://askubuntu.com/questions/630479/cannot-detect-power-supply-plug-unplug-on-ubuntu-14-04-2

https://bugs.launchpad.net/ubuntu/+source/upower/+bug/800339

https://bbs.archlinux.org/viewtopic.php?id=188632
Comment 9 Bastien Nocera 2015-06-26 09:10:02 UTC
(In reply to dcm-bugzilla from comment #8)
> This seems to be a recurring problem with people and it would be great if
> upower could be fixed rather than just handling it upstream on a
> laptop-model specific basis - examples of this issue with others:

But it's not a UPower bug...
Comment 10 Alexander Mezin 2015-06-26 09:27:09 UTC
(In reply to Bastien Nocera from comment #9)
> (In reply to dcm-bugzilla from comment #8)
> > This seems to be a recurring problem with people and it would be great if
> > upower could be fixed rather than just handling it upstream on a
> > laptop-model specific basis - examples of this issue with others:
> 
> But it's not a UPower bug...

It's not a UPower bug, but UPower still can solve the problem. In a generic way. Butter than kernel-side quirks.

I'd like to implement the solution in UPower, but I am not familiar with UPower's internals and Glib, so, most probably, it'll take too much time. More than I can devote to it.

I'll try to explain:
UPower can do polling of both batteries and ac adapters with dynamically changing rate. When no changes happen, interval is multiplied by 2, up to some big max_interval. When any notification arrives, interval is set to some very small value. It will do more polling than is necessary on a bug-free system, but it shouldn't hurt too much, I think.

This can solve every problem with ACPI on laptops I ever encountered.
Comment 11 Vadim A. Misbakh-Soloviov (mva) 2016-04-01 08:24:20 UTC
By the way, I have same issue on my laptop, although, if I run `watch -n0,1 acpi -b` it detects battery state changes (when I pulgging AC cord in and out) pretty fine. But UPower itself doesn't.

But there is a bit strange thing: UPower doesn't totally ignore AC plug/unplugs. It detect changes only in two cases:
1) battery discarging (not a status change "charged"/"discharging", but decreasing "energy"),
2) something getting access to /sys/class/power_supply/* (aka /sys/bus/acpi/drivers/{ac/ACPI0003:00,battery/PNP0C0A:03}/power_supply/*).

I.e. if I run `watch -n0,1 cat /sys/class/power_supply/ADP1/online` and `upower -m`, and then plug/unplug AC — upower do show changes (although, yes, with some half-second lag). And if I don't run watch for /sys/class/power_supply/ADP1/online — upower doesn't print anything until battery discharges a bit.


So, as far as I'm not familiar with kernel internals, I don't understand a reason of all of that behaviour:
1) why do `watch -n0,1 cat /proc/acpi/ac_adapter/ADP1/state` detects changes in the same moment power cor is {un,}plugged?
2) why do `watch acpi -b` detects AC/battery state change (even with half-second lag) // although, I've found that /sys/class/power_supply/BAT1/status changes with the same half-second lag, and exactly in same moment when `acpi -b` changes output.
3) why do upower has much longer lag? i.e. what kind of events it is waiting for?

So, is it kernel (driver) bug or what?

P.S. I also don't see UPower in DBus (there is only PowerManagement, which is another thing).

P.P.S. Alexander, can you describe what kind of quirk that was? Maybe it shall be updated again (to work with newer laptop models)? :)
Comment 12 GitLab Migration User 2018-06-04 13:23:39 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/upower/upower/issues/22.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.