CasaOS icon indicating copy to clipboard operation
CasaOS copied to clipboard

[STORY] Power usage widget

Open Lauren-ED209 opened this issue 2 years ago • 5 comments

Discussed in https://github.com/IceWhaleTech/CasaOS/discussions/207

Originally posted by bearfrieze May 18, 2022

Is your idea related to a problem? Please describe:

Power usage is a great metric to have access to at a glance, but it's inconvenient to measure it with a physical/analog power meter.

Describe the solution you'd like:

Consume power metrics provided by the OS (e.g. metrics provided by Intel RAPL via the powercap driver) and display power usage in a polished graphical widget similar to the existing CPU/RAM widget. Current power usage would be a must have, and aggregated stats (1 hour average, 24 hour average, max power last 24 hours, etc.) would be a nice addition. See UI sketch below for more details.

Describe alternatives you've considered:

Using a physical/analog power meter to measure power usage is an alternative that I have tried out, but it's not as easy/convenient as measuring power usage from metrics provided by the OS.

UI sketch:

power_meter_ui_sketch

POC:

I did a POC for measuring power usage from the ZimaBoard 832 using metrics provided by the OS (Ubuntu Server 22.04).

I have written this function (added to my .bashrc) to sample energy counter values provided by the kernel (which sources it from Intel RAPL) and do some math to determine wattage:

power() {
  t=$1
  a=`cat /sys/class/powercap/intel-rapl/intel-rapl\:0/energy_uj`
  sleep $t
  b=`cat /sys/class/powercap/intel-rapl/intel-rapl\:0/energy_uj`
  python3 -c "print(f'{($b - $a) / 1000000 / $t:.2f} W')"
}

t is the offset between first and second sample. I have have seen accurate readings at 0.5 seconds sampling offset which is plenty of resolution for my purposes. You can of course run the script in a while true; do power 0.5; done loop and get continuous power readings.

Sample output (idle --> full CPU load --> idle):

root@zima:~# while true; do power 0.5; done
1.83 W
1.83 W
1.82 W
9.00 W
9.41 W
9.44 W
9.62 W
9.79 W
2.96 W
1.83 W
1.83 W
1.83 W

Based on my readings with a power meter, the metrics provided by the power function are accurate.

More information on the powercap driver and Intel RAPL:

  • https://www.kernel.org/doc/html/latest/power/powercap/powercap.html
  • https://01.org/blogs/2014/running-average-power-limit-%E2%80%93-rapl

Challenges:

The power meter would never be compatible with all hardware/OS combinations, but could feasibly be implemented in a way that is compatible with several popular of hardware/OS combinations (e.g. ZimaBoard/Ubuntu Server).

Refer

  • https://github.com/IceWhaleTech/CasaOS/discussions/207 (12 upvotes)

Lauren-ED209 avatar Jun 17 '22 17:06 Lauren-ED209