5

I'm debugging a situation where the remaining battery percentage report is not accurate for my newly installed battery.

Regarding the answer of my previous question, various tools query the battery information directly from the battery itself. However, output of battery voltage differs between /sys/class/power_supply/BAT1/voltage_now and upower:

# upower
    voltage:             10.974 V

# /sys/...
10500000

Moreover, upower updates its statistics every 2 minutes, where /sys/class/power_supply/BAT1/voltage_now seems to be updated live. This also indicates that the sources of those two data-sources are probably different.

So, what are the data sources of those two data sources?

ceremcem
  • 2,351

1 Answers1

4

However, output of battery voltage differs between /sys/class/power_supply/BAT1/voltage_now and upower

This your question.

Moreover, upower updates its statistics every 2 minutes

Here you answered it yourself. It's just the update time. If you cat voltage_now and run upower at its update time, you will get the same result.

$ upower -i /org/freedesktop/UPower/devices/battery_BAT1 |egrep 'updated|voltage'; cat /sys/class/power_supply/BAT1/voltage_now
  updated:              Mon 17 Feb 2020 07:07:31 AM CAT (0 seconds ago)
    voltage:             17.156 V
17156000

Notice when the updated time is 0 seconds ago, the value is the same. Try the command multiple times and you see a larger difference when the time is 120 seonds ago.

If you want upower to give you an updated value instantly, you can force a refresh with

dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT1  org.freedesktop.UPower.Device.Refresh; upower -i /org/freedesktop/UPower/devices/battery_BAT1 |egrep 'updated|voltage'

So, what are the data sources of those two data sources?

The data source for upower is upowerd. upowerd would read the data from /sys/.../BAT1/* and store the history in /var/lib/upower/. When upower sends a request before the poll time configured for Upower, upowerd would reply with limiting data to last 120 seconds and returns the historical value.

Munzir Taha
  • 1,490
  • So you mean reading the .../voltage_now file actually creates a telegram, asks battery voltage from the battery pack via SMBUS and returns the answer (but not cache it for upower or like) under the hood? – ceremcem Feb 17 '20 at 08:42
  • @ceremcem: reading voltage_now directly would always give the realtime value, but upower doesn't read it directly, it read the value in upowerd. I added more info to the answer to explain this. Hope it's clear now – Munzir Taha Feb 17 '20 at 09:56
  • The answer of my previous question (see the link in my question) states that the voltage is queried via SMBUS. I'm still wondering if that SMBUS communication takes place as soon as we attempted to read the voltage_now file. That is still a mystery. – ceremcem Feb 17 '20 at 13:59
  • I guess, reading the voltage_now file via cat (or anything that reads the file) triggers a function in the kernel, kernel connects to the battery pack via SMBUS, read the data and write it to voltage_now file and let us read the file. It would be pointless otherwise. How could I read the voltage_now file in every second and get the "correct" voltage value then? – ceremcem Feb 17 '20 at 17:41
  • 1
    You are certainly right! When you cat voltage_now, the kernel would run some magic code and connect to the battery via SMBus, read the data and make it available via voltage_now. – Munzir Taha Feb 17 '20 at 19:15