stole a trick from btop to measure power

This commit is contained in:
Drath Vader
2025-10-21 15:30:22 +02:00
parent 3d6679ff2a
commit 90581aa8fd
2 changed files with 11 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#ifndef CPU_HPP
#define CPU_HPP
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <log.hpp>
@@ -34,4 +35,4 @@ void update(cpuSensors* g);
} // namespace cpu
#endif
#endif

View File

@@ -17,6 +17,8 @@ void detectHwmon(cpuSensors* target, std::string pci) {
}
void update(cpuSensors* c) {
static std::chrono::time_point<std::chrono::steady_clock> previous_ts;
static uint64_t previous_usage = 0;
// commented out unused values so its faster
if (c->hwmon == "") return; // break if no module
if (c->zenpower) {
@@ -31,6 +33,12 @@ void update(cpuSensors* c) {
} else {
c->tctl = std::stoi(readFile(c->hwmon + "/temp1_input", 2));
c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2));
// read power from intel-rapl? works on amd too, btop uses this trick, seems accurate
auto current_ts = std::chrono::steady_clock::now();
auto current_usage = std::stoul(readFile("/sys/class/powercap/intel-rapl:0/energy_uj", 24));
c->ptotal = (float)(current_usage - previous_usage) / (float)(std::chrono::duration_cast<std::chrono::microseconds>(current_ts - previous_ts).count());
previous_ts = current_ts;
previous_usage = current_usage;
}
// lots of reads but still faster than reading /proc/cpuinfo
@@ -42,4 +50,4 @@ void update(cpuSensors* c) {
c->avg_freq /= N_CPU;
}
} // namespace cpu
} // namespace cpu