From 5c6ae48d17c4405263b3f0dd8c7964ac5bc4626c Mon Sep 17 00:00:00 2001 From: Shreyas Kunder Date: Tue, 9 Sep 2025 22:29:20 +0000 Subject: [PATCH] feature: Metrics properties show metric scopes in Tracelib Related-To: NEO-15832 Signed-off-by: Shreyas Kunder --- .../driver_experimental/public/zex_metric.cpp | 8 ++++ .../driver/extension_function_address.cpp | 1 + .../include/level_zero/zet_intel_gpu_metric.h | 13 +++++- level_zero/tools/source/metrics/metric.cpp | 22 +++++++++ level_zero/tools/source/metrics/metric.h | 9 ++++ .../test_metric_ip_sampling_streamer.cpp | 45 +++++++++++++++++++ 6 files changed, 97 insertions(+), 1 deletion(-) diff --git a/level_zero/api/driver_experimental/public/zex_metric.cpp b/level_zero/api/driver_experimental/public/zex_metric.cpp index 02cca322ff..e4484543d5 100644 --- a/level_zero/api/driver_experimental/public/zex_metric.cpp +++ b/level_zero/api/driver_experimental/public/zex_metric.cpp @@ -127,6 +127,10 @@ ze_result_t ZE_APICALL zetIntelMetricScopeGetPropertiesExp(zet_intel_metric_scop return L0::metricScopeGetProperties(hMetricScope, pMetricScopeProperties); } +ze_result_t ZE_APICALL zetIntelMetricSupportedScopesGetExp(zet_metric_handle_t *phMetric, uint32_t *pCount, zet_intel_metric_scope_exp_handle_t *phScopes) { + return L0::getMetricSupportedScopes(phMetric, pCount, phScopes); +} + } // namespace L0 extern "C" { @@ -269,4 +273,8 @@ ze_result_t ZE_APICALL zetIntelMetricScopeGetPropertiesExp(zet_intel_metric_scop return L0::zetIntelMetricScopeGetPropertiesExp(hMetricScope, pMetricScopeProperties); } +ze_result_t ZE_APICALL zetIntelMetricSupportedScopesGetExp(zet_metric_handle_t *phMetric, uint32_t *pCount, zet_intel_metric_scope_exp_handle_t *phScopes) { + return L0::getMetricSupportedScopes(phMetric, pCount, phScopes); +} + } // extern "C" diff --git a/level_zero/core/source/driver/extension_function_address.cpp b/level_zero/core/source/driver/extension_function_address.cpp index d38ecd3efd..ef26e038a6 100644 --- a/level_zero/core/source/driver/extension_function_address.cpp +++ b/level_zero/core/source/driver/extension_function_address.cpp @@ -127,6 +127,7 @@ void *ExtensionFunctionAddressHelper::getExtensionFunctionAddress(const std::str RETURN_FUNC_PTR_IF_EXIST(zetIntelMetricCalculationOperationGetReportFormatExp); RETURN_FUNC_PTR_IF_EXIST(zetIntelMetricDecodeCalculateMultipleValuesExp); RETURN_FUNC_PTR_IF_EXIST(zetIntelMetricCalculateValuesExp); + RETURN_FUNC_PTR_IF_EXIST(zetIntelMetricSupportedScopesGetExp); // Graphs RETURN_FUNC_PTR_IF_EXIST(zeGraphCreateExp); diff --git a/level_zero/include/level_zero/zet_intel_gpu_metric.h b/level_zero/include/level_zero/zet_intel_gpu_metric.h index 543032f828..84a53fdd10 100644 --- a/level_zero/include/level_zero/zet_intel_gpu_metric.h +++ b/level_zero/include/level_zero/zet_intel_gpu_metric.h @@ -196,7 +196,8 @@ ze_result_t ZE_APICALL zetIntelMetricTracerDecodeExp( #endif // ZET_INTEL_METRIC_SCOPES_EXP_NAME typedef enum _zet_intel_metric_scopes_exp_version_t { ZET_INTEL_METRIC_SCOPES_EXP_VERSION_1_0 = ZE_MAKE_VERSION(1, 0), ///< version 1.0 - ZET_INTEL_METRIC_SCOPES_EXP_VERSION_CURRENT = ZET_INTEL_METRIC_SCOPES_EXP_VERSION_1_0, ///< latest known version + ZET_INTEL_METRIC_SCOPES_EXP_VERSION_1_1 = ZE_MAKE_VERSION(1, 1), ///< version 1.1 + ZET_INTEL_METRIC_SCOPES_EXP_VERSION_CURRENT = ZET_INTEL_METRIC_SCOPES_EXP_VERSION_1_1, ///< latest known version ZET_INTEL_METRIC_SCOPES_EXP_VERSION_FORCE_UINT32 = 0x7fffffff } zet_intel_metric_scopes_exp_version_t; @@ -236,6 +237,16 @@ ze_result_t ZE_APICALL zetIntelMetricScopeGetPropertiesExp( zet_intel_metric_scope_exp_handle_t hMetricScope, ///< [in] handle of the metric scope zet_intel_metric_scope_properties_exp_t *pMetricScopeProperties); ///< [out] pointer to the metric scope properties structure +ze_result_t ZE_APICALL zetIntelMetricSupportedScopesGetExp( + zet_metric_handle_t *phMetric, ///< [in] handle of the metric + uint32_t *pCount, ///< [in,out] pointer to the number of metric scopes available for the metric. + ///< If set to zero, then the driver shall update the value with the total + ///< number of metric scopes available for the metric. + zet_intel_metric_scope_exp_handle_t *phScopes); ///< [out][optional] [range(0, *pCount)] array of metric scopes handles + ///< available for the metric. If pCount is greater than zero but + ///< less than the total number of metric scopes available for the metric, + ///< then driver shall only return that number of metric scopes. + #ifndef ZET_INTEL_METRIC_CALCULATION_EXP_NAME /// @brief Extension name to query Intel Metric Calculation operations #define ZET_INTEL_METRIC_CALCULATION_EXP_NAME "ZET_intel_metric_calculation" diff --git a/level_zero/tools/source/metrics/metric.cpp b/level_zero/tools/source/metrics/metric.cpp index a83eab1690..d3e5902110 100644 --- a/level_zero/tools/source/metrics/metric.cpp +++ b/level_zero/tools/source/metrics/metric.cpp @@ -743,6 +743,19 @@ MetricImp *MultiDeviceMetricImp::getMetricAtSubDeviceIndex(uint32_t index) { return nullptr; } +ze_result_t MetricImp::getScopes(uint32_t *pCount, zet_intel_metric_scope_exp_handle_t *phScopes) { + if (*pCount == 0) { + *pCount = static_cast(scopes.size()); + return ZE_RESULT_SUCCESS; + } + + *pCount = std::min(*pCount, static_cast(scopes.size())); + for (uint32_t i = 0; i < *pCount; i++) { + phScopes[i] = scopes[i]; + } + return ZE_RESULT_SUCCESS; +} + ze_result_t metricGroupGet(zet_device_handle_t hDevice, uint32_t *pCount, zet_metric_group_handle_t *phMetricGroups) { auto device = Device::fromHandle(hDevice); return device->getMetricDeviceContext().metricGroupGet(pCount, phMetricGroups); @@ -1155,4 +1168,13 @@ ze_result_t metricAppendMarker(zet_command_list_handle_t hCommandList, zet_metri return metricGroupImp->getMetricSource().appendMarker(hCommandList, hMetricGroup, value); } +ze_result_t getMetricSupportedScopes( + zet_metric_handle_t *phMetric, + uint32_t *pScopesCount, + zet_intel_metric_scope_exp_handle_t *phMetricScopes) { + + auto metricImp = static_cast(Metric::fromHandle(*phMetric)); + return metricImp->getScopes(pScopesCount, phMetricScopes); +} + } // namespace L0 diff --git a/level_zero/tools/source/metrics/metric.h b/level_zero/tools/source/metrics/metric.h index 1ee48f1508..ba5057c353 100644 --- a/level_zero/tools/source/metrics/metric.h +++ b/level_zero/tools/source/metrics/metric.h @@ -201,6 +201,10 @@ class MetricDeviceContext { return computeMetricScopesInitialized; } + const std::vector> &getMetricScopes() const { + return metricScopes; + } + protected: bool areMetricGroupsFromSameSource(uint32_t count, zet_metric_group_handle_t *phMetricGroups, uint32_t *sourceType); bool areMetricsFromSameSource(uint32_t count, zet_metric_handle_t *phMetrics, uint32_t *sourceType); @@ -256,6 +260,10 @@ struct MetricImp : public Metric { bool isPredefined = true; bool isMultiDevice = false; MultiDeviceMetricImp *rootDeviceMetricImp = nullptr; + std::vector scopes = {}; + + public: + ze_result_t getScopes(uint32_t *pCount, zet_intel_metric_scope_exp_handle_t *phScopes); }; struct MultiDeviceMetricImp : public MetricImp { @@ -626,5 +634,6 @@ ze_result_t metricScopesGet(zet_context_handle_t hContext, zet_device_handle_t h zet_intel_metric_scope_exp_handle_t *phMetricScopes); ze_result_t metricScopeGetProperties(zet_intel_metric_scope_exp_handle_t hMetricScope, zet_intel_metric_scope_properties_exp_t *pMetricScopeProperties); ze_result_t metricAppendMarker(zet_command_list_handle_t hCommandList, zet_metric_group_handle_t hMetricGroup, uint32_t value); +ze_result_t getMetricSupportedScopes(zet_metric_handle_t *phMetric, uint32_t *pScopesCount, zet_intel_metric_scope_exp_handle_t *phMetricScopes); } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_ip_sampling_streamer.cpp b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_ip_sampling_streamer.cpp index 31d8297ecf..4c2b320a23 100644 --- a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_ip_sampling_streamer.cpp +++ b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_ip_sampling_streamer.cpp @@ -581,6 +581,51 @@ TEST_F(MetricIpSamplingStreamerTest, whenGetConcurrentMetricGroupsIsCalledThenCo } } +using MetricIpSamplingMetricSupportedScopeTest = MetricIpSamplingStreamerTest; + +class MockMetricImp : public MetricImp { + public: + using MetricImp::MetricImp; + + void setScopes(const std::vector &newScopes) { + scopes = newScopes; + } +}; + +TEST_F(MetricIpSamplingMetricSupportedScopeTest, givenMetricWhenGettingSupportedMetricScopesThenExpectedCountAndHandlesAreReturned) { + EXPECT_EQ(ZE_RESULT_SUCCESS, testDevices[0]->getMetricDeviceContext().enableMetricApi()); + + EXPECT_EQ(testDevices[0]->getMetricDeviceContext().getMetricScopes().size(), 0u); + + zet_metric_group_handle_t metricGroupHandle = MetricIpSamplingStreamerTest::getMetricGroup(testDevices[0]); + uint32_t metricCount = 0; + EXPECT_EQ(zetMetricGet(metricGroupHandle, &metricCount, nullptr), ZE_RESULT_SUCCESS); + metricCount = 1; + zet_metric_handle_t phMetric{}; + EXPECT_EQ(zetMetricGet(metricGroupHandle, &metricCount, &phMetric), ZE_RESULT_SUCCESS); + + zet_intel_metric_scope_properties_exp_t scopeProperties{}; + scopeProperties.stype = ZET_STRUCTURE_TYPE_INTEL_METRIC_SCOPE_PROPERTIES_EXP; + scopeProperties.pNext = nullptr; + + std::vector metricScopesHandles; + MockMetricScope *mockMetricScope = new MockMetricScope(scopeProperties, false); + metricScopesHandles.push_back(mockMetricScope->toHandle()); + + auto metricImp = static_cast(Metric::fromHandle(phMetric)); + metricImp->setScopes(metricScopesHandles); + + uint32_t metricScopesCount = 0; + EXPECT_EQ(zetIntelMetricSupportedScopesGetExp(&phMetric, &metricScopesCount, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(metricScopesCount, 1u); + + std::vector metricScopesHandle(metricScopesCount); + EXPECT_EQ(zetIntelMetricSupportedScopesGetExp(&phMetric, &metricScopesCount, metricScopesHandle.data()), ZE_RESULT_SUCCESS); + EXPECT_NE(metricScopesHandle[0], nullptr); + + delete mockMetricScope; +} + using MetricIpSamplingCalcOpMultiDevTest = MetricIpSamplingCalculateMultiDevFixture; HWTEST2_F(MetricIpSamplingCalcOpMultiDevTest, givenIpSamplingMetricGroupThenCreateAndDestroyCalcOpIsSuccessful, EustallSupportedPlatforms) {