Debian Codegraph @

level_zero/tools/test/unit_tests/sources/sysman/linux/pmt/test_pmt.cpp

raw
1 /*
2 * Copyright (C) 2021-2023 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h"
9
10 #include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h"
11
12 #include "mock_pmt.h"
13
14 extern bool sysmanUltsEnable;
15
16 namespace L0 {
17 namespace ult {
18
19 const std::map<std::string, uint64_t> dummyKeyOffsetMap = {
20 {"DUMMY_KEY", 0x0}};
21
22 class ZesPmtFixtureMultiDevice : public SysmanMultiDeviceFixture {
23 protected:
24 std::vector<ze_device_handle_t> deviceHandles;
25 std::unique_ptr<MockPmtFsAccess> pTestFsAccess;
26 std::map<uint32_t, L0::PlatformMonitoringTech *> mapOfSubDeviceIdToPmtObject;
27 void SetUp() override {
28 if (!sysmanUltsEnable) {
29 GTEST_SKIP();
30 }
31 SysmanMultiDeviceFixture::SetUp();
32 uint32_t subDeviceCount = 0;
33 Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr);
34 if (subDeviceCount == 0) {
35 deviceHandles.resize(1, device->toHandle());
36 } else {
37 deviceHandles.resize(subDeviceCount, nullptr);
38 Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
39 }
40 pTestFsAccess = std::make_unique<MockPmtFsAccess>();
41 PlatformMonitoringTech::create(deviceHandles, pTestFsAccess.get(), gpuUpstreamPortPathInPmt, mapOfSubDeviceIdToPmtObject);
42 }
43 void TearDown() override {
44 if (!sysmanUltsEnable) {
45 GTEST_SKIP();
46 }
47 SysmanMultiDeviceFixture::TearDown();
48 for (auto &subDeviceIdToPmtEntry : mapOfSubDeviceIdToPmtObject) {
49 delete subDeviceIdToPmtEntry.second;
50 subDeviceIdToPmtEntry.second = nullptr;
51 }
52 }
53 };
54
55 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenValidPmtHandlesForAllSubdevicesWillBeCreated) {}
56
57 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenenumerateRootTelemIndexThenCheckForErrorIflistDirectoryFails) {
58 pTestFsAccess->listDirectoryResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
59 EXPECT_EQ(ZE_RESULT_ERROR_NOT_AVAILABLE, PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt));
60 }
61
62 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenenumerateRootTelemIndexThenCheckForErrorIfgetRealPathFails) {
63 pTestFsAccess->getRealPathResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
64 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt));
65 }
66
67 TEST_F(ZesPmtFixtureMultiDevice, GivenWhenenumerateRootTelemIndexThenCheckForErrorIfgetRealPathSuccessButNoTelemetryNodeAndGPUDeviceShareRootPciPort) {
68 pTestFsAccess->returnInvalidRealPath = true;
69 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt));
70 }
71
72 TEST_F(ZesPmtFixtureMultiDevice, GivenTelemDirectoryContainNowTelemEntryWhenenumerateRootTelemIndexThenCheckForError) {
73 pTestFsAccess->returnTelemNodes = false;
74 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt));
75 }
76
77 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenCheckForErrorThatCouldHappenDuringWhileValidatingTelemNode) {
78 pTestFsAccess->getRealPathResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
79 PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt);
80 auto productFamily = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getHardwareInfo().platform.eProductFamily;
81 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
82 EXPECT_EQ(pPmt->init(pTestFsAccess.get(), gpuUpstreamPortPathInPmt, productFamily), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE);
83 }
84
85 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenCheckForErrorThatCouldHappenDuringGUIDRead) {
86 pTestFsAccess->readStringResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
87 PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt);
88 auto productFamily = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getHardwareInfo().platform.eProductFamily;
89 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
90 EXPECT_EQ(pPmt->init(pTestFsAccess.get(), gpuUpstreamPortPathInPmt, productFamily), ZE_RESULT_ERROR_NOT_AVAILABLE);
91 }
92
93 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenCheckForErrorIfGUIDReadValueIsNotSupported) {
94 pTestFsAccess->readInvalidString = true;
95 PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt);
96 auto productFamily = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getHardwareInfo().platform.eProductFamily;
97 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
98 EXPECT_EQ(pPmt->init(pTestFsAccess.get(), gpuUpstreamPortPathInPmt, productFamily), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
99 }
100
101 TEST_F(ZesPmtFixtureMultiDevice, GivenSomeKeyWhenCallingreadValueWithUint64TypeThenCheckForErrorBranches) {
102 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 0, 0);
103 uint64_t val = 0;
104
105 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, pPmt->readValue("SOMETHING", val));
106 }
107
108 TEST_F(ZesPmtFixtureMultiDevice, GivenSomeKeyWhenCallingreadValueWithUint32TypeThenCheckForErrorBranches) {
109 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 0, 0);
110 uint32_t val = 0;
111
112 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, pPmt->readValue("SOMETHING", val));
113 }
114
115 TEST_F(ZesPmtFixtureMultiDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenCheckForErrorThatCouldHappenDuringbaseOffsetRead) {
116 pTestFsAccess->readUnsignedResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
117 PlatformMonitoringTech::enumerateRootTelemIndex(pTestFsAccess.get(), gpuUpstreamPortPathInPmt);
118 auto productFamily = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getHardwareInfo().platform.eProductFamily;
119 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
120 EXPECT_EQ(pPmt->init(pTestFsAccess.get(), gpuUpstreamPortPathInPmt, productFamily), ZE_RESULT_ERROR_NOT_AVAILABLE);
121 }
122
123 inline static int openMockReturnFailure(const char *pathname, int flags) {
124 return -1;
125 }
126
127 inline static int openMockReturnSuccess(const char *pathname, int flags) {
128 NEO::SysCalls::closeFuncCalled = 0;
129 return 0;
130 }
131
132 ssize_t preadMockPmt(int fd, void *buf, size_t count, off_t offset) {
133 EXPECT_EQ(0u, NEO::SysCalls::closeFuncCalled);
134 *reinterpret_cast<uint32_t *>(buf) = 3u;
135 return count;
136 }
137
138 ssize_t preadMockPmt64(int fd, void *buf, size_t count, off_t offset) {
139 EXPECT_EQ(0u, NEO::SysCalls::closeFuncCalled);
140 *reinterpret_cast<uint64_t *>(buf) = 5u;
141 return count;
142 }
143
144 ssize_t preadMockPmtFailure(int fd, void *buf, size_t count, off_t offset) {
145 return -1;
146 }
147
148 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingreadValueWithUint32TypeAndOpenSysCallFailsThenreadValueFails) {
149 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
150 VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnFailure);
151 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> preadBackup(&NEO::SysCalls::sysCallsPread, preadMockPmt);
152
153 uint32_t val = 0;
154 pPmt->keyOffsetMap = dummyKeyOffsetMap;
155 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, pPmt->readValue("DUMMY_KEY", val));
156 }
157
158 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingReadValueWithUint32TypeThenSuccessIsReturned) {
159 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
160 VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnSuccess);
161 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> preadBackup(&NEO::SysCalls::sysCallsPread, preadMockPmt);
162
163 uint32_t val = 0;
164 pPmt->keyOffsetMap = dummyKeyOffsetMap;
165 EXPECT_EQ(ZE_RESULT_SUCCESS, pPmt->readValue("DUMMY_KEY", val));
166 EXPECT_EQ(val, 3u);
167 }
168
169 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingReadValueWithUint64TypeThenSuccessIsReturned) {
170 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
171 VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnSuccess);
172 VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> preadBackup(&NEO::SysCalls::sysCallsPread, preadMockPmt64);
173
174 uint64_t val = 0;
175 pPmt->keyOffsetMap = dummyKeyOffsetMap;
176 EXPECT_EQ(ZE_RESULT_SUCCESS, pPmt->readValue("DUMMY_KEY", val));
177 EXPECT_EQ(val, 5u);
178 }
179
180 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingreadValueWithUint64TypeAndOpenSysCallFailsThenreadValueFails) {
181 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
182 VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnFailure);
183
184 uint64_t val = 0;
185 pPmt->keyOffsetMap = dummyKeyOffsetMap;
186 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, pPmt->readValue("DUMMY_KEY", val));
187 }
188
189 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingreadValueWithUint32TypeAndPreadSysCallFailsThenreadValueFails) {
190 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
191 pPmt->telemetryDeviceEntry = baseTelemSysFS + "/" + telemNodeForSubdevice0 + "/" + telem;
192 pPmt->preadFunction = preadMockPmtFailure;
193
194 uint32_t val = 0;
195 pPmt->keyOffsetMap = dummyKeyOffsetMap;
196 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, pPmt->readValue("DUMMY_KEY", val));
197 }
198
199 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenCallingreadValueWithUint64TypeAndPreadSysCallFailsThenreadValueFails) {
200 auto pPmt = std::make_unique<PublicPlatformMonitoringTech>(pTestFsAccess.get(), 1, 0);
201 pPmt->telemetryDeviceEntry = baseTelemSysFS + "/" + telemNodeForSubdevice0 + "/" + telem;
202 pPmt->preadFunction = preadMockPmtFailure;
203
204 uint64_t val = 0;
205 pPmt->keyOffsetMap = dummyKeyOffsetMap;
206 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, pPmt->readValue("DUMMY_KEY", val));
207 }
208
209 TEST_F(ZesPmtFixtureMultiDevice, GivenValidSyscallsWhenDoingPMTInitThenPMTmapOfSubDeviceIdToPmtObjectWouldContainValidEntries) {
210 std::map<uint32_t, L0::PlatformMonitoringTech *> mapOfSubDeviceIdToPmtObject;
211 for (const auto &deviceHandle : deviceHandles) {
212 ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
213 Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
214 auto pPmt = new PublicPlatformMonitoringTech(pTestFsAccess.get(), deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE,
215 deviceProperties.subdeviceId);
216 auto productFamily = SysmanDeviceImp::getProductFamily(Device::fromHandle(deviceHandle));
217 UNRECOVERABLE_IF(nullptr == pPmt);
218 PublicPlatformMonitoringTech::doInitPmtObject(pTestFsAccess.get(), deviceProperties.subdeviceId, pPmt,
219 gpuUpstreamPortPathInPmt, mapOfSubDeviceIdToPmtObject, productFamily);
220 auto subDeviceIdToPmtEntry = mapOfSubDeviceIdToPmtObject.find(deviceProperties.subdeviceId);
221 EXPECT_EQ(subDeviceIdToPmtEntry->second, pPmt);
222 delete pPmt;
223 }
224 }
225
226 TEST_F(ZesPmtFixtureMultiDevice, GivenBaseOffsetReadFailWhenDoingPMTInitThenPMTmapOfSubDeviceIdToPmtObjectWouldBeEmpty) {
227 std::map<uint32_t, L0::PlatformMonitoringTech *> mapOfSubDeviceIdToPmtObject;
228 pTestFsAccess->readUnsignedResult = ZE_RESULT_ERROR_NOT_AVAILABLE;
229 for (const auto &deviceHandle : deviceHandles) {
230 ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
231 Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
232 auto pPmt = new PublicPlatformMonitoringTech(pTestFsAccess.get(), deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE,
233 deviceProperties.subdeviceId);
234 auto productFamily = SysmanDeviceImp::getProductFamily(Device::fromHandle(deviceHandle));
235 UNRECOVERABLE_IF(nullptr == pPmt);
236 PublicPlatformMonitoringTech::doInitPmtObject(pTestFsAccess.get(), deviceProperties.subdeviceId, pPmt,
237 gpuUpstreamPortPathInPmt, mapOfSubDeviceIdToPmtObject, productFamily);
238 EXPECT_TRUE(mapOfSubDeviceIdToPmtObject.empty());
239 }
240 }
241
242 TEST_F(ZesPmtFixtureMultiDevice, GivenNoPMTHandleInmapOfSubDeviceIdToPmtObjectWhenCallingreleasePmtObjectThenMapWouldGetEmpty) {
243 auto mapOriginal = pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject;
244 pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.clear();
245 for (const auto &deviceHandle : deviceHandles) {
246 ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
247 Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
248 pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.emplace(deviceProperties.subdeviceId, nullptr);
249 }
250 pLinuxSysmanImp->releasePmtObject();
251 EXPECT_TRUE(pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.empty());
252 pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject = mapOriginal;
253 }
254
255 class ZesPmtFixtureNoSubDevice : public SysmanDeviceFixture {
256 protected:
257 std::vector<ze_device_handle_t> deviceHandles;
258 std::unique_ptr<MockPmtFsAccess> pTestFsAccess;
259 std::map<uint32_t, L0::PlatformMonitoringTech *> mapOfSubDeviceIdToPmtObject;
260 void SetUp() override {
261 if (!sysmanUltsEnable) {
262 GTEST_SKIP();
263 }
264 SysmanDeviceFixture::SetUp();
265 uint32_t subDeviceCount = 0;
266 Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr);
267 if (subDeviceCount == 0) {
268 deviceHandles.resize(1, device->toHandle());
269 } else {
270 deviceHandles.resize(subDeviceCount, nullptr);
271 Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
272 }
273 pTestFsAccess = std::make_unique<MockPmtFsAccess>();
274 PlatformMonitoringTech::create(deviceHandles, pTestFsAccess.get(), gpuUpstreamPortPathInPmt, mapOfSubDeviceIdToPmtObject);
275 }
276 void TearDown() override {
277 if (!sysmanUltsEnable) {
278 GTEST_SKIP();
279 }
280 SysmanDeviceFixture::TearDown();
281 for (auto &subDeviceIdToPmtEntry : mapOfSubDeviceIdToPmtObject) {
282 delete subDeviceIdToPmtEntry.second;
283 subDeviceIdToPmtEntry.second = nullptr;
284 }
285 }
286 };
287
288 TEST_F(ZesPmtFixtureNoSubDevice, GivenValidDeviceHandlesWhenCreatingPMTHandlesThenValidPmtHandlesForAllSubdevicesWillBeCreated) {}
289
290 } // namespace ult
291 } // namespace L0