Created
April 22, 2019 19:48
-
-
Save jeena/e731eaefa82975b5ea1684a7d80bebc2 to your computer and use it in GitHub Desktop.
power-consumption.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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