refactor utils

This commit is contained in:
2025-12-28 19:55:38 +01:00
parent 2d3a481638
commit c7e2447932
6 changed files with 71 additions and 59 deletions

View File

@@ -11,11 +11,10 @@ namespace gpu {
/* takes pointer to a target gpu::sensors struct and updates its hwmon path and detects gpu_metrics */
void detectHwmon(gpuSensors* target, const std::string& pci) {
target->hwmon = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
target->hwmon = utils::getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
applog(LOG::INF, "Detected path \"{}\"", target->hwmon);
if (FILE* file = fopen((target->hwmon + REL_METRICS_PATH).c_str(), "r")) {
fclose(file);
if(utils::fileExists(target->hwmon + REL_METRICS_PATH)) {
applog(LOG::INF, "found gpu_metrics");
target->hasGpuMetrics = true;
}
@@ -24,29 +23,29 @@ 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];
g->vcore = fastReadNum(g->hwmon + "/in0_input", buf) / 1000.0;
g->vcore = utils::fastReadNum(g->hwmon + "/in0_input", buf) / 1000.0;
// g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256;
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;
g->vram_used = fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf);
// fastReadNum(g->hwmon + "/../../mem_info_vram_total", g->vram_total);
g->fan_speed = utils::fastReadNum(g->hwmon + "/fan1_input", buf);
g->power_cap = utils::fastReadNum(g->hwmon + "/power1_cap", buf) / 1000000;
// utils::fastReadNum(g->hwmon + "/power1_cap_max", g->power_cap_max) / 1000000;
g->vram_used = utils::fastReadNum(g->hwmon + "/../../mem_info_vram_used", buf);
// utils::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];
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);
g->clock_core = utils::fastReadNum(g->hwmon + "/freq1_input", buf) / 1000000;
g->clock_mem = utils::fastReadNum(g->hwmon + "/freq2_input", buf) / 1000000;
g->power_avg = utils::fastReadNum(g->hwmon + "/power1_input", buf) / 1000000;
g->temp_edge = utils::fastReadNum(g->hwmon + "/temp1_input", buf) / 1000;
g->usage_core = utils::fastReadNum(g->hwmon + "/../../gpu_busy_percent", buf);
}
/* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */
void updateFromMetrics(gpuSensors* g) {
char raw_metrics[MAX_METRICS_LENGTH];
if (!readFile(g->hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return;
if (!utils::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) {