#benchmark #energy #sustainability

rapl-energy

Small library for getting the CPU energy consumption from RAPL

24 releases

Uses new Rust 2024

new 0.1.23 Apr 2, 2025
0.1.22 Apr 1, 2025
0.1.19 Nov 22, 2024
0.1.15 Oct 22, 2024
0.1.2 Jun 26, 2024

#125 in Profiling


Used in energy-bench

GPL-3.0 license

27KB
498 lines

Rapl Energy

Small library for getting the CPU energy consumption from RAPL and friends.

This project is very much still a work in progress, and is mainly intended for internal use. However, I share it here should it be useful to anyone.

RAPL permissions

Reading RAPL requires elevated permissions.

I suggest adding a new rapl group.

sudo addgroup rapl
sudo usermod -aG rapl $(whoami)

And then adding entries to /etc/sysfs.conf for your RAPL domains and subdomains. Check your folder hierarchy in /sys/class/powercap/ to determine which domains you have available to your CPU.

Then for each domain, add the following lines to /etc/sysfs.conf. (Requires sysfsutils to be installed.)

For example, for package 0:

mode class/powercap/intel-rapl:0/energy_uj = 0440
owner class/powercap/intel-rapl:0/energy_uj = root:rapl

And for its first subdomain:

mode class/powercap/intel-rapl:0:0/energy_uj = 0440
owner class/powercap/intel-rapl:0:0/energy_uj = root:rapl

Finally, restart the sysfsutils service.

sudo systemctl restart sysfsutils

MSR permissions

Reading model-specific registers (MSR) requires elevated permissions.

sudo apt install msr-tools

You might need to run modprobe as well.

modprobe msr

One can then print the accumulated energy value as follows. (Where -a prints all CPUs, and -u prints the value as an unsigned decimal.) On my AMD system the address is 0xC001029A, which I found in https://github.com/amd/amd_energy. Check the documentation of your CPU to find your address.

sudo rdmsr 0xC001029A -au

Any executable making use of these MSR measurements must be run with sudo.

sudo ./target/debug/examples/msr

MSR group

Ideally I would like to create a group for MSR, to avoid needing to run with sudo every time. However, the approach below sadly does not resolve the problem. Please let me know if you know of a way to do this.

sudo groupadd msr
sudo chgrp msr /dev/cpu/*/msr
sudo chmod g+rw /dev/cpu/*/msr
sudo usermod -aG msr $(whoami)
newgrp msr

Dependencies

~1–11MB
~134K SLoC