This script estimates per-process CPU energy consumption over a sampling interval using Linux RAPL counters and CPU time attribution, similar to how Scaphandre operates.
- Read total CPU energy from (
energy_uj) - Take a snapshot of per-process CPU times
- Sleep for a short interval
- Take another energy + CPU snapshot
- Attribute total energy to each process based on CPU time share
For each process:
[ E_\text{process} = \left( \frac{\Delta T_\text{process}}{\sum \Delta T_\text{all processes}} \right) \times \Delta E_\text{total} ]
Where:
- ( \Delta T_\text{process} ) = Increase in CPU time (user + system) during the interval
- ( \sum \Delta T_\text{all processes} ) = Sum of CPU time increases for all processes
- ( \Delta E_\text{total} ) = Energy difference from RAPL over the interval
- Energy is in microjoules (µJ)
- Linux machine
- CPU with RAPL support (Intel / some AMD)
- Python 3
sudopermissionspsutilinstalled
Install psutil one of these ways:
Option 1 (system package):
sudo apt install python3-psutil