lil refactor
This commit is contained in:
125
src/gpu.cpp
125
src/gpu.cpp
@@ -1,97 +1,86 @@
|
||||
#include <gpu.hpp>
|
||||
|
||||
/*
|
||||
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 have a read pp_table function */
|
||||
/* would be neat to wrap it in a class but eh */
|
||||
|
||||
MAYBE PASS A POINTER TO SENSORS/SENSORS->GPU TO FUNCS
|
||||
*/
|
||||
namespace gpu {
|
||||
|
||||
std::string detectGpuHwmon(std::string pci) {
|
||||
std::string path = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
|
||||
std::println("[GpuHwmon] Detected path \"{}\"", path);
|
||||
return path;
|
||||
}
|
||||
/* takes pointer to a target gpu::sensors struct and updates its hwmon path and detects gpu_metrics */
|
||||
void detectHwmon(sensors* target, std::string pci) {
|
||||
target->hwmon = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
|
||||
std::println("[gpu::detectHwmon] Detected path \"{}\"", target->hwmon);
|
||||
|
||||
bool checkForMetrics(std::string hwmon) {
|
||||
if (FILE* file = fopen((hwmon + REL_METRICS_PATH).c_str(), "r")) {
|
||||
if (FILE* file = fopen((target->hwmon + REL_METRICS_PATH).c_str(), "r")) {
|
||||
fclose(file);
|
||||
std::println("[GpuMetrics] found metrics file");
|
||||
return true;
|
||||
std::println("[gpu::detectHwmon] found metrics file");
|
||||
target->hasGpuMetrics = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
gpu_t getGpuFromMetrics(std::string hwmon) {
|
||||
gpu_t gpu;
|
||||
char raw_metrics[MAX_METRICS_LENGTH];
|
||||
if(!readFile2(hwmon + REL_METRICS_PATH, raw_metrics, sizeof(raw_metrics))) return gpu; // return empty struct if cant read metrics
|
||||
auto header = reinterpret_cast<gpu::metrics_table_header*>(raw_metrics);
|
||||
|
||||
void updateFromFs(sensors* g) {
|
||||
// these are always read from fs
|
||||
g->fan_speed = std::stol(readFile(g->hwmon + "/fan1_input", 4));
|
||||
g->vcore = std::stol(readFile(g->hwmon + "/in0_input", 4)) / 1000.0f;
|
||||
g->power_cap = std::stol(readFile(g->hwmon + "/power1_cap", 16)) / 1000000;
|
||||
g->power_max = std::stol(readFile(g->hwmon + "/power1_cap_max", 16)) / 1000000;
|
||||
g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256;
|
||||
g->vram_used = std::stoul(readFile(g->hwmon + "/../../mem_info_vram_used", 16));
|
||||
g->vram_total = std::stoul(readFile(g->hwmon + "/../../mem_info_vram_total", 16));
|
||||
|
||||
g->core_clock = std::stoll(readFile(g->hwmon + "/freq1_input", 16)) / 1000000;
|
||||
g->mem_clock = std::stoll(readFile(g->hwmon + "/freq2_input", 16)) / 1000000;
|
||||
g->power_avg = std::stol(readFile(g->hwmon + "/power1_input", 16)) / 1000000;
|
||||
g->edge_temp = std::stol(readFile(g->hwmon + "/temp1_input", 2));
|
||||
g->core_usage = std::stoi(readFile(g->hwmon + "/../../gpu_busy_percent"));
|
||||
}
|
||||
|
||||
void updateFromMetrics(sensors* g) {
|
||||
char raw_metrics[MAX_METRICS_LENGTH];
|
||||
if (!readFile2(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) {
|
||||
|
||||
/* rx7800xt */
|
||||
/* rx5700xt?, rx7800xt */
|
||||
case 13: {
|
||||
auto metrics = reinterpret_cast<gpu::gpu_metrics_v1_3*>(raw_metrics);
|
||||
gpu.core_clock = metrics->average_vclk1_frequency; // why not gfxclock?
|
||||
gpu.mem_clock = metrics->average_gfxclk_frequency; // why gfxclock? min mem clock seems to be 96mhz, this is probably including time sleeping
|
||||
gpu.core_usage = metrics->average_gfx_activity;
|
||||
gpu.media_usage = metrics->average_mm_activity;
|
||||
gpu.umc_usage = metrics->average_umc_activity;
|
||||
gpu.edge_temp = metrics->temperature_edge;
|
||||
gpu.hotspot_temp = metrics->temperature_hotspot;
|
||||
gpu.mem_temp = metrics->temperature_mem;
|
||||
gpu.gfx_vrm_temp = metrics->temperature_vrgfx;
|
||||
gpu.soc_vrm_temp = metrics->temperature_vrsoc;
|
||||
gpu.mem_vrm_temp = metrics->temperature_vrmem;
|
||||
gpu.power_avg = metrics->average_socket_power;
|
||||
METRICS_IS(metrics::gpu_metrics_v1_3);
|
||||
g->core_clock = metrics->average_vclk1_frequency; // why not gfxclock?
|
||||
g->mem_clock = metrics->average_gfxclk_frequency; // why gfxclock? min mem clock seems to be 96mhz, this is probably including time sleeping
|
||||
g->core_usage = metrics->average_gfx_activity;
|
||||
g->media_usage = metrics->average_mm_activity;
|
||||
g->umc_usage = metrics->average_umc_activity;
|
||||
g->edge_temp = metrics->temperature_edge;
|
||||
g->hotspot_temp = metrics->temperature_hotspot;
|
||||
g->mem_temp = metrics->temperature_mem;
|
||||
g->gfx_vrm_temp = metrics->temperature_vrgfx;
|
||||
g->soc_vrm_temp = metrics->temperature_vrsoc;
|
||||
g->mem_vrm_temp = metrics->temperature_vrmem;
|
||||
g->power_avg = metrics->average_socket_power;
|
||||
// some are missing, we used to read them form fs for some reason
|
||||
break;
|
||||
}
|
||||
|
||||
/* ryzen 5 5600g */
|
||||
case 22: {
|
||||
auto metrics = reinterpret_cast<gpu::gpu_metrics_v2_2*>(raw_metrics);
|
||||
gpu.edge_temp = metrics->temperature_gfx / 100;
|
||||
gpu.power_avg = metrics->average_gfx_power;
|
||||
gpu.core_usage = metrics->average_gfx_activity;
|
||||
METRICS_IS(metrics::gpu_metrics_v2_2);
|
||||
g->edge_temp = metrics->temperature_gfx / 100;
|
||||
break;
|
||||
}
|
||||
|
||||
/* werid hardware i probably dont like */
|
||||
default: {
|
||||
std::println("[GpuMetrics] Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
|
||||
std::println("[gpu::updateFromMetrics] Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return gpu;
|
||||
}
|
||||
|
||||
gpu_t getGpuFromFs(std::string hwmon) {
|
||||
gpu_t gpu;
|
||||
|
||||
// these are always read from fs
|
||||
gpu.fan_speed = std::stol(readFile(hwmon + "/fan1_input", 4));
|
||||
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;
|
||||
gpu.vram_used = std::stoul(readFile(hwmon + "/../../mem_info_vram_used", 16));
|
||||
// gpu.vram_total = std::stoul(readFile(hwmon + "/../../mem_info_vram_total", 16));
|
||||
|
||||
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"));
|
||||
|
||||
return gpu;
|
||||
/* reads values from system and updates the target gpu::sensors struct */
|
||||
void update(sensors* g) {
|
||||
if (g->hasGpuMetrics)
|
||||
gpu::updateFromMetrics(g);
|
||||
else
|
||||
gpu::updateFromFs(g);
|
||||
}
|
||||
|
||||
gpu_t getGpu(std::string hwmon, bool hasMetrics) {
|
||||
if (hasMetrics) {
|
||||
return getGpuFromMetrics(hwmon);
|
||||
} else {
|
||||
return getGpuFromFs(hwmon);
|
||||
}
|
||||
}
|
||||
} // namespace gpu
|
||||
Reference in New Issue
Block a user