Score:4

Obtaining current total power consumption

fj flag

I am running Ubuntu 20.04 on ARM board (aarch64). I want to log total power consumption every 5 seconds. I tried several tools.

Powertop

I tried running powertop command. It outputs something like following:

PowerTOP v2.11    Overview   Idle stats   Frequency stats   Device stats   Devic

Summary: 432.8 wakeups/second,  0.0 GPU ops/seconds, 0.0 VFS ops/sec and 14.7% C

Power est.              Usage       Events/s    Category       Description
  322 mW     16.0 ms/s      75.2        Process        [PID 1202] weston --idle-
  239 mW      0.9 ms/s      60.1        kWork          kernfs_notify_workfn
  210 mW      9.7 ms/s      49.4        kWork          memlat_monitor_work
  146 mW      2.2 ms/s      36.0        Process        [PID 11] [rcu_preempt]
  121 mW     12.4 ms/s      25.8        Process        [PID 1160] weston --idle-
 91.4 mW      2.3 ms/s      22.2        Process        [PID 12] [rcuog/0]
 73.0 mW     12.8 ms/s      13.4        Interrupt      [407] msm_drm
 68.9 mW    237.3 µs/s      17.3        kWork          def_work_fn
 56.6 mW      2.4 ms/s      13.4        Timer          tick_sched_timer
 55.0 mW      1.7 ms/s      13.3        Timer          hrtimer_wakeup
 50.6 mW    120.4 µs/s      12.8        Timer          histtimer_fn
 46.7 mW      4.3 ms/s      10.1        Process        [PID 2547] /home/ideaforg
 29.6 mW      0.7 ms/s       7.2        Interrupt      [31] apps_rsc
 24.3 mW      1.1 ms/s       5.7        Process        [PID 949] /usr/sbin/rsysl
 23.0 mW     10.0 ms/s       1.9        Process        [PID 462] /lib/systemd/sy
 21.8 mW      1.9 ms/s       4.8        Process        [PID 2571] /home/ideaforg
 20.1 mW      6.6 ms/s       2.5        Process        [PID 1] /sbin/init

But it does not seem to give single number for total power consumption.

Powerstat

I also tried to log power consumption using powerstat command:

# powerstat
Running for 300.0 seconds (30 samples at 10.0 second intervals).
Power measurements will start in 180 seconds time.

  Time    User  Nice   Sys  Idle    IO  Run Ctxt/s  IRQ/s  Watts
08:55:57   0.3   0.0   0.8  98.9   0.0    2   1162    716   0.00
08:56:07   0.3   0.0   0.9  98.8   0.0    1   1220    784   0.00
08:56:17   0.3   0.0   1.1  98.6   0.0    1   1148    755   0.00
08:56:27   0.3   0.0   1.1  98.6   0.0    1   1272    804   0.00
^C-------- ----- ----- ----- ----- ----- ---- ------ ------ ------
 Average   0.3   0.0   1.0  98.7   0.0  1.2 1200.5  764.8   0.00
 GeoMean   0.3   0.0   1.0  98.7   0.0  1.2 1199.5  764.1   0.00
  StdDev   0.0   0.0   0.1   0.1   0.0  0.4   49.5   32.9   0.00
-------- ----- ----- ----- ----- ----- ---- ------ ------ ------
 Minimum   0.3   0.0   0.8  98.6   0.0  1.0 1148.0  716.4   0.00
 Maximum   0.3   0.0   1.1  98.9   0.0  2.0 1272.0  803.7   0.00
-------- ----- ----- ----- ----- ----- ---- ------ ------ ------
Summary:
System:   0.00 Watts on average with standard deviation 0.00
Note: Power calculated from battery capacity drain, may not be accurate.

Somehow it keeps giving 0 watts.

Fetching from /sys/class/power_supply sub directory:

Reference

I started looking at various sub-directories /sys/class/power_supply. I have connected this device to DC power supply (It does not run on battery). So, I first had a look at sys/class/power_supply/dc directory:

# cd /sys/class/power_supply/

# ls
battery  dc  pc_port  usb

# cd dc

# ls
current_max  input_current_limit  present    uevent       waiting_for_supplier
device       online               subsystem  voltage_max  wakeup24
hwmon0       power                type       voltage_now

# cd power/
# ls
autosuspend_delay_ms    wakeup               wakeup_expire_count
control                 wakeup_abort_count   wakeup_last_time_ms
runtime_active_time     wakeup_active        wakeup_max_time_ms
runtime_status          wakeup_active_count  wakeup_prevent_sleep_time_ms
runtime_suspended_time  wakeup_count         wakeup_total_time_ms

But I did not find any file that might contain current power consumption. Then I tried to find in pc_port directory:

# cd /sys/class/power_supply/pc_port

# ls
current_max  hwmon2  power      type    voltage_max           wakeup26
device       online  subsystem  uevent  waiting_for_supplier

# cd power/
# ls
autosuspend_delay_ms    wakeup               wakeup_expire_count
control                 wakeup_abort_count   wakeup_last_time_ms
runtime_active_time     wakeup_active        wakeup_max_time_ms
runtime_status          wakeup_active_count  wakeup_prevent_sleep_time_ms
runtime_suspended_time  wakeup_count         wakeup_total_time_ms

