2022-10-27 01:44:00 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
extern "C" {
|
|
|
|
#include <cmocka.h>
|
2023-03-03 10:20:43 +00:00
|
|
|
}
|
2022-10-27 01:44:00 +00:00
|
|
|
#include "stdio.h"
|
|
|
|
#include "../src/amdgpu.h"
|
|
|
|
#include "../src/cpu.h"
|
2023-03-03 10:21:26 +00:00
|
|
|
|
|
|
|
#define UNUSED(x) (void)(x)
|
2022-10-27 01:44:00 +00:00
|
|
|
|
|
|
|
static void test_amdgpu_verify_metrics(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
2023-01-26 05:12:35 +00:00
|
|
|
assert_false(amdgpu_verify_metrics("./missing_file"));
|
|
|
|
// unsupported struct size, format and content revision
|
|
|
|
assert_false(amdgpu_verify_metrics("./gpu_metrics_invalid"));
|
|
|
|
assert_true (amdgpu_verify_metrics("./gpu_metrics"));
|
2022-10-27 01:44:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 10:20:43 +00:00
|
|
|
static void test_amdgpu_get_instant_metrics(void **state) {
|
2022-10-27 01:44:00 +00:00
|
|
|
UNUSED(state);
|
|
|
|
struct amdgpu_common_metrics metrics;
|
|
|
|
|
|
|
|
// fail fetch gpu_metrics file
|
2023-01-26 05:12:35 +00:00
|
|
|
metrics_path = "./missing_file";
|
2022-10-27 01:44:00 +00:00
|
|
|
amdgpu_get_instant_metrics(&metrics);
|
|
|
|
|
|
|
|
// DGPU
|
2023-01-26 05:12:35 +00:00
|
|
|
metrics_path = "./gpu_metrics";
|
2022-10-27 01:44:00 +00:00
|
|
|
metrics = {};
|
|
|
|
amdgpu_get_instant_metrics(&metrics);
|
|
|
|
assert_int_equal(metrics.gpu_load_percent, 64);
|
|
|
|
assert_float_equal(metrics.average_gfx_power_w, 33, 0);
|
2023-04-14 17:14:58 +00:00
|
|
|
assert_float_equal(metrics.average_cpu_power_w, 0, 0);
|
2022-10-27 01:44:00 +00:00
|
|
|
assert_int_equal(metrics.current_gfxclk_mhz, 2165);
|
|
|
|
assert_int_equal(metrics.current_uclk_mhz, 1000);
|
|
|
|
assert_int_equal(metrics.gpu_temp_c, 36);
|
2023-04-14 17:14:58 +00:00
|
|
|
assert_int_equal(metrics.soc_temp_c, 0);
|
|
|
|
assert_int_equal(metrics.apu_cpu_temp_c, 0);
|
2022-10-27 01:44:00 +00:00
|
|
|
assert_false(metrics.is_power_throttled);
|
|
|
|
assert_false(metrics.is_current_throttled);
|
|
|
|
assert_false(metrics.is_temp_throttled);
|
|
|
|
assert_false(metrics.is_other_throttled);
|
|
|
|
|
2023-01-26 05:12:35 +00:00
|
|
|
// DGPU
|
|
|
|
metrics_path = "./gpu_metrics_reserved_throttle_bits";
|
|
|
|
metrics = {};
|
|
|
|
amdgpu_get_instant_metrics(&metrics);
|
|
|
|
assert_false(metrics.is_power_throttled);
|
|
|
|
assert_false(metrics.is_current_throttled);
|
|
|
|
assert_false(metrics.is_temp_throttled);
|
|
|
|
assert_false(metrics.is_other_throttled);
|
|
|
|
|
|
|
|
metrics_path = "./gpu_metrics_apu";
|
2022-10-27 01:44:00 +00:00
|
|
|
metrics = {};
|
|
|
|
amdgpu_get_instant_metrics(&metrics);
|
|
|
|
assert_int_equal(metrics.gpu_load_percent, 100);
|
|
|
|
assert_float_equal(metrics.average_gfx_power_w, 6.161, 0);
|
2023-04-14 17:14:58 +00:00
|
|
|
assert_float_equal(metrics.average_cpu_power_w, 9.235, 0);
|
2022-10-27 01:44:00 +00:00
|
|
|
assert_int_equal(metrics.current_gfxclk_mhz, 1040);
|
|
|
|
assert_int_equal(metrics.current_uclk_mhz, 687);
|
|
|
|
assert_int_equal(metrics.gpu_temp_c, 81);
|
|
|
|
assert_int_equal(metrics.soc_temp_c, 71);
|
2023-04-14 17:14:58 +00:00
|
|
|
assert_int_equal(metrics.apu_cpu_temp_c, 80);
|
2022-10-27 01:44:00 +00:00
|
|
|
assert_true(metrics.is_power_throttled);
|
|
|
|
assert_false(metrics.is_current_throttled);
|
|
|
|
assert_false(metrics.is_temp_throttled);
|
|
|
|
assert_false(metrics.is_other_throttled);
|
2023-01-26 05:12:35 +00:00
|
|
|
// amdgpu binary with everything throttled
|
2022-10-27 01:44:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 10:20:43 +00:00
|
|
|
static void test_amdgpu_get_samples_and_copy(void **state) {
|
2022-10-27 01:44:00 +00:00
|
|
|
UNUSED(state);
|
|
|
|
|
2023-03-03 10:20:43 +00:00
|
|
|
struct amdgpu_common_metrics metrics_buffer[100];
|
|
|
|
bool gpu_load_needs_dividing = false; //some GPUs report load as centipercent
|
2022-10-27 01:44:00 +00:00
|
|
|
amdgpu_get_samples_and_copy(metrics_buffer, gpu_load_needs_dividing);
|
|
|
|
gpu_load_needs_dividing = true;
|
|
|
|
amdgpu_get_samples_and_copy(metrics_buffer, gpu_load_needs_dividing);
|
|
|
|
}
|
|
|
|
|
2023-03-03 10:20:43 +00:00
|
|
|
static void test_amdgpu_get_metrics(void **state) {
|
2022-10-27 01:44:00 +00:00
|
|
|
UNUSED(state);
|
|
|
|
|
2023-12-01 10:03:41 +00:00
|
|
|
amdgpu_get_metrics(0x1435);
|
2022-10-27 01:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct CMUnitTest amdgpu_tests[] = {
|
|
|
|
cmocka_unit_test(test_amdgpu_verify_metrics),
|
|
|
|
cmocka_unit_test(test_amdgpu_get_instant_metrics),
|
|
|
|
cmocka_unit_test(test_amdgpu_get_samples_and_copy),
|
|
|
|
cmocka_unit_test(test_amdgpu_get_metrics)
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
return cmocka_run_group_tests(amdgpu_tests, NULL, NULL);
|
2024-05-25 14:41:19 +00:00
|
|
|
}
|