From dc4ba12e00f34fde001e66b4c33836c07d2db910 Mon Sep 17 00:00:00 2001 From: "Bill(Shuzhou) Liu" Date: Thu, 11 May 2023 10:42:22 -0500 Subject: [PATCH] Return NOT_SUPPORT for set function in VM guest Fix the unit tests which are fail in VM guest environment. Change-Id: Id7c58887692bbdecba54f5d2d8463b292e19b4ad --- rocm_smi/include/rocm_smi/rocm_smi_utils.h | 4 ++ rocm_smi/src/rocm_smi.cc | 48 +++++++++++++++++++ rocm_smi/src/rocm_smi_utils.cc | 19 ++++++++ .../amd_smi_test/functional/overdrive_read.cc | 6 +++ .../functional/overdrive_read_write.cc | 6 +++ .../functional/perf_level_read_write.cc | 6 +++ .../functional/power_cap_read_write.cc | 6 +++ 7 files changed, 95 insertions(+) diff --git a/rocm_smi/include/rocm_smi/rocm_smi_utils.h b/rocm_smi/include/rocm_smi/rocm_smi_utils.h index a8f4cfa848..d40b4e5404 100755 --- a/rocm_smi/include/rocm_smi/rocm_smi_utils.h +++ b/rocm_smi/include/rocm_smi/rocm_smi_utils.h @@ -198,6 +198,10 @@ class ScopedAcquire { DISALLOW_COPY_AND_ASSIGN(ScopedAcquire); }; +// The best effort way to decide whether it is in VM guest environment: +// In VM environment, the /proc/cpuinfo set hypervisor flag by default +bool is_vm_guest(); + } // namespace smi } // namespace amd diff --git a/rocm_smi/src/rocm_smi.cc b/rocm_smi/src/rocm_smi.cc index 12b8061a0c..75e92aff2e 100755 --- a/rocm_smi/src/rocm_smi.cc +++ b/rocm_smi/src/rocm_smi.cc @@ -850,6 +850,11 @@ rsmi_dev_overdrive_level_get(uint32_t dv_ind, uint32_t *od) { CHK_SUPPORT_NAME_ONLY(od) DEVICE_MUTEX + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + rsmi_status_t ret = get_dev_value_str(amd::smi::kDevOverDriveLevel, dv_ind, &val_str); if (ret != RSMI_STATUS_SUCCESS) { @@ -886,6 +891,12 @@ rsmi_dev_overdrive_level_set_v1(uint32_t dv_ind, uint32_t od) { if (od > kMaxOverdriveLevel) { return RSMI_STATUS_INVALID_ARGS; } + + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + DEVICE_MUTEX return set_dev_value(amd::smi::kDevOverDriveLevel, dv_ind, od); CATCH @@ -905,6 +916,11 @@ rsmi_dev_perf_level_set_v1(uint32_t dv_ind, rsmi_dev_perf_level_t perf_level) { return RSMI_STATUS_INVALID_ARGS; } + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + DEVICE_MUTEX return set_dev_value(amd::smi::kDevPerfLevel, dv_ind, perf_level); CATCH @@ -992,6 +1008,10 @@ static rsmi_status_t get_power_profiles(uint32_t dv_ind, } assert(val_vec.size() <= RSMI_MAX_NUM_POWER_PROFILES); if (val_vec.size() > RSMI_MAX_NUM_POWER_PROFILES + 1 || val_vec.size() < 1) { + // Guest may not have power related information. + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } return RSMI_STATUS_UNEXPECTED_SIZE; } // -1 for the header line, below @@ -1177,6 +1197,11 @@ rsmi_status_t rsmi_dev_clk_range_set(uint32_t dv_ind, uint64_t minclkvalue, return RSMI_STATUS_INVALID_ARGS; } + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + // Can only set the clock type for sys and mem type if (clkType != RSMI_CLK_TYPE_SYS && clkType != RSMI_CLK_TYPE_MEM) { return RSMI_STATUS_NOT_SUPPORTED; @@ -1601,6 +1626,11 @@ rsmi_dev_gpu_clk_freq_set(uint32_t dv_ind, return RSMI_STATUS_INVALID_ARGS; } + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + ret = rsmi_dev_gpu_clk_freq_get(dv_ind, clk_type, &freqs); if (ret != RSMI_STATUS_SUCCESS) { @@ -2058,6 +2088,10 @@ rsmi_dev_pci_bandwidth_set(uint32_t dv_ind, uint64_t bw_bitmask) { TRY REQUIRE_ROOT_ACCESS DEVICE_MUTEX + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } ret = rsmi_dev_pci_bandwidth_get(dv_ind, &bws); if (ret != RSMI_STATUS_SUCCESS) { @@ -2369,6 +2403,11 @@ rsmi_dev_fan_speed_set(uint32_t dv_ind, uint32_t sensor_ind, uint64_t speed) { REQUIRE_ROOT_ACCESS DEVICE_MUTEX + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + ret = rsmi_dev_fan_speed_max_get(dv_ind, sensor_ind, &max_speed); if (ret != RSMI_STATUS_SUCCESS) { @@ -2580,6 +2619,11 @@ rsmi_dev_power_cap_set(uint32_t dv_ind, uint32_t sensor_ind, uint64_t cap) { REQUIRE_ROOT_ACCESS DEVICE_MUTEX + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } + ret = rsmi_dev_power_cap_range_get(dv_ind, sensor_ind, &max, &min); if (ret != RSMI_STATUS_SUCCESS) { return ret; @@ -2622,6 +2666,10 @@ rsmi_dev_power_profile_set(uint32_t dv_ind, uint32_t dummy, (void)dummy; DEVICE_MUTEX + // Bare Metal only feature + if (amd::smi::is_vm_guest()) { + return RSMI_STATUS_NOT_SUPPORTED; + } rsmi_status_t ret = set_power_profile(dv_ind, profile); return ret; CATCH diff --git a/rocm_smi/src/rocm_smi_utils.cc b/rocm_smi/src/rocm_smi_utils.cc index 473d1e2b67..93ff80be74 100755 --- a/rocm_smi/src/rocm_smi_utils.cc +++ b/rocm_smi/src/rocm_smi_utils.cc @@ -234,5 +234,24 @@ rsmi_status_t ErrnoToRsmiStatus(int err) { } } +bool is_vm_guest() { + // the cpuinfo will set hypervisor flag in VM guest + const std::string hypervisor = "hypervisor"; + std::string line; + + // default to false if cannot find the file + std::ifstream infile("/proc/cpuinfo"); + if (infile.fail()) { + return false; + } + + while (std::getline(infile, line)) { + if (line.find(hypervisor) != std::string::npos) { + return true; + } + } + return false; +} + } // namespace smi } // namespace amd diff --git a/tests/amd_smi_test/functional/overdrive_read.cc b/tests/amd_smi_test/functional/overdrive_read.cc index ccc3df1eb7..4c609c50d4 100755 --- a/tests/amd_smi_test/functional/overdrive_read.cc +++ b/tests/amd_smi_test/functional/overdrive_read.cc @@ -99,6 +99,12 @@ void TestOverdriveRead::Run(void) { PrintDeviceHeader(processor_handles_[i]); err = amdsmi_get_gpu_overdrive_level(processor_handles_[i], &val_ui32); + if (err == AMDSMI_STATUS_NOT_SUPPORTED) { + IF_VERB(STANDARD) { + std::cout << "\t** Not supported on this machine" << std::endl; + } + continue; + } CHK_ERR_ASRT(err) IF_VERB(STANDARD) { std::cout << "\t**OverDrive Level:" << val_ui32 << std::endl; diff --git a/tests/amd_smi_test/functional/overdrive_read_write.cc b/tests/amd_smi_test/functional/overdrive_read_write.cc index a8f258061c..55662bae28 100755 --- a/tests/amd_smi_test/functional/overdrive_read_write.cc +++ b/tests/amd_smi_test/functional/overdrive_read_write.cc @@ -101,6 +101,12 @@ void TestOverdriveReadWrite::Run(void) { std::cout << "Set Overdrive level to 0%..." << std::endl; } ret = amdsmi_set_gpu_overdrive_level(processor_handles_[dv_ind], 0); + if (ret == AMDSMI_STATUS_NOT_SUPPORTED) { + IF_VERB(STANDARD) { + std::cout << "\t** Not supported on this machine" << std::endl; + } + continue; + } CHK_ERR_ASRT(ret) IF_VERB(STANDARD) { std::cout << "Set Overdrive level to 10%..." << std::endl; diff --git a/tests/amd_smi_test/functional/perf_level_read_write.cc b/tests/amd_smi_test/functional/perf_level_read_write.cc index 121e7b5a90..0e59053cb7 100755 --- a/tests/amd_smi_test/functional/perf_level_read_write.cc +++ b/tests/amd_smi_test/functional/perf_level_read_write.cc @@ -140,6 +140,12 @@ void TestPerfLevelReadWrite::Run(void) { " ..." << std::endl; } ret = amdsmi_set_gpu_perf_level(processor_handles_[dv_ind], orig_pfl); + if (ret == AMDSMI_STATUS_NOT_SUPPORTED) { + IF_VERB(STANDARD) { + std::cout << "\t** Not supported on this machine" << std::endl; + } + continue; + } CHK_ERR_ASRT(ret) ret = amdsmi_get_gpu_perf_level(processor_handles_[dv_ind], &pfl); CHK_ERR_ASRT(ret) diff --git a/tests/amd_smi_test/functional/power_cap_read_write.cc b/tests/amd_smi_test/functional/power_cap_read_write.cc index e0b05abe42..f6fd038ba4 100755 --- a/tests/amd_smi_test/functional/power_cap_read_write.cc +++ b/tests/amd_smi_test/functional/power_cap_read_write.cc @@ -125,6 +125,12 @@ void TestPowerCapReadWrite::Run(void) { end = clock(); cpu_time_used = ((double) (end - start)) * 1000000UL / CLOCKS_PER_SEC; + if (ret == AMDSMI_STATUS_NOT_SUPPORTED) { + IF_VERB(STANDARD) { + std::cout << "\t** Not supported on this machine" << std::endl; + } + continue; + } CHK_ERR_ASRT(ret) ret = amdsmi_get_power_cap_info(processor_handles_[dv_ind], 0, &info);