Debian Codegraph @

level_zero/sysman/test/unit_tests/sources/engine/linux/test_zes_engine.cpp

raw
1 /*
2 * Copyright (C) 2020-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/os_interface/linux/memory_info.h"
9
10 #include "level_zero/sysman/source/shared/linux/kmd_interface/sysman_kmd_interface.h"
11 #include "level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h"
12 #include "level_zero/sysman/test/unit_tests/sources/engine/linux/mock_engine.h"
13 #include "level_zero/sysman/test/unit_tests/sources/linux/mock_sysman_fixture.h"
14
15 namespace L0 {
16 namespace Sysman {
17 namespace ult {
18
19 constexpr uint32_t handleComponentCount = 6u;
20 constexpr uint32_t microSecondsToNanoSeconds = 1000u;
21
22 class ZesEngineFixture : public SysmanDeviceFixture {
23 protected:
24 L0::Sysman::SysmanDevice *device = nullptr;
25 };
26
27 class ZesEngineFixtureI915 : public ZesEngineFixture {
28 protected:
29 std::unique_ptr<MockEnginePmuInterfaceImp> pPmuInterface;
30 L0::Sysman::PmuInterface *pOriginalPmuInterface = nullptr;
31
32 void SetUp() override {
33 SysmanDeviceFixture::SetUp();
34 VariableBackup<decltype(NEO::SysCalls::sysCallsReadlink)> mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int {
35 std::string str = "../../devices/pci0000:37/0000:37:01.0/0000:38:00.0/0000:39:01.0/0000:3a:00.0/drm/renderD128";
36 std::memcpy(buf, str.c_str(), str.size());
37 return static_cast<int>(str.size());
38 });
39 MockEngineNeoDrm *pDrm = new MockEngineNeoDrm(const_cast<NEO::RootDeviceEnvironment &>(pSysmanDeviceImp->getRootDeviceEnvironment()));
40 pDrm->setupIoctlHelper(pSysmanDeviceImp->getRootDeviceEnvironment().getHardwareInfo()->platform.eProductFamily);
41 auto &osInterface = pSysmanDeviceImp->getRootDeviceEnvironment().osInterface;
42 osInterface->setDriverModel(std::unique_ptr<MockEngineNeoDrm>(pDrm));
43
44 pLinuxSysmanImp->pSysmanKmdInterface.reset(new SysmanKmdInterfaceI915Upstream(pLinuxSysmanImp->getSysmanProductHelper()));
45 pLinuxSysmanImp->pSysmanKmdInterface->initFsAccessInterface(*pDrm);
46
47 pPmuInterface = std::make_unique<MockEnginePmuInterfaceImp>(pLinuxSysmanImp);
48 pPmuInterface->mockPmuFd = 10;
49 pPmuInterface->mockActiveTime = 987654321;
50 pPmuInterface->mockTimestamp = 87654321;
51 pOriginalPmuInterface = pLinuxSysmanImp->pPmuInterface;
52 pPmuInterface->pSysmanKmdInterface = pLinuxSysmanImp->pSysmanKmdInterface.get();
53 pLinuxSysmanImp->pPmuInterface = pPmuInterface.get();
54
55 pSysmanDeviceImp->pEngineHandleContext->handleList.clear();
56 pSysmanDeviceImp->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.isIntegratedDevice = true;
57 device = pSysmanDevice;
58 getEngineHandles(0);
59 }
60
61 void TearDown() override {
62 pLinuxSysmanImp->pPmuInterface = pOriginalPmuInterface;
63 SysmanDeviceFixture::TearDown();
64 }
65
66 std::vector<zes_engine_handle_t> getEngineHandles(uint32_t count) {
67
68 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
69 uint32_t mockReadVal = 23;
70 std::ostringstream oStream;
71 oStream << mockReadVal;
72 std::string value = oStream.str();
73 memcpy(buf, value.data(), count);
74 return count;
75 });
76
77 std::vector<zes_engine_handle_t> handles(count, nullptr);
78 EXPECT_EQ(zesDeviceEnumEngineGroups(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
79 return handles;
80 }
81 };
82
83 TEST_F(ZesEngineFixtureI915, GivenComponentCountZeroWhenCallingzesDeviceEnumEngineGroupsThenNonZeroCountIsReturnedAndVerifyCallSucceeds) {
84
85 uint32_t count = 0;
86 EXPECT_EQ(ZE_RESULT_SUCCESS, zesDeviceEnumEngineGroups(device->toHandle(), &count, NULL));
87 EXPECT_EQ(count, handleComponentCount);
88
89 uint32_t testcount = count + 1;
90 EXPECT_EQ(ZE_RESULT_SUCCESS, zesDeviceEnumEngineGroups(device->toHandle(), &testcount, NULL));
91 EXPECT_EQ(testcount, count);
92
93 count = 0;
94 std::vector<zes_engine_handle_t> handles(count, nullptr);
95 EXPECT_EQ(zesDeviceEnumEngineGroups(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
96 EXPECT_EQ(count, handleComponentCount);
97 }
98
99 TEST_F(ZesEngineFixtureI915, GivenValidEngineHandlesWhenCallingZesEngineGetPropertiesThenVerifyCallSucceeds) {
100 zes_engine_properties_t properties;
101 auto handle = getEngineHandles(handleComponentCount);
102
103 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[0], &properties));
104 EXPECT_EQ(ZES_ENGINE_GROUP_RENDER_SINGLE, properties.type);
105 EXPECT_FALSE(properties.onSubdevice);
106
107 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[1], &properties));
108 EXPECT_EQ(ZES_ENGINE_GROUP_RENDER_SINGLE, properties.type);
109 EXPECT_FALSE(properties.onSubdevice);
110
111 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[2], &properties));
112 EXPECT_EQ(ZES_ENGINE_GROUP_MEDIA_DECODE_SINGLE, properties.type);
113 EXPECT_FALSE(properties.onSubdevice);
114
115 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[3], &properties));
116 EXPECT_EQ(ZES_ENGINE_GROUP_MEDIA_ENCODE_SINGLE, properties.type);
117 EXPECT_FALSE(properties.onSubdevice);
118
119 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[4], &properties));
120 EXPECT_EQ(ZES_ENGINE_GROUP_COPY_SINGLE, properties.type);
121 EXPECT_FALSE(properties.onSubdevice);
122
123 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetProperties(handle[5], &properties));
124 EXPECT_EQ(ZES_ENGINE_GROUP_MEDIA_ENHANCEMENT_SINGLE, properties.type);
125 EXPECT_FALSE(properties.onSubdevice);
126 }
127
128 TEST_F(ZesEngineFixtureI915, GivenValidEngineHandleAndIntegratedDeviceWhenCallingZesEngineGetActivityThenVerifyCallReturnsSuccess) {
129 zes_engine_stats_t stats = {};
130 auto handles = getEngineHandles(handleComponentCount);
131 EXPECT_EQ(handleComponentCount, handles.size());
132
133 for (auto handle : handles) {
134 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetActivity(handle, &stats));
135 EXPECT_EQ(pPmuInterface->mockActiveTime / microSecondsToNanoSeconds, stats.activeTime);
136 EXPECT_EQ(pPmuInterface->mockTimestamp / microSecondsToNanoSeconds, stats.timestamp);
137 }
138 }
139
140 TEST_F(ZesEngineFixtureI915, GivenValidEngineHandleAndDiscreteDeviceWhenCallingZesEngineGetActivityThenVerifyCallReturnsSuccess) {
141 pSysmanDeviceImp->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.isIntegratedDevice = false;
142 zes_engine_stats_t stats = {};
143 auto handles = getEngineHandles(handleComponentCount);
144 EXPECT_EQ(handleComponentCount, handles.size());
145
146 for (auto handle : handles) {
147 EXPECT_EQ(ZE_RESULT_SUCCESS, zesEngineGetActivity(handle, &stats));
148 EXPECT_EQ(pPmuInterface->mockActiveTime / microSecondsToNanoSeconds, stats.activeTime);
149 EXPECT_EQ(pPmuInterface->mockTimestamp / microSecondsToNanoSeconds, stats.timestamp);
150 }
151 }
152
153 TEST_F(ZesEngineFixtureI915, GivenTestDiscreteDevicesAndValidEngineHandleWhenCallingZesEngineGetActivityAndPMUGetEventTypeFailsThenVerifyEngineGetActivityReturnsFailure) {
154
155 VariableBackup<decltype(NEO::SysCalls::sysCallsReadlink)> mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int {
156 return -1;
157 });
158
159 pSysmanDeviceImp->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.isIntegratedDevice = false;
160 auto pOsEngineTest1 = L0::Sysman::OsEngine::create(pOsSysman, ZES_ENGINE_GROUP_RENDER_SINGLE, 0u, 0u, false);
161
162 zes_engine_stats_t stats = {};
163 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, pOsEngineTest1->getActivity(&stats));
164
165 auto pOsEngineTest2 = L0::Sysman::OsEngine::create(pOsSysman, ZES_ENGINE_GROUP_RENDER_SINGLE, 0u, 0u, false);
166 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, pOsEngineTest2->getActivity(&stats));
167 }
168
169 TEST_F(ZesEngineFixtureI915, GivenTestIntegratedDevicesAndValidEngineHandleWhenCallingZesEngineGetActivityAndPMUGetEventTypeFailsThenVerifyEngineGetActivityReturnsFailure) {
170
171 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
172 return -1;
173 });
174
175 zes_engine_stats_t stats = {};
176 auto pOsEngineTest1 = L0::Sysman::OsEngine::create(pOsSysman, ZES_ENGINE_GROUP_RENDER_SINGLE, 0u, 0u, false);
177 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, pOsEngineTest1->getActivity(&stats));
178 }
179
180 TEST_F(ZesEngineFixtureI915, GivenValidEngineHandleWhenCallingZesEngineGetActivityAndPmuReadFailsThenVerifyEngineGetActivityReturnsFailure) {
181 pPmuInterface->mockPmuReadFailureReturnValue = -1;
182 zes_engine_stats_t stats = {};
183 auto handles = getEngineHandles(handleComponentCount);
184 EXPECT_EQ(handleComponentCount, handles.size());
185
186 for (auto handle : handles) {
187 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesEngineGetActivity(handle, &stats));
188 }
189 }
190
191 TEST_F(ZesEngineFixtureI915, GivenValidEngineHandleWhenCallingZesEngineGetActivityAndperfEventOpenFailsThenVerifyEngineGetActivityReturnsFailure) {
192
193 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
194 uint32_t mockReadVal = 23;
195 std::ostringstream oStream;
196 oStream << mockReadVal;
197 std::string value = oStream.str();
198 memcpy(buf, value.data(), count);
199 return count;
200 });
201
202 pPmuInterface->mockPerfEventFailureReturnValue = -1;
203 EXPECT_EQ(-1, pPmuInterface->pmuInterfaceOpen(0, -1, 0));
204 }
205
206 TEST_F(ZesEngineFixtureI915, GivenValidOsSysmanPointerWhenRetrievingEngineTypeAndInstancesAndIfEngineInfoQueryFailsThenErrorIsReturned) {
207 std::set<std::pair<zes_engine_group_t, L0::Sysman::EngineInstanceSubDeviceId>> engineGroupInstance;
208
209 auto &osInterface = pSysmanDeviceImp->getRootDeviceEnvironment().osInterface;
210 auto *pDrm = osInterface->getDriverModel()->as<MockEngineNeoDrm>();
211 pDrm->mockSysmanQueryEngineInfoReturnFalse = false;
212
213 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, L0::Sysman::OsEngine::getNumEngineTypeAndInstances(engineGroupInstance, pOsSysman));
214 }
215
216 TEST_F(ZesEngineFixtureI915, givenEngineInfoQuerySupportedWhenQueryingEngineInfoThenEngineInfoIsCreatedWithEngines) {
217 auto drm = std::make_unique<DrmMockEngine>((const_cast<NEO::RootDeviceEnvironment &>(pSysmanDeviceImp->getRootDeviceEnvironment())));
218 ASSERT_NE(nullptr, drm);
219 std::vector<MemoryRegion> memRegions{
220 {{drm_i915_gem_memory_class::I915_MEMORY_CLASS_SYSTEM, 0}, 1024, 0}};
221 drm->memoryInfo.reset(new MemoryInfo(memRegions, *drm));
222 drm->sysmanQueryEngineInfo();
223 auto engineInfo = drm->getEngineInfo();
224 ASSERT_NE(nullptr, engineInfo);
225 EXPECT_EQ(2u, engineInfo->getEngineInfos().size());
226 }
227
228 TEST_F(ZesEngineFixtureI915, GivenEngineInfoWithVideoQuerySupportedWhenQueryingEngineInfoWithVideoThenEngineInfoIsCreatedWithEngines) {
229 auto drm = std::make_unique<DrmMockEngine>((const_cast<NEO::RootDeviceEnvironment &>(pSysmanDeviceImp->getRootDeviceEnvironment())));
230 ASSERT_NE(nullptr, drm);
231 std::vector<MemoryRegion> memRegions{
232 {{drm_i915_gem_memory_class::I915_MEMORY_CLASS_SYSTEM, 0}, 1024, 0}};
233 drm->memoryInfo.reset(new MemoryInfo(memRegions, *drm));
234 drm->sysmanQueryEngineInfo();
235 auto engineInfo = drm->getEngineInfo();
236 ASSERT_NE(nullptr, engineInfo);
237 EXPECT_EQ(2u, engineInfo->getEngineInfos().size());
238 }
239
240 TEST_F(ZesEngineFixtureI915, GivenEngineInfoWithVideoQueryFailsThenFailureIsReturned) {
241 auto drm = std::make_unique<DrmMockEngineInfoFailing>((const_cast<NEO::RootDeviceEnvironment &>(pSysmanDeviceImp->getRootDeviceEnvironment())));
242 ASSERT_NE(nullptr, drm);
243 EXPECT_FALSE(drm->sysmanQueryEngineInfo());
244 }
245
246 } // namespace ult
247 } // namespace Sysman
248 } // namespace L0