diff --git a/include/utils.hpp b/include/utils.hpp index 4a722d2..8f8bd79 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -22,18 +22,16 @@ ssize_t readFile(const std::string& path, char* buffer, size_t maxlen); ssize_t readFile(const char* path, char* buffer, size_t maxlen); std::string getFirstDir(const std::string_view& path); -/* reads a number from file REALLY fast */ -template +/* reads a number fast and divides it if needed */ +template inline void fastReadNum(const std::string& path, char (&buf)[N], T& result) { readFile(path, buf, N); - std::from_chars(buf, buf + N, result); -} - -/* reads a number slightly slower and divides it */ -template -inline void fastReadNumDiv(const std::string& path, char (&buf)[N], T& result) { + if constexpr (Divider == 1) { + std::from_chars(buf, buf + N, result); + return; + } uint64_t temp{}; - fastReadNum(path, buf, temp); + std::from_chars(buf, buf + N, temp); if constexpr (std::is_floating_point_v) result = temp / static_cast(Divider); else diff --git a/src/cpu.cpp b/src/cpu.cpp index 3a73a25..ef09845 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -50,14 +50,14 @@ void update(cpuSensors* c) { // commented out unused values so its faster if (c->zenpower) { - utils::fastReadNumDiv<1000>(c->hwmon + "/temp1_input", buf, c->tdie); - utils::fastReadNumDiv<1000>(c->hwmon + "/temp2_input", buf, c->tctl); - utils::fastReadNumDiv<1000>(c->hwmon + "/temp3_input", buf, c->tccd1); - utils::fastReadNumDiv<1000>(c->hwmon + "/in1_input", buf, c->vcore); - utils::fastReadNumDiv<1000>(c->hwmon + "/in2_input", buf, c->vsoc); + utils::fastReadNum<1000>(c->hwmon + "/temp1_input", buf, c->tdie); + utils::fastReadNum<1000>(c->hwmon + "/temp2_input", buf, c->tctl); + utils::fastReadNum<1000>(c->hwmon + "/temp3_input", buf, c->tccd1); + utils::fastReadNum<1000>(c->hwmon + "/in1_input", buf, c->vcore); + utils::fastReadNum<1000>(c->hwmon + "/in2_input", buf, c->vsoc); } else { - utils::fastReadNumDiv<1000>(c->hwmon + "/temp1_input", buf, c->tctl); - utils::fastReadNumDiv<1000>(c->hwmon + "/temp3_input", buf, c->tccd1); + utils::fastReadNum<1000>(c->hwmon + "/temp1_input", buf, c->tctl); + utils::fastReadNum<1000>(c->hwmon + "/temp3_input", buf, c->tccd1); } } diff --git a/src/gpu.cpp b/src/gpu.cpp index 8aff6e3..d2995e4 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -26,10 +26,10 @@ 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]; - utils::fastReadNumDiv<1000>(g->hwmon + "/in0_input", buf, g->vcore); + utils::fastReadNum<1000>(g->hwmon + "/in0_input", buf, g->vcore); // g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256; utils::fastReadNum(g->hwmon + "/fan1_input", buf, g->fan_speed); - utils::fastReadNumDiv<1000000>(g->hwmon + "/power1_cap", buf, g->power_cap); + utils::fastReadNum<1000000>(g->hwmon + "/power1_cap", buf, g->power_cap); // utils::fastReadNum(g->hwmon + "/power1_cap_max", g->power_cap_max) / 1000000; utils::fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf, g->vram_used); // utils::fastReadNum(g->hwmon + "/../../mem_info_vram_total", g->vram_total); @@ -38,10 +38,10 @@ void fillMissingMetrics(gpuSensors* g) { /* get sensors for pre-vega cards (rx480/580) */ void updateFromFs(gpuSensors* g) { static char buf[32]; - utils::fastReadNumDiv<1000000>(g->hwmon + "/freq1_input", buf, g->clock_core); - utils::fastReadNumDiv<1000000>(g->hwmon + "/freq2_input", buf, g->clock_mem); - utils::fastReadNumDiv<1000000>(g->hwmon + "/power1_input", buf, g->power_avg); - utils::fastReadNumDiv<1000>(g->hwmon + "/temp1_input", buf, g->temp_edge); + utils::fastReadNum<1000000>(g->hwmon + "/freq1_input", buf, g->clock_core); + utils::fastReadNum<1000000>(g->hwmon + "/freq2_input", buf, g->clock_mem); + utils::fastReadNum<1000000>(g->hwmon + "/power1_input", buf, g->power_avg); + utils::fastReadNum<1000>(g->hwmon + "/temp1_input", buf, g->temp_edge); utils::fastReadNum(g->hwmon + "/../../gpu_busy_percent", buf, g->usage_core); } diff --git a/src/sys.cpp b/src/sys.cpp index cbea1a3..8d05274 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -81,5 +81,5 @@ void sys::updateSensors(sysSensors* s) { static char buf[8]; utils::fastReadNum(s->hwmon + "/fan2_input", buf, s->fan_speed[0]); utils::fastReadNum(s->hwmon + "/fan7_input", buf, s->fan_speed[1]); - utils::fastReadNumDiv<1000>(s->hwmon + "/temp5_input", buf, s->t_sensor); + utils::fastReadNum<1000>(s->hwmon + "/temp5_input", buf, s->t_sensor); }