Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2024-2026 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/test/common/test_macros/hw_test.h"
9 #include "shared/test/common/test_macros/test.h"
10
11 #include "level_zero/core/source/context/context_imp.h"
12 #include "level_zero/core/source/device/device.h"
13 #include "level_zero/core/source/gfx_core_helpers/l0_gfx_core_helper.h"
14 #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
15 #include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric_source.h"
16 #include "level_zero/zet_api.h"
17 #include "level_zero/zet_intel_gpu_metric.h"
18
19 #include "gtest/gtest.h"
20
21 namespace L0 {
22 namespace ult {
23
24 TEST(MultidevMetric, GivenMultideviceMetricCreatedThenReferenceIsUpdatedSuccessfully) {
25 MockMetricSource mockMetricSource{};
26 zet_intel_metric_scope_properties_exp_t scopeProperties{};
27 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
28 scopeProperties.pNext = nullptr;
29 MockMetricScope mockMetricScope(scopeProperties, false, 0);
30 std::vector<MetricScopeImp *> mockMetricScopes{&mockMetricScope};
31 MockMetric mockMetric(mockMetricSource, mockMetricScopes);
32
33 std::vector<MetricImp *> subDeviceMetrics;
34 subDeviceMetrics.push_back(&mockMetric);
35
36 MultiDeviceMetricImp *multiDevMetric = MultiDeviceMetricImp::create(mockMetricSource, subDeviceMetrics, mockMetricScopes);
37 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, multiDevMetric->destroy());
38
39 zet_metric_properties_t properties;
40 EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, multiDevMetric->getProperties(&properties));
41
42 MetricImp *metric = multiDevMetric->getMetricAtSubDeviceIndex(0);
43 EXPECT_EQ(metric, &mockMetric);
44 MultiDeviceMetricImp *multiDevMetricGet = mockMetric.getRootDevMetric();
45 EXPECT_EQ(multiDevMetricGet, multiDevMetric);
46
47 // asking for an index beyond the size of submetrics array should be handled
48 metric = multiDevMetric->getMetricAtSubDeviceIndex(2);
49 EXPECT_EQ(metric, nullptr);
50
51 delete multiDevMetric;
52 }
53
54 class CalcOperationFixture : public DeviceFixture,
55 public ::testing::Test {
56 protected:
57 void SetUp() override;
58 void TearDown() override;
59 MockMetricSource mockMetricSource{};
60 MockMetricGroup *mockMetricGroup;
61 zet_metric_group_handle_t hMetricGroup = nullptr;
62
63 MockMetricScope *mockMetricScope;
64 zet_intel_metric_scope_exp_handle_t hMetricScope = nullptr;
65 std::vector<MetricScopeImp *> mockMetricScopes{};
66
67 DebugManagerStateRestore restorer;
68 };
69
70 void CalcOperationFixture::SetUp() {
71 DeviceFixture::setUp();
72 mockMetricGroup = new MockMetricGroup(mockMetricSource);
73 hMetricGroup = mockMetricGroup->toHandle();
74
75 zet_intel_metric_scope_properties_exp_t scopeProperties{};
76 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
77 scopeProperties.pNext = nullptr;
78
79 mockMetricScope = new MockMetricScope(scopeProperties, false, 0);
80 mockMetricScopes.push_back(mockMetricScope);
81 hMetricScope = mockMetricScope->toHandle();
82 }
83
84 void CalcOperationFixture::TearDown() {
85 DeviceFixture::tearDown();
86 delete mockMetricGroup;
87 delete mockMetricScope;
88 hMetricGroup = nullptr;
89 hMetricScope = nullptr;
90 mockMetricScopes.clear();
91 }
92
93 class MetricScopesMultiDeviceFixture : public MultiDeviceFixture,
94 public ::testing::Test {
95 protected:
96 void SetUp() override;
97 void TearDown() override;
98
99 L0::Device *rootDevice = nullptr;
100 L0::Device *subDevice = nullptr;
101 MockMetricDeviceContext *mockRootDeviceContext = nullptr;
102 MockMetricSource *mockRootMetricSource = nullptr;
103
104 MockMetricDeviceContext *mockSubDeviceContext = nullptr;
105 MockMetricSource *mockSubMetricSource = nullptr;
106 DebugManagerStateRestore restorer;
107 };
108
109 void MetricScopesMultiDeviceFixture::SetUp() {
110
111 debugManager.flags.EnableImplicitScaling.set(1);
112
113 MultiDeviceFixture::numRootDevices = 1;
114 MultiDeviceFixture::numSubDevices = 2;
115
116 MultiDeviceFixture::setUp();
117
118 rootDevice = driverHandle->devices[0];
119 auto &rootDeviceRef = *static_cast<Device *>(rootDevice);
120 subDevice = rootDeviceRef.subDevices[0];
121 auto &subDeviceRef = *static_cast<Device *>(subDevice);
122
123 mockRootMetricSource = new MockMetricSource();
124 mockRootMetricSource->isAvailableReturn = true;
125
126 mockRootDeviceContext = new MockMetricDeviceContext(*rootDevice);
127 mockRootDeviceContext->clearAllSources();
128 mockRootDeviceContext->setMockMetricSource(mockRootMetricSource);
129 mockRootDeviceContext->setMultiDeviceCapable(true);
130 rootDeviceRef.metricContext.reset(mockRootDeviceContext);
131
132 mockSubMetricSource = new MockMetricSource();
133 mockSubMetricSource->isAvailableReturn = true;
134
135 mockSubDeviceContext = new MockMetricDeviceContext(*subDevice);
136 mockSubDeviceContext->clearAllSources();
137 mockSubDeviceContext->setMockMetricSource(mockSubMetricSource);
138 mockSubDeviceContext->setMultiDeviceCapable(false);
139 subDeviceRef.metricContext.reset(mockSubDeviceContext);
140 }
141
142 void MetricScopesMultiDeviceFixture::TearDown() {
143 MultiDeviceFixture::tearDown();
144
145 mockRootMetricSource = nullptr;
146 mockRootDeviceContext = nullptr;
147 rootDevice = nullptr;
148 subDevice = nullptr;
149 }
150
151 TEST_F(CalcOperationFixture, WhenCreatingCalcOpAndInvalidParamsPassedThenErrorIsHandled) {
152
153 // Aggregation window is zero
154 zet_intel_metric_calculation_exp_desc_t calculationDesc{
155 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
156 nullptr, // pNext
157 1, // metricGroupCount
158 &hMetricGroup, // phMetricGroups
159 0, // metricCount
160 nullptr, // phMetrics
161 0, // timeWindowsCount
162 nullptr, // pCalculationTimeWindows
163 0, // timeAggregationWindow
164 1, // metricScopesCount
165 &hMetricScope, // phMetricScopes
166 };
167
168 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
169
170 // No metric groups or metrics
171 calculationDesc.timeAggregationWindow = 100;
172 calculationDesc.metricGroupCount = 0;
173 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
174 device->toHandle(), &calculationDesc,
175 &hCalculationOperation));
176 }
177
178 TEST_F(CalcOperationFixture, WhenCreatingCalcOpWithMixedSourcesThenErrorIsReturned) {
179
180 MockMetricSource mockMetricSource2{};
181 mockMetricSource2.setType(UINT32_MAX);
182 MockMetricGroup mockMetricGroup2(mockMetricSource2);
183
184 // metric groups from different source
185 std::vector<zet_metric_group_handle_t> metricGroups{hMetricGroup, mockMetricGroup2.toHandle()};
186 zet_intel_metric_calculation_exp_desc_t calculationDesc{
187 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
188 nullptr, // pNext
189 2, // metricGroupCount
190 metricGroups.data(), // phMetricGroups
191 0, // metricCount
192 nullptr, // phMetrics
193 0, // timeWindowsCount
194 nullptr, // pCalculationTimeWindows
195 1000, // timeAggregationWindow
196 1, // metricScopesCount
197 &hMetricScope, // phMetricScopes
198 };
199
200 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
201 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
202 device->toHandle(), &calculationDesc,
203 &hCalculationOperation));
204
205 // metrics from different source
206 MockMetric mockMetric(mockMetricSource, mockMetricScopes);
207 MockMetric mockMetric2(mockMetricSource2, mockMetricScopes);
208 std::vector<zet_metric_handle_t> metrics{mockMetric.toHandle(), mockMetric2.toHandle()};
209
210 calculationDesc.metricGroupCount = 1;
211 calculationDesc.phMetricGroups = &hMetricGroup;
212 calculationDesc.metricCount = 2;
213 calculationDesc.phMetrics = metrics.data();
214 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
215 device->toHandle(), &calculationDesc,
216 &hCalculationOperation));
217
218 // metrics and metric group from different source
219 calculationDesc.metricCount = 1;
220 calculationDesc.phMetrics = &metrics[1];
221 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
222 device->toHandle(), &calculationDesc,
223 &hCalculationOperation));
224 }
225
226 TEST_F(CalcOperationFixture, WhenCreatingCalcOpWithMixedHierarchiesThenErrorIsReturned) {
227 MockMetricGroup mockMetricGroup2(mockMetricSource);
228 mockMetricGroup2.isMultiDevice = true;
229
230 // metric groups from different hierarchy
231 std::vector<zet_metric_group_handle_t> metricGroups{hMetricGroup, mockMetricGroup2.toHandle()};
232 zet_intel_metric_calculation_exp_desc_t calculationDesc{
233 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
234 nullptr, // pNext
235 2, // metricGroupCount
236 metricGroups.data(), // phMetricGroups
237 0, // metricCount
238 nullptr, // phMetrics
239 0, // timeWindowsCount
240 nullptr, // pCalculationTimeWindows
241 1000, // timeAggregationWindow
242 1, // metricScopesCount
243 &hMetricScope, // phMetricScopes
244 };
245
246 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
247 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
248 device->toHandle(), &calculationDesc,
249 &hCalculationOperation));
250
251 MockMetric mockMetric(mockMetricSource, mockMetricScopes), mockMetric2(mockMetricSource, mockMetricScopes);
252 mockMetric2.setMultiDevice(true);
253
254 std::vector<zet_metric_handle_t> metrics{mockMetric.toHandle(), mockMetric2.toHandle()};
255
256 calculationDesc.metricGroupCount = 1;
257 calculationDesc.phMetricGroups = &hMetricGroup;
258 calculationDesc.metricCount = 2;
259 calculationDesc.phMetrics = metrics.data();
260 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
261 device->toHandle(), &calculationDesc,
262 &hCalculationOperation));
263 }
264
265 TEST_F(CalcOperationFixture, WhenCreatingCalcSubDeviceOnlyAcceptsOneScope) {
266
267 std::vector<zet_intel_metric_scope_exp_handle_t> mockMetricScopes{hMetricScope, hMetricScope};
268
269 zet_intel_metric_calculation_exp_desc_t calculationDesc{
270 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
271 nullptr, // pNext
272 1, // metricGroupCount
273 &hMetricGroup, // phMetricGroups
274 0, // metricCount
275 nullptr, // phMetrics
276 0, // timeWindowsCount
277 nullptr, // pCalculationTimeWindows
278 1000, // timeAggregationWindow
279 2, // metricScopesCount
280 mockMetricScopes.data(), // phMetricScopes
281 };
282
283 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
284 EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, zetIntelMetricCalculationOperationCreateExp(context,
285 device->toHandle(), &calculationDesc,
286 &hCalculationOperation));
287 }
288
289 TEST_F(CalcOperationFixture, WhenCreatingCalcOpUseTheSourceFromMetricGroupOrMetricWhenAvailable) {
290
291 MockMetricGroup mockMetricGroup2(mockMetricSource);
292
293 std::vector<zet_metric_group_handle_t> metricGroups{hMetricGroup, mockMetricGroup2.toHandle()};
294 zet_intel_metric_calculation_exp_desc_t calculationDesc{
295 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
296 nullptr, // pNext
297 2, // metricGroupCount
298 metricGroups.data(), // phMetricGroups
299 0, // metricCount
300 nullptr, // phMetrics
301 0, // timeWindowsCount
302 nullptr, // pCalculationTimeWindows
303 1000, // timeAggregationWindow
304 1, // metricScopesCount
305 &hMetricScope, // phMetricScopes
306 };
307
308 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
309 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context,
310 device->toHandle(), &calculationDesc,
311 &hCalculationOperation));
312 EXPECT_NE(nullptr, hCalculationOperation);
313 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalculationOperation));
314
315 MockMetric mockMetric(mockMetricSource, mockMetricScopes), mockMetric2(mockMetricSource, mockMetricScopes);
316 std::vector<zet_metric_handle_t> metrics{mockMetric.toHandle(), mockMetric.toHandle()};
317
318 calculationDesc.metricGroupCount = 0;
319 calculationDesc.phMetricGroups = nullptr;
320 calculationDesc.metricCount = 2;
321 calculationDesc.phMetrics = metrics.data();
322 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context,
323 device->toHandle(), &calculationDesc,
324 &hCalculationOperation));
325 EXPECT_NE(nullptr, hCalculationOperation);
326 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalculationOperation));
327 }
328
329 TEST_F(CalcOperationFixture, WhenCreatingCalcOpObjectToAndFromHandleBaseClassWorkAsExpected) {
330 std::vector<MetricImp *> mockMetricsInReport{};
331 std::vector<MetricImp *> mockExcludedMetrics{};
332
333 std::vector<MetricScopeImp *> mockMetricScopes{mockMetricScope};
334
335 MockMetricCalcOp mockMetricCalcOp(false, mockMetricScopes, mockMetricsInReport, mockExcludedMetrics);
336 auto hMockCalcOp = mockMetricCalcOp.toHandle();
337
338 EXPECT_EQ(false, mockMetricCalcOp.isRootDevice());
339 auto mockCalcOp = MetricCalcOp::fromHandle(hMockCalcOp);
340 EXPECT_NE(nullptr, mockCalcOp);
341 }
342
343 TEST_F(CalcOperationFixture, WhenGettingMetricsInReportAndExcludedMetricsThenTheyAreReturnedCorrectly) {
344 std::vector<MetricImp *> mockMetricsInReport{};
345 MockMetric mockMetricInReport(mockMetricSource, mockMetricScopes);
346 mockMetricsInReport.push_back(&mockMetricInReport);
347
348 std::vector<MetricImp *> mockExcludedMetrics{};
349 MockMetric mockExcludedMetric(mockMetricSource, mockMetricScopes);
350 mockExcludedMetrics.push_back(&mockExcludedMetric);
351
352 std::vector<MetricScopeImp *> mockMetricScopes{mockMetricScope};
353
354 MockMetricCalcOp mockMetricCalcOp(false, mockMetricScopes, mockMetricsInReport, mockExcludedMetrics);
355 uint32_t metricCount = 0;
356 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetReportFormatExp(mockMetricCalcOp.toHandle(), &metricCount, nullptr, nullptr));
357 EXPECT_EQ(metricCount, 1U);
358 zet_metric_handle_t metricHandle = nullptr;
359 zet_intel_metric_scope_exp_handle_t metricScopeHandle = nullptr;
360 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetReportFormatExp(mockMetricCalcOp.toHandle(), &metricCount, &metricHandle, &metricScopeHandle));
361 EXPECT_EQ(metricCount, 1U);
362 EXPECT_EQ(metricHandle, mockMetricInReport.toHandle());
363 EXPECT_EQ(metricScopeHandle, mockMetricScope->toHandle());
364 EXPECT_EQ(metricCount, mockMetricCalcOp.getMetricsInReportCount());
365
366 metricCount = 0;
367 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetExcludedMetricsExp(mockMetricCalcOp.toHandle(), &metricCount, nullptr));
368 EXPECT_EQ(metricCount, 1U);
369
370 zet_metric_handle_t excludedMetricHandle = nullptr;
371 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetExcludedMetricsExp(mockMetricCalcOp.toHandle(), &metricCount, &excludedMetricHandle));
372 EXPECT_EQ(metricCount, 1U);
373 EXPECT_EQ(excludedMetricHandle, mockExcludedMetric.toHandle());
374 EXPECT_NE(excludedMetricHandle, metricHandle);
375 }
376
377 using MetricRuntimeFixture = Test<DeviceFixture>;
378
379 TEST_F(MetricRuntimeFixture, WhenRunTimeEnableIsDoneThenReturnSuccess) {
380
381 auto mockDeviceContext = new MockMetricDeviceContext(*device);
382 mockDeviceContext->clearAllSources();
383 auto metricSource = new MockMetricSource();
384 metricSource->isAvailableReturn = true;
385 mockDeviceContext->setMockMetricSource(metricSource);
386
387 auto l0Device = static_cast<Device *>(device);
388 l0Device->metricContext.reset(mockDeviceContext);
389
390 EXPECT_EQ(ZE_RESULT_SUCCESS, zetDeviceEnableMetricsExp(device->toHandle()));
391 l0Device->metricContext.reset();
392 }
393
394 TEST_F(MetricRuntimeFixture, WhenRunTimeEnableIsDoneMultipleTimesThenEnableIsDoneOnlyOnce) {
395
396 auto mockDeviceContext = new MockMetricDeviceContext(*device);
397 mockDeviceContext->clearAllSources();
398 auto metricSource = new MockMetricSource();
399 metricSource->isAvailableReturn = true;
400 mockDeviceContext->setMockMetricSource(metricSource);
401
402 auto l0Device = static_cast<Device *>(device);
403 l0Device->metricContext.reset(mockDeviceContext);
404
405 EXPECT_EQ(ZE_RESULT_SUCCESS, zetDeviceEnableMetricsExp(device->toHandle()));
406 EXPECT_EQ(metricSource->enableCallCount, 1u);
407 EXPECT_EQ(ZE_RESULT_SUCCESS, zetDeviceEnableMetricsExp(device->toHandle()));
408 EXPECT_EQ(metricSource->enableCallCount, 1u);
409 l0Device->metricContext.reset();
410 }
411
412 TEST_F(MetricRuntimeFixture, WhenRunTimeEnableIsDoneAndNoSourcesAreAvailableThenReturnError) {
413
414 auto mockDeviceContext = new MockMetricDeviceContext(*device);
415 mockDeviceContext->clearAllSources();
416 auto metricSource = new MockMetricSource();
417 metricSource->isAvailableReturn = false;
418 mockDeviceContext->setMockMetricSource(metricSource);
419
420 auto l0Device = static_cast<Device *>(device);
421 l0Device->metricContext.reset(mockDeviceContext);
422
423 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, zetDeviceEnableMetricsExp(device->toHandle()));
424 EXPECT_EQ(metricSource->enableCallCount, 1u);
425 l0Device->metricContext.reset();
426 }
427
428 TEST_F(MetricScopesMultiDeviceFixture, WhenGettingMetricScopesForSubDeviceTheyAreCorrectlyEnumerated) {
429
430 uint32_t metricScopesCount = 0;
431 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
432 subDevice->toHandle(),
433 &metricScopesCount,
434 nullptr));
435
436 EXPECT_EQ(metricScopesCount, 1u);
437 zet_intel_metric_scope_exp_handle_t metricScope;
438 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
439 subDevice->toHandle(),
440 &metricScopesCount,
441 &metricScope));
442 EXPECT_EQ(metricScopesCount, 1u);
443
444 zet_intel_metric_scope_properties_exp_t scopeProperties{};
445 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
446 scopeProperties.pNext = nullptr;
447
448 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(metricScope,
449 &scopeProperties));
450
451 // ID of compute scopes should be 0, sub-devices support single compute scope
452 std::string expectedName = computeScopeNamePrefix.data() + std::to_string(0);
453 EXPECT_STREQ(scopeProperties.name, expectedName.c_str());
454 std::string expectedDescription = computeScopeDescriptionPrefix.data() + std::to_string(0);
455 EXPECT_STREQ(scopeProperties.description, expectedDescription.c_str());
456 }
457
458 TEST_F(MetricScopesMultiDeviceFixture, WhenMultipleSourcesAreAvailableComputeMetricScopesAreEnumeratedOnlyOnce) {
459
460 auto metricSource2 = new MockMetricSource();
461 metricSource2->isAvailableReturn = true;
462 mockSubDeviceContext->setMockMetricSourceAtIndex(100, metricSource2);
463
464 uint32_t metricScopesCount = 0;
465 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
466 subDevice->toHandle(),
467 &metricScopesCount,
468 nullptr));
469 EXPECT_EQ(metricScopesCount, 1u);
470 }
471
472 TEST_F(MetricScopesMultiDeviceFixture, MetricScopeObjectToAndFromHandleBaseClassWorkAsExpected) {
473
474 zet_intel_metric_scope_properties_exp_t scopeProperties{};
475 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
476 scopeProperties.pNext = nullptr;
477
478 MockMetricScope mockMetricScope(scopeProperties, false, 0);
479 auto hMockScope = mockMetricScope.toHandle();
480
481 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(hMockScope,
482 &scopeProperties));
483 auto mockScope = MetricScope::fromHandle(hMockScope);
484 EXPECT_NE(nullptr, mockScope);
485 }
486
487 template <typename GfxFamily>
488 struct MockL0GfxCoreHelperNOTSupportMetricsAggregation : L0::L0GfxCoreHelperHw<GfxFamily> {
489 bool supportMetricsAggregation() const override { return false; }
490 };
491
492 HWTEST_F(MetricScopesMultiDeviceFixture, GivenRootDeviceThatDoesNOTSupportsMetricsAggregationWhenGettingMetricScopesComputeScopesIDMatchSubdevice) {
493
494 MockL0GfxCoreHelperNOTSupportMetricsAggregation<FamilyType> mockL0GfxCoreHelper;
495 std::unique_ptr<ApiGfxCoreHelper> l0GfxCoreHelperBackup(static_cast<ApiGfxCoreHelper *>(&mockL0GfxCoreHelper));
496 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
497 uint32_t metricScopesCount = 0;
498
499 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
500 rootDevice->toHandle(),
501 &metricScopesCount,
502 nullptr));
503 EXPECT_EQ(metricScopesCount, numSubDevices);
504 std::vector<zet_intel_metric_scope_exp_handle_t> mockMetricScopes(metricScopesCount);
505 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
506 rootDevice->toHandle(),
507 &metricScopesCount,
508 mockMetricScopes.data()));
509 EXPECT_EQ(metricScopesCount, numSubDevices);
510 zet_intel_metric_scope_properties_exp_t scopeProperties{};
511 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
512 scopeProperties.pNext = nullptr;
513
514 for (uint32_t i = 0; i < metricScopesCount; i++) {
515 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(mockMetricScopes[i],
516 &scopeProperties));
517 EXPECT_EQ(scopeProperties.iD, i);
518 // ID of compute scopes should be sub-dev ID
519 std::string expectedName = computeScopeNamePrefix.data() + std::to_string(i);
520 EXPECT_STREQ(scopeProperties.name, expectedName.c_str());
521 std::string expectedDescription = computeScopeDescriptionPrefix.data() + std::to_string(i);
522 EXPECT_STREQ(scopeProperties.description, expectedDescription.c_str());
523 }
524
525 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
526 l0GfxCoreHelperBackup.release();
527 }
528
529 template <typename GfxFamily>
530 struct MockL0GfxCoreHelperSupportMetricsAggregation : L0::L0GfxCoreHelperHw<GfxFamily> {
531 bool supportMetricsAggregation() const override { return true; }
532 };
533
534 HWTEST_F(MetricScopesMultiDeviceFixture, GivenRootDeviceThatSupportsMetricsAggregationWhenGettingMetricScopesAggregatedScopeShouldBeFirst) {
535
536 MockL0GfxCoreHelperSupportMetricsAggregation<FamilyType> mockL0GfxCoreHelper;
537 std::unique_ptr<ApiGfxCoreHelper> l0GfxCoreHelperBackup(static_cast<ApiGfxCoreHelper *>(&mockL0GfxCoreHelper));
538 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
539
540 uint32_t metricScopesCount = 0;
541
542 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
543 rootDevice->toHandle(),
544 &metricScopesCount,
545 nullptr));
546 EXPECT_EQ(metricScopesCount, numRootDevices + numSubDevices);
547 std::vector<zet_intel_metric_scope_exp_handle_t> mockMetricScopes(metricScopesCount);
548 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
549 rootDevice->toHandle(),
550 &metricScopesCount,
551 mockMetricScopes.data()));
552 EXPECT_EQ(metricScopesCount, numRootDevices + numSubDevices);
553 zet_intel_metric_scope_properties_exp_t scopeProperties{};
554 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
555 scopeProperties.pNext = nullptr;
556
557 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(mockMetricScopes[0],
558 &scopeProperties));
559
560 // Aggregated scope must be first with ID 0
561 EXPECT_EQ(scopeProperties.iD, 0u);
562 EXPECT_STREQ(scopeProperties.name, aggregatedScopeName.data());
563 EXPECT_STREQ(scopeProperties.description, aggregatedScopeDescription.data());
564
565 for (uint32_t i = 1; i < metricScopesCount; i++) {
566 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(mockMetricScopes[i],
567 &scopeProperties));
568 EXPECT_EQ(scopeProperties.iD, i);
569 // ID of compute scopes should be sub-dev ID+1 (since aggregated scope is 0)
570 std::string expectedName = computeScopeNamePrefix.data() + std::to_string(i - 1);
571 EXPECT_STREQ(scopeProperties.name, expectedName.c_str());
572 std::string expectedDescription = computeScopeDescriptionPrefix.data() + std::to_string(i - 1);
573 EXPECT_STREQ(scopeProperties.description, expectedDescription.c_str());
574 }
575
576 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
577 l0GfxCoreHelperBackup.release();
578 }
579
580 HWTEST_F(MetricScopesMultiDeviceFixture, GivenMetricsAggregationIsSupportedWhenCreatingCalcOpAggregatedScopeShouldBeFirstIntheOutputReport) {
581
582 MockL0GfxCoreHelperSupportMetricsAggregation<FamilyType> mockL0GfxCoreHelper;
583 std::unique_ptr<ApiGfxCoreHelper> l0GfxCoreHelperBackup(static_cast<ApiGfxCoreHelper *>(&mockL0GfxCoreHelper));
584 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
585
586 uint32_t metricScopesCount = 0;
587 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
588 rootDevice->toHandle(),
589 &metricScopesCount,
590 nullptr));
591 EXPECT_EQ(metricScopesCount, numRootDevices + numSubDevices);
592 std::vector<zet_intel_metric_scope_exp_handle_t> mockMetricScopes(metricScopesCount);
593 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
594 rootDevice->toHandle(),
595 &metricScopesCount,
596 mockMetricScopes.data()));
597
598 EXPECT_EQ(metricScopesCount, numRootDevices + numSubDevices);
599
600 MockMetricGroup mockMetricGroup(*mockRootMetricSource);
601 mockMetricGroup.isMultiDevice = true;
602 zet_metric_group_handle_t hMetricGroup = mockMetricGroup.toHandle();
603
604 // send aggregated scope last
605 std::swap(mockMetricScopes[0], mockMetricScopes[metricScopesCount - 1]);
606
607 // Aggregation window is zero
608 zet_intel_metric_calculation_exp_desc_t calculationDesc{
609 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
610 nullptr, // pNext
611 1, // metricGroupCount
612 &hMetricGroup, // phMetricGroups
613 0, // metricCount
614 nullptr, // phMetrics
615 0, // timeWindowsCount
616 nullptr, // pCalculationTimeWindows
617 100, // timeAggregationWindow
618 metricScopesCount, // metricScopesCount
619 mockMetricScopes.data(), // phMetricScopes
620 };
621
622 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
623 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
624 rootDevice->toHandle(), &calculationDesc,
625 &hCalculationOperation));
626 EXPECT_NE(nullptr, hCalculationOperation);
627
628 uint32_t metricCount = 0;
629 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetReportFormatExp(hCalculationOperation, &metricCount, nullptr, nullptr));
630
631 // Expect one metric per scope requested: MockMetricSource::calcOperationCreate() will create one mock metric per metric group
632 EXPECT_EQ(metricCount, metricScopesCount);
633 std::vector<zet_metric_handle_t> metricHandles(metricCount);
634 std::vector<zet_intel_metric_scope_exp_handle_t> metricScopeHandles(metricCount);
635 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetReportFormatExp(hCalculationOperation, &metricCount, metricHandles.data(), metricScopeHandles.data()));
636
637 for (uint32_t i = 0; i < metricCount; i++) {
638 zet_intel_metric_scope_properties_exp_t scopeProperties{};
639 scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP;
640 scopeProperties.pNext = nullptr;
641
642 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopeGetPropertiesExp(metricScopeHandles[i],
643 &scopeProperties));
644 if (i == 0) {
645 // Aggregated scope must be first with ID 0
646 EXPECT_EQ(scopeProperties.iD, 0u);
647 EXPECT_STREQ(scopeProperties.name, aggregatedScopeName.data());
648 EXPECT_STREQ(scopeProperties.description, aggregatedScopeDescription.data());
649 } else {
650 EXPECT_EQ(scopeProperties.iD, i);
651 // ID of compute scopes should be sub-dev ID+1 (since aggregated scope is 0)
652 std::string expectedName = computeScopeNamePrefix.data() + std::to_string(i - 1);
653 EXPECT_STREQ(scopeProperties.name, expectedName.c_str());
654 std::string expectedDescription = computeScopeDescriptionPrefix.data() + std::to_string(i - 1);
655 EXPECT_STREQ(scopeProperties.description, expectedDescription.c_str());
656 }
657 }
658
659 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalculationOperation));
660
661 rootDevice->getNEODevice()->getRootDeviceEnvironmentRef().apiGfxCoreHelper.swap(l0GfxCoreHelperBackup);
662 l0GfxCoreHelperBackup.release();
663 }
664
665 TEST_F(MetricScopesMultiDeviceFixture, WhenCalcOperationCreateIsCalledWithDuplicateMetricGroupsMetricsAndScopesThenDuplicatesAreRemoved) {
666
667 uint32_t metricScopesCount = 0;
668 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
669 rootDevice->toHandle(),
670 &metricScopesCount,
671 nullptr));
672 ASSERT_GE(metricScopesCount, 2u);
673 std::vector<zet_intel_metric_scope_exp_handle_t> availableScopes(metricScopesCount);
674 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricScopesGetExp(context->toHandle(),
675 rootDevice->toHandle(),
676 &metricScopesCount,
677 availableScopes.data()));
678
679 // Create array with duplicates from available scopes
680 std::vector<zet_intel_metric_scope_exp_handle_t> metricScopes = {
681 availableScopes[0], availableScopes[1], availableScopes[1], availableScopes[0]};
682
683 std::vector<MetricScopeImp *> metricScopeImps = {
684 static_cast<MetricScopeImp *>(MetricScope::fromHandle(availableScopes[0])),
685 static_cast<MetricScopeImp *>(MetricScope::fromHandle(availableScopes[1])),
686 static_cast<MetricScopeImp *>(MetricScope::fromHandle(availableScopes[1])),
687 static_cast<MetricScopeImp *>(MetricScope::fromHandle(availableScopes[0]))};
688
689 // Create mock metric group for root device
690 MockMetricGroup mockMetricGroup(*mockRootMetricSource);
691 mockMetricGroup.isMultiDevice = true;
692 zet_metric_group_handle_t hMetricGroup = mockMetricGroup.toHandle();
693
694 // Create mock metrics
695 MockMetric mockMetric1(*mockRootMetricSource, metricScopeImps);
696 MockMetric mockMetric2(*mockRootMetricSource, metricScopeImps);
697 MockMetric mockMetric3(*mockRootMetricSource, metricScopeImps);
698 mockMetric1.setMultiDevice(true);
699 mockMetric2.setMultiDevice(true);
700 mockMetric3.setMultiDevice(true);
701
702 // Arrays with duplicates
703 std::vector<zet_metric_group_handle_t> duplicateMetricGroups = {hMetricGroup, hMetricGroup, hMetricGroup};
704 std::vector<zet_metric_handle_t> duplicateMetrics = {
705 mockMetric1.toHandle(), mockMetric2.toHandle(), mockMetric1.toHandle(),
706 mockMetric3.toHandle(), mockMetric2.toHandle()};
707
708 zet_intel_metric_calculation_exp_desc_t calculationDesc{
709 ZET_INTEL_STRUCTURE_TYPE_METRIC_CALCULATION_DESC_EXP,
710 nullptr, // pNext
711 static_cast<uint32_t>(duplicateMetricGroups.size()), // metricGroupCount
712 duplicateMetricGroups.data(), // phMetricGroups
713 static_cast<uint32_t>(duplicateMetrics.size()), // metricCount
714 duplicateMetrics.data(), // phMetrics
715 0, // timeWindowsCount
716 nullptr, // pCalculationTimeWindows
717 1000, // timeAggregationWindow
718 static_cast<uint32_t>(metricScopes.size()), // metricScopesCount
719 metricScopes.data(), // phMetricScopes
720 };
721
722 zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation;
723 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationCreateExp(context->toHandle(),
724 rootDevice->toHandle(), &calculationDesc,
725 &hCalculationOperation));
726
727 uint32_t reportMetricCount = 0;
728 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationGetReportFormatExp(hCalculationOperation, &reportMetricCount, nullptr, nullptr));
729
730 // 4 metrics -> 1 metric from group after removing duplicates + 3 metrics after removing duplicates
731 // 2 scopes -> 2 scopes after removing duplicates
732 // 8 = 1*2 scopes + 3*2 scopes
733 EXPECT_EQ(reportMetricCount, 8U);
734
735 EXPECT_EQ(ZE_RESULT_SUCCESS, zetIntelMetricCalculationOperationDestroyExp(hCalculationOperation));
736 }
737
738 using MetricGroupTest = Test<DeviceFixture>;
739
740 TEST_F(MetricGroupTest, GivenNoMetricSourceIsAvailableThenNoMetricGroupsAreReturnedAndErrorIsReturned) {
741
742 auto mockDeviceContext = new MockMetricDeviceContext(*device);
743 mockDeviceContext->clearAllSources();
744
745 device->metricContext.reset(mockDeviceContext);
746
747 uint32_t metricGroupCount = 1;
748 zet_metric_group_handle_t metricGroupHandle = nullptr;
749
750 EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, zetMetricGroupGet(device->toHandle(), &metricGroupCount, &metricGroupHandle));
751 EXPECT_EQ(metricGroupCount, 0U);
752 device->metricContext.reset();
753 }
754
755 TEST_F(MetricGroupTest, GivenMetricSourceInitFailsThenNoMetricGroupsAreReturnedAndErrorIsReturned) {
756
757 auto mockDeviceContext = new MockMetricDeviceContext(*device);
758 mockDeviceContext->clearAllSources();
759 auto metricSource = new MockMetricSource();
760 metricSource->isAvailableReturn = true;
761 mockDeviceContext->setMockMetricSource(metricSource);
762
763 device->metricContext.reset(mockDeviceContext);
764
765 uint32_t metricGroupCount = 1;
766 zet_metric_group_handle_t metricGroupHandle = nullptr;
767
768 EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, zetMetricGroupGet(device->toHandle(), &metricGroupCount, &metricGroupHandle));
769 EXPECT_EQ(metricGroupCount, 0U);
770 device->metricContext.reset();
771 }
772
773 } // namespace ult
774 } // namespace L0