diff --git a/include/cpu.hpp b/include/cpu.hpp index 3294896..a85ad64 100644 --- a/include/cpu.hpp +++ b/include/cpu.hpp @@ -1,6 +1,7 @@ #ifndef CPU_HPP #define CPU_HPP +#include #include #include #include @@ -34,4 +35,4 @@ void update(cpuSensors* g); } // namespace cpu -#endif \ No newline at end of file +#endif diff --git a/src/cpu.cpp b/src/cpu.cpp index 176daa6..960a772 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -17,6 +17,8 @@ void detectHwmon(cpuSensors* target, std::string pci) { } void update(cpuSensors* c) { + static std::chrono::time_point 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(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 \ No newline at end of file +} // namespace cpu