From 4e0a7f2f67695d1ca44bc9361dfdf5d80dc832ea Mon Sep 17 00:00:00 2001 From: "Bill(Shuzhou) Liu" Date: Fri, 9 Feb 2024 09:22:51 -0600 Subject: [PATCH] Support set min or max clock In addition to be able to set clock range, new setextremum option is added to set only min/max clock as sometimes one of them may not be supported. Change-Id: I7c91ba308f3fc6c78efc88117509c515d403a6cb --- include/rocm_smi/rocm_smi.h | 25 +++++++++++++++++ python_smi_tools/rocm_smi.py | 50 +++++++++++++++++++++++++++++++++- src/rocm_smi.cc | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) diff --git a/include/rocm_smi/rocm_smi.h b/include/rocm_smi/rocm_smi.h index da3f75add1..28f0bd795b 100755 --- a/include/rocm_smi/rocm_smi.h +++ b/include/rocm_smi/rocm_smi.h @@ -2761,6 +2761,31 @@ rsmi_status_t rsmi_dev_clk_range_set(uint32_t dv_ind, uint64_t minclkvalue, uint64_t maxclkvalue, rsmi_clk_type_t clkType); +/** + * @brief This function sets the clock min/max level + * + * @details Given a device index @p dv_ind, a clock value @p minclkvalue, + * a maximum clock value @p maxclkvalue and a clock type @p clkType this function + * will set the sclk|mclk range + * + * @param[in] dv_ind a device index + * + * @param[in] level RSMI_FREQ_IND_MIN|RSMI_FREQ_IND_MAX + * + * @param[in] clkvalue value to apply to the clock level. Frequency values + * are in MHz. + * + * @param[in] clkType RSMI_CLK_TYPE_SYS | RSMI_CLK_TYPE_MEM level type + * + * @retval ::RSMI_STATUS_SUCCESS call was successful + * @retval ::RSMI_STATUS_NOT_SUPPORTED installed software or hardware does not + * support this function with the given arguments + * @retval ::RSMI_STATUS_INVALID_ARGS the provided arguments are not valid + */ +rsmi_status_t rsmi_dev_clk_extremum_set(uint32_t dv_ind, rsmi_freq_ind_t level, + uint64_t clkvalue, + rsmi_clk_type_t clkType); + /** * @brief This function sets the clock frequency information * diff --git a/python_smi_tools/rocm_smi.py b/python_smi_tools/rocm_smi.py index d65fb4a3a2..e910b0bc30 100755 --- a/python_smi_tools/rocm_smi.py +++ b/python_smi_tools/rocm_smi.py @@ -1057,6 +1057,51 @@ def setClockRange(deviceList, clkType, minvalue, maxvalue, autoRespond): if ret == rsmi_status_t.RSMI_STATUS_NOT_SUPPORTED: printLog(device, 'Setting %s range is not supported for this device.' % (clkType), None) +def setClockExtremum(deviceList, level, clkType, clkValue, autoRespond): + """ Set the range for the specified clktype in the PowerPlay table for a list of devices. + + Parameters: + deviceList -- List of DRM devices (can be a single-item list) + level -- [min|max] Minimum value or Maximum value + clktype -- [sclk|mclk] Which clock type to apply the range to + clkValue -- clock value to apply to the level + autoRespond -- Response to automatically provide for all prompts + """ + global RETCODE + if level not in {'min', 'max'}: + printLog(None, 'Invalid extremum identifier %s, use min or max' % (level), None) + logging.error('Unsupported clock extremum %s', level) + RETCODE = 1 + return + + if clkType not in {'sclk', 'mclk'}: + printLog(None, 'Invalid clock type identifier %s, use sclk or mclk ' % (clkType), None) + logging.error('Unsupported clock type %s', clkType) + RETCODE = 1 + return + + point = 0 + if level == "max": + point = 1 + try: + int(clkValue) + except ValueError: + printErrLog(None, 'Unable to set %s' % (clkValue)) + logging.error('%s is not an integer', clkValue) + RETCODE = 1 + return + confirmOutOfSpecWarning(autoRespond) + printLogSpacer(' Set Valid %s Extremum ' % (clkType)) + for device in deviceList: + ret = rocmsmi.rsmi_dev_clk_extremum_set(device, rsmi_freq_ind_t(int(point)), int(clkValue), rsmi_clk_names_dict[clkType]) + if rsmi_ret_ok(ret, device, silent=True): + printLog(device, 'Successfully set %s %s to %s(MHz)' % (level, clkType, clkValue), None) + else: + printErrLog(device, 'Unable to set %s %s to %s(MHz)' % (level, clkType, clkValue)) + RETCODE = 1 + if ret == rsmi_status_t.RSMI_STATUS_NOT_SUPPORTED: + printLog(device, 'Setting %s %s clock is not supported for this device.' % (level, clkType), None) + def setVoltageCurve(deviceList, point, clk, volt, autoRespond): """ Set voltage curve for a point in the PowerPlay table for a list of devices. @@ -3786,6 +3831,7 @@ if __name__ == '__main__': groupAction.add_argument('--setvc', help='Change SCLK Voltage Curve (MHz mV) for a specific point', metavar=('POINT', 'SCLK', 'SVOLT'), nargs=3) groupAction.add_argument('--setsrange', help='Set min and max SCLK speed', metavar=('SCLKMIN', 'SCLKMAX'), nargs=2) + groupAction.add_argument('--setextremum', help='Set min/max of SCLK/MCLK speed', metavar=('min|max', "sclk|mclk", 'CLK'), nargs=3) groupAction.add_argument('--setmrange', help='Set min and max MCLK speed', metavar=('MCLKMIN', 'MCLKMAX'), nargs=2) groupAction.add_argument('--setfan', help='Set GPU Fan Speed (Level or %%)', metavar='LEVEL') groupAction.add_argument('--setperflevel', help='Set Performance Level', metavar='LEVEL') @@ -3855,7 +3901,7 @@ if __name__ == '__main__': or args.resetclocks or args.setprofile or args.resetprofile or args.setoverdrive or args.setmemoverdrive \ or args.setpoweroverdrive or args.resetpoweroverdrive or args.rasenable or args.rasdisable or \ args.rasinject or args.gpureset or args.setperfdeterminism or args.setslevel or args.setmlevel or \ - args.setvc or args.setsrange or args.setmrange or args.setclock or \ + args.setvc or args.setsrange or args.setextremum or args.setmrange or args.setclock or \ args.setcomputepartition or args.setmemorypartition or args.resetcomputepartition or args.resetmemorypartition: relaunchAsSudo() @@ -4082,6 +4128,8 @@ if __name__ == '__main__': setProfile(deviceList, args.setprofile) if args.setvc: setVoltageCurve(deviceList, args.setvc[0], args.setvc[1], args.setvc[2], args.autorespond) + if args.setextremum: + setClockExtremum(deviceList, args.setextremum[0], args.setextremum[1], args.setextremum[2], args.autorespond) if args.setsrange: setClockRange(deviceList, 'sclk', args.setsrange[0], args.setsrange[1], args.autorespond) if args.setmrange: diff --git a/src/rocm_smi.cc b/src/rocm_smi.cc index 29a535c509..200466947c 100755 --- a/src/rocm_smi.cc +++ b/src/rocm_smi.cc @@ -1377,6 +1377,58 @@ static rsmi_status_t get_od_clk_volt_info(uint32_t dv_ind, CATCH } +rsmi_status_t rsmi_dev_clk_extremum_set(uint32_t dv_ind, rsmi_freq_ind_t level, + uint64_t clkvalue, + rsmi_clk_type_t clkType) { + TRY + rsmi_status_t ret; + std::ostringstream ss; + ss << __PRETTY_FUNCTION__ << "| ======= start ======="; + LOG_TRACE(ss); + + if (clkType != RSMI_CLK_TYPE_SYS && clkType != RSMI_CLK_TYPE_MEM) { + return RSMI_STATUS_INVALID_ARGS; + } + if (level != RSMI_FREQ_IND_MIN && level != RSMI_FREQ_IND_MAX) { + return RSMI_STATUS_INVALID_ARGS; + } + + std::map clk_char_map = { + {RSMI_CLK_TYPE_SYS, "s"}, + {RSMI_CLK_TYPE_MEM, "m"}, + }; + DEVICE_MUTEX + + // Set perf. level to manual so that we can then set the power profile + ret = rsmi_dev_perf_level_set_v1(dv_ind, RSMI_DEV_PERF_LEVEL_MANUAL); + if (ret != RSMI_STATUS_SUCCESS) { + return ret; + } + + // For clock frequency setting, enter a new value by writing a string that + // contains "s/m index clock" to the file. The index should be 0 if to set + // minimum clock. And 1 if to set maximum clock. E.g., "s 0 500" will update + // minimum sclk to be 500 MHz. "m 1 800" will update maximum mclk to 800Mhz. + + std::string sysvalue = clk_char_map[clkType]; + sysvalue += ' ' + std::to_string(level); + sysvalue += ' ' + std::to_string(clkvalue); + sysvalue += '\n'; + + ret = set_dev_range(dv_ind, sysvalue); + if (ret != RSMI_STATUS_SUCCESS) { + return ret; + } + + ret = set_dev_range(dv_ind, "c"); + if (ret != RSMI_STATUS_SUCCESS) { + return ret; + } + + return RSMI_STATUS_SUCCESS; + CATCH +} + rsmi_status_t rsmi_dev_clk_range_set(uint32_t dv_ind, uint64_t minclkvalue, uint64_t maxclkvalue, rsmi_clk_type_t clkType) {