Debian Codegraph @

level_zero/tools/test/unit_tests/sources/metrics/metric_ip_sampling_fixture.cpp

raw
1 /*
2 * Copyright (C) 2022-2026 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "level_zero/tools/test/unit_tests/sources/metrics/metric_ip_sampling_fixture.h"
9
10 #include "level_zero/core/source/context/context_imp.h"
11 #include "level_zero/core/source/device/device.h"
12 #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
13 #include "level_zero/tools/source/metrics/metric_ip_sampling_source.h"
14 #include "level_zero/tools/source/metrics/metric_oa_source.h"
15 #include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric_ip_sampling.h"
16 #include <level_zero/zet_api.h>
17
18 namespace L0 {
19 extern std::vector<_ze_driver_handle_t *> *globalDriverHandles;
20
21 namespace ult {
22
23 void MetricIpSamplingMultiDevFixture::SetUp() {
24
25 debugManager.flags.EnableImplicitScaling.set(1);
26
27 MultiDeviceFixture::numRootDevices = 1;
28 MultiDeviceFixture::numSubDevices = 2;
29 MultiDeviceFixture::setUp();
30 testDevices.reserve(MultiDeviceFixture::numRootDevices +
31 (MultiDeviceFixture::numRootDevices *
32 MultiDeviceFixture::numSubDevices));
33 for (auto device : driverHandle->devices) {
34 testDevices.push_back(device);
35 auto &l0Device = *device;
36 const uint32_t subDeviceCount = static_cast<uint32_t>(l0Device.subDevices.size());
37 for (uint32_t i = 0; i < subDeviceCount; i++) {
38 testDevices.push_back(l0Device.subDevices[i]);
39 }
40 }
41
42 osInterfaceVector.reserve(testDevices.size());
43 for (auto device : testDevices) {
44 auto mockMetricIpSamplingOsInterface = new MockMetricIpSamplingOsInterface();
45 osInterfaceVector.push_back(mockMetricIpSamplingOsInterface);
46 std::unique_ptr<MetricIpSamplingOsInterface> metricIpSamplingOsInterface = std::unique_ptr<MetricIpSamplingOsInterface>(mockMetricIpSamplingOsInterface);
47 auto &metricSource = device->getMetricDeviceContext().getMetricSource<IpSamplingMetricSourceImp>();
48 metricSource.metricCount = platformIpMetricCountXe;
49 metricSource.setMetricOsInterface(metricIpSamplingOsInterface);
50
51 auto &metricOaSource = device->getMetricDeviceContext().getMetricSource<OaMetricSourceImp>();
52 metricOaSource.setInitializationState(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
53 }
54 globalDriverHandles->push_back(driverHandle.get());
55 }
56
57 void MetricIpSamplingMultiDevFixture::TearDown() {
58 MultiDeviceFixture::tearDown();
59 globalDriverHandles->clear();
60 }
61
62 void MetricIpSamplingFixture::SetUp() {
63 DeviceFixture::setUp();
64 auto mockMetricIpSamplingOsInterface = new MockMetricIpSamplingOsInterface();
65 osInterfaceVector.push_back(mockMetricIpSamplingOsInterface);
66 std::unique_ptr<MetricIpSamplingOsInterface> metricIpSamplingOsInterface = std::unique_ptr<MetricIpSamplingOsInterface>(mockMetricIpSamplingOsInterface);
67
68 auto &metricSource = device->getMetricDeviceContext().getMetricSource<IpSamplingMetricSourceImp>();
69 metricSource.metricCount = platformIpMetricCountXe;
70 metricSource.setMetricOsInterface(metricIpSamplingOsInterface);
71
72 auto &metricOaSource = device->getMetricDeviceContext().getMetricSource<OaMetricSourceImp>();
73 metricOaSource.setInitializationState(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
74
75 globalDriverHandles->push_back(driverHandle.get());
76 }
77
78 void MetricIpSamplingFixture::TearDown() {
79 DeviceFixture::tearDown();
80 globalDriverHandles->clear();
81 }
82
83 void MetricIpSamplingCalculateBaseFixture::initRawReports() {
84 MockRawDataHelper::rawElementsToRawReports(static_cast<uint32_t>(rawDataElements.size()), rawDataElements, rawReports);
85 rawReportsBytesSize = sizeof(rawReports[0][0]) * rawReports[0].size() * rawReports.size();
86
87 MockRawDataHelper::rawElementsToRawReports(static_cast<uint32_t>(rawDataElementsOverflow.size()), rawDataElementsOverflow, rawReportsOverflow);
88 rawReportsBytesSizeOverflow = sizeof(rawReportsOverflow[0][0]) * rawReportsOverflow[0].size() * rawReportsOverflow.size();
89 }
90
91 void MetricIpSamplingCalculateBaseFixture::initCalHandles(L0::ContextImp *context,
92 L0::Device *device) {
93
94 uint32_t metricScopesCount = 0;
95 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
96 device->toHandle(),
97 &metricScopesCount,
98 nullptr));
99 scopesPerDevice[device].resize(metricScopesCount);
100 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
101 device->toHandle(),
102 &metricScopesCount,
103 scopesPerDevice[device].data()));
104
105 uint32_t metricGroupCount = 1;
106 zetMetricGroupGet(device->toHandle(), &metricGroupCount, &metricGroupHandlePerDevice[device]);
107
108 calcDescPerDevice[device].stype = ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP;
109 calcDescPerDevice[device].pNext = nullptr; // pNext
110 calcDescPerDevice[device].metricGroupCount = metricGroupCount; // metricGroupCount
111 calcDescPerDevice[device].phMetricGroups = &metricGroupHandlePerDevice[device]; // phMetricGroups
112 calcDescPerDevice[device].metricCount = 0; // metricCount
113 calcDescPerDevice[device].phMetrics = nullptr; // phMetrics
114 calcDescPerDevice[device].timeWindowsCount = 0; // timeWindowsCount
115 calcDescPerDevice[device].pCalculationTimeWindows = nullptr; // pCalculationTimeWindows
116 calcDescPerDevice[device].timeAggregationWindow = 0; // timeAggregationWindow
117 calcDescPerDevice[device].metricScopesCount = metricScopesCount; // metricScopesCount
118 calcDescPerDevice[device].phMetricScopes = scopesPerDevice[device].data(); // phMetricScopes
119 }
120
121 void MetricIpSamplingCalculateBaseFixture::cleanUpHandles() {
122 metricGroupHandlePerDevice.clear();
123 scopesPerDevice.clear();
124 calcDescPerDevice.clear();
125 }
126
127 void MetricIpSamplingCalculateMultiDevFixture::SetUp() {
128 MetricIpSamplingMultiDevFixture::SetUp();
129
130 for (auto device : testDevices) {
131 EXPECT_EQ(ZE_RESULT_SUCCESS, device->getMetricDeviceContext().enableMetricApi());
132 initCalHandles(context, device);
133 }
134 rootDevice = testDevices[0];
135 initRawReports();
136 }
137
138 void MetricIpSamplingCalculateMultiDevFixture::TearDown() {
139 MetricIpSamplingMultiDevFixture::TearDown();
140 rootDevice = nullptr;
141 cleanUpHandles();
142 }
143
144 void MetricIpSamplingCalculateSingleDevFixture::SetUp() {
145 MetricIpSamplingFixture::SetUp();
146 device->getMetricDeviceContext().enableMetricApi();
147
148 initCalHandles(context, device);
149 initRawReports();
150 }
151
152 void MetricIpSamplingCalculateSingleDevFixture::TearDown() {
153 MetricIpSamplingFixture::TearDown();
154 cleanUpHandles();
155 }
156
157 void MetricIpSamplingMetricsAggregationMultiDevFixture::initMultiRawReports() {
158
159 MockRawDataHelper::rawElementsToRawReports(static_cast<uint32_t>(rawDataElements2.size()), rawDataElements2, rawReports2);
160 rawReports2BytesSize = sizeof(rawReports2[0][0]) * rawReports2[0].size() * rawReports2.size();
161
162 MockRawDataHelper::rawElementsToRawReports(static_cast<uint32_t>(rawDataElementsAppend.size()), rawDataElementsAppend, rawReportsAppend);
163 rawReportsAppendBytesSize = sizeof(rawReportsAppend[0][0]) * rawReportsAppend[0].size() * rawReportsAppend.size();
164 }
165
166 void MetricIpSamplingMetricsAggregationMultiDevFixture::SetUp() {
167 MetricIpSamplingMultiDevFixture::SetUp();
168 initRawReports();
169 initMultiRawReports();
170
171 rootDevice = testDevices[0];
172 EXPECT_EQ(ZE_RESULT_SUCCESS, rootDevice->getMetricDeviceContext().enableMetricApi());
173
174 uint32_t metricGroupCount = 1;
175 EXPECT_EQ(ZE_RESULT_SUCCESS, zetMetricGroupGet(rootDevice->toHandle(), &metricGroupCount, &rootDevMetricGroupHandle));
176 EXPECT_EQ(metricGroupCount, 1u);
177 EXPECT_NE(rootDevMetricGroupHandle, nullptr);
178
179 zet_intel_metric_calculation_exp_desc_t calculationDesc{};
180 calculationDesc.stype = ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP;
181 calculationDesc.pNext = nullptr;
182 calculationDesc.metricGroupCount = 1;
183 calculationDesc.phMetricGroups = &rootDevMetricGroupHandle;
184 calculationDesc.metricCount = 0;
185 calculationDesc.phMetrics = nullptr;
186 calculationDesc.timeWindowsCount = 0;
187 calculationDesc.pCalculationTimeWindows = nullptr;
188 calculationDesc.timeAggregationWindow = 0;
189
190 zet_intel_metric_scope_properties_exp_t scopeProperties{};
191 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
192 scopeProperties.pNext = nullptr;
193
194 scopeProperties.iD = 0; // aggregated scope
195 mockMetricScopeAggregated = new MockMetricScope(scopeProperties, true, 0);
196 hMockScopeAggregated = mockMetricScopeAggregated->toHandle();
197
198 scopeProperties.iD = 1; // compute scope for sub-device 0, since aggregated scope is present, ID is sub-device index + 1
199 mockMetricScopeCompute0 = new MockMetricScope(scopeProperties, false, 0);
200 hMockScopeCompute0 = mockMetricScopeCompute0->toHandle();
201
202 scopeProperties.iD = 2; // compute scope for sub-device 1, since aggregated scope is present, ID is sub-device index + 1
203 mockMetricScopeCompute1 = new MockMetricScope(scopeProperties, false, 1);
204 hMockScopeCompute1 = mockMetricScopeCompute1->toHandle();
205
206 hComputeMetricScopes = {hMockScopeCompute0, hMockScopeCompute1};
207 hAllScopes = {hMockScopeAggregated, hMockScopeCompute0, hMockScopeCompute1};
208
209 // Override scopes for all metrics to include all scopes for testing purposes
210 uint32_t metricCount = 0;
211 EXPECT_EQ(zetMetricGet(rootDevMetricGroupHandle, &metricCount, nullptr), ZE_RESULT_SUCCESS);
212 std::vector<zet_metric_handle_t> hMetrics(metricCount);
213 EXPECT_EQ(zetMetricGet(rootDevMetricGroupHandle, &metricCount, hMetrics.data()), ZE_RESULT_SUCCESS);
214 for (auto &hMetric : hMetrics) {
215 auto metric = static_cast<MockMetric *>(Metric::fromHandle(hMetric));
216 metric->setScopes(hAllScopes);
217 }
218
219 // Calculation operation for sub-device 0, compute scope 1
220 calculationDesc.metricScopesCount = 1;
221 calculationDesc.phMetricScopes = &hMockScopeCompute0;
222 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
223 rootDevice->toHandle(), &calculationDesc,
224 &hCalcOpCompScope1));
225 EXPECT_NE(hCalcOpCompScope1, nullptr);
226 hCalcOps.push_back(hCalcOpCompScope1);
227
228 // Calculation operation for sub-device 1, compute scope 2
229 calculationDesc.phMetricScopes = &hMockScopeCompute1;
230 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
231 rootDevice->toHandle(), &calculationDesc,
232 &hCalcOpCompScope2));
233 EXPECT_NE(hCalcOpCompScope2, nullptr);
234 hCalcOps.push_back(hCalcOpCompScope2);
235
236 // Calculation operation for all subdevices
237 calculationDesc.metricScopesCount = 2;
238 calculationDesc.phMetricScopes = hComputeMetricScopes.data();
239 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
240 rootDevice->toHandle(), &calculationDesc,
241 &hCalcOpAllCompScopes));
242 EXPECT_NE(hCalcOpAllCompScopes, nullptr);
243 hCalcOps.push_back(hCalcOpAllCompScopes);
244
245 // Calculation operation for only aggregated scope
246 calculationDesc.metricScopesCount = 1;
247 calculationDesc.phMetricScopes = &hMockScopeAggregated;
248 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
249 rootDevice->toHandle(), &calculationDesc,
250 &hCalcOpAggScope));
251 EXPECT_NE(hCalcOpAggScope, nullptr);
252 hCalcOps.push_back(hCalcOpAggScope);
253
254 // Calculation operation for all scopes aggregated and two compute
255 calculationDesc.metricScopesCount = 3;
256 calculationDesc.phMetricScopes = hAllScopes.data();
257 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
258 rootDevice->toHandle(), &calculationDesc,
259 &hCalcOpAllScopes));
260 EXPECT_NE(hCalcOpAllScopes, nullptr);
261 hCalcOps.push_back(hCalcOpAllScopes);
262 }
263
264 void MetricIpSamplingMetricsAggregationMultiDevFixture::TearDown() {
265 MetricIpSamplingMultiDevFixture::TearDown();
266 hCalcOps.clear();
267
268 rootDevice = nullptr;
269 delete mockMetricScopeAggregated;
270 mockMetricScopeAggregated = nullptr;
271 hMockScopeAggregated = nullptr;
272 delete mockMetricScopeCompute0;
273 mockMetricScopeCompute0 = nullptr;
274 hMockScopeCompute0 = nullptr;
275 delete mockMetricScopeCompute1;
276 mockMetricScopeCompute1 = nullptr;
277 hMockScopeCompute1 = nullptr;
278
279 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalcOpCompScope1));
280 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalcOpCompScope2));
281 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalcOpAllCompScopes));
282 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalcOpAggScope));
283 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalcOpAllScopes));
284 }
285
286 } // namespace ult
287 } // namespace L0