From 9bcec42145e772e298d16ac2b24f2f68618e9c4a Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 13 Sep 2025 23:54:41 +0200 Subject: [PATCH] gonna need to refactor --- include/gpu.hpp | 4 +++- src/gpu.cpp | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/include/gpu.hpp b/include/gpu.hpp index ef73e6c..5b04647 100644 --- a/include/gpu.hpp +++ b/include/gpu.hpp @@ -7,6 +7,8 @@ #include #include +#define REL_METRICS_PATH = "/../../gpu_metrics" + struct gpu_t { uint16_t fan_speed = 0; uint16_t core_clock = 0; @@ -37,7 +39,7 @@ struct GpuMetricsHeader_t { std::string detectGpuHwmon(std::string pci); gpu_t getGpu(GpuMetricsHeader_t header, std::string hw); -gpu_t getGpu(std::string hw); +gpu_t getGpu(std::string hw, bool hasMetrics); GpuMetricsHeader_t getGpuMetricsHeader(std::string path); #endif \ No newline at end of file diff --git a/src/gpu.cpp b/src/gpu.cpp index b4fa410..9f7e0fc 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -1,6 +1,28 @@ #include -struct GpuMetrics_v1_1_t { +/* +have a bool checkForMetrics(std::string hwmon) function, just checks if file exists +have a gpu_t getGpu(std::string path, bool hasMetrics) function +maybe have a read pp_table function + +MAYBE PASS A POINTER TO SENSORS/SENSORS->GPU TO FUNCS + +general order of operation + 1. [main] get hwmon path from the detectGpuHwmon func + 2. [main] check if we have gpu_metrics present and set hasMetrics accordingly + 3. [main, loop] keep calling getGpu with hasMetrics + 3. [gpu] getGpu called, parses whatever metrics we read from sysfs, then checks if hasMetrics == true + - if true, reads file at (hwmon + REL_METRICS_PATH) into buf + - reads first 4 bytes into GpuMetricsHeader_t with reinterpret_cast + - parses them for format and content version + - skips first 4 bytes of the buf and reinterpret_casts the rest into a correct gpu_metrics type (switch case?) + - populates gpu_t with values from gpu_metrics + - if false, reads the rest of relevant metrics from sysfs + - returns gpu_t + + */ + +struct [[gnu::packed]] GpuMetrics_v1_1_t { uint16_t temperature_edge; uint16_t temperature_hotspot; uint16_t temperature_mem; @@ -37,7 +59,7 @@ struct GpuMetrics_v1_1_t { uint16_t temperature_hbm; }; -struct GpuMetrics_v1_3_t { +struct [[gnu::packed]] GpuMetrics_v1_3_t { uint16_t temperature_edge; uint16_t temperature_hotspot; uint16_t temperature_mem; @@ -102,7 +124,9 @@ GpuMetricsHeader_t getGpuMetricsHeader(std::string path) { /* will return empty metric if fails */ auto getGpuMetrics(GpuMetricsHeader_t h, std::string path) { GpuMetrics_v1_3_t metrics; + // void* raw_data; uint8_t rev = h.format_revision * 10 + h.content_revision; + // const auto* metrics = reinterpret_cast(raw_data); // raw data is actual metrics read from disk switch (rev) { case 13: { @@ -128,7 +152,7 @@ gpu_t getGpu(GpuMetricsHeader_t header, std::string hw) { gpu.vcore = std::stol(readFile(hw + "/hwmon/hwmon" + std::to_string(hwmon) + "/in0_input")) / 1000.0; gpu.power_cap = std::stol(readFile(hw + "/hwmon/hwmon" + std::to_string(hwmon) + "/power1_cap", 3)); gpu.power_max = std::stol(readFile(hw + "/hwmon/hwmon" + std::to_string(hwmon) + "/power1_cap_max", 3)); - gpu.fan_pwm = std::stoi(readFile(hwmon + "/pwm1", 4)) * 100 / 256; // not super useful + gpu.fan_pwm = std::stoi(readFile(hwmon + "/pwm1", 4)) * 100 / 256; // not super useful gpu.vram_used = std::stoul(readFile(hw + "/mem_info_vram_used")); gpu.vram_total = std::stoul(readFile(hw + "/mem_info_vram_total")); @@ -163,7 +187,7 @@ gpu_t getGpu(GpuMetricsHeader_t header, std::string hw) { return gpu; } -gpu_t getGpu(std::string hwmon) { +gpu_t getGpu(std::string hwmon, bool hasMetrics) { gpu_t gpu; // these are always read from fs @@ -171,16 +195,18 @@ gpu_t getGpu(std::string hwmon) { gpu.vcore = std::stol(readFile(hwmon + "/in0_input", 4)) / 1000.0; gpu.power_cap = std::stol(readFile(hwmon + "/power1_cap", 16)) / 1000000; // gpu.power_max = std::stol(readFile(hwmon + "/power1_cap_max", 16)) / 1000000; - gpu.fan_pwm = std::stoi(readFile(hwmon + "/pwm1", 4)) * 100 / 256; // not super useful + gpu.fan_pwm = std::stoi(readFile(hwmon + "/pwm1", 4)) * 100 / 256; gpu.vram_used = std::stoul(readFile(hwmon + "/../../mem_info_vram_used", 16)); // gpu.vram_total = std::stoul(readFile(hwmon + "/../../mem_info_vram_total", 16)); // these also exist in gpu_metrics? - gpu.core_clock = std::stoll(readFile(hwmon + "/freq1_input", 16)) / 1000000; - gpu.mem_clock = std::stoll(readFile(hwmon + "/freq2_input", 16)) / 1000000; - gpu.power_avg = std::stol(readFile(hwmon + "/power1_input", 16)) / 1000000; - gpu.edge_temp = std::stol(readFile(hwmon + "/temp1_input", 2)); - gpu.core_usage = std::stoi(readFile(hwmon + "/../../gpu_busy_percent")); + if (!hasMetrics) { + gpu.core_clock = std::stoll(readFile(hwmon + "/freq1_input", 16)) / 1000000; + gpu.mem_clock = std::stoll(readFile(hwmon + "/freq2_input", 16)) / 1000000; + gpu.power_avg = std::stol(readFile(hwmon + "/power1_input", 16)) / 1000000; + gpu.edge_temp = std::stol(readFile(hwmon + "/temp1_input", 2)); + gpu.core_usage = std::stoi(readFile(hwmon + "/../../gpu_busy_percent")); + } if (gpu.fan_speed > 9999) gpu.fan_speed = 9999;