refactor utils

This commit is contained in:
2025-12-28 19:55:38 +01:00
parent 2d3a481638
commit c7e2447932
6 changed files with 71 additions and 59 deletions

View File

@@ -11,7 +11,7 @@ void detectHwmon(cpuSensors* target, const std::string& pci) {
std::string path = "/sys/bus/pci/devices/" + pci + "/hwmon";
try {
target->hwmon = getFirstDir(path);
target->hwmon = utils::getFirstDir(path);
} catch (...) {
applog(LOG::ERR, "NO CPU HWMON KERNEL MODULE DETECTED");
return;
@@ -34,15 +34,15 @@ void update(cpuSensors* c) {
// 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
curr.ts = std::chrono::steady_clock::now();
// fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", curr.energy);
curr.energy = fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", buf);
// utils::fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", curr.energy);
curr.energy = utils::fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", buf);
c->ptotal = (float)(curr.energy - prev.energy) / (float)(std::chrono::duration_cast<std::chrono::microseconds>(curr.ts - prev.ts).count());
prev = curr;
// lots of reads but still faster than reading /proc/cpuinfo
c->avg_freq = 0;
for (uint8_t i = 0; i < N_CPU; i++) {
readFile("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, sizeof(buf));
utils::readFile("/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;
@@ -51,14 +51,14 @@ void update(cpuSensors* c) {
// commented out unused values so its faster
if (c->zenpower) {
c->tdie = fastReadNum(c->hwmon + "/temp1_input", buf) / 1000;
c->tctl = fastReadNum(c->hwmon + "/temp2_input", buf) / 1000;
c->tccd1 = fastReadNum(c->hwmon + "/temp3_input", buf) / 1000;
c->vcore = fastReadNum(c->hwmon + "/in1_input", buf) / 1000.0;
c->vsoc = fastReadNum(c->hwmon + "/in2_input", buf) / 1000.0;
c->tdie = utils::fastReadNum(c->hwmon + "/temp1_input", buf) / 1000;
c->tctl = utils::fastReadNum(c->hwmon + "/temp2_input", buf) / 1000;
c->tccd1 = utils::fastReadNum(c->hwmon + "/temp3_input", buf) / 1000;
c->vcore = utils::fastReadNum(c->hwmon + "/in1_input", buf) / 1000.0;
c->vsoc = utils::fastReadNum(c->hwmon + "/in2_input", buf) / 1000.0;
} else {
c->tctl = fastReadNum(c->hwmon + "/temp1_input", buf) / 1000;
c->tccd1 = fastReadNum(c->hwmon + "/temp3_input", buf) / 1000;
c->tctl = utils::fastReadNum(c->hwmon + "/temp1_input", buf) / 1000;
c->tccd1 = utils::fastReadNum(c->hwmon + "/temp3_input", buf) / 1000;
}
}

View File

@@ -11,11 +11,10 @@ namespace gpu {
/* takes pointer to a target gpu::sensors struct and updates its hwmon path and detects gpu_metrics */
void detectHwmon(gpuSensors* target, const std::string& pci) {
target->hwmon = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
target->hwmon = utils::getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
applog(LOG::INF, "Detected path \"{}\"", target->hwmon);
if (FILE* file = fopen((target->hwmon + REL_METRICS_PATH).c_str(), "r")) {
fclose(file);
if(utils::fileExists(target->hwmon + REL_METRICS_PATH)) {
applog(LOG::INF, "found gpu_metrics");
target->hasGpuMetrics = true;
}
@@ -24,29 +23,29 @@ void detectHwmon(gpuSensors* target, const std::string& pci) {
/* some metrics are missing in gpu_metrics, we always fill them from fs, unused metrics are commented out */
void fillMissingMetrics(gpuSensors* g) {
static char buf[32];
g->vcore = fastReadNum(g->hwmon + "/in0_input", buf) / 1000.0;
g->vcore = utils::fastReadNum(g->hwmon + "/in0_input", buf) / 1000.0;
// g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256;
g->fan_speed = fastReadNum(g->hwmon + "/fan1_input", buf);
g->power_cap = fastReadNum(g->hwmon + "/power1_cap", buf) / 1000000;
// fastReadNum(g->hwmon + "/power1_cap_max", g->power_cap_max) / 1000000;
g->vram_used = fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf);
// fastReadNum(g->hwmon + "/../../mem_info_vram_total", g->vram_total);
g->fan_speed = utils::fastReadNum(g->hwmon + "/fan1_input", buf);
g->power_cap = utils::fastReadNum(g->hwmon + "/power1_cap", buf) / 1000000;
// utils::fastReadNum(g->hwmon + "/power1_cap_max", g->power_cap_max) / 1000000;
g->vram_used = utils::fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf);
// utils::fastReadNum(g->hwmon + "/../../mem_info_vram_total", g->vram_total);
}
/* get sensors for pre-vega cards (rx480/580) */
void updateFromFs(gpuSensors* g) {
static char buf[32];
g->clock_core = fastReadNum(g->hwmon + "/freq1_input", buf) / 1000000;
g->clock_mem = fastReadNum(g->hwmon + "/freq2_input", buf) / 1000000;
g->power_avg = fastReadNum(g->hwmon + "/power1_input", buf) / 1000000;
g->temp_edge = fastReadNum(g->hwmon + "/temp1_input", buf) / 1000;
g->usage_core = fastReadNum(g->hwmon + "/../../gpu_busy_percent", buf);
g->clock_core = utils::fastReadNum(g->hwmon + "/freq1_input", buf) / 1000000;
g->clock_mem = utils::fastReadNum(g->hwmon + "/freq2_input", buf) / 1000000;
g->power_avg = utils::fastReadNum(g->hwmon + "/power1_input", buf) / 1000000;
g->temp_edge = utils::fastReadNum(g->hwmon + "/temp1_input", buf) / 1000;
g->usage_core = utils::fastReadNum(g->hwmon + "/../../gpu_busy_percent", buf);
}
/* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */
void updateFromMetrics(gpuSensors* g) {
char raw_metrics[MAX_METRICS_LENGTH];
if (!readFile(g->hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return;
if (!utils::readFile(g->hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return;
auto header = reinterpret_cast<metrics::metrics_table_header*>(raw_metrics);
switch (header->format_revision * 10 + header->content_revision) {

View File

@@ -29,7 +29,7 @@ void sys::getTimeDate(TimeDate* td) {
void sys::getRam(Memory* mem) {
static char buf[128]; // file is longer but we only need first 128 chars
readFile("/proc/meminfo", buf, sizeof(buf));
utils::readFile("/proc/meminfo", buf, sizeof(buf));
mem->free = getVal("MemAvailable:", std::string_view(buf));
mem->total = getVal("MemTotal:", std::string_view(buf));
mem->used = mem->total - mem->free;
@@ -55,8 +55,8 @@ void sys::getNetwork(Network* net, const std::string& device) {
_net current;
current.ts = std::chrono::steady_clock::now();
current.rx = fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf);
current.tx = fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf);
current.rx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf);
current.tx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf);
float ts_diff = std::chrono::duration_cast<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
net->download = (current.rx - prev.rx) / ts_diff;
net->upload = (current.tx - prev.tx) / ts_diff;
@@ -66,7 +66,7 @@ void sys::getNetwork(Network* net, const std::string& device) {
void sys::detectHwmon(sysSensors* s, const std::string& platform) {
// we dont really need getFirstDir since its a static folder, but its useful for try/catch
try {
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
s->hwmon = utils::getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
} catch (...) {
applog(LOG::ERR, "Can't open sys hwmon. Module loaded? Kernel params added?");
return;
@@ -77,7 +77,7 @@ void sys::updateSensors(sysSensors* s) {
if (s->hwmon.length() < 1)
return;
static char buf[8];
s->fan_speed[0] = fastReadNum(s->hwmon + "/fan2_input", buf);
s->fan_speed[1] = fastReadNum(s->hwmon + "/fan7_input", buf);
s->t_sensor = fastReadNum(s->hwmon + "/temp5_input", buf) / 1000;
s->fan_speed[0] = utils::fastReadNum(s->hwmon + "/fan2_input", buf);
s->fan_speed[1] = utils::fastReadNum(s->hwmon + "/fan7_input", buf);
s->t_sensor = utils::fastReadNum(s->hwmon + "/temp5_input", buf) / 1000;
}

View File

@@ -11,10 +11,12 @@
#include <string_view>
#include <utils.hpp>
namespace utils {
/* pads given string to length */
std::string pad(std::string s, uint8_t len, JUST j) {
std::string pad(std::string s, uint8_t len, just j) {
if (len > s.size())
s.insert(j == JUST::LEFT ? s.end() : s.begin(), len - s.size(), FILL_CHAR);
s.insert(j == just::left ? s.end() : s.begin(), len - s.size(), FILL_CHAR);
return s;
}
@@ -65,3 +67,5 @@ std::string getFirstDir(std::string_view path) {
return entry.path();
return "";
}
}