Files
rocm-systems/tests/amd_smi_test/functional/dynamic_metrics_test.cc
T
Poag, Charis 01b4fe6614 [SWDEV-535159] Add support for GPU partition metrics (#490)
[SWDEV-535159] Add support for GPU partition metrics

Changes include:
  - Internal logic to smart-switch between gpu_metrics/xcp_metrics files
  - [WIP] Initial plumbing for new partition metric API

Change-Id: I4340fb1b48bac0117d80d5d486b9e871430d5cd8
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Add amdsmi_get_gpu_partition_metrics_info() + minor cleanup

Change-Id: I5d60604f18baddbd03852dc90e88aa0b8107d50e
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Fix partition metric logic + update logging/tests

Change-Id: I9e89b19ead17694c54e224f8e13ff8ee3eb2e22a
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Adjust amd-smi metric/monitor/default to show (some) partition information

Change-Id: I2e8d2745876a19bdaec3c039daa97345c9f701b5
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Add C++ tests

Change-Id: Ib9eb0b57a6d7a280992e05a4c6eba632826952ef
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Remove modification of energy counter, not needed

Change-Id: I5c48eaaae248ee6dc79abba609d837ec35d78022
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

[CLI] amd-smi metric: cleaned up N/A'd multi-valued to show just N/A

Changes:
1. amd-smi metric: cleaned up N/A'd multi-valued to show just N/A
ex.
JPEG_ACTIVITY: [N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A]

Now just shows: N/A

2. [Python Unit Test] Changed testname TestAmdSmiPythonBDF(unittest.TestCase) ->
 AmdSmiPythonUnitTest

Test name was confusing.

Change-Id: Ieb3b036f30002fd22362508eb9fc5d443df395ae
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Log cleanup

Change-Id: I1b1a95f1844d35bec7a7bd8cb996f87e4914c069
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Add amd-smi partition-metrics CLI + general cleanup

Change-Id: Ia91488e6cb3a4d62b4087afbddfe0b3bb9378fdc
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

[1.3 metrics] Remove forwards compatibility for partition metrics

Change-Id: Iab928983e6f6f1587bc9307f6f3fa2b2696ca6f7
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Fixed violation output not showing % + general cleanup

Change-Id: Icac1b0a55b18c7628b07109ae0c377d17e0825f1
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

Clean up amdsmi_get_gpu_partition_metrics_info & amd-smi partition-metric outputs

Change-Id: I6427028b980874641e9ffb3b5d88ad493dbf9cf4
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Fix metrics not found + extra logging/formatting

Change-Id: I841a27bb2c305e97ec7579a13ac915e5be497c3a
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Update license to current default

Change-Id: I0de9b8a2d5dbbeab4491097f0354ba17b0d30866
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Cleanup for review

Change-Id: I96ed25c3f2b8968eea1af24c5e5860c2b4e74e6e
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Moderize updated/new interal APIs.

Change-Id: I3c48a250eeb703709b14cb5ffa68268d8321626c
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Remove extra logging in dynamic metrics

Change-Id: Idb97547bcbe143d6fa1cb5cb278ffe4da615ce14
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Remove amd-smi partition-metric command

Change-Id: Ib83c17e5cd7e0da3798198943bddd46c296b411c
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Move new CLI updates to another PR + minor fixes

Change-Id: I3b1163eec12f9b5f7d95ee33de08e168cec1b1fe
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Allow dynamic metrics to work for gpu/xcp metrics 1.9+/1.1+

Updated some logging as well.

Change-Id: I2ed9f5a5ef8afb1520508820ca6153525f0644b4
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Allow dyn gpu/xcp metric v1.9+/v1.1+

Added tests for quick check

Change-Id: I576d6f6582a55afb08e5ac57791ce95e2fa184a2
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Update tests for larger subset of version checks

Change-Id: I3cdf4f8bb4fc6161f4c76566939f90545d0f362a
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

* Fix XCP metrics in gpu/partition metric pre-v1.9/v1.1 (dynamic)

Change-Id: I4dabc1ed6bef6b86c8e7f92bf9cb5992f3966fe2
Signed-off-by: Charis Poag <Charis.Poag@amd.com>

---------

Signed-off-by: Charis Poag <Charis.Poag@amd.com>
2025-10-20 14:43:40 -05:00

204 lines
7.9 KiB
C++

/*
* Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <amd_smi_test/test_base.h>
#include <gtest/gtest.h>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <vector>
#include "rocm_smi/rocm_smi_gpu_metrics.h"
namespace amd::smi {
// Forward declarations of internal helpers we exercise in this unit-test.
AMDGpuMetricVersionFlags_t translate_header_to_flag_version(
const AMDGpuMetricsHeader_v1_t& metrics_header, bool is_partition_metrics,
const std::string& file_path);
GpuMetricsBasePtr amdgpu_metrics_factory(AMDGpuMetricVersionFlags_t gpu_metric_version,
bool is_partition_metrics, const std::string& file_path);
} // namespace amd::smi
namespace {
// Version helper checker
auto GetExpectedMetricVersionFlag(uint16_t major, uint16_t minor, bool is_partition_metrics)
-> amd::smi::AMDGpuMetricVersionFlags_t {
using Flag = amd::smi::AMDGpuMetricVersionFlags_t;
if (is_partition_metrics) {
if (major == 1) {
if (minor == 0) {
return Flag::kGpuXcpMetricV10;
} else if (minor >= 1) {
return Flag::kGpuXcpMetricDynV11Plus;
} else {
return Flag::kGpuMetricNone;
}
}
} else { // GPU metrics
if (major == 1) {
switch (minor) {
case 0: return Flag::kGpuMetricNone;
case 1: return Flag::kGpuMetricV11;
case 2: return Flag::kGpuMetricV12;
case 3: return Flag::kGpuMetricV13;
case 4: return Flag::kGpuMetricV14;
case 5: return Flag::kGpuMetricV15;
case 6: return Flag::kGpuMetricV16;
case 7: return Flag::kGpuMetricV17;
case 8: return Flag::kGpuMetricV18;
default: return Flag::kGpuMetricDynV19Plus;
}
}
}
return Flag::kGpuMetricNone;
}
// pass a header we want to test against
auto BuildFakeMetricsBlob(amd::smi::AMDGpuMetricsHeader_v1_t new_header) -> std::vector<uint8_t> {
if (new_header.m_structure_size < sizeof(new_header)) {
throw std::runtime_error("Header size too small");
}
amd::smi::AMDGpuMetricsHeader_v1_t header{};
header.m_structure_size = static_cast<uint16_t>(sizeof(header));
header.m_format_revision = new_header.m_format_revision;
header.m_content_revision = new_header.m_content_revision;
const uint8_t* begin = reinterpret_cast<const uint8_t*>(&header);
return std::vector<uint8_t>(begin, begin + sizeof(header));
}
auto WriteBlobToTempFile(const std::vector<uint8_t>& blob,
const std::string& filename = "amdsmi_fake_metrics.bin")
-> std::filesystem::path {
auto temp_dir = std::filesystem::temp_directory_path();
auto file_path = temp_dir / filename;
std::ofstream stream(file_path, std::ios::binary | std::ios::trunc);
stream.write(reinterpret_cast<const char*>(blob.data()),
static_cast<std::streamsize>(blob.size()));
stream.close();
return file_path;
}
} // namespace
TEST(AmdSmiDynamicMetricTest, GPUMetricDynamicVersionSupported) {
const bool is_partition_metrics = false;
for (auto ver : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}) {
std::string test_detail = "[GPUMetric";
if (ver >= 9) {
test_detail += "Dynamic] ";
} else {
test_detail += "] ";
}
std::cout << test_detail << "Checking version 1." << ver << std::endl;
SCOPED_TRACE(testing::Message() << "Subtest for minor version: 1." << ver);
const auto blob = BuildFakeMetricsBlob(amd::smi::AMDGpuMetricsHeader_v1_t{
.m_structure_size = sizeof(amd::smi::AMDGpuMetricsHeader_v1_t),
.m_format_revision = 1,
.m_content_revision = static_cast<uint16_t>(ver), // Known minor versions
});
const auto fake_path =
WriteBlobToTempFile(blob, "amdsmi_fake_gpu_metrics_v1" + std::to_string(ver) + ".bin");
ASSERT_FALSE(blob.empty());
ASSERT_TRUE(std::filesystem::exists(fake_path));
const auto* header = reinterpret_cast<const amd::smi::AMDGpuMetricsHeader_v1_t*>(blob.data());
const auto flag = amd::smi::translate_header_to_flag_version(*header, is_partition_metrics,
fake_path.string());
EXPECT_EQ(flag, GetExpectedMetricVersionFlag(1, ver, is_partition_metrics))
<< "Version 1." << ver << " should be treated as supported";
auto gpu_metrics_ptr =
amd::smi::amdgpu_metrics_factory(flag, is_partition_metrics, fake_path.string());
if (ver != 0) {
EXPECT_NE(gpu_metrics_ptr, nullptr)
<< "Factory must create metrics object for supported version";
} else {
EXPECT_EQ(gpu_metrics_ptr, nullptr)
<< "Factory must not create metrics object for unsupported versions";
}
if (gpu_metrics_ptr) {
std::cout << test_detail << "Created valid object for version 1." << ver << std::endl;
} else {
std::cout << test_detail << "Unsupported Metric Version"
<< " | Failed to create valid object for version 1." << ver << std::endl;
}
std::filesystem::remove(fake_path);
}
}
TEST(AmdSmiDynamicMetricTest, XCPMetricDynamicVersionSupported) {
const bool is_partition_metrics = true;
for (auto ver : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}) {
std::string test_detail = "[XCPMetric";
if (ver >= 1) {
test_detail += "Dynamic] ";
} else {
test_detail += "] ";
}
std::cout << test_detail << "Checking version 1." << ver << std::endl;
SCOPED_TRACE(testing::Message() << "Subtest for minor version: 1." << ver);
const auto blob = BuildFakeMetricsBlob(amd::smi::AMDGpuMetricsHeader_v1_t{
.m_structure_size = sizeof(amd::smi::AMDGpuMetricsHeader_v1_t),
.m_format_revision = 1,
.m_content_revision = static_cast<uint16_t>(ver), // Known minor versions
});
const auto fake_path =
WriteBlobToTempFile(blob, "amdsmi_fake_xcp_metrics_v1" + std::to_string(ver) + ".bin");
ASSERT_FALSE(blob.empty());
ASSERT_TRUE(std::filesystem::exists(fake_path));
const auto* header = reinterpret_cast<const amd::smi::AMDGpuMetricsHeader_v1_t*>(blob.data());
const auto flag = amd::smi::translate_header_to_flag_version(*header, is_partition_metrics,
fake_path.string());
EXPECT_EQ(flag, GetExpectedMetricVersionFlag(1, ver, is_partition_metrics))
<< "Version 1." << ver << " should be treated as supported";
auto xcp_metrics_ptr =
amd::smi::amdgpu_metrics_factory(flag, is_partition_metrics, fake_path.string());
EXPECT_NE(xcp_metrics_ptr, nullptr)
<< "Factory must create metrics object for supported version";
if (xcp_metrics_ptr) {
std::cout << test_detail << "Created valid object for version 1." << ver << std::endl;
} else {
std::cout << test_detail << "Failed to create valid object for version 1." << ver
<< std::endl;
}
std::filesystem::remove(fake_path);
}
}