get rid of macro, use inline func

This commit is contained in:
2025-12-28 19:06:23 +01:00
parent 12d7ab9b0f
commit 2d3a481638
5 changed files with 44 additions and 38 deletions

View File

@@ -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 <size_t N>
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

View File

@@ -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);

View File

@@ -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<std::chrono::steady_clock> ts; };
struct EnergyTs {
uint64_t energy = 0;
std::chrono::time_point<std::chrono::steady_clock> 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<std::chrono::microseconds>(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;
}
}

View File

@@ -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 [*]) */

View File

@@ -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<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;
@@ -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;
}