Add Prelim ULTs for power

Related-To: LOCI-3127

Signed-off-by: Bellekallu Rajkiran <bellekallu.rajkiran@intel.com>
This commit is contained in:
Bellekallu Rajkiran
2022-07-05 10:17:44 +00:00
committed by Compute-Runtime-Automation
parent 8e94b71654
commit fd1417532d
4 changed files with 1091 additions and 6 deletions

View File

@@ -6,15 +6,19 @@
set(L0_TESTS_TOOLS_SYSMAN_POWER_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}test_zes_power.cpp
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}mock_sysfs_power.h
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}test_zes_power_helper.cpp
)
if((NEO_ENABLE_i915_PRELIM_DETECTION) AND ("${BRANCH_TYPE}" STREQUAL ""))
list(REMOVE_ITEM L0_TESTS_TOOLS_SYSMAN_POWER_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/test_zes_power_helper.cpp
if(NEO_ENABLE_i915_PRELIM_DETECTION)
list(APPEND L0_TESTS_TOOLS_SYSMAN_POWER_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/test_zes_power_prelim.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_zes_power_helper_prelim.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_power_prelim.h
)
else()
list(APPEND L0_TESTS_TOOLS_SYSMAN_POWER_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/test_zes_power.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_zes_power_helper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_power.h
)
endif()

View File

@@ -0,0 +1,375 @@
/*
* Copyright (C) 2020-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/helpers/string.h"
#include "level_zero/tools/source/sysman/power/linux/os_power_imp_prelim.h"
#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h"
#include "sysman/linux/pmt/pmt.h"
#include "sysman/power/power_imp.h"
#include "sysman/sysman_imp.h"
extern bool sysmanUltsEnable;
using ::testing::DoDefault;
using ::testing::Matcher;
using ::testing::Return;
namespace L0 {
namespace ult {
constexpr uint64_t setEnergyCounter = (83456u * 1048576u);
constexpr uint64_t offset = 0x400;
const std::string deviceName("device");
const std::string baseTelemSysFS("/sys/class/intel_pmt");
const std::string hwmonDir("device/hwmon");
const std::string i915HwmonDir("device/hwmon/hwmon2");
const std::string nonI915HwmonDir("device/hwmon/hwmon1");
const std::string i915HwmonDirTile0("device/hwmon/hwmon3");
const std::string i915HwmonDirTile1("device/hwmon/hwmon4");
const std::vector<std::string> listOfMockedHwmonDirs = {"hwmon0", "hwmon1", "hwmon2", "hwmon3", "hwmon4"};
const std::string sustainedPowerLimit("power1_max");
const std::string criticalPowerLimit("power1_crit");
const std::string energyCounterNode("energy1_input");
const std::string defaultPowerLimit("power1_max_default");
constexpr uint64_t expectedEnergyCounter = 123456785u;
constexpr uint64_t expectedEnergyCounterTile0 = 123456785u;
constexpr uint64_t expectedEnergyCounterTile1 = 128955785u;
constexpr uint32_t mockDefaultPowerLimitVal = 300000000;
const std::map<std::string, uint64_t> deviceKeyOffsetMapPower = {
{"PACKAGE_ENERGY", 0x400},
{"COMPUTE_TEMPERATURES", 0x68},
{"SOC_TEMPERATURES", 0x60},
{"CORE_TEMPERATURES", 0x6c}};
class PowerSysfsAccess : public SysfsAccess {};
template <>
struct Mock<PowerSysfsAccess> : public PowerSysfsAccess {
ze_result_t mockReadResult = ZE_RESULT_SUCCESS;
ze_result_t mockReadValUnsignedLongResult = ZE_RESULT_SUCCESS;
ze_result_t mockWriteResult = ZE_RESULT_SUCCESS;
ze_result_t mockscanDirEntriesResult = ZE_RESULT_SUCCESS;
ze_result_t getValString(const std::string file, std::string &val) {
ze_result_t result = ZE_RESULT_ERROR_UNKNOWN;
if (file.compare(i915HwmonDir + "/" + "name") == 0) {
val = "i915";
result = ZE_RESULT_SUCCESS;
} else if (file.compare(nonI915HwmonDir + "/" + "name") == 0) {
result = ZE_RESULT_ERROR_NOT_AVAILABLE;
} else if (file.compare(i915HwmonDirTile1 + "/" + "name") == 0) {
val = "i915_gt1";
result = ZE_RESULT_SUCCESS;
} else if (file.compare(i915HwmonDirTile0 + "/" + "name") == 0) {
val = "i915_gt0";
result = ZE_RESULT_SUCCESS;
} else {
val = "garbageI915";
result = ZE_RESULT_SUCCESS;
}
return result;
}
uint64_t sustainedPowerLimitVal = 0;
uint64_t criticalPowerLimitVal = 0;
ze_result_t getValUnsignedLongHelper(const std::string file, uint64_t &val);
ze_result_t getValUnsignedLong(const std::string file, uint64_t &val) {
ze_result_t result = ZE_RESULT_SUCCESS;
if (file.compare(i915HwmonDir + "/" + sustainedPowerLimit) == 0) {
val = sustainedPowerLimitVal;
} else if (file.compare(i915HwmonDir + "/" + criticalPowerLimit) == 0) {
val = criticalPowerLimitVal;
} else if (file.compare(i915HwmonDirTile0 + "/" + energyCounterNode) == 0) {
val = expectedEnergyCounterTile0;
} else if (file.compare(i915HwmonDirTile1 + "/" + energyCounterNode) == 0) {
val = expectedEnergyCounterTile1;
} else if (file.compare(i915HwmonDir + "/" + energyCounterNode) == 0) {
val = expectedEnergyCounter;
} else {
result = ZE_RESULT_ERROR_NOT_AVAILABLE;
}
return result;
}
ze_result_t getValUnsignedInt(const std::string file, uint32_t &val) {
ze_result_t result = ZE_RESULT_SUCCESS;
if (file.compare(i915HwmonDir + "/" + defaultPowerLimit) == 0) {
val = mockDefaultPowerLimitVal;
} else {
result = ZE_RESULT_ERROR_NOT_AVAILABLE;
}
return result;
}
ze_result_t setVal(const std::string file, const int val) {
ze_result_t result = ZE_RESULT_SUCCESS;
if (file.compare(i915HwmonDir + "/" + sustainedPowerLimit) == 0) {
sustainedPowerLimitVal = static_cast<uint64_t>(val);
} else if (file.compare(i915HwmonDir + "/" + criticalPowerLimit) == 0) {
criticalPowerLimitVal = static_cast<uint64_t>(val);
} else {
result = ZE_RESULT_ERROR_NOT_AVAILABLE;
}
return result;
}
ze_result_t getscanDirEntries(const std::string file, std::vector<std::string> &listOfEntries) {
if (file.compare(hwmonDir) == 0) {
listOfEntries = listOfMockedHwmonDirs;
return ZE_RESULT_SUCCESS;
}
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
ze_result_t read(const std::string file, uint64_t &val) override {
if (mockReadValUnsignedLongResult != ZE_RESULT_SUCCESS) {
return mockReadValUnsignedLongResult;
}
return getValUnsignedLong(file, val);
}
ze_result_t read(const std::string file, std::string &val) override {
if (mockReadResult != ZE_RESULT_SUCCESS) {
return mockReadResult;
}
return getValString(file, val);
}
ze_result_t read(const std::string file, uint32_t &val) override {
if (mockReadResult != ZE_RESULT_SUCCESS) {
return mockReadResult;
}
return getValUnsignedInt(file, val);
}
ze_result_t write(const std::string file, const int val) override {
if (mockWriteResult != ZE_RESULT_SUCCESS) {
return mockWriteResult;
}
return setVal(file, val);
}
ze_result_t scanDirEntries(const std::string file, std::vector<std::string> &listOfEntries) override {
if (mockscanDirEntriesResult != ZE_RESULT_SUCCESS) {
return mockscanDirEntriesResult;
}
return getscanDirEntries(file, listOfEntries);
}
Mock<PowerSysfsAccess>() = default;
};
class PowerPmt : public PlatformMonitoringTech {
public:
PowerPmt(FsAccess *pFsAccess, ze_bool_t onSubdevice, uint32_t subdeviceId) : PlatformMonitoringTech(pFsAccess, onSubdevice, subdeviceId) {}
using PlatformMonitoringTech::closeFunction;
using PlatformMonitoringTech::keyOffsetMap;
using PlatformMonitoringTech::openFunction;
using PlatformMonitoringTech::preadFunction;
using PlatformMonitoringTech::telemetryDeviceEntry;
};
template <>
struct Mock<PowerPmt> : public PowerPmt {
~Mock() override {
rootDeviceTelemNodeIndex = 0;
}
Mock<PowerPmt>(FsAccess *pFsAccess, ze_bool_t onSubdevice, uint32_t subdeviceId) : PowerPmt(pFsAccess, onSubdevice, subdeviceId) {}
void mockedInit(FsAccess *pFsAccess) {
std::string gpuUpstreamPortPath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0";
if (ZE_RESULT_SUCCESS != PlatformMonitoringTech::enumerateRootTelemIndex(pFsAccess, gpuUpstreamPortPath)) {
return;
}
telemetryDeviceEntry = "/sys/class/intel_pmt/telem2/telem";
}
};
class PowerFsAccess : public FsAccess {};
template <>
struct Mock<PowerFsAccess> : public PowerFsAccess {
ze_result_t listDirectory(const std::string directory, std::vector<std::string> &listOfTelemNodes) override {
if (directory.compare(baseTelemSysFS) == 0) {
listOfTelemNodes.push_back("telem1");
listOfTelemNodes.push_back("telem2");
listOfTelemNodes.push_back("telem3");
listOfTelemNodes.push_back("telem4");
listOfTelemNodes.push_back("telem5");
return ZE_RESULT_SUCCESS;
}
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
ze_result_t getRealPath(const std::string path, std::string &buf) override {
if (path.compare("/sys/class/intel_pmt/telem1") == 0) {
buf = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem1";
} else if (path.compare("/sys/class/intel_pmt/telem2") == 0) {
buf = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem2";
} else if (path.compare("/sys/class/intel_pmt/telem3") == 0) {
buf = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem3";
} else if (path.compare("/sys/class/intel_pmt/telem4") == 0) {
buf = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem4";
} else if (path.compare("/sys/class/intel_pmt/telem5") == 0) {
buf = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem5";
} else {
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
return ZE_RESULT_SUCCESS;
}
Mock<PowerFsAccess>() = default;
};
class PublicLinuxPowerImp : public L0::LinuxPowerImp {
public:
PublicLinuxPowerImp(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) : LinuxPowerImp(pOsSysman, onSubdevice, subdeviceId) {}
using LinuxPowerImp::pPmt;
};
class SysmanDevicePowerFixture : public SysmanDeviceFixture {
protected:
std::unique_ptr<PublicLinuxPowerImp> pPublicLinuxPowerImp;
std::unique_ptr<Mock<PowerPmt>> pPmt;
std::unique_ptr<Mock<PowerFsAccess>> pFsAccess;
std::unique_ptr<Mock<PowerSysfsAccess>> pSysfsAccess;
SysfsAccess *pSysfsAccessOld = nullptr;
FsAccess *pFsAccessOriginal = nullptr;
OsPower *pOsPowerOriginal = nullptr;
std::vector<ze_device_handle_t> deviceHandles;
void SetUp() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
SysmanDeviceFixture::SetUp();
pFsAccess = std::make_unique<NiceMock<Mock<PowerFsAccess>>>();
pFsAccessOriginal = pLinuxSysmanImp->pFsAccess;
pLinuxSysmanImp->pFsAccess = pFsAccess.get();
pSysfsAccessOld = pLinuxSysmanImp->pSysfsAccess;
pSysfsAccess = std::make_unique<NiceMock<Mock<PowerSysfsAccess>>>();
pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get();
uint32_t subDeviceCount = 0;
Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr);
if (subDeviceCount == 0) {
deviceHandles.resize(1, device->toHandle());
} else {
deviceHandles.resize(subDeviceCount, nullptr);
Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
}
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
auto pPmt = new NiceMock<Mock<PowerPmt>>(pFsAccess.get(), deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE,
deviceProperties.subdeviceId);
pPmt->mockedInit(pFsAccess.get());
pPmt->keyOffsetMap = deviceKeyOffsetMapPower;
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.emplace(deviceProperties.subdeviceId, pPmt);
}
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
}
void TearDown() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
pLinuxSysmanImp->pFsAccess = pFsAccessOriginal;
pLinuxSysmanImp->pSysfsAccess = pSysfsAccessOld;
SysmanDeviceFixture::TearDown();
}
std::vector<zes_pwr_handle_t> getPowerHandles(uint32_t count) {
std::vector<zes_pwr_handle_t> handles(count, nullptr);
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
return handles;
}
};
class SysmanDevicePowerMultiDeviceFixture : public SysmanMultiDeviceFixture {
protected:
std::unique_ptr<PublicLinuxPowerImp> pPublicLinuxPowerImp;
std::unique_ptr<Mock<PowerPmt>> pPmt;
std::unique_ptr<Mock<PowerFsAccess>> pFsAccess;
std::unique_ptr<Mock<PowerSysfsAccess>> pSysfsAccess;
SysfsAccess *pSysfsAccessOld = nullptr;
FsAccess *pFsAccessOriginal = nullptr;
OsPower *pOsPowerOriginal = nullptr;
std::vector<ze_device_handle_t> deviceHandles;
std::map<uint32_t, L0::PlatformMonitoringTech *> mapOriginal;
void SetUp() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
SysmanMultiDeviceFixture::SetUp();
pFsAccess = std::make_unique<NiceMock<Mock<PowerFsAccess>>>();
pFsAccessOriginal = pLinuxSysmanImp->pFsAccess;
pLinuxSysmanImp->pFsAccess = pFsAccess.get();
pSysfsAccessOld = pLinuxSysmanImp->pSysfsAccess;
pSysfsAccess = std::make_unique<NiceMock<Mock<PowerSysfsAccess>>>();
pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get();
uint32_t subDeviceCount = 0;
Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr);
if (subDeviceCount == 0) {
deviceHandles.resize(1, device->toHandle());
} else {
deviceHandles.resize(subDeviceCount, nullptr);
Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
}
mapOriginal = pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject;
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.clear();
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
auto pPmt = new NiceMock<Mock<PowerPmt>>(pFsAccess.get(), deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE,
deviceProperties.subdeviceId);
pPmt->mockedInit(pFsAccess.get());
pPmt->keyOffsetMap = deviceKeyOffsetMapPower;
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.emplace(deviceProperties.subdeviceId, pPmt);
}
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
}
void TearDown() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
for (auto &pmtMapElement : pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject) {
if (pmtMapElement.second) {
delete pmtMapElement.second;
pmtMapElement.second = nullptr;
}
}
pLinuxSysmanImp->pFsAccess = pFsAccessOriginal;
pLinuxSysmanImp->pSysfsAccess = pSysfsAccessOld;
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject = mapOriginal;
SysmanMultiDeviceFixture::TearDown();
}
std::vector<zes_pwr_handle_t> getPowerHandles(uint32_t count) {
std::vector<zes_pwr_handle_t> handles(count, nullptr);
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
return handles;
}
};
} // namespace ult
} // namespace L0

View File

@@ -0,0 +1,157 @@
/*
* Copyright (C) 2021-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/tools/test/unit_tests/sources/sysman/power/linux/mock_sysfs_power_prelim.h"
extern bool sysmanUltsEnable;
namespace L0 {
namespace ult {
constexpr uint32_t powerHandleComponentCountMultiDevice = 3u;
class SysmanDevicePowerMultiDeviceFixtureHelper : public SysmanDevicePowerMultiDeviceFixture {
protected:
void SetUp() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
SysmanDevicePowerMultiDeviceFixture::SetUp();
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
if (subDeviceCount == 0) {
deviceHandles.resize(1, device->toHandle());
} else {
deviceHandles.resize(subDeviceCount, nullptr);
Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
}
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
}
void TearDown() override {
if (!sysmanUltsEnable) {
GTEST_SKIP();
}
SysmanDevicePowerMultiDeviceFixture::TearDown();
}
};
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenInvalidComponentCountWhenEnumeratingPowerDomainsThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) {
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCountMultiDevice);
count = count + 1;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCountMultiDevice);
}
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenValidPowerPointerWhenGettingCardPowerDomainWhenhwmonInterfaceExistsAndThenCallSucceds) {
zes_pwr_handle_t phPower = {};
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), &phPower), ZE_RESULT_SUCCESS);
}
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenValidPowerHandleWhenGettingPowerPropertiesThenCallSucceeds) {
auto handles = getPowerHandles(powerHandleComponentCountMultiDevice);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
if (properties.onSubdevice) {
EXPECT_FALSE(properties.canControl);
EXPECT_EQ(properties.defaultLimit, -1);
} else {
EXPECT_EQ(properties.canControl, true);
EXPECT_EQ(properties.defaultLimit, (int32_t)(mockDefaultPowerLimitVal / milliFactor));
}
EXPECT_EQ(properties.isEnergyThresholdSupported, false);
EXPECT_EQ(properties.maxLimit, -1);
EXPECT_EQ(properties.minLimit, -1);
}
}
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenScanDiectoriesFailAndPmtIsNullForSubDeviceZeroWhenGettingCardPowerThenReturnsFailure) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
for (auto &pmtMapElement : pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject) {
if (pmtMapElement.first == 0) {
delete pmtMapElement.second;
pmtMapElement.second = nullptr;
}
}
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
zes_pwr_handle_t phPower = {};
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), &phPower), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
}
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenValidPowerHandleWhenGettingPowerEnergyCounterThenValidPowerReadingsRetrieved) {
auto handles = getPowerHandles(powerHandleComponentCountMultiDevice);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
zes_power_energy_counter_t energyCounter = {};
ASSERT_EQ(ZE_RESULT_SUCCESS, zesPowerGetEnergyCounter(handle, &energyCounter));
if (properties.subdeviceId == 0) {
EXPECT_EQ(energyCounter.energy, expectedEnergyCounterTile0);
} else if (properties.subdeviceId == 1) {
EXPECT_EQ(energyCounter.energy, expectedEnergyCounterTile1);
}
}
}
TEST_F(SysmanDevicePowerMultiDeviceFixtureHelper, GivenSetPowerLimitsWhenGettingPowerLimitsThenLimitsSetEarlierAreRetrieved) {
auto handles = getPowerHandles(powerHandleComponentCountMultiDevice);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
zes_power_sustained_limit_t sustainedSet = {};
zes_power_sustained_limit_t sustainedGet = {};
sustainedSet.power = 300000;
if (!properties.onSubdevice) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handle, &sustainedSet, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, &sustainedGet, nullptr, nullptr));
EXPECT_EQ(sustainedGet.power, sustainedSet.power);
} else {
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handle, &sustainedSet, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, &sustainedGet, nullptr, nullptr));
}
zes_power_burst_limit_t burstGet = {};
if (!properties.onSubdevice) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, nullptr, &burstGet, nullptr));
EXPECT_EQ(burstGet.enabled, false);
EXPECT_EQ(burstGet.power, -1);
} else {
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, nullptr, &burstGet, nullptr));
}
zes_power_peak_limit_t peakSet = {};
zes_power_peak_limit_t peakGet = {};
peakSet.powerAC = 300000;
if (!properties.onSubdevice) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handle, nullptr, nullptr, &peakSet));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, nullptr, nullptr, &peakGet));
EXPECT_EQ(peakGet.powerAC, peakSet.powerAC);
EXPECT_EQ(peakGet.powerDC, -1);
} else {
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, nullptr, nullptr, &peakGet));
}
}
}
} // namespace ult
} // namespace L0

View File

@@ -0,0 +1,549 @@
/*
* Copyright (C) 2020-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/tools/source/sysman/power/linux/os_power_imp_prelim.h"
#include "level_zero/tools/test/unit_tests/sources/sysman/power/linux/mock_sysfs_power_prelim.h"
namespace L0 {
namespace ult {
static int fakeFileDescriptor = 123;
constexpr uint64_t convertJouleToMicroJoule = 1000000u;
constexpr uint32_t powerHandleComponentCount = 1u;
inline static int openMockPower(const char *pathname, int flags) {
if (strcmp(pathname, "/sys/class/intel_pmt/telem2/telem") == 0) {
return fakeFileDescriptor;
}
if (strcmp(pathname, "/sys/class/intel_pmt/telem3/telem") == 0) {
return fakeFileDescriptor;
}
return -1;
}
inline static int closeMockPower(int fd) {
if (fd == fakeFileDescriptor) {
return 0;
}
return -1;
}
ssize_t preadMockPower(int fd, void *buf, size_t count, off_t offset) {
uint64_t *mockBuf = static_cast<uint64_t *>(buf);
*mockBuf = setEnergyCounter;
return count;
}
TEST_F(SysmanDevicePowerFixture, GivenComponentCountZeroWhenEnumeratingPowerDomainsWhenhwmonInterfaceExistsThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) {
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
}
TEST_F(SysmanDevicePowerFixture, GivenInvalidComponentCountWhenEnumeratingPowerDomainsWhenhwmonInterfaceExistsThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) {
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
count = count + 1;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
}
TEST_F(SysmanDevicePowerFixture, GivenComponentCountZeroWhenEnumeratingPowerDomainsWhenhwmonInterfaceExistsThenValidPowerHandlesIsReturned) {
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
std::vector<zes_pwr_handle_t> handles(count, nullptr);
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
for (auto handle : handles) {
EXPECT_NE(handle, nullptr);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerPointerWhenGettingCardPowerDomainWhenhwmonInterfaceExistsAndThenCallSucceds) {
zes_pwr_handle_t phPower = {};
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), &phPower), ZE_RESULT_SUCCESS);
}
TEST_F(SysmanDevicePowerFixture, GivenInvalidPowerPointerWhenGettingCardPowerDomainAndThenReturnsFailure) {
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), nullptr), ZE_RESULT_ERROR_INVALID_NULL_POINTER);
}
TEST_F(SysmanDevicePowerFixture, GivenUninitializedPowerHandlesAndWhenGettingCardPowerDomainThenReurnsFailure) {
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
zes_pwr_handle_t phPower = {};
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), &phPower), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
}
TEST_F(SysmanDevicePowerFixture, GivenScanDiectoriesFailAndPmtIsNullWhenGettingCardPowerThenReturnsFailure) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
for (auto &pmtMapElement : pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject) {
if (pmtMapElement.second) {
delete pmtMapElement.second;
pmtMapElement.second = nullptr;
}
}
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
zes_pwr_handle_t phPower = {};
EXPECT_EQ(zesDeviceGetCardPowerDomain(device->toHandle(), &phPower), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerPropertiesWhenhwmonInterfaceExistsThenCallSucceeds) {
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
EXPECT_FALSE(properties.onSubdevice);
EXPECT_EQ(properties.subdeviceId, 0u);
EXPECT_EQ(properties.canControl, true);
EXPECT_EQ(properties.isEnergyThresholdSupported, false);
EXPECT_EQ(properties.defaultLimit, (int32_t)(mockDefaultPowerLimitVal / milliFactor));
EXPECT_EQ(properties.maxLimit, -1);
EXPECT_EQ(properties.minLimit, -1);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerPropertiesWhenHwmonInterfaceExistThenLimitsReturnsUnkown) {
pSysfsAccess->mockReadResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
EXPECT_FALSE(properties.onSubdevice);
EXPECT_EQ(properties.subdeviceId, 0u);
EXPECT_EQ(properties.defaultLimit, -1);
EXPECT_EQ(properties.maxLimit, -1);
EXPECT_EQ(properties.minLimit, -1);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerPropertiesWhenHwmonInterfaceExistThenMaxLimitIsUnsupported) {
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
EXPECT_FALSE(properties.onSubdevice);
EXPECT_EQ(properties.subdeviceId, 0u);
EXPECT_EQ(properties.defaultLimit, (int32_t)(mockDefaultPowerLimitVal / milliFactor));
EXPECT_EQ(properties.maxLimit, -1);
EXPECT_EQ(properties.minLimit, -1);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerPropertiesThenHwmonInterfaceExistAndMinLimitIsUnknown) {
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
EXPECT_FALSE(properties.onSubdevice);
EXPECT_EQ(properties.subdeviceId, 0u);
EXPECT_EQ(properties.defaultLimit, (int32_t)(mockDefaultPowerLimitVal / milliFactor));
EXPECT_EQ(properties.maxLimit, -1);
EXPECT_EQ(properties.minLimit, -1);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerEnergyCounterFailedWhenHwmonInterfaceExistThenValidErrorCodeReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
auto pPmt = static_cast<NiceMock<Mock<PowerPmt>> *>(pLinuxSysmanImp->getPlatformMonitoringTechAccess(deviceProperties.subdeviceId));
pPmt->openFunction = openMockPower;
pPmt->closeFunction = closeMockPower;
pPmt->preadFunction = preadMockPower;
}
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
for (auto handle : handles) {
zes_power_energy_counter_t energyCounter = {};
uint64_t expectedEnergyCounter = convertJouleToMicroJoule * (setEnergyCounter / 1048576);
ASSERT_EQ(ZE_RESULT_SUCCESS, zesPowerGetEnergyCounter(handle, &energyCounter));
EXPECT_EQ(energyCounter.energy, expectedEnergyCounter);
}
}
TEST_F(SysmanDevicePowerFixture, GivenSetPowerLimitsWhenGettingPowerLimitsWhenHwmonInterfaceExistThenLimitsSetEarlierAreRetrieved) {
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_sustained_limit_t sustainedSet = {};
zes_power_sustained_limit_t sustainedGet = {};
sustainedSet.enabled = 1;
sustainedSet.interval = 10;
sustainedSet.power = 300000;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handle, &sustainedSet, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, &sustainedGet, nullptr, nullptr));
EXPECT_EQ(sustainedGet.power, sustainedSet.power);
zes_power_burst_limit_t burstGet = {};
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handle, nullptr, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, nullptr, &burstGet, nullptr));
EXPECT_EQ(burstGet.enabled, false);
EXPECT_EQ(burstGet.power, -1);
zes_power_peak_limit_t peakSet = {};
zes_power_peak_limit_t peakGet = {};
peakSet.powerAC = 300000;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handle, nullptr, nullptr, &peakSet));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handle, nullptr, nullptr, &peakGet));
EXPECT_EQ(peakGet.powerAC, peakSet.powerAC);
EXPECT_EQ(peakGet.powerDC, -1);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandlesWhenCallingSetAndGetPowerLimitExtWhenHwmonInterfaceExistThenUnsupportedFeatureIsReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
uint32_t limitCount = 0;
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimitsExt(handle, &limitCount, nullptr));
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimitsExt(handle, &limitCount, nullptr));
}
}
TEST_F(SysmanDevicePowerFixture, GivenReadingSustainedPowerLimitNodeReturnErrorWhenSetOrGetPowerLimitsWhenHwmonInterfaceExistForSustainedPowerLimitEnabledThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockWriteResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (auto handle : handles) {
zes_power_sustained_limit_t sustainedSet = {};
zes_power_sustained_limit_t sustainedGet = {};
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handle, &sustainedSet, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, &sustainedGet, nullptr, nullptr));
}
}
TEST_F(SysmanDevicePowerFixture, GivenReadingPeakPowerLimitNodeReturnErrorWhenSetOrGetPowerLimitsWhenHwmonInterfaceExistForPeakPowerLimitEnabledThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockWriteResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (auto handle : handles) {
zes_power_peak_limit_t peakSet = {};
zes_power_peak_limit_t peakGet = {};
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handle, nullptr, nullptr, &peakSet));
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, nullptr, nullptr, &peakGet));
}
}
TEST_F(SysmanDevicePowerFixture, GivenReadingSustainedPowerNodeReturnErrorWhenGetPowerLimitsForSustainedPowerWhenHwmonInterfaceExistThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (auto handle : handles) {
zes_power_sustained_limit_t sustainedGet = {};
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, &sustainedGet, nullptr, nullptr));
}
}
TEST_F(SysmanDevicePowerFixture, GivenReadingpeakPowerNodeReturnErrorWhenGetPowerLimitsForpeakPowerWhenHwmonInterfaceExistThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (auto handle : handles) {
zes_power_peak_limit_t peakGet = {};
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, nullptr, nullptr, &peakGet));
}
}
TEST_F(SysmanDevicePowerFixture, GivenwritingSustainedPowerNodeReturnErrorWhenSetPowerLimitsForSustainedPowerWhenHwmonInterfaceExistThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockWriteResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
zes_power_sustained_limit_t sustainedSet = {};
sustainedSet.enabled = 1;
sustainedSet.interval = 10;
sustainedSet.power = 300000;
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handles[0], &sustainedSet, nullptr, nullptr));
}
TEST_F(SysmanDevicePowerFixture, GivenwritingSustainedPowerIntervalNodeReturnErrorWhenSetPowerLimitsForSustainedPowerIntervalWhenHwmonInterfaceExistThenProperErrorCodesReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockWriteResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
zes_power_sustained_limit_t sustainedSet = {};
sustainedSet.enabled = 1;
sustainedSet.interval = 10;
sustainedSet.power = 300000;
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handles[0], &sustainedSet, nullptr, nullptr));
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenWritingToSustainedPowerEnableNodeWithoutPermissionsThenValidErrorIsReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
pSysfsAccess->mockWriteResult = ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
zes_power_sustained_limit_t sustainedSet = {};
sustainedSet.enabled = 0;
EXPECT_EQ(ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS, zesPowerSetLimits(handles[0], &sustainedSet, nullptr, nullptr));
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleAndPermissionsThenFirstDisableSustainedPowerLimitAndThenEnableItAndCheckSuccesIsReturned) {
auto handles = getPowerHandles(powerHandleComponentCount);
zes_power_sustained_limit_t sustainedSet = {};
zes_power_sustained_limit_t sustainedGet = {};
sustainedSet.enabled = 0;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handles[0], &sustainedSet, nullptr, nullptr));
sustainedSet.power = 300000;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerSetLimits(handles[0], &sustainedSet, nullptr, nullptr));
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetLimits(handles[0], &sustainedGet, nullptr, nullptr));
EXPECT_EQ(sustainedGet.power, sustainedSet.power);
}
TEST_F(SysmanDevicePowerFixture, GivenScanDiectoriesFailAndPmtIsNotNullPointerThenPowerModuleIsSupported) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(device)->getProperties(&deviceProperties);
PublicLinuxPowerImp *pPowerImp = new PublicLinuxPowerImp(pOsSysman, deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE, deviceProperties.subdeviceId);
EXPECT_TRUE(pPowerImp->isPowerModuleSupported());
delete pPowerImp;
}
TEST_F(SysmanDevicePowerFixture, GivenComponentCountZeroWhenEnumeratingPowerDomainsThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
}
TEST_F(SysmanDevicePowerFixture, GivenInvalidComponentCountWhenEnumeratingPowerDomainsThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
count = count + 1;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
}
TEST_F(SysmanDevicePowerFixture, GivenComponentCountZeroWhenEnumeratingPowerDomainsThenValidPowerHandlesIsReturned) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
uint32_t count = 0;
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
EXPECT_EQ(count, powerHandleComponentCount);
std::vector<zes_pwr_handle_t> handles(count, nullptr);
EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
for (auto handle : handles) {
EXPECT_NE(handle, nullptr);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerPropertiesThenCallSucceeds) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_properties_t properties;
EXPECT_EQ(ZE_RESULT_SUCCESS, zesPowerGetProperties(handle, &properties));
EXPECT_FALSE(properties.onSubdevice);
EXPECT_EQ(properties.subdeviceId, 0u);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerEnergyCounterThenValidPowerReadingsRetrieved) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
auto pPmt = static_cast<NiceMock<Mock<PowerPmt>> *>(pLinuxSysmanImp->getPlatformMonitoringTechAccess(deviceProperties.subdeviceId));
pPmt->openFunction = openMockPower;
pPmt->closeFunction = closeMockPower;
pPmt->preadFunction = preadMockPower;
}
for (auto handle : handles) {
zes_power_energy_counter_t energyCounter;
uint64_t expectedEnergyCounter = convertJouleToMicroJoule * (setEnergyCounter / 1048576);
ASSERT_EQ(ZE_RESULT_SUCCESS, zesPowerGetEnergyCounter(handle, &energyCounter));
EXPECT_EQ(energyCounter.energy, expectedEnergyCounter);
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerEnergyCounterWhenEnergyHwmonFileReturnsErrorAndPmtFailsThenFailureIsReturned) {
pSysfsAccess->mockReadValUnsignedLongResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
for (auto &subDeviceIdToPmtEntry : pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject) {
delete subDeviceIdToPmtEntry.second;
}
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.clear();
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.emplace(deviceProperties.subdeviceId, nullptr);
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_energy_counter_t energyCounter = {};
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetEnergyCounter(handle, &energyCounter));
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerEnergyThresholdThenUnsupportedFeatureErrorIsReturned) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
zes_energy_threshold_t threshold;
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetEnergyThreshold(handle, &threshold));
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenSettingPowerEnergyThresholdThenUnsupportedFeatureErrorIsReturned) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
double threshold = 0;
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetEnergyThreshold(handle, threshold));
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenGettingPowerLimitsThenUnsupportedFeatureErrorIsReturned) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_sustained_limit_t sustained;
zes_power_burst_limit_t burst;
zes_power_peak_limit_t peak;
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerGetLimits(handle, &sustained, &burst, &peak));
}
}
TEST_F(SysmanDevicePowerFixture, GivenValidPowerHandleWhenSettingPowerLimitsThenUnsupportedFeatureErrorIsReturned) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto handle : handles) {
zes_power_sustained_limit_t sustained;
zes_power_burst_limit_t burst;
zes_power_peak_limit_t peak;
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesPowerSetLimits(handle, &sustained, &burst, &peak));
}
}
TEST_F(SysmanDevicePowerMultiDeviceFixture, GivenValidPowerHandleWhenGettingPowerEnergyCounterWhenEnergyHwmonFailsThenValidPowerReadingsRetrievedFromPmt) {
pSysfsAccess->mockscanDirEntriesResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
for (const auto &handle : pSysmanDeviceImp->pPowerHandleContext->handleList) {
delete handle;
}
pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
pSysmanDeviceImp->pPowerHandleContext->init(deviceHandles, device->toHandle());
auto handles = getPowerHandles(powerHandleComponentCount);
for (auto &deviceHandle : deviceHandles) {
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
auto pPmt = static_cast<NiceMock<Mock<PowerPmt>> *>(pLinuxSysmanImp->getPlatformMonitoringTechAccess(deviceProperties.subdeviceId));
pPmt->openFunction = openMockPower;
pPmt->closeFunction = closeMockPower;
pPmt->preadFunction = preadMockPower;
}
for (auto handle : handles) {
zes_power_energy_counter_t energyCounter;
uint64_t expectedEnergyCounter = convertJouleToMicroJoule * (setEnergyCounter / 1048576);
ASSERT_EQ(ZE_RESULT_SUCCESS, zesPowerGetEnergyCounter(handle, &energyCounter));
EXPECT_EQ(energyCounter.energy, expectedEnergyCounter);
}
}
} // namespace ult
} // namespace L0