EnergyMeter
EnergyMeter is a Python module that estimates the energy consumption of CPU, memory, GPU, and storage with only three lines of code: on Linux with RAPL, NVIDIA's NVML and eBPF, and on macOS (Apple Silicon) with powermetrics and the kernel's energy and I/O counters. This was developed during the development of the article Fadel Argerich, M., & Patiño-Martínez, M. (2024). Measuring and Improving the Energy Efficiency of Large Language Models Inference. IEEE Access.
Update: version 3.0 is out!
- macOS on Apple Silicon: CPU, GPU and Neural Engine energy via
powermetrics, DRAM via the kernel's energy counters, and disk via the kernel's I/O counters. - Terminal command:
energymeter start/status/stopmeasures the whole machine while you do anything else. - Prometheus: live metrics at
/metrics, from Python or the terminal. - Disk: default figures for each disk type (Apple SSD, NVMe, SATA SSD, hard drive), so disk values are now optional, and whole-disk tracking without sudo (
disk_scope="system"). - Linux: RAPL is read directly and sampled every second, so counter wraparounds no longer corrupt long measurements (pyRAPL is no longer a dependency).
Version 2.0 improved the accuracy of GPU energy while reducing the meter's overhead.
How to install
You can install EnergyMeter via pip or uv and PyPi: pip install ai-energymeter, or by cloning this repository and using pip:
git clone https://github.com/maufadel/EnergyMeter.git
cd EnergyMeter
pip install .
How to use
The most basic usage of EnergyMeter is as follows:
from energymeter import EnergyMeter
em = EnergyMeter(label="Test Meter", # A label to identify the measurement
include_idle=False) # If energy used during idle should be accounted for in the measurement. Defaults to False.
# Disk energy is estimated from the bytes read and written. By default EnergyMeter uses typical
# figures for your disk's type (see "Disk" below). For better estimates, give your own:
# EnergyMeter(disk_avg_speed=1600*1e6, # average speed of your storage in bytes/s (see below how to get it)
# disk_active_power=6, # Watts while reading or writing (usually in your storage's specs)
# disk_idle_power=1.42) # Watts while idle (usually in your storage's specs)
em.begin()
# --> CODE YOU WANT TO MEASURE <--
em.end()
# Plot energy consumption per component.
meter.plot_total_joules_per_component()
# or print(em.get_total_joules_per_component())
You can check the notebook Measuring_energy_consumption.ipynb for more details.
From the terminal
Installing the package also installs an energymeter command. It measures the energy used by the whole machine between a start and a stop, while you do anything else in the meantime:
energymeter start --label "Video call"
# ... use your computer ...
energymeter status # is a meter running, and for how long?
energymeter stop # prints energy per component; add --json or --output result.json
CPU, DRAM and GPU (and the Neural Engine on Apple Silicon) are read from system-wide counters, so they include everything running on the machine.
Disk. The CLI also tracks the disk: every byte read or written by any app (downloads, copies, ...), with no sudo. On macOS it counts the internal disk (the counters iostat uses); on Linux, every physical disk from /proc/diskstats (partitions, LVM/LUKS, RAID and loop devices are skipped, since their I/O lands on the disks underneath). stop shows how many MB each disk read and wrote, and its energy.
Disk energy is estimated from those bytes and each disk's speed and power. Unless you give them, EnergyMeter uses rough figures for the disk's type, which it detects on its own:
| Disk type | Speed | Active power | Idle power |
|---|---|---|---|
| Apple SSD (Apple Silicon) | 3 GB/s | 3 W | 0.05 W |
| NVMe SSD | 2 GB/s | 5 W | 0.5 W |
| SATA SSD | 0.5 GB/s | 3 W | 0.3 W |
| Hard drive | 0.15 GB/s | 6.5 W | 4.5 W |
Override any of them with --disk-speed, --disk-active-power and --disk-idle-power (the values then apply to every disk), or turn the disk off with --no-disk. Intel Macs need all three options. Idle power only counts with --include-idle.
From Python, the disk values also default to the table above (energymeter.DISK_DEFAULTS); any you pass replace them. The library counts only the code you measure by default: the kernel's per-process counters on macOS, and bpftrace on Linux, which needs bpftrace with passwordless sudo (without it the disk is skipped with a warning) and uses the type of the disk holding the working directory. To count the whole disk by any app instead, with no sudo, use EnergyMeter(disk_scope="system"). em.get_disk_details() shows the bytes, figures and energy per disk.
Prometheus
EnergyMeter can serve live metrics for Prometheus while it measures, both from Python and from the terminal:
em = EnergyMeter(ignore_disk=True, label="LLM inference", prometheus_port=9464)
em.begin() # metrics are served at http://127.0.0.1:9464/metrics until em.cleanup()
# ...
em.end()
em.cleanup()
energymeter start --label "Video call" --prometheus-port 9464
energymeter stop # the metrics server stops with the meter
By default the server only listens on this machine (127.0.0.1). If Prometheus runs on another host or in Docker, pass prometheus_addr="0.0.0.0" (or --prometheus-addr 0.0.0.0).
Point Prometheus at it:
scrape_configs:
- job_name: energymeter
scrape_interval: 5s
static_configs:
- targets: ["localhost:9464"]
Metrics (each has a label label with the meter's label):
| Metric | Type | Meaning |
|---|---|---|
energymeter_energy_joules_total{component="cpu|dram|gpu|disk|ane"} |
counter | Energy used since begin(). It stops growing at end() and restarts from 0 on the next begin(). |
energymeter_running |
gauge | 1 between begin() and end(). |
energymeter_start_time_seconds |
gauge | Unix time of begin(). |
energymeter_duration_seconds |
gauge | Time measured so far. |
Average power in watts is rate(energymeter_energy_joules_total[1m]). Disk energy on Linux (bpftrace) only appears after end(). With the CLI, the server stops with energymeter stop, so the last few seconds before it may not be scraped; read them from energymeter stop --json if you need them.
How to get storage details
You can benchmark your storage speed with the Flexible I/O tester (FIO) as recommended by Google in the following tutorial: https://cloud.google.com/compute/docs/disks/benchmarking-pd-performance
Storage specs usually include data regarding the power consumption during active (reading or writing) and for idle periods.
Troubleshooting
RAPL
CPU and DRAM energy are read from the RAPL counters in /sys/class/powercap/intel-rapl:* (AMD CPUs use the same files). Since Linux 5.10 only root can read them. Either run your script (or energymeter start and energymeter stop) with sudo, or make the counters readable until the next reboot:
sudo chmod a+r /sys/class/powercap/intel-rapl:*/energy_uj /sys/class/powercap/intel-rapl:*/intel-rapl:*/energy_uj
The counters wrap around after a fixed amount of energy (often a few minutes to an hour of work), so EnergyMeter reads them every second (rapl_sampling_rate) and corrects for wraparounds; long measurements are safe.
bpftrace
Also note that you need to have bpftrace installed. On ubuntu, you can install it with the following command:
sudo apt-get install -y bpftrace
For other operating systems, please check https://github.com/bpftrace/bpftrace/blob/master/INSTALL.md.
bpftrace runs with sudo -n, which never asks for a password, so it needs a passwordless sudo rule. Add one with sudo visudo -f /etc/sudoers.d/energymeter, replacing youruser and the path (which bpftrace) with yours:
youruser ALL=(root) NOPASSWD: /usr/bin/bpftrace
Without it, per-process disk tracking is skipped with a warning; disk_scope="system" (and the CLI) need no sudo at all.
Limitations
EnergyMeter must run on bare metal (not in a virtual machine): either Linux on Intel CPUs and NVIDIA GPUs, or macOS on Apple Silicon. These requirements are inherited from the tools used for tracking the energy consumption: RAPL (Intel), NVML (NVIDIA) and eBPF (Linux); powermetrics (which needs passwordless sudo) and the kernel's IOReport energy counters (macOS). On macOS, GPU memory and temperature are not available. These tools are known to have a high accuracy thanks to their access to low level sensors, but it is important to keep in mind that the energy consumption metrics provided are still estimations and vary according to different factors including hardware configuration and software versions. Additionally, the energy consumed by cooling, screens and other components not mentioned here are not included in our measurements, so the total energy consumed will likely be different to the total sum of the consumption of CPU, memory, GPU, and storage.
Authorship and License
I am developing EnergyMeter as a part of my PhD program at Universidad Politécnica de Madrid. EnergyMeter is open sourced under an MIT License.
If you found this repository useful, please cite our work:
@article{argerich2024measuring,
title={Measuring and Improving the Energy Efficiency of Large Language Models Inference},
author={Fadel Argerich, Mauricio and Pati{\~n}o-Mart{\'\i}nez, Marta},
journal={IEEE Access},
year={2024},
publisher={IEEE}
}
Metadata
Release files for ai-energymeter 3.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_energymeter-3.0.0.tar.gz | 31.1 kB | Without provenance | Details for ai_energymeter-3.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_energymeter-3.0.0-py3-none-any.whl | Python 3 | none | any | Details for ai_energymeter-3.0.0-py3-none-any.whl |
Total release size: 63.6 kB
Release files / ai_energymeter-3.0.0.tar.gz
| Download URL | ai_energymeter-3.0.0.tar.gz |
|---|---|
| Size | 31.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b9938b7f89f60f702f7a63b305a292fc785e5c1dfbb3fcf88fb6a90d33de4e4
|
|
BLAKE2b-256 checksum How to use checksums |
dc33eb04e3a0e37ed85f3999f545e0d1b49cbd625a7b1abe26e021ecfcca1da2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / ai_energymeter-3.0.0-py3-none-any.whl
| Download URL | ai_energymeter-3.0.0-py3-none-any.whl |
|---|---|
| Size | 32.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c0d6473a0beaa1769f2291a4b2b094faefb18bafeb64300104e32f5304bebac
|
|
BLAKE2b-256 checksum How to use checksums |
bb8fb90078117b03787f3ce1ca2e063308029343dff5783f96e24b8e97df8d19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Security for ai-energymeter 3.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.