From 861c2c2e331420528ac541da5d8dbba6b481f160 Mon Sep 17 00:00:00 2001 From: Chris Freehill Date: Mon, 12 Nov 2018 17:25:14 -0600 Subject: [PATCH] Add rsmi_dev_busy_percent_get() Also: correct some comments, ifdef out unused code --- include/rocm_smi/rocm_smi.h | 36 ++++++++++++++----- include/rocm_smi/rocm_smi_device.h | 6 +++- src/rocm_smi.cc | 22 +++++++++++- src/rocm_smi_device.cc | 14 +++++--- tests/rocm_smi_test/functional/rsmi_sanity.cc | 17 +++++++++ tests/rocm_smi_test/main.cc | 20 +---------- 6 files changed, 81 insertions(+), 34 deletions(-) diff --git a/include/rocm_smi/rocm_smi.h b/include/rocm_smi/rocm_smi.h index 94f8921da3..28dcb053a6 100755 --- a/include/rocm_smi/rocm_smi.h +++ b/include/rocm_smi/rocm_smi.h @@ -277,10 +277,10 @@ rsmi_status_t rsmi_num_monitor_devices(uint32_t *num_devices); /** * @brief Get the unique PCI device identifier associated for a device * - * @details Give a device index @p dev_ind and a pointer to a uint64_t @p + * @details Give a device index @p dv_ind and a pointer to a uint64_t @p * bdfid, this function will write the Bus/Device/Function PCI identifier - * (BDFID) associated with device @p dev_ind to the value pointed to by - * @bdfid. + * (BDFID) associated with device @p dv_ind to the value pointed to by + * @p bdfid. * * @param[in] dv_ind a device index * @@ -290,7 +290,7 @@ rsmi_status_t rsmi_num_monitor_devices(uint32_t *num_devices); * @retval RSMI_STATUS_SUCCESS is returned upon successful call. */ -rsmi_status_t rsmi_dev_pci_id_get(uint32_t dev_ind, uint64_t *bdfid); +rsmi_status_t rsmi_dev_pci_id_get(uint32_t dv_ind, uint64_t *bdfid); /** * @brief Get the device id associated with the device with provided device @@ -739,8 +739,8 @@ rsmi_dev_power_max_get(uint32_t dv_ind, uint32_t sensor_ind, uint64_t *power); * * @param[in] dv_ind a device index * - * @param[in] sensor_ind a 0-based sensor index. Normally, this will be 0. - * If a device has more than one sensor, it could be greater than 0. + * @param[in] sensor_ind a 0-based sensor index. Normally, this will be 0. + * If a device has more than one sensor, it could be greater than 0. * * @param[inout] status a pointer to rsmi_power_profile_status that will be * populated by a call to this function @@ -769,7 +769,7 @@ rsmi_dev_power_profile_presets_get(uint32_t dv_ind, uint32_t sensor_ind, * @param[in] profile a rsmi_power_profile_preset_masks that hold the mask * of the desired new power profile * - * @retval RSMI_STATUS_SUCCESS is returned upon successful call. + * @retval RSMI_STATUS_SUCCESS is returned upon successful call. * */ rsmi_status_t @@ -786,11 +786,31 @@ rsmi_dev_power_profile_set(uint32_t dv_ind, uint32_t sensor_ind, * @param[inout] status_string A pointer to a const char * which will be made * to point to a description of the provided error code * - * @retval RSMI_STATUS_SUCCESS is returned upon successful call + * @retval RSMI_STATUS_SUCCESS is returned upon successful call * */ rsmi_status_t rsmi_status_string(rsmi_status_t status, const char **status_string); + +/** + * @brief Get percentage of time device is busy doing any processing + * + * @details Given a device index @p dv_ind, this function returns the + * percentage of time that the specified device is busy. The device is + * considered busy if any one or more of its sub-blocks are working, and idle + * if none of the sub-blocks are working. + * + * @param[in] dv_ind a device index + * + * @param[inout] busy_percent a pointer to the uint32_t to which the busy + * percent will be written + * + * @retval RSMI_STATUS_SUCCESS is returned upon successful call + * + */ +rsmi_status_t +rsmi_dev_busy_percent_get(uint32_t dv_ind, uint32_t *busy_percent); + #ifdef __cplusplus } #endif // __cplusplus diff --git a/include/rocm_smi/rocm_smi_device.h b/include/rocm_smi/rocm_smi_device.h index 8a3c6448d4..b306b54606 100755 --- a/include/rocm_smi/rocm_smi_device.h +++ b/include/rocm_smi/rocm_smi_device.h @@ -64,7 +64,8 @@ enum DevInfoTypes { kDevDevID, kDevGPUMClk, kDevGPUSClk, - kDevPowerProfileMode + kDevPowerProfileMode, + kDevUsage, }; class Device { @@ -78,7 +79,10 @@ class Device { const std::shared_ptr& power_monitor() {return power_monitor_;} void set_power_monitor(std::shared_ptr pm) {power_monitor_ = pm;} +#if 0 // This is not being used right now. int readDevInfo(DevInfoTypes type, uint32_t *val); +#endif + int readDevInfo(DevInfoTypes type, std::string *val); int readDevInfo(DevInfoTypes type, std::vector *retVec); int writeDevInfo(DevInfoTypes type, uint64_t val); diff --git a/src/rocm_smi.cc b/src/rocm_smi.cc index 249a932ca8..6d7e7ff658 100755 --- a/src/rocm_smi.cc +++ b/src/rocm_smi.cc @@ -102,7 +102,8 @@ static rsmi_status_t errno_to_rsmi_status(uint32_t err) { case 0: return RSMI_STATUS_SUCCESS; case EACCES: return RSMI_STATUS_PERMISSION; case EPERM: return RSMI_STATUS_NOT_SUPPORTED; - case ENOENT: return RSMI_STATUS_FILE_ERROR; + case ENOENT: + case EISDIR: return RSMI_STATUS_FILE_ERROR; default: return RSMI_STATUS_UNKNOWN_ERROR; } } @@ -1026,3 +1027,22 @@ rsmi_status_string(rsmi_status_t status, const char **status_string) { return RSMI_STATUS_SUCCESS; CATCH } + +rsmi_status_t +rsmi_dev_busy_percent_get(uint32_t dv_ind, uint32_t *busy_percent) { + TRY + std::string val_str; + rsmi_status_t ret = get_dev_value_str(amd::smi::kDevUsage, dv_ind, + &val_str); + if (ret != RSMI_STATUS_SUCCESS) { + return ret; + } + + errno = 0; + *busy_percent = strtoul(val_str.c_str(), nullptr, 10); + assert(errno == 0); + + return RSMI_STATUS_SUCCESS; + + CATCH +} diff --git a/src/rocm_smi_device.cc b/src/rocm_smi_device.cc index 1f1a63cdc9..3ec5382850 100755 --- a/src/rocm_smi_device.cc +++ b/src/rocm_smi_device.cc @@ -64,7 +64,9 @@ static const char *kDevDevIDFName = "device"; static const char *kDevOverDriveLevelFName = "pp_sclk_od"; static const char *kDevGPUSClkFName = "pp_dpm_sclk"; static const char *kDevGPUMClkFName = "pp_dpm_mclk"; -static const char *kDevPowerProfileModeName = "pp_power_profile_mode"; +static const char *kDevPowerProfileModeFName = "pp_power_profile_mode"; +static const char *kDevUsageFName = "gpu_busy_percent"; + static const char *kDevPerfLevelAutoStr = "auto"; static const char *kDevPerfLevelLowStr = "low"; static const char *kDevPerfLevelHighStr = "high"; @@ -81,7 +83,8 @@ static const std::map kDevAttribNameMap = { {kDevDevID, kDevDevIDFName}, {kDevGPUMClk, kDevGPUMClkFName}, {kDevGPUSClk, kDevGPUSClkFName}, - {kDevPowerProfileMode, kDevPowerProfileModeName}, + {kDevPowerProfileMode, kDevPowerProfileModeFName}, + {kDevUsage, kDevUsageFName}, }; static const std::map kDevPerfLvlMap = { @@ -114,7 +117,6 @@ Device::Device(std::string p, RocmSMI_env_vars const *e) : path_(p), env_(e) { Device:: ~Device() { } -// TODO(cfreehil): cache values that are constant int Device::readDevInfoStr(DevInfoTypes type, std::string *retStr) { auto tempPath = path_; @@ -242,12 +244,12 @@ int Device::readDevInfoMultiLineStr(DevInfoTypes type, return 0; } +#if 0 int Device::readDevInfo(DevInfoTypes type, uint32_t *val) { assert(val != nullptr); std::string tempStr; int ret; - switch (type) { case kDevDevID: ret = readDevInfoStr(type, &tempStr); @@ -255,6 +257,7 @@ int Device::readDevInfo(DevInfoTypes type, uint32_t *val) { *val = std::stoi(tempStr, 0, 16); break; + case kDevUsage: case kDevOverDriveLevel: ret = readDevInfoStr(type, &tempStr); RET_IF_NONZERO(ret); @@ -266,7 +269,7 @@ int Device::readDevInfo(DevInfoTypes type, uint32_t *val) { } return 0; } - +#endif int Device::readDevInfo(DevInfoTypes type, std::vector *val) { assert(val != nullptr); @@ -289,6 +292,7 @@ int Device::readDevInfo(DevInfoTypes type, std::string *val) { switch (type) { case kDevPerfLevel: + case kDevUsage: case kDevOverDriveLevel: case kDevDevID: return readDevInfoStr(type, val); diff --git a/tests/rocm_smi_test/functional/rsmi_sanity.cc b/tests/rocm_smi_test/functional/rsmi_sanity.cc index 3c167dd5e0..d75f9bd5ff 100755 --- a/tests/rocm_smi_test/functional/rsmi_sanity.cc +++ b/tests/rocm_smi_test/functional/rsmi_sanity.cc @@ -549,6 +549,23 @@ void TestSanity::Run(void) { std::cout << f.num_supported << std::endl; print_frequencies(&f); } + err = rsmi_dev_busy_percent_get(i, &val_ui32); + if (err != RSMI_STATUS_SUCCESS) { + if (err == RSMI_STATUS_FILE_ERROR) { + IF_VERB(STANDARD) { + std::cout << "\t**GPU Busy Percent: Not supported on this machine" + << std::endl; + } + } else { + CHK_ERR_ASRT(err) + } + } else { + IF_VERB(STANDARD) { + std::cout << "\t**GPU Busy Percent (Percent Idle):" << std::dec << + val_ui32 << " (" << 100 - val_ui32 << ")" << std::endl; + } + } + char name[20]; err = rsmi_dev_name_get(i, name, 20); CHK_ERR_ASRT(err) diff --git a/tests/rocm_smi_test/main.cc b/tests/rocm_smi_test/main.cc index d6b548cf9d..20f13ec56a 100755 --- a/tests/rocm_smi_test/main.cc +++ b/tests/rocm_smi_test/main.cc @@ -103,24 +103,6 @@ TEST(rsmitst, RSMISanityTest) { RunGenericTest(&tst); } -#if 0 - -TEST(rocrtstFunc, IPC) { - IPCTest ipc; - RunGenericTest(&ipc); -} - -TEST(rocrtstFunc, MemoryAccessTests) { - MemoryAccessTest mt; - RunCustomTestProlog(&mt); - mt.CPUAccessToGPUMemoryTest(); - mt.GPUAccessToCPUMemoryTest(); - RunCustomTestEpilog(&mt); -} - - - -#endif int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); @@ -130,7 +112,7 @@ int main(int argc, char** argv) { // Set some default values settings.verbosity = 1; settings.monitor_verbosity = 1; - settings.num_iterations = 5; + settings.num_iterations = 1; if (ProcessCmdline(&settings, argc, argv)) {