Skip to content

Instantly share code, notes, and snippets.

@Green-Sky
Created January 29, 2017 10:06
Show Gist options
  • Save Green-Sky/934d78d782401737cdb4b2d95762df42 to your computer and use it in GitHub Desktop.
Save Green-Sky/934d78d782401737cdb4b2d95762df42 to your computer and use it in GitHub Desktop.
quick n dirty code to monitor powerconsumption
#include <iostream>
#include <chrono>
#include <thread>
#include <fstream>
#define PATH "/sys/class/powercap/intel-rapl:0/energy_uj"
int main(void) {
std::ifstream file(PATH);
long lastJ;
file >> lastJ;
auto lastT = std::chrono::high_resolution_clock::now();
file.seekg(0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
for (;;) {
auto nowT = std::chrono::high_resolution_clock::now();
long nowJ;
file >> nowJ;
auto deltaT = nowT - lastT;
lastT = nowT;
long deltaJ = nowJ - lastJ;
lastJ = nowJ;
std::cerr << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b" << (double)deltaJ / std::chrono::duration_cast<std::chrono::microseconds>(deltaT).count() << " W ";
file.seekg(0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment