Debian Codegraph @

level_zero/sysman/test/unit_tests/sources/power/linux/mock_sysfs_power.h

raw
1 /*
2 * Copyright (C) 2020-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #pragma once
9
10 #include "shared/source/helpers/string.h"
11
12 #include "level_zero/sysman/source/api/power/linux/sysman_os_power_imp.h"
13 #include "level_zero/sysman/source/api/power/sysman_power_imp.h"
14 #include "level_zero/sysman/source/device/sysman_device_imp.h"
15 #include "level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h"
16 #include "level_zero/sysman/source/sysman_const.h"
17 #include "level_zero/sysman/test/unit_tests/sources/linux/mock_sysman_fixture.h"
18 #include "level_zero/sysman/test/unit_tests/sources/shared/linux/kmd_interface/mock_sysman_kmd_interface_i915.h"
19
20 namespace L0 {
21 namespace Sysman {
22 namespace ult {
23
24 constexpr uint64_t setEnergyCounter = (83456u * 1048576u);
25 constexpr uint64_t mockKeyOffset = 0x420;
26 constexpr uint32_t mockLimitCount = 2u;
27 const std::string hwmonDir("device/hwmon");
28 const std::string i915HwmonDir("device/hwmon/hwmon2");
29 const std::string nonI915HwmonDir("device/hwmon/hwmon1");
30 const std::string i915HwmonDirTile0("device/hwmon/hwmon3");
31 const std::string i915HwmonDirTile1("device/hwmon/hwmon4");
32 const std::vector<std::string> listOfMockedHwmonDirs = {"hwmon0", "hwmon1", "hwmon2", "hwmon3", "hwmon4"};
33 const std::string sustainedPowerLimit("power1_max");
34 const std::string sustainedPowerLimitInterval("power1_max_interval");
35 const std::string criticalPowerLimit1("curr1_crit");
36 const std::string criticalPowerLimit2("power1_crit");
37 const std::string energyCounterNode("energy1_input");
38 const std::string defaultPowerLimit("power1_rated_max");
39 constexpr uint64_t expectedEnergyCounter = 123456785u;
40 constexpr uint64_t expectedEnergyCounterTile0 = 123456785u;
41 constexpr uint64_t expectedEnergyCounterTile1 = 128955785u;
42 constexpr uint32_t mockDefaultPowerLimitVal = 600000000;
43 constexpr uint64_t mockMinPowerLimitVal = 300000000;
44 constexpr uint64_t mockMaxPowerLimitVal = 600000000;
45
46 const std::string realPathTelem = "/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/telem1";
47 const std::string sysfsPathTelem = "/sys/class/intel_pmt/telem1";
48 const std::string telemOffsetFileName("/sys/class/intel_pmt/telem1/offset");
49 const std::string telemGuidFileName("/sys/class/intel_pmt/telem1/guid");
50 const std::string telemFileName("/sys/class/intel_pmt/telem1/telem");
51
52 struct MockPowerSysfsAccessInterface : public L0::Sysman::SysFsAccessInterface {
53
54 ze_result_t mockReadResult = ZE_RESULT_SUCCESS;
55 ze_result_t mockReadPeakResult = ZE_RESULT_SUCCESS;
56 ze_result_t mockWriteResult = ZE_RESULT_SUCCESS;
57 ze_result_t mockReadIntResult = ZE_RESULT_SUCCESS;
58 ze_result_t mockWritePeakLimitResult = ZE_RESULT_SUCCESS;
59 ze_result_t mockscanDirEntriesResult = ZE_RESULT_SUCCESS;
60 std::vector<ze_result_t> mockReadValUnsignedLongResult{};
61 std::vector<ze_result_t> mockWriteUnsignedResult{};
62
63 ze_result_t getValString(const std::string file, std::string &val) {
64 ze_result_t result = ZE_RESULT_ERROR_UNKNOWN;
65 if (file.compare(i915HwmonDir + "/" + "name") == 0) {
66 val = "i915";
67 result = ZE_RESULT_SUCCESS;
68 } else if (file.compare(nonI915HwmonDir + "/" + "name") == 0) {
69 result = ZE_RESULT_ERROR_NOT_AVAILABLE;
70 } else if (file.compare(i915HwmonDirTile1 + "/" + "name") == 0) {
71 val = "i915_gt1";
72 result = ZE_RESULT_SUCCESS;
73 } else if (file.compare(i915HwmonDirTile0 + "/" + "name") == 0) {
74 val = "i915_gt0";
75 result = ZE_RESULT_SUCCESS;
76 } else {
77 val = "garbageI915";
78 result = ZE_RESULT_SUCCESS;
79 }
80 return result;
81 }
82
83 uint64_t sustainedPowerLimitVal = 0;
84 uint64_t criticalPowerLimitVal = 0;
85 int32_t sustainedPowerLimitIntervalVal = 0;
86
87 ze_result_t getValUnsignedLongHelper(const std::string file, uint64_t &val);
88 ze_result_t getValUnsignedLong(const std::string file, uint64_t &val) {
89 ze_result_t result = ZE_RESULT_SUCCESS;
90 if (file.compare(i915HwmonDir + "/" + sustainedPowerLimit) == 0) {
91 val = sustainedPowerLimitVal;
92 } else if ((file.compare(i915HwmonDir + "/" + criticalPowerLimit1) == 0) || (file.compare(i915HwmonDir + "/" + criticalPowerLimit2) == 0)) {
93 if (mockReadPeakResult != ZE_RESULT_SUCCESS) {
94 return mockReadPeakResult;
95 }
96 val = criticalPowerLimitVal;
97 } else if (file.compare(i915HwmonDirTile0 + "/" + energyCounterNode) == 0) {
98 val = expectedEnergyCounterTile0;
99 } else if (file.compare(i915HwmonDirTile1 + "/" + energyCounterNode) == 0) {
100 val = expectedEnergyCounterTile1;
101 } else if (file.compare(i915HwmonDir + "/" + energyCounterNode) == 0) {
102 val = expectedEnergyCounter;
103 } else if (file.compare(i915HwmonDir + "/" + defaultPowerLimit) == 0) {
104 val = mockDefaultPowerLimitVal;
105 } else {
106 result = ZE_RESULT_ERROR_NOT_AVAILABLE;
107 }
108
109 return result;
110 }
111
112 ze_result_t getValUnsignedInt(const std::string file, uint32_t &val) {
113 ze_result_t result = ZE_RESULT_SUCCESS;
114 if (file.compare(i915HwmonDir + "/" + defaultPowerLimit) == 0) {
115 val = mockDefaultPowerLimitVal;
116 } else {
117 result = ZE_RESULT_ERROR_NOT_AVAILABLE;
118 }
119 return result;
120 }
121
122 ze_result_t setVal(const std::string file, const int val) {
123 ze_result_t result = ZE_RESULT_SUCCESS;
124 if (file.compare(i915HwmonDir + "/" + sustainedPowerLimit) == 0) {
125 sustainedPowerLimitVal = static_cast<uint64_t>(val);
126 } else if ((file.compare(i915HwmonDir + "/" + sustainedPowerLimitInterval) == 0)) {
127 sustainedPowerLimitIntervalVal = val;
128 } else if ((file.compare(i915HwmonDir + "/" + criticalPowerLimit1) == 0) || (file.compare(i915HwmonDir + "/" + criticalPowerLimit2) == 0)) {
129 if (mockWritePeakLimitResult != ZE_RESULT_SUCCESS) {
130 return mockWritePeakLimitResult;
131 }
132 criticalPowerLimitVal = static_cast<uint64_t>(val);
133 } else {
134 result = ZE_RESULT_ERROR_NOT_AVAILABLE;
135 }
136
137 return result;
138 }
139
140 ze_result_t getscanDirEntries(const std::string file, std::vector<std::string> &listOfEntries) {
141 if (file.compare(hwmonDir) == 0) {
142 listOfEntries = listOfMockedHwmonDirs;
143 return ZE_RESULT_SUCCESS;
144 }
145 return ZE_RESULT_ERROR_NOT_AVAILABLE;
146 }
147
148 ze_result_t read(const std::string file, uint64_t &val) override {
149 ze_result_t result = ZE_RESULT_SUCCESS;
150 if (!mockReadValUnsignedLongResult.empty()) {
151 result = mockReadValUnsignedLongResult.front();
152 mockReadValUnsignedLongResult.erase(mockReadValUnsignedLongResult.begin());
153 if (result != ZE_RESULT_SUCCESS) {
154 return result;
155 }
156 }
157
158 return getValUnsignedLong(file, val);
159 }
160
161 ze_result_t read(const std::string file, int32_t &val) override {
162 if (mockReadIntResult != ZE_RESULT_SUCCESS) {
163 return mockReadIntResult;
164 }
165
166 if (file.compare(i915HwmonDir + "/" + sustainedPowerLimitInterval) == 0) {
167 val = sustainedPowerLimitIntervalVal;
168 return ZE_RESULT_SUCCESS;
169 }
170
171 return ZE_RESULT_ERROR_NOT_AVAILABLE;
172 }
173
174 ze_result_t read(const std::string file, std::string &val) override {
175 if (mockReadResult != ZE_RESULT_SUCCESS) {
176 return mockReadResult;
177 }
178
179 return getValString(file, val);
180 }
181
182 ze_result_t read(const std::string file, uint32_t &val) override {
183 if (mockReadResult != ZE_RESULT_SUCCESS) {
184 return mockReadResult;
185 }
186 return getValUnsignedInt(file, val);
187 }
188
189 ze_result_t write(const std::string file, const int val) override {
190 if (mockWriteResult != ZE_RESULT_SUCCESS) {
191 return mockWriteResult;
192 }
193
194 return setVal(file, val);
195 }
196
197 ze_result_t write(const std::string file, const uint64_t val) override {
198 ze_result_t result = ZE_RESULT_SUCCESS;
199 if (!mockWriteUnsignedResult.empty()) {
200 result = mockWriteUnsignedResult.front();
201 mockWriteUnsignedResult.erase(mockWriteUnsignedResult.begin());
202 if (result != ZE_RESULT_SUCCESS) {
203 return result;
204 }
205 }
206
207 if (file.compare(i915HwmonDir + "/" + sustainedPowerLimit) == 0) {
208 if (val < mockMinPowerLimitVal) {
209 sustainedPowerLimitVal = mockMinPowerLimitVal;
210 } else if (val > mockMaxPowerLimitVal) {
211 sustainedPowerLimitVal = mockMaxPowerLimitVal;
212 } else {
213 sustainedPowerLimitVal = val;
214 }
215 } else if ((file.compare(i915HwmonDir + "/" + criticalPowerLimit1) == 0) || (file.compare(i915HwmonDir + "/" + criticalPowerLimit2) == 0)) {
216 if (mockWritePeakLimitResult != ZE_RESULT_SUCCESS) {
217 return mockWritePeakLimitResult;
218 }
219 criticalPowerLimitVal = val;
220 } else {
221 result = ZE_RESULT_ERROR_NOT_AVAILABLE;
222 }
223
224 return result;
225 }
226
227 ze_result_t scanDirEntries(const std::string file, std::vector<std::string> &listOfEntries) override {
228 if (mockscanDirEntriesResult != ZE_RESULT_SUCCESS) {
229 return mockscanDirEntriesResult;
230 }
231
232 return getscanDirEntries(file, listOfEntries);
233 }
234
235 MockPowerSysfsAccessInterface() = default;
236 };
237
238 struct MockPowerFsAccessInterface : public L0::Sysman::FsAccessInterface {
239 MockPowerFsAccessInterface() = default;
240 };
241
242 class PublicLinuxPowerImp : public L0::Sysman::LinuxPowerImp {
243 public:
244 PublicLinuxPowerImp(L0::Sysman::OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId, zes_power_domain_t powerDomain) : L0::Sysman::LinuxPowerImp(pOsSysman, onSubdevice, subdeviceId, powerDomain) {}
245 using L0::Sysman::LinuxPowerImp::isTelemetrySupportAvailable;
246 using L0::Sysman::LinuxPowerImp::pSysfsAccess;
247 };
248
249 class SysmanDevicePowerFixtureI915 : public SysmanDeviceFixture {
250 protected:
251 L0::Sysman::SysmanDevice *device = nullptr;
252 MockSysmanKmdInterfacePrelim *pSysmanKmdInterface = nullptr;
253 MockPowerSysfsAccessInterface *pSysfsAccess = nullptr;
254 MockPowerFsAccessInterface *pFsAccess = nullptr;
255
256 void SetUp() override {
257 SysmanDeviceFixture::SetUp();
258 device = pSysmanDevice;
259 pSysmanKmdInterface = new MockSysmanKmdInterfacePrelim(pLinuxSysmanImp->getSysmanProductHelper());
260 pFsAccess = new MockPowerFsAccessInterface();
261 pSysfsAccess = new MockPowerSysfsAccessInterface();
262 pSysmanKmdInterface->pFsAccess.reset(pFsAccess);
263 pSysmanKmdInterface->pSysfsAccess.reset(pSysfsAccess);
264 pLinuxSysmanImp->pSysmanKmdInterface.reset(pSysmanKmdInterface);
265 pLinuxSysmanImp->pFsAccess = pFsAccess;
266 getPowerHandles(0);
267 }
268 void TearDown() override {
269 SysmanDeviceFixture::TearDown();
270 }
271
272 std::vector<zes_pwr_handle_t> getPowerHandles(uint32_t count) {
273 std::vector<zes_pwr_handle_t> handles(count, nullptr);
274 EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
275 return handles;
276 }
277 };
278
279 class SysmanDevicePowerFixtureXe : public SysmanDeviceFixture {
280 protected:
281 L0::Sysman::SysmanDevice *device = nullptr;
282 void SetUp() override {
283 SysmanDeviceFixture::SetUp();
284 device = pSysmanDevice;
285 pSysmanDeviceImp->pPowerHandleContext->handleList.clear();
286 }
287 void TearDown() override {
288 SysmanDeviceFixture::TearDown();
289 }
290 };
291
292 class SysmanDevicePowerMultiDeviceFixture : public SysmanMultiDeviceFixture {
293 protected:
294 L0::Sysman::SysmanDevice *device = nullptr;
295 std::unique_ptr<PublicLinuxPowerImp> pPublicLinuxPowerImp;
296 MockSysmanKmdInterfacePrelim *pSysmanKmdInterface = nullptr;
297 MockPowerSysfsAccessInterface *pSysfsAccess = nullptr;
298
299 void SetUp() override {
300 SysmanMultiDeviceFixture::SetUp();
301 device = pSysmanDevice;
302 pSysmanKmdInterface = new MockSysmanKmdInterfacePrelim(pLinuxSysmanImp->getSysmanProductHelper());
303 pSysfsAccess = new MockPowerSysfsAccessInterface();
304 pSysmanKmdInterface->pSysfsAccess.reset(pSysfsAccess);
305 pLinuxSysmanImp->pSysmanKmdInterface.reset(pSysmanKmdInterface);
306 }
307 void TearDown() override {
308 SysmanMultiDeviceFixture::TearDown();
309 }
310
311 std::vector<zes_pwr_handle_t> getPowerHandles(uint32_t count) {
312 std::vector<zes_pwr_handle_t> handles(count, nullptr);
313 EXPECT_EQ(zesDeviceEnumPowerDomains(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
314 return handles;
315 }
316 };
317
318 } // namespace ult
319 } // namespace Sysman
320 } // namespace L0