From 2d3a481638587037f71189ea83dcfe178d7c5df1 Mon Sep 17 00:00:00 2001 From: Drath Date: Sun, 28 Dec 2025 19:06:23 +0100 Subject: [PATCH] get rid of macro, use inline func --- include/utils.hpp | 22 ++++++++++++---------- main.cpp | 8 ++++---- src/cpu.cpp | 24 ++++++++++++++---------- src/gpu.cpp | 18 +++++++++--------- src/sys.cpp | 10 +++++----- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/include/utils.hpp b/include/utils.hpp index e658850..b533487 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -7,16 +7,7 @@ #define FILL_CHAR ' ' -// wrapper around readFile func -#define fastReadNum(path, val) readFile(path, buf, sizeof(buf)); val = std::strtoll(buf, NULL, 10) - -enum JUST { - RIGHT, - LEFT -}; - -// pretty prints num with given decs, 0 by default. its basically a macro so we inline it -inline std::string pNum(auto num, uint8_t decs = 0) { return std::format("{:.{}f}", num, decs); } +enum JUST { RIGHT, LEFT }; std::string pad(std::string s, uint8_t len, JUST j = JUST::RIGHT); std::string center(std::string str, uint8_t len); @@ -25,4 +16,15 @@ ssize_t readFile(std::string path, char* buffer, size_t maxlen); ssize_t readFile(const char* path, char* buffer, size_t maxlen); std::string getFirstDir(std::string_view path); +template +inline long long fastReadNum(const std::string& path, char (&buf)[N]) { + readFile(path, buf, N); + return std::strtoll(buf, nullptr, 10); +} + +// pretty prints num with given decs, 0 by default. its basically a macro so we inline it +inline std::string pNum(auto num, uint8_t decs = 0) { + return std::format("{:.{}f}", num, decs); +} + #endif diff --git a/main.cpp b/main.cpp index 452f5cf..3f1c065 100644 --- a/main.cpp +++ b/main.cpp @@ -49,7 +49,7 @@ void signalHandler(int) { } void hassThread(std::stop_token st, Sensors* s) { - applog(LOG::INF, "Thread started"); + applog(LOG::START, "Thread started"); HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc"); pc_power.connect(); @@ -66,7 +66,7 @@ void hassThread(std::stop_token st, Sensors* s) { } void sensorsThread(std::stop_token st, Sensors* s) { - applog(LOG::INF, "Thread started, poll interval {} ms", LCD_INTERVAL_MS); + applog(LOG::START, "Thread started, poll interval {} ms", LCD_INTERVAL_MS); cpu::detectHwmon(&s->cpu, "0000:00:18.3"); gpu::detectHwmon(&s->gpu, "0000:0b:00.0"); sys::detectHwmon(&s->sys, "nct6775.656"); @@ -94,7 +94,7 @@ void sensorsThread(std::stop_token st, Sensors* s) { } void displayThread(std::stop_token st, Sensors* s) { - applog(LOG::INF, "Thread started"); + applog(LOG::START, "Thread started"); // init internal variables of LCDDisplay class LCDDisplay lcd(LCD_PORT); @@ -165,7 +165,7 @@ void displayThread(std::stop_token st, Sensors* s) { } int main() { - applog(LOG::START, "Program started"); // to make logs clearer + applog(LOG::START, "Application started", __FILE__); // to make logs clearer std::signal(SIGINT, signalHandler); std::signal(SIGTERM, signalHandler); diff --git a/src/cpu.cpp b/src/cpu.cpp index c8cbc47..562639b 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -17,13 +17,16 @@ void detectHwmon(cpuSensors* target, const std::string& pci) { return; } - target->zenpower = false; // we were using readFileToStr to figure this out, we dont f/w it anymore, find another way (see if hwmon/in1_input exists?) + target->zenpower = false; // we were using readFileToStr to figure this out, we dont f/w it anymore, find another way (see if hwmon/in1_input exists?) applog(LOG::INF, "Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon); } void update(cpuSensors* c) { static char buf[32]; - struct EnergyTs { uint64_t energy = 0; std::chrono::time_point ts; }; + struct EnergyTs { + uint64_t energy = 0; + std::chrono::time_point ts; + }; static EnergyTs prev; EnergyTs curr; @@ -31,7 +34,8 @@ 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); + // fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", curr.energy); + curr.energy = fastReadNum("/sys/class/powercap/intel-rapl:0/energy_uj", buf); c->ptotal = (float)(curr.energy - prev.energy) / (float)(std::chrono::duration_cast(curr.ts - prev.ts).count()); prev = curr; @@ -47,14 +51,14 @@ void update(cpuSensors* c) { // 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; + 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; } else { - fastReadNum(c->hwmon + "/temp1_input", c->tctl) / 1000; - fastReadNum(c->hwmon + "/temp3_input", c->tccd1) / 1000; + c->tctl = fastReadNum(c->hwmon + "/temp1_input", buf) / 1000; + c->tccd1 = fastReadNum(c->hwmon + "/temp3_input", buf) / 1000; } } diff --git a/src/gpu.cpp b/src/gpu.cpp index 5ecc6e9..8438cb2 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -24,23 +24,23 @@ 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]; - fastReadNum(g->hwmon + "/in0_input", g->vcore) / 1000.0; + g->vcore = fastReadNum(g->hwmon + "/in0_input", buf) / 1000.0; // g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256; - fastReadNum(g->hwmon + "/fan1_input", g->fan_speed); - fastReadNum(g->hwmon + "/power1_cap", g->power_cap) / 1000000; + 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; - fastReadNum(g->hwmon + "/../../mem_info_vram_used", g->vram_used); + g->vram_used = fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf); // 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]; - fastReadNum(g->hwmon + "/freq1_input", g->clock_core) / 1000000; - fastReadNum(g->hwmon + "/freq2_input", g->clock_mem) / 1000000; - fastReadNum(g->hwmon + "/power1_input", g->power_avg) / 1000000; - fastReadNum(g->hwmon + "/temp1_input", g->temp_edge) / 1000; - fastReadNum(g->hwmon + "/../../gpu_busy_percent", g->usage_core); + 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); } /* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */ diff --git a/src/sys.cpp b/src/sys.cpp index 5571c2f..9a540a2 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -55,8 +55,8 @@ void sys::getNetwork(Network* net, const std::string& device) { _net current; current.ts = std::chrono::steady_clock::now(); - fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", current.rx); - fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", current.tx); + current.rx = fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf); + current.tx = fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf); float ts_diff = std::chrono::duration_cast(current.ts - prev.ts).count() / 1000000.0f; net->download = (current.rx - prev.rx) / ts_diff; net->upload = (current.tx - prev.tx) / ts_diff; @@ -77,7 +77,7 @@ void sys::updateSensors(sysSensors* s) { if (s->hwmon.length() < 1) return; static char buf[8]; - fastReadNum(s->hwmon + "/fan2_input", s->fan_speed[0]); - fastReadNum(s->hwmon + "/fan7_input", s->fan_speed[1]); - fastReadNum(s->hwmon + "/temp5_input", s->t_sensor) / 1000; + 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; }