lil refactor

This commit is contained in:
Drath
2025-09-14 21:33:58 +02:00
parent 2ceb08f5ca
commit 329837f92d
5 changed files with 197 additions and 110 deletions

View File

@@ -1,45 +1,89 @@
#include <cpu.hpp>
cpu_hwmon_t detectCpuHwmon(std::string pci) {
cpu_hwmon_t hw;
namespace cpu {
void detectHwmon(sensors* target, std::string pci) {
std::string path = "/sys/bus/pci/devices/" + pci + "/hwmon";
try {
for (const auto& entry : std::filesystem::directory_iterator(path))
hw.path = getFirstDir(path);
target->hwmon = getFirstDir(path);
} catch (...) {
std::println("[CpuHwmon] NO CPU HWMON KERNEL MODULE DETECTED");
return hw;
std::println("[cpu::detectHwmon] NO CPU HWMON KERNEL MODULE DETECTED");
return;
}
hw.zenpower = readFile((hw.path + "/name"), 128) == "zenpower";
std::println("[CpuHwmon] Detected module \"{}\" in path \"{}\"", hw.zenpower ? "zenpower" : "k10temp", hw.path);
return hw;
target->zenpower = readFile((target->hwmon + "/name"), 128) == "zenpower";
std::println("[cpu::detectHwmon] Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon);
}
cpu_t getCpu(cpu_hwmon_t hw) {
cpu_t cpu;
void update(sensors* c) {
// commented out unused values so its faster
if (hw.path == "") return cpu; // break if no module
if (hw.zenpower) {
if (c->hwmon == "") return; // break if no module
if (c->zenpower) {
// cpu.tdie = std::stoi(readFile(hw.path + "/temp1_input", 2));
cpu.tctl = std::stoi(readFile(hw.path + "/temp2_input", 2));
cpu.tccd1 = std::stoi(readFile(hw.path + "/temp3_input", 2));
cpu.pcore = (double)(std::stoi(readFile(hw.path + "/power1_input", 3)) / 100.0);
cpu.psoc = (double)(std::stoi(readFile(hw.path + "/power2_input", 3)) / 100.0);
cpu.ptotal = cpu.pcore + cpu.psoc;
cpu.vcore = (double)(std::stoul(readFile(hw.path + "/in1_input", 4)) / 1000.0);
c->tctl = std::stoi(readFile(c->hwmon + "/temp2_input", 2));
c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2));
c->pcore = (double)(std::stoi(readFile(c->hwmon + "/power1_input", 3)) / 100.0);
c->psoc = (double)(std::stoi(readFile(c->hwmon + "/power2_input", 3)) / 100.0);
c->ptotal = c->pcore + c->psoc;
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 {
cpu.tctl = std::stoi(readFile(hw.path + "/temp1_input", 2));
cpu.tccd1 = std::stoi(readFile(hw.path + "/temp3_input", 2));
c->tctl = std::stoi(readFile(c->hwmon+ "/temp1_input", 2));
c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2));
}
// lots of reads but still faster than reading /proc/cpuinfo
char buf[5];
for (uint8_t i = 0; i < N_CPU; i++) {
readFile2("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq", buf, sizeof(buf));
cpu.avg_freq += strtol(buf, NULL, 10);
c->avg_freq += strtol(buf, NULL, 10);
}
cpu.avg_freq /= N_CPU;
c->avg_freq /= N_CPU;
}
return cpu;
}
// cpu_hwmon_t detectCpuHwmon(std::string pci) {
// cpu_hwmon_t hw;
// std::string path = "/sys/bus/pci/devices/" + pci + "/hwmon";
// try {
// for (const auto& entry : std::filesystem::directory_iterator(path))
// hw.path = getFirstDir(path);
// } catch (...) {
// std::println("[CpuHwmon] NO CPU HWMON KERNEL MODULE DETECTED");
// return hw;
// }
// hw.zenpower = readFile((hw.path + "/name"), 128) == "zenpower";
// std::println("[CpuHwmon] Detected module \"{}\" in path \"{}\"", hw.zenpower ? "zenpower" : "k10temp", hw.path);
// return hw;
// }
// cpu_t getCpu(cpu_hwmon_t hw) {
// cpu_t cpu;
// // commented out unused values so its faster
// if (hw.path == "") return cpu; // break if no module
// if (hw.zenpower) {
// // cpu.tdie = std::stoi(readFile(hw.path + "/temp1_input", 2));
// cpu.tctl = std::stoi(readFile(hw.path + "/temp2_input", 2));
// cpu.tccd1 = std::stoi(readFile(hw.path + "/temp3_input", 2));
// cpu.pcore = (double)(std::stoi(readFile(hw.path + "/power1_input", 3)) / 100.0);
// cpu.psoc = (double)(std::stoi(readFile(hw.path + "/power2_input", 3)) / 100.0);
// cpu.ptotal = cpu.pcore + cpu.psoc;
// cpu.vcore = (double)(std::stoul(readFile(hw.path + "/in1_input", 4)) / 1000.0);
// // cpu.vsoc = (double)(std::stoul(readFile(hw.path + "/in2_input")) / 1000.0);
// } else {
// cpu.tctl = std::stoi(readFile(hw.path + "/temp1_input", 2));
// cpu.tccd1 = std::stoi(readFile(hw.path + "/temp3_input", 2));
// }
// // lots of reads but still faster than reading /proc/cpuinfo
// char buf[5];
// for (uint8_t i = 0; i < N_CPU; i++) {
// readFile2("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq", buf, sizeof(buf));
// cpu.avg_freq += strtol(buf, NULL, 10);
// }
// cpu.avg_freq /= N_CPU;
// return cpu;
// }
} // namespace cpu