Debian Codegraph @

level_zero/sysman/test/unit_tests/sources/frequency/linux/mock_sysman_frequency.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/os_interface/product_helper_hw.h"
11 #include "shared/source/release_helper/release_helper.h"
12 #include "shared/test/common/test_macros/mock_method_macros.h"
13
14 #include "level_zero/sysman/source/api/frequency/linux/sysman_os_frequency_imp.h"
15 #include "level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h"
16
17 #include "gtest/gtest.h"
18 #include "igfxfmid.h"
19
20 namespace L0 {
21 namespace Sysman {
22 namespace ult {
23
24 const std::string minFreqFile("gt/gt0/rps_min_freq_mhz");
25 const std::string minDefaultFreqFile("gt/gt0/.defaults/rps_min_freq_mhz");
26 const std::string maxFreqFile("gt/gt0/rps_max_freq_mhz");
27 const std::string boostFreqFile("gt/gt0/rps_boost_freq_mhz");
28 const std::string maxDefaultFreqFile("gt/gt0/.defaults/rps_max_freq_mhz");
29 const std::string requestFreqFile("gt/gt0/punit_req_freq_mhz");
30 const std::string tdpFreqFile("gt/gt0/rapl_PL1_freq_mhz");
31 const std::string actualFreqFile("gt/gt0/rps_act_freq_mhz");
32 const std::string efficientFreqFile("gt/gt0/rps_RP1_freq_mhz");
33 const std::string maxValFreqFile("gt/gt0/rps_RP0_freq_mhz");
34 const std::string minValFreqFile("gt/gt0/rps_RPn_freq_mhz");
35 const std::string throttleReasonStatusFile("gt/gt0/throttle_reason_status");
36 const std::string throttleReasonPL1File("gt/gt0/throttle_reason_pl1");
37 const std::string throttleReasonPL2File("gt/gt0/throttle_reason_pl2");
38 const std::string throttleReasonPL4File("gt/gt0/throttle_reason_pl4");
39 const std::string throttleReasonThermalFile("gt/gt0/throttle_reason_thermal");
40
41 const std::string minFreqFileLegacy("gt_min_freq_mhz");
42 const std::string maxFreqFileLegacy("gt_max_freq_mhz");
43 const std::string boostFreqFileLegacy("gt_boost_freq_mhz");
44 const std::string requestFreqFileLegacy("gt_cur_freq_mhz");
45 const std::string tdpFreqFileLegacy("rapl_PL1_freq_mhz");
46 const std::string actualFreqFileLegacy("gt_act_freq_mhz");
47 const std::string efficientFreqFileLegacy("gt_RP1_freq_mhz");
48 const std::string maxValFreqFileLegacy("gt_RP0_freq_mhz");
49 const std::string minValFreqFileLegacy("gt_RPn_freq_mhz");
50 const std::string throttleReasonStatusFileLegacy("gt_throttle_reason_status");
51 const std::string throttleReasonPL1FileLegacy("gt_throttle_reason_status_pl1");
52 const std::string throttleReasonPL2FileLegacy("gt_throttle_reason_status_pl2");
53 const std::string throttleReasonPL4FileLegacy("gt_throttle_reason_status_pl4");
54 const std::string throttleReasonThermalFileLegacy("gt_throttle_reason_status_thermal");
55
56 struct MockFrequencySysfsAccess : public L0::Sysman::SysFsAccessInterface {
57 double mockMin = 0;
58 double mockMax = 0;
59 double mockBoost = 0;
60 double mockRequest = 0;
61 double mockDefaultMin = 1;
62 double mockDefaultMax = 1000;
63 double mockTdp = 0;
64 double mockActual = 0;
65 double mockEfficient = 0;
66 double mockMaxVal = 0;
67 double mockMinVal = 0;
68 uint32_t throttleVal = 0;
69 uint32_t throttleReasonPL1Val = 0;
70 uint32_t throttleReasonPL2Val = 0;
71 uint32_t throttleReasonPL4Val = 0;
72 uint32_t throttleReasonThermalVal = 0;
73 ze_result_t mockReadDoubleValResult = ZE_RESULT_SUCCESS;
74 ze_result_t mockReadUnsignedIntResult = ZE_RESULT_SUCCESS;
75 ze_result_t mockReadRequestResult = ZE_RESULT_SUCCESS;
76 ze_result_t mockReadTdpResult = ZE_RESULT_SUCCESS;
77 ze_result_t mockReadEfficientResult = ZE_RESULT_SUCCESS;
78 ze_result_t mockReadActualResult = ZE_RESULT_SUCCESS;
79 ze_result_t mockReadMinValResult = ZE_RESULT_SUCCESS;
80 ze_result_t mockReadMaxValResult = ZE_RESULT_SUCCESS;
81 ze_result_t mockReadMaxResult = ZE_RESULT_SUCCESS;
82 ze_result_t mockReadPL1Result = ZE_RESULT_SUCCESS;
83 ze_result_t mockReadPL2Result = ZE_RESULT_SUCCESS;
84 ze_result_t mockReadPL4Result = ZE_RESULT_SUCCESS;
85 ze_result_t mockReadThermalResult = ZE_RESULT_SUCCESS;
86 ze_result_t mockWriteMaxResult = ZE_RESULT_SUCCESS;
87 ze_result_t mockWriteMinResult = ZE_RESULT_SUCCESS;
88 ze_bool_t isLegacy = false;
89
90 ADDMETHOD_NOBASE(directoryExists, bool, true, (const std::string path));
91
92 ze_result_t read(const std::string file, double &val) override {
93 if (mockReadDoubleValResult != ZE_RESULT_SUCCESS) {
94 return mockReadDoubleValResult;
95 }
96
97 if (isLegacy) {
98 return getValLegacy(file, val);
99 }
100
101 if (file.compare(minFreqFile) == 0) {
102 val = mockMin;
103 } else if (file.compare(maxFreqFile) == 0) {
104 if (mockReadMaxResult != ZE_RESULT_SUCCESS) {
105 return mockReadMaxResult;
106 }
107 val = mockMax;
108 } else if (file.compare(requestFreqFile) == 0) {
109 if (mockReadRequestResult != ZE_RESULT_SUCCESS) {
110 return mockReadRequestResult;
111 }
112 val = mockRequest;
113 } else if (file.compare(tdpFreqFile) == 0) {
114 if (mockReadTdpResult != ZE_RESULT_SUCCESS) {
115 return mockReadTdpResult;
116 }
117 val = mockTdp;
118 } else if (file.compare(actualFreqFile) == 0) {
119 if (mockReadActualResult != ZE_RESULT_SUCCESS) {
120 return mockReadActualResult;
121 }
122 val = mockActual;
123 } else if (file.compare(efficientFreqFile) == 0) {
124 if (mockReadEfficientResult != ZE_RESULT_SUCCESS) {
125 return mockReadEfficientResult;
126 }
127 val = mockEfficient;
128 } else if (file.compare(maxValFreqFile) == 0) {
129 if (mockReadMaxValResult != ZE_RESULT_SUCCESS) {
130 return mockReadMaxValResult;
131 }
132 val = mockMaxVal;
133 } else if (file.compare(minValFreqFile) == 0) {
134 if (mockReadMinValResult != ZE_RESULT_SUCCESS) {
135 return mockReadMinValResult;
136 }
137 val = mockMinVal;
138 } else if (file.compare(minDefaultFreqFile) == 0) {
139 val = mockDefaultMin;
140 } else if (file.compare(maxDefaultFreqFile) == 0) {
141 val = mockDefaultMax;
142 } else {
143 return ZE_RESULT_ERROR_NOT_AVAILABLE;
144 }
145
146 return ZE_RESULT_SUCCESS;
147 }
148
149 ze_result_t read(const std::string file, uint32_t &val) override {
150 if (mockReadUnsignedIntResult != ZE_RESULT_SUCCESS) {
151 return mockReadUnsignedIntResult;
152 }
153
154 if (isLegacy) {
155 return getValU32Legacy(file, val);
156 }
157 if (file.compare(throttleReasonStatusFile) == 0) {
158 val = throttleVal;
159 }
160 if (file.compare(throttleReasonPL1File) == 0) {
161 if (mockReadPL1Result != ZE_RESULT_SUCCESS) {
162 return mockReadPL1Result;
163 }
164 val = throttleReasonPL1Val;
165 }
166 if (file.compare(throttleReasonPL2File) == 0) {
167 if (mockReadPL2Result != ZE_RESULT_SUCCESS) {
168 return mockReadPL2Result;
169 }
170 val = throttleReasonPL2Val;
171 }
172 if (file.compare(throttleReasonPL4File) == 0) {
173 if (mockReadPL4Result != ZE_RESULT_SUCCESS) {
174 return mockReadPL4Result;
175 }
176 val = throttleReasonPL4Val;
177 }
178 if (file.compare(throttleReasonThermalFile) == 0) {
179 if (mockReadThermalResult != ZE_RESULT_SUCCESS) {
180 return mockReadThermalResult;
181 }
182 val = throttleReasonThermalVal;
183 }
184
185 return ZE_RESULT_SUCCESS;
186 }
187
188 ze_result_t getValLegacy(const std::string file, double &val) {
189 if (file.compare(minFreqFileLegacy) == 0) {
190 val = mockMin;
191 }
192 if (file.compare(maxFreqFileLegacy) == 0) {
193 val = mockMax;
194 }
195 if (file.compare(requestFreqFileLegacy) == 0) {
196 val = mockRequest;
197 }
198 if (file.compare(tdpFreqFileLegacy) == 0) {
199 val = mockTdp;
200 }
201 if (file.compare(actualFreqFileLegacy) == 0) {
202 val = mockActual;
203 }
204 if (file.compare(efficientFreqFileLegacy) == 0) {
205 val = mockEfficient;
206 }
207 if (file.compare(maxValFreqFileLegacy) == 0) {
208 val = mockMaxVal;
209 }
210 if (file.compare(minValFreqFileLegacy) == 0) {
211 val = mockMinVal;
212 }
213
214 return ZE_RESULT_SUCCESS;
215 }
216
217 ze_result_t getValU32Legacy(const std::string file, uint32_t &val) {
218 if (file.compare(throttleReasonStatusFileLegacy) == 0) {
219 val = throttleVal;
220 }
221 if (file.compare(throttleReasonPL1FileLegacy) == 0) {
222 val = throttleReasonPL1Val;
223 }
224 if (file.compare(throttleReasonPL2FileLegacy) == 0) {
225 val = throttleReasonPL2Val;
226 }
227 if (file.compare(throttleReasonPL4FileLegacy) == 0) {
228 val = throttleReasonPL4Val;
229 }
230 if (file.compare(throttleReasonThermalFileLegacy) == 0) {
231 val = throttleReasonThermalVal;
232 }
233
234 return ZE_RESULT_SUCCESS;
235 }
236
237 ze_result_t setVal(const std::string file, const double val) {
238 if (file.compare(minFreqFile) == 0) {
239 if (mockWriteMinResult != ZE_RESULT_SUCCESS) {
240 return mockWriteMinResult;
241 }
242 mockMin = val;
243 }
244 if (file.compare(maxFreqFile) == 0) {
245 if (mockWriteMaxResult != ZE_RESULT_SUCCESS) {
246 return mockWriteMaxResult;
247 }
248 mockMax = val;
249 }
250 if (file.compare(boostFreqFile) == 0) {
251 mockBoost = val;
252 }
253 if (file.compare(requestFreqFile) == 0) {
254 mockRequest = val;
255 }
256 if (file.compare(tdpFreqFile) == 0) {
257 mockTdp = val;
258 }
259 if (file.compare(actualFreqFile) == 0) {
260 mockActual = val;
261 }
262 if (file.compare(efficientFreqFile) == 0) {
263 mockEfficient = val;
264 }
265 if (file.compare(maxValFreqFile) == 0) {
266 mockMaxVal = val;
267 }
268 if (file.compare(minValFreqFile) == 0) {
269 mockMinVal = val;
270 }
271
272 return ZE_RESULT_SUCCESS;
273 }
274
275 ze_result_t setValU32(const std::string file, uint32_t val) {
276 if (file.compare(throttleReasonStatusFile) == 0) {
277 throttleVal = val;
278 }
279 if (file.compare(throttleReasonPL1File) == 0) {
280 throttleReasonPL1Val = val;
281 }
282 if (file.compare(throttleReasonPL2File) == 0) {
283 throttleReasonPL2Val = val;
284 }
285 if (file.compare(throttleReasonPL4File) == 0) {
286 throttleReasonPL4Val = val;
287 }
288 if (file.compare(throttleReasonThermalFile) == 0) {
289 throttleReasonThermalVal = val;
290 }
291
292 return ZE_RESULT_SUCCESS;
293 }
294
295 ze_result_t setValLegacy(const std::string file, const double val) {
296 if (file.compare(minFreqFileLegacy) == 0) {
297 mockMin = val;
298 } else if (file.compare(maxFreqFileLegacy) == 0) {
299 mockMax = val;
300 } else if (file.compare(boostFreqFileLegacy) == 0) {
301 mockBoost = val;
302 } else if (file.compare(requestFreqFileLegacy) == 0) {
303 mockRequest = val;
304 } else if (file.compare(tdpFreqFileLegacy) == 0) {
305 mockTdp = val;
306 } else if (file.compare(actualFreqFileLegacy) == 0) {
307 mockActual = val;
308 } else if (file.compare(efficientFreqFileLegacy) == 0) {
309 mockEfficient = val;
310 } else if (file.compare(maxValFreqFileLegacy) == 0) {
311 mockMaxVal = val;
312 } else if (file.compare(minValFreqFileLegacy) == 0) {
313 mockMinVal = val;
314 } else {
315 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
316 }
317
318 return ZE_RESULT_SUCCESS;
319 }
320
321 ze_result_t setValU32Legacy(const std::string file, uint32_t val) {
322 if (file.compare(throttleReasonStatusFileLegacy) == 0) {
323 throttleVal = val;
324 }
325 if (file.compare(throttleReasonPL1FileLegacy) == 0) {
326 throttleReasonPL1Val = val;
327 }
328 if (file.compare(throttleReasonPL2FileLegacy) == 0) {
329 throttleReasonPL2Val = val;
330 }
331 if (file.compare(throttleReasonPL4FileLegacy) == 0) {
332 throttleReasonPL4Val = val;
333 }
334 if (file.compare(throttleReasonThermalFileLegacy) == 0) {
335 throttleReasonThermalVal = val;
336 }
337
338 return ZE_RESULT_SUCCESS;
339 }
340
341 ze_result_t write(const std::string file, double val) override {
342 if (isLegacy) {
343 return setValLegacy(file, val);
344 }
345 return setVal(file, val);
346 }
347
348 MockFrequencySysfsAccess() = default;
349 ~MockFrequencySysfsAccess() override = default;
350 };
351
352 class PublicLinuxFrequencyImp : public L0::Sysman::LinuxFrequencyImp {
353 public:
354 PublicLinuxFrequencyImp(L0::Sysman::OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId, zes_freq_domain_t type) : L0::Sysman::LinuxFrequencyImp(pOsSysman, onSubdevice, subdeviceId, type) {}
355 using L0::Sysman::LinuxFrequencyImp::getMaxVal;
356 using L0::Sysman::LinuxFrequencyImp::getMin;
357 using L0::Sysman::LinuxFrequencyImp::getMinVal;
358 using L0::Sysman::LinuxFrequencyImp::pSysfsAccess;
359 };
360
361 } // namespace ult
362 } // namespace Sysman
363 } // namespace L0