rename funcs, move get_val

This commit is contained in:
2025-12-14 14:45:06 +01:00
parent cd94018426
commit a271711441
5 changed files with 28 additions and 29 deletions

View File

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

View File

@@ -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<metrics::metrics_table_header*>(raw_metrics);
switch (header->format_revision * 10 + header->content_revision) {

View File

@@ -7,6 +7,17 @@
#include <sys.hpp>
#include <utils.hpp>
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]);

View File

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