获取当前总功耗

获取当前总功耗

我在 ARM 主板 (aarch64) 上运行 Ubuntu 20.04。我想每 5 秒记录一次总功耗。我尝试了几种工具。

动力顶篷

我尝试运行 powertop 命令。它输出类似以下内容:

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

但它似乎没有给出总功耗的单一数字。

电源稳压器

我还尝试使用 powerstat 命令记录功耗:

# 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.

不知为何,它的功率一直是 0 瓦。

/sys/class/power_supply子目录获取:

参考

我开始查看各种子目录/sys/class/power_supply。我已将此设备连接到直流电源(它不靠电池供电)。因此,我首先查看了sys/class/power_supply/dc目录:

# 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

但是我没有找到任何可能包含当前功耗的文件。然后我尝试在pc_port目录中查找:

# 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

我仍然没有找到任何可能表明包含当前功耗的合理文件名。

如何获得当前的总功耗?

答案1

说到电源设备内置统计报告功能级别,并非所有设备都支持提供统计数据...使用upower指挥优力-e使用如下选项列出电源设备(这是测试系统上的演示):

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

然后使用选项检查所需设备的信息-i...名称非常具有描述性,因此直接电源线:

$ 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'

正如您所见,它没有统计数据...因此,有关当前功耗的信息(以及其他信息) 无法从该设备中拉出。

电池:

$ 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

确实有统计数据,其中energy-rate:你可以隔离并格式化以进行打印,如下所示:

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

但是,统计信息更新间隔可能会有所不同,并updated:显示统计信息上次更新的时间,但您可以强制刷新某个设备的统计信息,例如:

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

并且你可以每隔energy-rate例如三秒拉一次,如下所示:

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

相关内容