Cleanup cpu.cpp and and support for gpu_metrics_v2_3

pull/980/head
sfrcorne 1 year ago committed by flightlessmango
parent eab6b9161a
commit cd74b27da2

@ -28,17 +28,21 @@ bool amdgpu_verify_metrics(const std::string& path)
return false;
}
switch (header.format_revision)
{
case 1:
switch (header.format_revision)
{
case 1: // v1_1, v1_2, v1_3
if(header.content_revision<=0 || header.content_revision>3)// v1_0, not naturally aligned
break;
cpuStats.cpu_type = "GPU";
return true;
case 2:
case 2: // v2_1, v2_2, v2_3
if(header.content_revision<=0 || header.content_revision>3)// v2_0, not naturally aligned
break;
cpuStats.cpu_type = "APU";
return true;
default:
default:
break;
}
}
SPDLOG_WARN("Unsupported gpu_metrics version: {}.{}", header.format_revision, header.content_revision);
return false;
@ -47,7 +51,7 @@ bool amdgpu_verify_metrics(const std::string& path)
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
void amdgpu_get_instant_metrics(struct amdgpu_common_metrics *metrics) {
FILE *f;
void *buf[MAX(sizeof(struct gpu_metrics_v1_3), sizeof(struct gpu_metrics_v2_3))];
void *buf[MAX(sizeof(struct gpu_metrics_v1_3), sizeof(struct gpu_metrics_v2_3))/sizeof(void*)+1];
struct metrics_table_header* header = (metrics_table_header*)buf;
f = fopen(metrics_path.c_str(), "rb");
@ -55,8 +59,8 @@ void amdgpu_get_instant_metrics(struct amdgpu_common_metrics *metrics) {
return;
// Read the whole file
if (!fread(buf, sizeof(buf), 1, f) == 0) {
SPDLOG_DEBUG("Failed to read amdgpu metrics file '{}'", metrics_path.c_str());
if (fread(buf, sizeof(buf), 1, f) != 0) {
SPDLOG_DEBUG("amdgpu metrics file '{}' is larger than the buffer", metrics_path.c_str());
fclose(f);
return;
}
@ -83,6 +87,12 @@ void amdgpu_get_instant_metrics(struct amdgpu_common_metrics *metrics) {
metrics->average_gfx_power_w = amdgpu_metrics->average_gfx_power / 1000.f;
metrics->average_cpu_power_w = amdgpu_metrics->average_cpu_power / 1000.f;
// Use cpu + soc power similar to 'get_cpu_power_k10temp' in 'cpu.cpp' ?
//metrics->average_cpu_power_w = amdgpu_metrics->average_cpu_power / 1000.f + amdgpu_metrics->average_soc_power / 1000.f;
// average_cpu_power_w fallback ?
//if(!( amdgpu_metrics->average_cpu_power ^ 0xffff && amdgpu_metrics->average_soc_power ^ 0xffff ))
// metrics->average_cpu_power_w = amdgpu_metrics->average_socket_power / 1000.f - amdgpu_metrics->average_gfx_power / 1000.f;
metrics->current_gfxclk_mhz = amdgpu_metrics->current_gfxclk;
metrics->current_uclk_mhz = amdgpu_metrics->current_uclk;

@ -96,59 +96,6 @@ struct gpu_metrics_v1_3 {
uint64_t indep_throttle_status;
};
struct gpu_metrics_v2_2 {
struct metrics_table_header common_header;
/* Temperature */
uint16_t temperature_gfx; // gfx temperature on APUs
uint16_t temperature_soc; // soc temperature on APUs
uint16_t temperature_core[8]; // CPU core temperature on APUs
uint16_t temperature_l3[2];
/* Utilization */
uint16_t average_gfx_activity;
uint16_t average_mm_activity; // UVD or VCN
/* Driver attached timestamp (in ns) */
uint64_t system_clock_counter;
/* Power/Energy */
uint16_t average_socket_power; // dGPU + APU power on A + A platform
uint16_t average_cpu_power;
uint16_t average_soc_power;
uint16_t average_gfx_power;
uint16_t average_core_power[8]; // CPU core power on APUs
/* Average clocks */
uint16_t average_gfxclk_frequency;
uint16_t average_socclk_frequency;
uint16_t average_uclk_frequency;
uint16_t average_fclk_frequency;
uint16_t average_vclk_frequency;
uint16_t average_dclk_frequency;
/* Current clocks */
uint16_t current_gfxclk;
uint16_t current_socclk;
uint16_t current_uclk;
uint16_t current_fclk;
uint16_t current_vclk;
uint16_t current_dclk;
uint16_t current_coreclk[8]; // CPU core clocks
uint16_t current_l3clk[2];
/* Throttle status (ASIC dependent) */
uint32_t throttle_status;
/* Fans */
uint16_t fan_pwm;
uint16_t padding[3];
/* Throttle status (ASIC independent) */
uint64_t indep_throttle_status;
};
struct gpu_metrics_v2_3 {
struct metrics_table_header common_header;

@ -381,9 +381,9 @@ bool CPUStats::UpdateCpuPower() {
return true;
}
static bool find_temp_input(const std::string path, std::string& input, const std::string& name)
static bool find_input(const std::string& path, const char* input_prefix, std::string& input, const std::string& name)
{
auto files = ls(path.c_str(), "temp", LS_FILES);
auto files = ls(path.c_str(), input_prefix, LS_FILES);
for (auto& file : files) {
if (!ends_with(file, "_label"))
continue;
@ -402,9 +402,9 @@ static bool find_temp_input(const std::string path, std::string& input, const st
return false;
}
static bool find_fallback_temp_input(const std::string path, std::string& input)
static bool find_fallback_input(const std::string& path, const char* input_prefix, std::string& input)
{
auto files = ls(path.c_str(), "temp", LS_FILES);
auto files = ls(path.c_str(), input_prefix, LS_FILES);
if (!files.size())
return false;
@ -413,7 +413,7 @@ static bool find_fallback_temp_input(const std::string path, std::string& input)
if (!ends_with(file, "_input"))
continue;
input = path + "/" + file;
SPDLOG_DEBUG("fallback cpu temp input: {}", input);
SPDLOG_DEBUG("fallback cpu {} input: {}", input_prefix, input);
return true;
}
return false;
@ -433,23 +433,24 @@ bool CPUStats::GetCpuFile() {
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
if (name == "coretemp") {
find_temp_input(path, input, "Package id 0");
find_input(path, "temp", input, "Package id 0");
break;
}
else if ((name == "zenpower" || name == "k10temp")) {
find_temp_input(path, input, "Tdie");
if (!find_input(path, "temp", input, "Tdie"))
find_input(path, "temp", input, "Tctl");
break;
} else if (name == "atk0110") {
find_temp_input(path, input, "CPU Temperature");
find_input(path, "temp", input, "CPU Temperature");
break;
} else if (name == "it8603") {
find_temp_input(path, input, "temp1");
find_input(path, "temp", input, "temp1");
break;
} else {
path.clear();
}
}
if (path.empty() || (!file_exists(input) && !find_fallback_temp_input(path, input))) {
if (path.empty() || (!file_exists(input) && !find_fallback_input(path, "temp", input))) {
SPDLOG_ERROR("Could not find cpu temp sensor location");
return false;
} else {
@ -459,27 +460,6 @@ bool CPUStats::GetCpuFile() {
return true;
}
static bool find_input(const std::string& path, const char* input_prefix, std::string& input, const std::string& name)
{
auto files = ls(path.c_str(), input_prefix, LS_FILES);
for (auto& file : files) {
if (!ends_with(file, "_label"))
continue;
auto label = read_line(path + "/" + file);
if (label != name)
continue;
auto uscore = file.find_first_of("_");
if (uscore != std::string::npos) {
file.erase(uscore, std::string::npos);
input = path + "/" + file + "_input";
return true;
}
}
return false;
}
static CPUPowerData_k10temp* init_cpu_power_data_k10temp(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_k10temp>();

Loading…
Cancel
Save