From 4324379b8e37c1e5e63e808caab8a8656c165929 Mon Sep 17 00:00:00 2001 From: Drath Date: Sun, 2 Nov 2025 12:38:15 +0100 Subject: [PATCH] use the new macro, refactor a lil --- src/cpu.cpp | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index 4be17e0..b8b0dbf 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -23,37 +23,40 @@ 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) { - // cpu.tdie = std::stoi(readFile(hw.path + "/temp1_input", 2)); - c->tctl = std::stoi(readFile(c->hwmon + "/temp2_input", 2)); - c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2)); - c->vcore = (double)(std::stoul(readFile(c->hwmon + "/in1_input", 4)) / 1000.0); - // cpu.vsoc = (double)(std::stoul(readFile(hw.path + "/in2_input")) / 1000.0); - } else { - c->tctl = std::stoi(readFile(c->hwmon + "/temp1_input", 2)); - c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2)); - } + static char buf[32]; + struct EnergyTs { uint64_t energy = 0; std::chrono::time_point ts; }; + + static EnergyTs prev; + EnergyTs curr; // read power from intel-rapl? works on amd too, btop uses this trick, seems accurate // it tracks with "socket power" in hwinfo on windows, its actually the same thing i had in lcdsmartie - 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; + curr.ts = std::chrono::steady_clock::now(); + fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", curr.energy); + c->ptotal = (float)(curr.energy - prev.energy) / (float)(std::chrono::duration_cast(curr.ts - prev.ts).count()); + prev = curr; // lots of reads but still faster than reading /proc/cpuinfo - char buf[8]; c->avg_freq = 0; for (uint8_t i = 0; i < N_CPU; i++) { readFile2("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, sizeof(buf)); c->avg_freq += strtol(buf, NULL, 10) / 1000; } c->avg_freq /= N_CPU; + + if (c->hwmon == "") return; // break if no module + + // commented out unused values so its faster + if (c->zenpower) { + fastReadNum(c->hwmon + "/temp1_input", c->tdie) / 1000; + fastReadNum(c->hwmon + "/temp2_input", c->tctl) / 1000; + fastReadNum(c->hwmon + "/temp3_input", c->tccd1) / 1000; + fastReadNum(c->hwmon + "/in1_input", c->vcore) / 1000.0; + fastReadNum(c->hwmon + "/in2_input", c->vsoc) / 1000.0; + } else { + fastReadNum(c->hwmon + "/temp1_input", c->tctl) / 1000; + fastReadNum(c->hwmon + "/temp3_input", c->tccd1) / 1000; + } } } // namespace cpu