Debian Codegraph @

level_zero/sysman/test/unit_tests/sources/scheduler/linux/mock_sysfs_scheduler.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/linux/engine_info.h"
11 #include "shared/source/os_interface/linux/xe/xedrm.h"
12
13 #include "level_zero/sysman/source/api/scheduler/linux/sysman_os_scheduler_imp.h"
14 #include "level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h"
15 #include "level_zero/sysman/test/unit_tests/sources/linux/mock_sysman_hw_device_id.h"
16
17 namespace L0 {
18 namespace Sysman {
19 namespace ult {
20
21 typedef struct SchedulerConfigValues {
22 uint64_t defaultVal;
23 uint64_t actualVal;
24 } SchedulerConfigValues_t;
25
26 typedef struct SchedulerConfig {
27 SchedulerConfigValues_t timeOut;
28 SchedulerConfigValues_t timeSclice;
29 SchedulerConfigValues_t heartBeat;
30 } SchedulerConfig_t;
31
32 class SchedulerFileProperties {
33 bool isAvailable = false;
34 ::mode_t mode = 0;
35
36 public:
37 SchedulerFileProperties() = default;
38 SchedulerFileProperties(bool isAvailable, ::mode_t mode) : isAvailable(isAvailable), mode(mode) {}
39
40 bool getAvailability() {
41 return isAvailable;
42 }
43
44 bool hasMode(::mode_t mode) {
45 return mode & this->mode;
46 }
47 };
48
49 struct MockSchedulerSysfsAccess : public L0::Sysman::SysFsAccessInterface {
50
51 std::string preemptTimeout{};
52 std::string defaultpreemptTimeout{};
53 std::string timesliceDuration{};
54 std::string defaulttimesliceDuration{};
55 std::string heartbeatInterval{};
56 std::string defaultheartbeatInterval{};
57 std::string engineDir{};
58 std::vector<std::string> listOfMockedEngines = {"rcs0", "ccs0", "bcs0", "vcs0", "vcs1", "vecs0"};
59
60 ze_result_t mockReadFileFailureError = ZE_RESULT_SUCCESS;
61 ze_result_t mockWriteFileStatus = ZE_RESULT_SUCCESS;
62 ze_result_t mockGetScanDirEntryError = ZE_RESULT_SUCCESS;
63
64 std::vector<ze_result_t> mockReadReturnValues{ZE_RESULT_SUCCESS, ZE_RESULT_SUCCESS, ZE_RESULT_SUCCESS, ZE_RESULT_ERROR_NOT_AVAILABLE};
65
66 uint32_t mockReadCount = 0;
67 bool mockReadReturnStatus = false;
68
69 std::map<std::string, SchedulerConfig_t *> engineSchedMap;
70 std::map<std::string, SchedulerFileProperties> engineSchedFilePropertiesMap;
71
72 ze_result_t getValForError(const std::string file, uint64_t &val) {
73 return ZE_RESULT_ERROR_NOT_AVAILABLE;
74 }
75
76 ze_result_t getValForErrorWhileWrite(const std::string file, const uint64_t val) {
77 return ZE_RESULT_ERROR_NOT_AVAILABLE;
78 }
79
80 void cleanUpMap() {
81 for (std::string mappedEngine : listOfMockedEngines) {
82 auto it = engineSchedMap.find(mappedEngine);
83 if (it != engineSchedMap.end()) {
84 delete it->second;
85 }
86 }
87 }
88
89 ze_result_t getFileProperties(const std::string file, SchedulerFileProperties &fileProps) {
90 auto iterator = engineSchedFilePropertiesMap.find(file);
91 if (iterator != engineSchedFilePropertiesMap.end()) {
92 fileProps = static_cast<SchedulerFileProperties>(iterator->second);
93 return ZE_RESULT_SUCCESS;
94 }
95 return ZE_RESULT_ERROR_UNKNOWN;
96 }
97
98 ze_result_t setFileProperties(const std::string &engine, const std::string file, bool isAvailable, ::mode_t mode) {
99 auto iterator = std::find(listOfMockedEngines.begin(), listOfMockedEngines.end(), engine);
100 if (iterator != listOfMockedEngines.end()) {
101 engineSchedFilePropertiesMap[engineDir + "/" + engine + "/" + file] = SchedulerFileProperties(isAvailable, mode);
102 return ZE_RESULT_SUCCESS;
103 }
104 return ZE_RESULT_ERROR_UNKNOWN;
105 }
106
107 ze_result_t read(const std::string file, uint64_t &val) override {
108
109 if (mockReadReturnStatus == true) {
110
111 if (mockReadCount < mockReadReturnValues.size()) {
112 ze_result_t returnValue = mockReadReturnValues[mockReadCount];
113 mockReadCount++;
114 return returnValue;
115 }
116 }
117
118 if (mockReadFileFailureError != ZE_RESULT_SUCCESS) {
119 return mockReadFileFailureError;
120 }
121
122 SchedulerFileProperties fileProperties;
123 ze_result_t result = getFileProperties(file, fileProperties);
124 if (ZE_RESULT_SUCCESS == result) {
125 if (!fileProperties.getAvailability()) {
126 return ZE_RESULT_ERROR_NOT_AVAILABLE;
127 }
128 if (!fileProperties.hasMode(S_IRUSR)) {
129 return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
130 }
131 } else {
132 return ZE_RESULT_ERROR_UNKNOWN;
133 }
134
135 for (std::string mappedEngine : listOfMockedEngines) {
136 if (file.find(mappedEngine) == std::string::npos) {
137 continue;
138 }
139 auto it = engineSchedMap.find(mappedEngine);
140 if (it == engineSchedMap.end()) {
141 return ZE_RESULT_ERROR_NOT_AVAILABLE;
142 }
143 if (file.compare((file.length() - preemptTimeout.length()),
144 preemptTimeout.length(),
145 preemptTimeout) == 0) {
146 if (file.find(".defaults") != std::string::npos) {
147 val = it->second->timeOut.defaultVal;
148 } else {
149 val = it->second->timeOut.actualVal;
150 }
151 return ZE_RESULT_SUCCESS;
152 }
153 if (file.compare((file.length() - timesliceDuration.length()),
154 timesliceDuration.length(),
155 timesliceDuration) == 0) {
156 if (file.find(".defaults") != std::string::npos) {
157 val = it->second->timeSclice.defaultVal;
158 } else {
159 val = it->second->timeSclice.actualVal;
160 }
161 return ZE_RESULT_SUCCESS;
162 }
163 if (file.compare((file.length() - heartbeatInterval.length()),
164 heartbeatInterval.length(),
165 heartbeatInterval) == 0) {
166 if (file.find(".defaults") != std::string::npos) {
167 val = it->second->heartBeat.defaultVal;
168 } else {
169 val = it->second->heartBeat.actualVal;
170 }
171 return ZE_RESULT_SUCCESS;
172 }
173 }
174 return ZE_RESULT_ERROR_NOT_AVAILABLE;
175 }
176
177 ze_result_t write(const std::string file, const uint64_t val) override {
178
179 if (mockWriteFileStatus != ZE_RESULT_SUCCESS) {
180 return mockWriteFileStatus;
181 }
182
183 SchedulerFileProperties fileProperties;
184 ze_result_t result = getFileProperties(file, fileProperties);
185 if (ZE_RESULT_SUCCESS == result) {
186 if (!fileProperties.getAvailability()) {
187 return ZE_RESULT_ERROR_NOT_AVAILABLE;
188 }
189 if (!fileProperties.hasMode(S_IWUSR)) {
190 return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
191 }
192 } else {
193 return ZE_RESULT_ERROR_UNKNOWN;
194 }
195
196 for (std::string mappedEngine : listOfMockedEngines) { // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
197 if (file.find(mappedEngine) == std::string::npos) {
198 continue;
199 }
200 SchedulerConfig_t *schedConfig = new SchedulerConfig_t();
201 if (file.compare((file.length() - preemptTimeout.length()),
202 preemptTimeout.length(),
203 preemptTimeout) == 0) {
204 if (file.find(".defaults") != std::string::npos) {
205 schedConfig->timeOut.defaultVal = val;
206 } else {
207 schedConfig->timeOut.actualVal = val;
208 }
209 auto ret = engineSchedMap.emplace(mappedEngine, schedConfig);
210 if (ret.second == false) {
211 auto itr = engineSchedMap.find(mappedEngine);
212 if (file.find(".defaults") != std::string::npos) {
213 itr->second->timeOut.defaultVal = val;
214 } else {
215 itr->second->timeOut.actualVal = val;
216 }
217 delete schedConfig;
218 }
219 return ZE_RESULT_SUCCESS;
220 }
221 if (file.compare((file.length() - timesliceDuration.length()),
222 timesliceDuration.length(),
223 timesliceDuration) == 0) {
224 if (file.find(".defaults") != std::string::npos) {
225 schedConfig->timeSclice.defaultVal = val;
226 } else {
227 schedConfig->timeSclice.actualVal = val;
228 }
229 auto ret = engineSchedMap.emplace(mappedEngine, schedConfig);
230 if (ret.second == false) {
231 auto itr = engineSchedMap.find(mappedEngine);
232 if (file.find(".defaults") != std::string::npos) {
233 itr->second->timeSclice.defaultVal = val;
234 } else {
235 itr->second->timeSclice.actualVal = val;
236 }
237 delete schedConfig;
238 }
239 return ZE_RESULT_SUCCESS;
240 }
241 if (file.compare((file.length() - heartbeatInterval.length()),
242 heartbeatInterval.length(),
243 heartbeatInterval) == 0) {
244 if (file.find(".defaults") != std::string::npos) {
245 schedConfig->heartBeat.defaultVal = val;
246 } else {
247 schedConfig->heartBeat.actualVal = val;
248 }
249 auto ret = engineSchedMap.emplace(mappedEngine, schedConfig);
250 if (ret.second == false) {
251 auto itr = engineSchedMap.find(mappedEngine);
252 if (file.find(".defaults") != std::string::npos) {
253 itr->second->heartBeat.defaultVal = val;
254 } else {
255 itr->second->heartBeat.actualVal = val;
256 }
257 delete schedConfig;
258 }
259 return ZE_RESULT_SUCCESS;
260 }
261 delete schedConfig;
262 }
263 return ZE_RESULT_ERROR_NOT_AVAILABLE;
264 }
265
266 ze_result_t scanDirEntries(const std::string file, std::vector<std::string> &listOfEntries) override {
267
268 if (mockGetScanDirEntryError != ZE_RESULT_SUCCESS) {
269 return mockGetScanDirEntryError;
270 }
271
272 if (!isDirectoryAccessible(engineDir)) {
273 return ZE_RESULT_ERROR_NOT_AVAILABLE;
274 }
275 if (!(engineDirectoryPermissions & S_IRUSR)) {
276 return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
277 }
278 listOfEntries = listOfMockedEngines;
279 return ZE_RESULT_SUCCESS;
280 }
281 ze_result_t getscanDirEntriesStatusReturnError(const std::string file, std::vector<std::string> &listOfEntries) {
282 return ZE_RESULT_ERROR_NOT_AVAILABLE;
283 }
284
285 void setEngineDirectoryPermission(::mode_t permission) {
286 engineDirectoryPermissions = permission;
287 }
288
289 protected:
290 MockSchedulerSysfsAccess() = default;
291
292 private:
293 ::mode_t engineDirectoryPermissions = S_IRUSR | S_IWUSR;
294 bool isDirectoryAccessible(const std::string dir) {
295 if (dir.compare(engineDir) == 0) {
296 return true;
297 }
298 return false;
299 }
300 };
301
302 struct MockSchedulerSysfsAccessI915 : MockSchedulerSysfsAccess {
303
304 MockSchedulerSysfsAccessI915() {
305 preemptTimeout = "preempt_timeout_ms";
306 defaultpreemptTimeout = ".defaults/preempt_timeout_ms";
307 timesliceDuration = "timeslice_duration_ms";
308 defaulttimesliceDuration = ".defaults/timeslice_duration_ms";
309 heartbeatInterval = "heartbeat_interval_ms";
310 defaultheartbeatInterval = ".defaults/heartbeat_interval_ms";
311 engineDir = "engine";
312 }
313 };
314
315 struct MockSchedulerSysfsAccessXe : MockSchedulerSysfsAccess {
316
317 std::string defaultMaximumJobTimeout = ".defaults/job_timeout_max";
318 SchedulerConfigValues_t defaultjobTimeout{};
319
320 MockSchedulerSysfsAccessXe() {
321 preemptTimeout = "preempt_timeout_us";
322 defaultpreemptTimeout = ".defaults/preempt_timeout_us";
323 timesliceDuration = "timeslice_duration_us";
324 defaulttimesliceDuration = ".defaults/timeslice_duration_us";
325 heartbeatInterval = "job_timeout_ms";
326 defaultheartbeatInterval = ".defaults/job_timeout_ms";
327 engineDir = "device/tile0/gt0/engines";
328 }
329
330 ze_result_t read(const std::string file, uint64_t &val) override {
331
332 auto status = MockSchedulerSysfsAccess::read(file, val);
333 if (status == ZE_RESULT_ERROR_NOT_AVAILABLE && mockReadReturnStatus == false) {
334 if (file.compare((file.length() - defaultMaximumJobTimeout.length()),
335 defaultMaximumJobTimeout.length(),
336 defaultMaximumJobTimeout) == 0) {
337 val = defaultjobTimeout.defaultVal;
338 return ZE_RESULT_SUCCESS;
339 }
340 }
341 return status;
342 }
343
344 ze_result_t write(const std::string file, const uint64_t val) override {
345 auto status = MockSchedulerSysfsAccess::write(file, val);
346 if (status == ZE_RESULT_ERROR_NOT_AVAILABLE && mockWriteFileStatus == false) {
347 if (file.compare((file.length() - defaultMaximumJobTimeout.length()),
348 defaultMaximumJobTimeout.length(),
349 defaultMaximumJobTimeout) == 0) {
350 defaultjobTimeout.defaultVal = val;
351 return ZE_RESULT_SUCCESS;
352 }
353 }
354 return status;
355 }
356 };
357
358 class PublicLinuxSchedulerImp : public L0::Sysman::LinuxSchedulerImp {
359 public:
360 using LinuxSchedulerImp::pSysfsAccess;
361 };
362
363 struct MockSchedulerNeoDrm : public Drm {
364 using Drm::getEngineInfo;
365 using Drm::setupIoctlHelper;
366 const int mockFd = 0;
367 MockSchedulerNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique<MockSysmanHwDeviceIdDrm>(mockFd, ""), rootDeviceEnvironment) {}
368
369 bool sysmanQueryEngineInfo() override {
370
371 StackVec<std::vector<EngineCapabilities>, 2> engineInfosPerTile;
372
373 for (uint32_t tileId = 0; tileId < 2; tileId++) {
374 std::vector<EngineCapabilities> engineInfo;
375 EngineClassInstance instance{};
376 instance.engineClass = DRM_XE_ENGINE_CLASS_RENDER;
377 instance.engineInstance = 0;
378 engineInfo.push_back({instance, {}});
379 instance.engineClass = DRM_XE_ENGINE_CLASS_RENDER;
380 instance.engineInstance = 1;
381 engineInfo.push_back({instance, {}});
382 instance.engineClass = DRM_XE_ENGINE_CLASS_COMPUTE;
383 instance.engineInstance = 0;
384 engineInfo.push_back({instance, {}});
385 instance.engineClass = DRM_XE_ENGINE_CLASS_COMPUTE;
386 instance.engineInstance = 1;
387 engineInfo.push_back({instance, {}});
388 instance.engineClass = DRM_XE_ENGINE_CLASS_COPY;
389 instance.engineInstance = 0;
390 engineInfo.push_back({instance, {}});
391 instance.engineClass = DRM_XE_ENGINE_CLASS_COPY;
392 instance.engineInstance = 1;
393 engineInfo.push_back({instance, {}});
394 instance.engineClass = DRM_XE_ENGINE_CLASS_VIDEO_DECODE;
395 instance.engineInstance = 0;
396 engineInfo.push_back({instance, {}});
397 instance.engineClass = DRM_XE_ENGINE_CLASS_VIDEO_DECODE;
398 instance.engineInstance = 1;
399 engineInfo.push_back({instance, {}});
400 instance.engineClass = DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE;
401 instance.engineInstance = 0;
402 engineInfo.push_back({instance, {}});
403 instance.engineClass = DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE;
404 instance.engineInstance = 1;
405 engineInfo.push_back({instance, {}});
406 engineInfosPerTile.push_back(engineInfo);
407 }
408
409 this->engineInfo.reset(new EngineInfo(this, engineInfosPerTile));
410 return true;
411 }
412
413 bool queryEngineInfoMockReturnFalse() {
414 return false;
415 }
416 void resetEngineInfo() {
417 engineInfo.reset();
418 }
419 };
420
421 } // namespace ult
422 } // namespace Sysman
423 } // namespace L0