Skip to content

Instantly share code, notes, and snippets.

@jeena
Created April 22, 2019 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeena/e731eaefa82975b5ea1684a7d80bebc2 to your computer and use it in GitHub Desktop.
Save jeena/e731eaefa82975b5ea1684a7d80bebc2 to your computer and use it in GitHub Desktop.
power-consumption.py
#!/usr/bin/env python3
"""
This stript when run collects the power consumption of a NUC during
one minute and writes it, together with the date/time into a file
for future use.
I want to try to see how much my NUC consumes during different phases
during a day/week.
"""
import os
import time
from datetime import datetime
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
offset = 1 * 60
energyPath = "/sys/class/powercap/intel-rapl:0/energy_uj"
fd = os.open(energyPath, os.O_RDONLY)
old_energy = int(os.pread(fd, 127, 0))
while True:
time.sleep(offset)
energy = int(os.pread(fd, 127, 0))
watt = int((energy - old_energy) / 1000000)
old_energy = energy
buf = datetime.now().strftime('%Y-%m-%d/%H:%M:%S') + " %d\n" % (watt)
with open("power-consumption.txt", "a") as f:
f.write(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment