Set and get DPM policy for GPU device

Add new APIs to set and get dpm policy for the GPU device.

Change-Id: I26fa49cd17d0ce66bda3446c38945a6cf35717ff
This commit is contained in:
Bill(Shuzhou) Liu
2024-02-22 08:38:54 -06:00
committed by Shuzhou Liu
parent 2f8f34946e
commit 108e6d4ae6
15 changed files with 506 additions and 39 deletions
+128
View File
@@ -145,6 +145,7 @@ static uint64_t get_multiplier_from_str(char units_char) {
return multiplier;
}
/**
* Parse a string of the form:
* "<int index>: <int freq><freq. unit string> <|*>"
@@ -2014,6 +2015,133 @@ rsmi_dev_gpu_clk_freq_set(uint32_t dv_ind,
CATCH
}
rsmi_status_t
rsmi_dev_dpm_policy_set(uint32_t dv_ind,
uint32_t policy_id) {
rsmi_status_t ret;
TRY
std::ostringstream ss;
ss << __PRETTY_FUNCTION__ << " | ======= start =======";
LOG_TRACE(ss);
REQUIRE_ROOT_ACCESS
DEVICE_MUTEX
GET_DEV_FROM_INDX
std::string value("soc_pstate ");
value += std::to_string(policy_id);
int ret = dev->writeDevInfo(amd::smi::kDevDPMPolicy , value);
return amd::smi::ErrnoToRsmiStatus(ret);
CATCH
}
rsmi_status_t
rsmi_dev_dpm_policy_get(uint32_t dv_ind,
rsmi_dpm_policy_t* policy) {
rsmi_status_t ret;
std::vector<std::string> val_vec;
if (policy == nullptr) {
return RSMI_STATUS_INVALID_ARGS;
}
*policy = {};
TRY
std::ostringstream ss;
ss << __PRETTY_FUNCTION__ << " | ======= start =======";
LOG_TRACE(ss);
DEVICE_MUTEX
ret = GetDevValueVec(amd::smi::kDevDPMPolicy, dv_ind, &val_vec);
if (ret == RSMI_STATUS_FILE_ERROR) {
ss << __PRETTY_FUNCTION__ << " | ======= end ======="
<< ", GetDevValueVec() ret was RSMI_STATUS_FILE_ERROR "
<< "-> reporting RSMI_STATUS_NOT_SUPPORTED";
LOG_ERROR(ss);
return RSMI_STATUS_NOT_SUPPORTED;
}
if (ret != RSMI_STATUS_SUCCESS) {
ss << __PRETTY_FUNCTION__ << " | ======= end ======="
<< ", GetDevValueVec() ret was not RSMI_STATUS_SUCCESS"
<< " -> reporting " << amd::smi::getRSMIStatusString(ret);
LOG_ERROR(ss);
return ret;
}
/*
It will reply on the number but no string as it may vary from soc to soc.
The current pstate marked with *
soc pstate
0 : soc_pstate_default
1 : soc_pstate_0
2 : soc_pstate_1*
3 : soc_pstate_2
*/
bool see_soc_pstate = false;
bool see_current = false;
policy->num_supported = 0;
for (uint32_t i = 0; i < val_vec.size(); ++i) {
auto current_line = amd::smi::trim(val_vec[i]);
if (current_line == "soc pstate") {
see_soc_pstate = true;
continue;
}
if (see_soc_pstate == false) continue;
// Get tokens: <integer> : <string *>
std::vector<std::string> tokens;
std::istringstream f(current_line);
std::string s;
while (getline(f, s, ':')) {
tokens.push_back(s);
}
int value = 0;
// At the end
if (tokens.size() < 2 || !amd::smi::stringToInteger(tokens[0], value)) {
break;
}
if (value < 0 || policy->num_supported >= RSMI_MAX_NUM_PM_POLICIES) {
ss << __PRETTY_FUNCTION__ << " | ======= end ======="
<< ", Unexpeced pstat data: the id is negative or too many policies.";
LOG_ERROR(ss);
return RSMI_STATUS_UNEXPECTED_DATA;
}
policy->policies[policy->num_supported].policy_id = value;
std::string description = amd::smi::trim(tokens[1]);
if (current_line.back() == '*') { // current policy
description.pop_back(); // remove last *
description = amd::smi::trim(description);
policy->current = policy->num_supported;
see_current = true;
}
strncpy(policy->policies[policy->num_supported].policy_description,
description.c_str(),
RSMI_MAX_POLICY_NAME-1);
policy->num_supported++;
} // end for
if (!see_soc_pstate) {
return RSMI_STATUS_NOT_SUPPORTED;
}
if (!see_current) {
ss << __PRETTY_FUNCTION__ << " | ======= end ======="
<< ", Unexpeced pstat data: cannot find the current policy.";
LOG_ERROR(ss);
return RSMI_STATUS_UNEXPECTED_DATA;
}
// Cannot find it
return RSMI_STATUS_SUCCESS;
CATCH
}
static std::vector<std::string> pci_name_files = {
"/usr/share/misc/pci.ids",
"/usr/share/hwdata/pci.ids",
+7
View File
@@ -136,6 +136,7 @@ static const char *kDevAvailableComputePartitionFName =
"available_compute_partition";
static const char *kDevComputePartitionFName = "current_compute_partition";
static const char *kDevMemoryPartitionFName = "current_memory_partition";
static const char* kDevDPMPolicyFName = "pm_policy"; // The PM policy for pstat and XGMI
// Firmware version files
static const char *kDevFwVersionAsdFName = "fw_version/asd_fw_version";
@@ -315,6 +316,7 @@ static const std::map<DevInfoTypes, const char *> kDevAttribNameMap = {
{kDevNumaNode, kDevNumaNodeFName},
{kDevGpuMetrics, kDevGpuMetricsFName},
{kDevPmMetrics, kDevPmMetricsFName},
{kDevDPMPolicy, kDevDPMPolicyFName},
{kDevRegMetrics, kDevRegMetricsFName},
{kDevGpuReset, kDevGpuResetFName},
{kDevAvailableComputePartition, kDevAvailableComputePartitionFName},
@@ -472,6 +474,7 @@ Device::devInfoTypesStrings = {
{kDevComputePartition, "kDevComputePartition"},
{kDevMemoryPartition, "kDevMemoryPartition"},
{kDevPCieVendorID, "kDevPCieVendorID"},
{kDevDPMPolicy, "kDevDPMPolicy"},
};
static const std::map<const char *, dev_depends_t> kDevFuncDependsMap = {
@@ -533,6 +536,8 @@ static const std::map<const char *, dev_depends_t> kDevFuncDependsMap = {
{"rsmi_topo_numa_affinity_get", {{kDevNumaNodeFName}, {}}},
{"rsmi_dev_gpu_metrics_info_get", {{kDevGpuMetricsFName}, {}}},
{"rsmi_dev_pm_metrics_info_get", {{kDevPmMetricsFName}, {}}},
{"rsmi_dev_dpm_policy_get", {{kDevDPMPolicyFName}, {}}},
{"rsmi_dev_dpm_policy_set", {{kDevDPMPolicyFName}, {}}},
{"rsmi_dev_reg_table_info_get", {{kDevRegMetricsFName}, {}}},
{"rsmi_dev_gpu_reset", {{kDevGpuResetFName}, {}}},
{"rsmi_dev_compute_partition_get", {{kDevComputePartitionFName}, {}}},
@@ -938,6 +943,7 @@ int Device::writeDevInfo(DevInfoTypes type, std::string val) {
case kDevPCIEClk:
case kDevPowerODVoltage:
case kDevSOCClk:
case kDevDPMPolicy:
return writeDevInfoStr(type, val);
case kDevComputePartition:
case kDevMemoryPartition:
@@ -1219,6 +1225,7 @@ int Device::readDevInfo(DevInfoTypes type, std::vector<std::string> *val) {
case kDevErrCntHDP:
case kDevErrCntXGMIWAFL:
case kDevMemPageBad:
case kDevDPMPolicy:
return readDevInfoMultiLineStr(type, val);
break;
+10
View File
@@ -257,6 +257,16 @@ bool IsInteger(const std::string & n_str) {
return (*tmp == 0);
}
bool stringToInteger(const std::string & n_str, int& value) {
try {
value = std::stoi(trim(n_str), nullptr);
return true;
} catch (...) {
return false;
}
return false;
}
rsmi_status_t handleException() {
try {
throw;