I still did not find any sensible file name that might indicate to contain current power consumption.

How can I obtain current total power consumption?

us flag
@graham No, it does not answer the question. The other question suggests to use powertop, which OP is already using
24601 avatar
in flag
@ArchismanPanigrahi Thanks - missed that...
Score:5
jp flag

Speaking on the power devices builtin statistics reporting capabilities level, not all devices support providing statistics ... Use the upower command of UPower to list power devices with the option -e like so(this is a demonstration on a test system):

$ upower -e
/org/freedesktop/UPower/devices/line_power_ADP0
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/DisplayDevice

Then check the information of the device you want with the option -i ... Names are pretty descriptive, so the direct power line:

$ upower -i /org/freedesktop/UPower/devices/line_power_ADP0
  native-path:          ADP0
  power supply:         yes
  updated:              Fri 11 Aug 2023 03:45:32 PM +03 (68 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              yes
    icon-name:          'ac-adapter-symbolic'

and as you can see, it doesn't have statistics ... So, information about current power consumption (among other information) can't be pulled from that device.

While the battery:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0
  native-path:          BAT0
  vendor:               LGC
  model:                L16L2PB2
  serial:               5545
  power supply:         yes
  updated:              Fri 11 Aug 2023 03:47:33 PM +03 (2 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               charging
    warning-level:       none
    energy:              5.06 Wh
    energy-empty:        0 Wh
    energy-full:         17.99 Wh
    energy-full-design:  30 Wh
    energy-rate:         9.238 W
    voltage:             7.744 V
    charge-cycles:       N/A
    time to full:        1.4 hours
    percentage:          28%
    capacity:            59.9667%
    technology:          lithium-polymer
    icon-name:          'battery-low-charging-symbolic'
  History (charge):
    1691758053  28.000  charging
  History (rate):
    1691758053  9.238   charging

does have statistics and among those is energy-rate: which you can isolate and format for printing with something like this:

$ upower --show-info /org/freedesktop/UPower/devices/battery_BAT0 |
awk '/energy-rate:/{print $2}'
10.828 # <--- output

However, the statistics update interval might vary and updated: shows the time the statistics were last updated, but you can force statistics refresh of a certain device with for example:

busctl call org.freedesktop.UPower \
/org/freedesktop/UPower/devices/battery_BAT0 \
org.freedesktop.UPower.Device Refresh

and you can pull for e.g. energy-rate every for example three seconds with something like this:

while sleep 3
do
# Refresh device statistics
busctl call org.freedesktop.UPower \
/org/freedesktop/UPower/devices/battery_BAT0 \
org.freedesktop.UPower.Device Refresh
# Query the device statistics
upower -i /org/freedesktop/UPower/devices/battery_BAT0 |
awk '/energy-rate:/{print $2}'
done
us flag
The ARM board most likely does not have a battery
Raffa avatar
jp flag
@ArchismanPanigrahi … and as it appears its line_power device doesn’t have statistics .,. However, the battery example in my answer is only an example and the example battery device can be changed to any other power device as long as it has statistics … The point is the power device's statistics, if it has them, can be pulled from multiple sources including some the ones mentioned in the question, but if the device doesn’t have them, then they are most likely certainly not going to be available anywhere … speaking on the device reported accurate level and not estimates by external tools.
anir avatar
fj flag
Sorry to reply to this excellent answers after many days. For me, its showing statistic for three devices: `/org/freedesktop/UPower/devices/battery_bms`, `/org/freedesktop/UPower/devices/DisplayDevice` and `/org/freedesktop/UPower/devices/battery_battery`. [Here is a dump of their output](https://ctxt.io/2/AABQQp9aEA). Given that I have connected power adaptor directly to my rasberry pi, which one of these three I should be looking at? I believe it must be `battery_bms`, right? `DisplayDevice` seems to be of monitor and `battery_battery` seems to be somewhat invalid device?
Raffa avatar
jp flag
@RajS *"Given that I have connected power adaptor directly to my rasberry pi, which one of these three I should be looking at?"* ... For your purpose, unfortunately none ... Battery or [BMS](https://www.synopsys.com/glossary/what-is-a-battery-management-system.html) only reflect power used by the machine when battery is the only power source i.e. power adapter unplugged or otherwise they show the power drawn by the battery itself while it's charging and level-up when the battery gets fully charged... `DisplayDevice` is all batteries combined if there are more than one or same as battery if one
anir avatar
fj flag
Well my device has no battery. I always connect my board directly to power mains using adapter. So, I am guessing whats going on here with all the battery related things.
Raffa avatar
jp flag
@RajS Well, it appears that your device has got a stand-alone BMS (as BMS is usually bundled with the battery package itself) see for example https://askubuntu.com/a/1406221 ... I guess maybe it's your lucky day :-) as in this case and without a battery it appears to be measuring system power ... If so, then either `battery_bms` or `DisplayDevice` should do.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.