diff --git a/include/utils.hpp b/include/utils.hpp index db9ca66..e858294 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -7,8 +7,8 @@ #define FILL_CHAR ' ' -// wrapper around readFile2 func so we can avoid readFile and reuse one buffer, this should be faster and less bug prone? -#define fastReadNum(path, val) readFile2(path, buf, sizeof(buf)); val = strtoll(buf, NULL, 10) +// wrapper around readFile func so we can avoid readFileToStr and reuse one buffer, this should be faster and less bug prone? +#define fastReadNum(path, val) readFile(path, buf, sizeof(buf)); val = strtoll(buf, NULL, 10) enum JUST { RIGHT, @@ -21,10 +21,9 @@ inline std::string pNum(auto num, uint8_t decs = 0) { return std::format("{:.{}f std::string pad(std::string s, uint8_t len, JUST j = JUST::RIGHT); std::string center(std::string str, uint8_t len); std::string pBytes(double bytes, bool isKbytes = false); -std::string readFile(std::string path, size_t trim = 4096); -ssize_t readFile2(std::string path, char* buffer, size_t maxlen); -ssize_t readFile2(const char* path, char* buffer, size_t maxlen); -int get_val(std::string_view target, std::string_view content); +std::string readFileToStr(std::string path, size_t trim = 4096); +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); #endif diff --git a/src/cpu.cpp b/src/cpu.cpp index 65949c9..c5f850b 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -16,7 +16,7 @@ void detectHwmon(cpuSensors* target, std::string pci) { log(LOG::ERR, "cpu::detectHwmon", "NO CPU HWMON KERNEL MODULE DETECTED"); return; } - target->zenpower = readFile((target->hwmon + "/name"), 128) == "zenpower"; + target->zenpower = readFileToStr((target->hwmon + "/name"), 128) == "zenpower"; log(LOG::INF, "cpu::detectHwmon", "Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon); } @@ -37,7 +37,7 @@ void update(cpuSensors* c) { // lots of reads but still faster than reading /proc/cpuinfo c->avg_freq = 0; for (uint8_t i = 0; i < N_CPU; i++) { - readFile2("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, sizeof(buf)); + 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; diff --git a/src/gpu.cpp b/src/gpu.cpp index 1c2443d..587b291 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -46,7 +46,7 @@ void updateFromFs(gpuSensors* g) { /* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */ void updateFromMetrics(gpuSensors* g) { char raw_metrics[MAX_METRICS_LENGTH]; - if (!readFile2(g->hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return; + if (!readFile(g->hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return; auto header = reinterpret_cast(raw_metrics); switch (header->format_revision * 10 + header->content_revision) { diff --git a/src/sys.cpp b/src/sys.cpp index 124ea89..c4ef488 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -7,6 +7,17 @@ #include #include +int getVal(std::string_view target, std::string_view content) { + int result = -1; + std::size_t start = content.find(target); + if (start != std::string::npos) { + int begin = start + target.length(); + std::size_t end = content.find("kB", start); + result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10); + } + return result; +} + void sys::getTimeDate(TimeDate* td) { auto now = std::chrono::system_clock::now(); auto local = std::chrono::current_zone()->to_local(now); @@ -17,9 +28,9 @@ void sys::getTimeDate(TimeDate* td) { } void sys::getRam(Memory* mem) { - auto content = readFile("/proc/meminfo", 128); // file is longer than that but its all we need - mem->free = get_val("MemAvailable:", content); - mem->total = get_val("MemTotal:", content); + auto content = readFileToStr("/proc/meminfo", 128); // file is longer than that but its all we need + mem->free = getVal("MemAvailable:", content); + mem->total = getVal("MemTotal:", content); mem->used = mem->total - mem->free; } @@ -62,7 +73,7 @@ void sys::detectHwmon(sysSensors* s, const std::string& platform) { } void sys::updateSensors(sysSensors* s) { - if(s->hwmon.length() < 1) + if (s->hwmon.length() < 1) return; static char buf[8]; fastReadNum(s->hwmon + "/fan2_input", s->fan_speed[0]); diff --git a/src/utils.cpp b/src/utils.cpp index 310e6ee..4241a7d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -43,7 +43,7 @@ std::string pBytes(double bytes, bool isKbytes) { } /* SENSOR GETTERS + UTILS*/ -ssize_t readFile2(const char* path, char* buffer, size_t maxlen) { +ssize_t readFile(const char* path, char* buffer, size_t maxlen) { int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) { log(LOG::ERR, "readFile2", "Error reading {}", path); @@ -55,30 +55,19 @@ ssize_t readFile2(const char* path, char* buffer, size_t maxlen) { return len; } -ssize_t readFile2(std::string path, char* buffer, size_t maxlen) { - return readFile2(path.c_str(), buffer, maxlen); +ssize_t readFile(std::string path, char* buffer, size_t maxlen) { + return readFile(path.c_str(), buffer, maxlen); } -std::string readFile(std::string path, size_t trim) { +std::string readFileToStr(std::string path, size_t trim) { char* buf = new char[trim + 2]; - ssize_t len = readFile2(path, buf, trim + 2); + ssize_t len = readFile(path, buf, trim + 2); std::string out(buf); delete[] buf; if (!len) return "0"; return out.substr(0, len - 1); } -int get_val(std::string_view target, std::string_view content) { - int result = -1; - std::size_t start = content.find(target); - if (start != std::string::npos) { - int begin = start + target.length(); - std::size_t end = content.find("kB", start); - result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10); - } - return result; -} - std::string getFirstDir(std::string_view path) { for (const auto& entry : std::filesystem::directory_iterator(path)) return entry.path();