From 315b1abaf9c8aadda69920f5d2cb964f487cabf4 Mon Sep 17 00:00:00 2001 From: Sunday Clement Date: Thu, 19 Jun 2025 15:32:35 -0400 Subject: [PATCH] rocr: Add hsa-agent Queries for Clock Counters Support has been added to query the following HSA_AMD_INFO_GET_CLOCK_COUNTERS agent info exposed through the hsa api in rocr, rather than the user having to make a direct IOCTL call through the kernel driver. Signed-off-by: Sunday Clement [ROCm/ROCR-Runtime commit: e97d06530e822940e9db38a17dfc311a62646346] --- .../core/runtime/amd_aie_agent.cpp | 3 +++ .../core/runtime/amd_cpu_agent.cpp | 3 +++ .../core/runtime/amd_gpu_agent.cpp | 13 ++++++++++++ .../runtime/hsa-runtime/inc/hsa_ext_amd.h | 20 +++++++++++++++++-- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_aie_agent.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_aie_agent.cpp index 1bb319abd8..07159453a5 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_aie_agent.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_aie_agent.cpp @@ -253,6 +253,9 @@ hsa_status_t AieAgent::GetInfo(hsa_agent_info_t attribute, void *value) const { case HSA_AMD_AGENT_INFO_MEMORY_PROPERTIES: std::memset(value, 0, sizeof(uint8_t) * 8); break; + case HSA_AMD_AGENT_INFO_CLOCK_COUNTERS: + std::memset(value, 0, sizeof(hsa_amd_clock_counters_t)); + break; default: *reinterpret_cast(value) = 0; return HSA_STATUS_ERROR_INVALID_ARGUMENT; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp index 369aace289..5cd3cb8a4f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp @@ -417,6 +417,9 @@ hsa_status_t CpuAgent::GetInfo(hsa_agent_info_t attribute, void* value) const { case HSA_AMD_AGENT_INFO_SCRATCH_LIMIT_CURRENT: *((uint64_t*)value) = 0; break; + case HSA_AMD_AGENT_INFO_CLOCK_COUNTERS: + memset(value, 0, sizeof(hsa_amd_clock_counters_t)); + break; default: return HSA_STATUS_ERROR_INVALID_ARGUMENT; break; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp index 27ae3ccba6..ec7c9180d5 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp @@ -1674,6 +1674,19 @@ hsa_status_t GpuAgent::GetInfo(hsa_agent_info_t attribute, void* value) const { case HSA_AMD_AGENT_INFO_SCRATCH_LIMIT_CURRENT: *((uint64_t*)value) = scratch_limit_async_threshold_; break; + case HSA_AMD_AGENT_INFO_CLOCK_COUNTERS: { + HsaClockCounters hsakmt_counters = {}; + hsa_amd_clock_counters_t* counters = static_cast(value); + + if (hsaKmtGetClockCounters(node_id(), &hsakmt_counters) == HSAKMT_STATUS_SUCCESS ) { + counters->cpu_clock_counter = hsakmt_counters.CPUClockCounter; + counters->gpu_clock_counter = hsakmt_counters.GPUClockCounter; + counters->system_clock_counter = hsakmt_counters.SystemClockCounter; + counters->system_clock_frequency = hsakmt_counters.SystemClockFrequencyHz; + break; + } + return HSA_STATUS_ERROR; + } default: return HSA_STATUS_ERROR_INVALID_ARGUMENT; break; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h index 9c5cbb2bc1..08c8dededa 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h @@ -61,9 +61,10 @@ * - 1.8 - hsa_amd_memory_get_preferred_copy_engine * - 1.9 - hsa_amd_portable_export_dmabuf_v2 * - 1.10 - hsa_amd_vmem_address_reserve: HSA_AMD_VMEM_ADDRESS_NO_REGISTER + * - 1.11 - hsa_amd_agent_info_t: HSA_AMD_AGENT_INFO_CLOCK_COUNTERS */ #define HSA_AMD_INTERFACE_VERSION_MAJOR 1 -#define HSA_AMD_INTERFACE_VERSION_MINOR 10 +#define HSA_AMD_INTERFACE_VERSION_MINOR 11 #ifdef __cplusplus extern "C" { @@ -476,6 +477,16 @@ typedef enum { HSA_IOMMU_SUPPORT_V2 = 1, } hsa_amd_iommu_version_t; +/** + * @brief Structure containing information on the agent's clock counters. + */ +typedef struct hsa_amd_clock_counters_s { + uint64_t gpu_clock_counter; + uint64_t cpu_clock_counter; + uint64_t system_clock_counter; + uint64_t system_clock_frequency; +} hsa_amd_clock_counters_t; + /** * @brief Agent attributes. */ @@ -685,7 +696,12 @@ typedef enum hsa_amd_agent_info_s { * * The type of this attribute is uint64_t. */ - HSA_AMD_AGENT_INFO_SCRATCH_LIMIT_CURRENT = 0xA117 + HSA_AMD_AGENT_INFO_SCRATCH_LIMIT_CURRENT = 0xA117, + /** + * Queries the driver for clock counters of the agent. + * The type of this attribute is hsa_amd_clock_counters_t. + */ + HSA_AMD_AGENT_INFO_CLOCK_COUNTERS = 0xA118 } hsa_amd_agent_info_t; /**