From b1ec78b54b7db492a10a7ebe782c1e9b920a802f Mon Sep 17 00:00:00 2001 From: "Galantsev, Dmitrii" Date: Thu, 10 Apr 2025 20:39:06 +0000 Subject: [PATCH] Add amdsmi_get_gpu_busy_percent This is required for GPU busy percent in RDC Change-Id: Idf2ab72993ecc8227958e6eb47f36fc68c93759f Signed-off-by: Galantsev, Dmitrii [ROCm/amdsmi commit: 955ceac78a9c0864da87cdbfda27084914612e03] --- projects/amdsmi/CMakeLists.txt | 2 +- projects/amdsmi/include/amd_smi/amdsmi.h | 19 +++++++ .../amdsmi/py-interface/amdsmi_wrapper.py | 7 ++- projects/amdsmi/rust-interface/src/amdsmi.rs | 51 +++++++++++++++++++ .../rust-interface/src/amdsmi_wrapper.rs | 6 +++ projects/amdsmi/src/amd_smi/amd_smi.cc | 5 ++ 6 files changed, 87 insertions(+), 3 deletions(-) diff --git a/projects/amdsmi/CMakeLists.txt b/projects/amdsmi/CMakeLists.txt index 5efad00a9d..c3d132c848 100644 --- a/projects/amdsmi/CMakeLists.txt +++ b/projects/amdsmi/CMakeLists.txt @@ -28,7 +28,7 @@ find_program(GIT NAMES git) ## Setup the package version based on git tags. set(PKG_VERSION_GIT_TAG_PREFIX "amdsmi_pkg_ver") -get_package_version_number("25.4.0" ${PKG_VERSION_GIT_TAG_PREFIX} GIT) +get_package_version_number("25.4.2" ${PKG_VERSION_GIT_TAG_PREFIX} GIT) message("Package version: ${PKG_VERSION_STR}") set(${AMD_SMI_LIBS_TARGET}_VERSION_MAJOR "${CPACK_PACKAGE_VERSION_MAJOR}") set(${AMD_SMI_LIBS_TARGET}_VERSION_MINOR "${CPACK_PACKAGE_VERSION_MINOR}") diff --git a/projects/amdsmi/include/amd_smi/amdsmi.h b/projects/amdsmi/include/amd_smi/amdsmi.h index cef79c2d6e..1862435180 100644 --- a/projects/amdsmi/include/amd_smi/amdsmi.h +++ b/projects/amdsmi/include/amd_smi/amdsmi.h @@ -3737,6 +3737,25 @@ amdsmi_status_t amdsmi_set_gpu_fan_speed(amdsmi_processor_handle processor_handl * @{ */ +/** + * @brief Get GPU busy percent from gpu_busy_percent sysfs file + * + * @ingroup tagClkPowerPerfQuery + * + * @platform{gpu_bm_linux} + * + * @details Given a processor handle @p processor_handle, this function returns GPU busy + * percentage. + * + * @param[in] processor_handle a processor handle + * + * @param[in,out] gpu_busy_percent Direct output from the gpu_busy_percent sysfs file + * + * @return ::amdsmi_status_t | ::AMDSMI_STATUS_SUCCESS on success, non-zero on fail + */ +amdsmi_status_t amdsmi_get_gpu_busy_percent(amdsmi_processor_handle processor_handle, + uint32_t *gpu_busy_percent); + /** * @brief Get coarse grain utilization counter of the specified device * diff --git a/projects/amdsmi/py-interface/amdsmi_wrapper.py b/projects/amdsmi/py-interface/amdsmi_wrapper.py index fd1acf4125..df4b23e7a9 100644 --- a/projects/amdsmi/py-interface/amdsmi_wrapper.py +++ b/projects/amdsmi/py-interface/amdsmi_wrapper.py @@ -2467,6 +2467,9 @@ amdsmi_reset_gpu_fan.argtypes = [amdsmi_processor_handle, uint32_t] amdsmi_set_gpu_fan_speed = _libraries['libamd_smi.so'].amdsmi_set_gpu_fan_speed amdsmi_set_gpu_fan_speed.restype = amdsmi_status_t amdsmi_set_gpu_fan_speed.argtypes = [amdsmi_processor_handle, uint32_t, uint64_t] +amdsmi_get_gpu_busy_percent = _libraries['libamd_smi.so'].amdsmi_get_gpu_busy_percent +amdsmi_get_gpu_busy_percent.restype = amdsmi_status_t +amdsmi_get_gpu_busy_percent.argtypes = [amdsmi_processor_handle, ctypes.POINTER(ctypes.c_uint32)] amdsmi_get_utilization_count = _libraries['libamd_smi.so'].amdsmi_get_utilization_count amdsmi_get_utilization_count.restype = amdsmi_status_t amdsmi_get_utilization_count.argtypes = [amdsmi_processor_handle, ctypes.POINTER(struct_amdsmi_utilization_counter_t), uint32_t, ctypes.POINTER(ctypes.c_uint64)] @@ -3212,8 +3215,8 @@ __all__ = \ 'amdsmi_get_gpu_available_counters', 'amdsmi_get_gpu_bad_page_info', 'amdsmi_get_gpu_bad_page_threshold', 'amdsmi_get_gpu_bdf_id', - 'amdsmi_get_gpu_board_info', 'amdsmi_get_gpu_cache_info', - 'amdsmi_get_gpu_compute_partition', + 'amdsmi_get_gpu_board_info', 'amdsmi_get_gpu_busy_percent', + 'amdsmi_get_gpu_cache_info', 'amdsmi_get_gpu_compute_partition', 'amdsmi_get_gpu_compute_process_gpus', 'amdsmi_get_gpu_compute_process_info', 'amdsmi_get_gpu_compute_process_info_by_pid', diff --git a/projects/amdsmi/rust-interface/src/amdsmi.rs b/projects/amdsmi/rust-interface/src/amdsmi.rs index 13df4ded8e..a60f47de3b 100644 --- a/projects/amdsmi/rust-interface/src/amdsmi.rs +++ b/projects/amdsmi/rust-interface/src/amdsmi.rs @@ -1940,6 +1940,57 @@ pub fn amdsmi_set_gpu_fan_speed( Ok(()) } +/// Get the GPU busy percent of the device with the specified processor handle. +/// +/// Given a processor handle `processor_handle`, this function returns the GPU busy percent +/// for the specified processor. +/// +/// # Arguments +/// +/// * `processor_handle` - A handle to the processor which is being queried. +/// +/// # Returns +/// +/// * `AmdsmiResult` - Returns `Ok(u32)` containing the GPU busy percent if successful, or an error if it fails. +/// +/// # Example +/// +/// ```rust +/// # use amdsmi::*; +/// # +/// # fn main() { +/// # // Initialize the AMD SMI library +/// # amdsmi_init(AmdsmiInitFlagsT::AmdsmiInitAmdGpus).expect("Failed to initialize AMD SMI"); +/// # +/// // Example processor_handle, assuming the number of processors is greater than zero +/// let processor_handle = amdsmi_get_processor_handles!()[0]; +/// +/// // Retrieve the GPU busy percent +/// match amdsmi_get_gpu_busy_percent(processor_handle) { +/// Ok(gpu_busy_percent) => println!("GPU Busy Percent: {}", gpu_busy_percent), +/// Err(AmdsmiStatusT::AmdsmiStatusNotSupported) => println!("amdsmi_get_gpu_busy_percent() not supported on this device"), +/// Err(e) => panic!("Failed to get GPU busy_percent level: {}", e), +/// } +/// # +/// # // Shut down the AMD SMI library +/// # amdsmi_shut_down().expect("Failed to shut down AMD SMI"); +/// # } +/// ``` +/// +/// # Errors +/// +/// This function will return the error in [`AmdsmiStatusT`] if the underlying `amdsmi_wrapper::amdsmi_get_gpu_busy_percent` call fails. +pub fn amdsmi_get_gpu_busy_percent( + processor_handle: AmdsmiProcessorHandle +) -> AmdsmiResult<(u32)> { + let mut gpu_busy_percent = 0; + call_unsafe!(amdsmi_wrapper::amdsmi_get_gpu_busy_percent( + processor_handle, + &mut gpu_busy_percent + )); + Ok(gpu_busy_percent) +} + /// /// Retrieves the utilization count for the specified processor handle and counter types. /// diff --git a/projects/amdsmi/rust-interface/src/amdsmi_wrapper.rs b/projects/amdsmi/rust-interface/src/amdsmi_wrapper.rs index 4522e3970c..5987f455b6 100644 --- a/projects/amdsmi/rust-interface/src/amdsmi_wrapper.rs +++ b/projects/amdsmi/rust-interface/src/amdsmi_wrapper.rs @@ -2556,6 +2556,12 @@ extern "C" { speed: u64, ) -> AmdsmiStatusT; } +extern "C" { + pub fn amdsmi_get_gpu_busy_percent( + processor_handle: AmdsmiProcessorHandle, + gpu_busy_percent: *mut u32, + ) -> AmdsmiStatusT; +} extern "C" { pub fn amdsmi_get_utilization_count( processor_handle: AmdsmiProcessorHandle, diff --git a/projects/amdsmi/src/amd_smi/amd_smi.cc b/projects/amdsmi/src/amd_smi/amd_smi.cc index ac50b11f31..9a92966cb7 100644 --- a/projects/amdsmi/src/amd_smi/amd_smi.cc +++ b/projects/amdsmi/src/amd_smi/amd_smi.cc @@ -3213,6 +3213,11 @@ amdsmi_status_t amdsmi_reset_gpu(amdsmi_processor_handle processor_handle) { return rsmi_wrapper(rsmi_dev_gpu_reset, processor_handle, 0); } +amdsmi_status_t amdsmi_get_gpu_busy_percent(amdsmi_processor_handle processor_handle, + uint32_t *gpu_busy_percent) { + return rsmi_wrapper(rsmi_dev_busy_percent_get, processor_handle, 0, gpu_busy_percent); +} + amdsmi_status_t amdsmi_get_utilization_count(amdsmi_processor_handle processor_handle, amdsmi_utilization_counter_t utilization_counters[], uint32_t count,