| 1 |
/* |
| 2 |
* Copyright (C) 2020-2025 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
#include "shared/test/common/test_macros/mock_method_macros.h" |
| 10 |
#include "shared/test/common/test_macros/test.h" |
| 11 |
|
| 12 |
#include "level_zero/core/source/device/device.h" |
| 13 |
#include "level_zero/core/test/unit_tests/white_box.h" |
| 14 |
#include "level_zero/tools/source/metrics/metric.h" |
| 15 |
#include "level_zero/tools/source/metrics/metric_oa_enumeration_imp.h" |
| 16 |
#include "level_zero/tools/source/metrics/metric_oa_query_imp.h" |
| 17 |
#include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric_oa_enumeration.h" |
| 18 |
#include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric_source.h" |
| 19 |
|
| 20 |
namespace L0 { |
| 21 |
namespace ult { |
| 22 |
|
| 23 |
template <> |
| 24 |
struct WhiteBox<::L0::MetricQuery> : public ::L0::MetricQuery { |
| 25 |
using BaseClass = ::L0::MetricQuery; |
| 26 |
}; |
| 27 |
|
| 28 |
template <> |
| 29 |
struct WhiteBox<::L0::MetricQueryPool> : public ::L0::MetricQueryPool { |
| 30 |
using BaseClass = ::L0::MetricQuery; |
| 31 |
}; |
| 32 |
|
| 33 |
using MetricQuery = WhiteBox<::L0::MetricQuery>; |
| 34 |
using MetricQueryPool = WhiteBox<::L0::MetricQueryPool>; |
| 35 |
|
| 36 |
using MetricsLibraryApi::ClientData_1_0; |
| 37 |
using MetricsLibraryApi::ClientDataLinuxAdapter_1_0; |
| 38 |
using MetricsLibraryApi::ClientType_1_0; |
| 39 |
using MetricsLibraryApi::CommandBufferData_1_0; |
| 40 |
using MetricsLibraryApi::CommandBufferSize_1_0; |
| 41 |
using MetricsLibraryApi::ConfigurationActivateData_1_0; |
| 42 |
using MetricsLibraryApi::ConfigurationCreateData_1_0; |
| 43 |
using MetricsLibraryApi::ConfigurationHandle_1_0; |
| 44 |
using MetricsLibraryApi::ContextCreateData_1_0; |
| 45 |
using MetricsLibraryApi::ContextHandle_1_0; |
| 46 |
using MetricsLibraryApi::GetReportData_1_0; |
| 47 |
using MetricsLibraryApi::LinuxAdapterType; |
| 48 |
using MetricsLibraryApi::MarkerCreateData_1_0; |
| 49 |
using MetricsLibraryApi::MarkerHandle_1_0; |
| 50 |
using MetricsLibraryApi::OverrideCreateData_1_0; |
| 51 |
using MetricsLibraryApi::OverrideHandle_1_0; |
| 52 |
using MetricsLibraryApi::ParameterType; |
| 53 |
using MetricsLibraryApi::QueryCreateData_1_0; |
| 54 |
using MetricsLibraryApi::QueryHandle_1_0; |
| 55 |
using MetricsLibraryApi::StatusCode; |
| 56 |
using MetricsLibraryApi::TypedValue_1_0; |
| 57 |
using MetricsLibraryApi::ValueType; |
| 58 |
|
| 59 |
struct MockMetricsLibraryApi { |
| 60 |
|
| 61 |
// Original api functions. |
| 62 |
static StatusCode ML_STDCALL contextCreate(ClientType_1_0 clientType, ContextCreateData_1_0 *createData, ContextHandle_1_0 *handle); |
| 63 |
static StatusCode ML_STDCALL contextDelete(const ContextHandle_1_0 handle); |
| 64 |
static StatusCode ML_STDCALL getParameter(const ParameterType parameter, ValueType *type, TypedValue_1_0 *value); |
| 65 |
static StatusCode ML_STDCALL commandBufferGet(const CommandBufferData_1_0 *data); |
| 66 |
static StatusCode ML_STDCALL commandBufferGetSize(const CommandBufferData_1_0 *data, CommandBufferSize_1_0 *size); |
| 67 |
static StatusCode ML_STDCALL queryCreate(const QueryCreateData_1_0 *createData, QueryHandle_1_0 *handle); |
| 68 |
static StatusCode ML_STDCALL queryDelete(const QueryHandle_1_0 handle); |
| 69 |
static StatusCode ML_STDCALL overrideCreate(const OverrideCreateData_1_0 *createData, OverrideHandle_1_0 *handle); |
| 70 |
static StatusCode ML_STDCALL overrideDelete(const OverrideHandle_1_0 handle); |
| 71 |
static StatusCode ML_STDCALL markerCreate(const MarkerCreateData_1_0 *createData, MarkerHandle_1_0 *handle); |
| 72 |
static StatusCode ML_STDCALL markerDelete(const MarkerHandle_1_0 handle); |
| 73 |
static StatusCode ML_STDCALL configurationCreate(const ConfigurationCreateData_1_0 *createData, ConfigurationHandle_1_0 *handle); |
| 74 |
static StatusCode ML_STDCALL configurationActivate(const ConfigurationHandle_1_0 handle, const ConfigurationActivateData_1_0 *activateData); |
| 75 |
static StatusCode ML_STDCALL configurationDeactivate(const ConfigurationHandle_1_0 handle); |
| 76 |
static StatusCode ML_STDCALL configurationDelete(const ConfigurationHandle_1_0 handle); |
| 77 |
static StatusCode ML_STDCALL getData(GetReportData_1_0 *data); |
| 78 |
|
| 79 |
StatusCode contextCreateImpl(ClientType_1_0 clientType, ContextCreateData_1_0 *createData, ContextHandle_1_0 *handle) { |
| 80 |
*handle = contextCreateOutHandle; |
| 81 |
|
| 82 |
return contextCreateResult; |
| 83 |
} |
| 84 |
|
| 85 |
StatusCode queryCreateImpl(const QueryCreateData_1_0 *createData, QueryHandle_1_0 *handle) { |
| 86 |
*handle = queryCreateOutHandle; |
| 87 |
if (!queryCreateResults.empty()) { |
| 88 |
auto retVal = queryCreateResults.back(); |
| 89 |
queryCreateResults.pop_back(); |
| 90 |
return retVal; |
| 91 |
} |
| 92 |
return queryCreateResult; |
| 93 |
} |
| 94 |
|
| 95 |
StatusCode configurationCreateImpl(const ConfigurationCreateData_1_0 *createData, ConfigurationHandle_1_0 *handle) { |
| 96 |
*handle = configurationCreateOutHandle; |
| 97 |
|
| 98 |
return configurationCreateResult; |
| 99 |
} |
| 100 |
|
| 101 |
StatusCode configurationActivateImpl(const ConfigurationHandle_1_0 handle, const ConfigurationActivateData_1_0 *activateData) { |
| 102 |
EXPECT_LT(0u, configurationActivationCounter); |
| 103 |
configurationActivationCounter--; |
| 104 |
|
| 105 |
return configurationActivateResult; |
| 106 |
} |
| 107 |
|
| 108 |
StatusCode configurationDeactivateImpl(const ConfigurationHandle_1_0 handle) { |
| 109 |
EXPECT_LT(0u, configurationDeactivationCounter); |
| 110 |
configurationDeactivationCounter--; |
| 111 |
|
| 112 |
return configurationDeactivateResult; |
| 113 |
} |
| 114 |
StatusCode getParameterImpl(const ParameterType parameter, ValueType *type, TypedValue_1_0 *value) { |
| 115 |
*value = getParameterOutValue; |
| 116 |
return getParameterResult; |
| 117 |
} |
| 118 |
StatusCode commandBufferGetImpl(const CommandBufferData_1_0 *data) { |
| 119 |
return commandBufferGetResult; |
| 120 |
} |
| 121 |
|
| 122 |
StatusCode commandBufferGetSizeImpl(const CommandBufferData_1_0 *data, CommandBufferSize_1_0 *size) { |
| 123 |
*size = commandBufferGetSizeOutSize; |
| 124 |
return commandBufferGetSizeResult; |
| 125 |
} |
| 126 |
|
| 127 |
StatusCode getDataImpl(GetReportData_1_0 *data) { |
| 128 |
if (getDataExpectedData) { |
| 129 |
EXPECT_EQ(data->Query.Slot, getDataExpectedData->Query.Slot); |
| 130 |
EXPECT_EQ(data->Query.SlotsCount, getDataExpectedData->Query.SlotsCount); |
| 131 |
EXPECT_EQ(data->Query.Handle.data, getDataExpectedData->Query.Handle.data); |
| 132 |
EXPECT_EQ(data->Query.Data, getDataExpectedData->Query.Data); |
| 133 |
EXPECT_EQ(data->Query.DataSize, getDataExpectedData->Query.DataSize); |
| 134 |
EXPECT_EQ(data->Type, getDataExpectedData->Type); |
| 135 |
} |
| 136 |
|
| 137 |
if (!getDataResults.empty()) { |
| 138 |
auto retVal = getDataResults.back(); |
| 139 |
getDataResults.pop_back(); |
| 140 |
return retVal; |
| 141 |
} |
| 142 |
return getDataResult; |
| 143 |
} |
| 144 |
|
| 145 |
ContextHandle_1_0 contextCreateOutHandle{}; |
| 146 |
QueryHandle_1_0 queryCreateOutHandle{}; |
| 147 |
ConfigurationHandle_1_0 configurationCreateOutHandle{}; |
| 148 |
TypedValue_1_0 getParameterOutValue{}; |
| 149 |
CommandBufferSize_1_0 commandBufferGetSizeOutSize{}; |
| 150 |
GetReportData_1_0 *getDataExpectedData = nullptr; |
| 151 |
std::vector<StatusCode> queryCreateResults; |
| 152 |
std::vector<StatusCode> getDataResults; |
| 153 |
StatusCode contextCreateResult = StatusCode::Success; |
| 154 |
StatusCode queryCreateResult = StatusCode::Success; |
| 155 |
StatusCode configurationCreateResult = StatusCode::Success; |
| 156 |
StatusCode configurationActivateResult = StatusCode::Success; |
| 157 |
StatusCode configurationDeactivateResult = StatusCode::Success; |
| 158 |
StatusCode getParameterResult = StatusCode::Success; |
| 159 |
StatusCode commandBufferGetResult = StatusCode::Success; |
| 160 |
StatusCode commandBufferGetSizeResult = StatusCode::Success; |
| 161 |
StatusCode getDataResult = StatusCode::Success; |
| 162 |
|
| 163 |
uint32_t configurationActivationCounter = 0u; |
| 164 |
uint32_t configurationDeactivationCounter = 0u; |
| 165 |
}; |
| 166 |
|
| 167 |
template <> |
| 168 |
struct Mock<MetricsLibrary> : public MetricsLibrary { |
| 169 |
|
| 170 |
public: |
| 171 |
Mock(::L0::OaMetricSourceImp &metricSource); |
| 172 |
~Mock() override; |
| 173 |
|
| 174 |
using MetricsLibrary::handle; |
| 175 |
using MetricsLibrary::initializationState; |
| 176 |
|
| 177 |
// Api mock enable/disable. |
| 178 |
void setMockedApi(MockMetricsLibraryApi *mockedApi); |
| 179 |
|
| 180 |
ADDMETHOD_NOBASE(load, bool, true, ()); |
| 181 |
ADDMETHOD_NOBASE(getContextData, bool, true, (::L0::Device &, ContextCreateData_1_0 &)); |
| 182 |
|
| 183 |
bool getMetricQueryReportSize(size_t &rawDataSize) override { |
| 184 |
rawDataSize = getMetricQueryReportSizeOutSize; |
| 185 |
return getMetricQueryReportSizeResult; |
| 186 |
} |
| 187 |
|
| 188 |
// Not mocked metrics library functions. |
| 189 |
bool metricsLibraryGetContextData(::L0::Device &device, ContextCreateData_1_0 &contextData) { return MetricsLibrary::getContextData(device, contextData); } |
| 190 |
ClientCallbacks_1_0 &metricsLibraryGetCallbacks() { return callbacks; } |
| 191 |
|
| 192 |
// Original metrics library implementation used by metric context. |
| 193 |
::L0::MetricsLibrary *metricsLibrary = nullptr; |
| 194 |
|
| 195 |
// Mocked metrics library api version. |
| 196 |
// We cannot use a static instance here since the gtest validates memory usage, |
| 197 |
// and mocked functions will stay in memory longer than the test. |
| 198 |
static MockMetricsLibraryApi *g_mockApi; // NOLINT(readability-identifier-naming) |
| 199 |
|
| 200 |
size_t getMetricQueryReportSizeOutSize{}; |
| 201 |
bool getMetricQueryReportSizeResult = true; |
| 202 |
}; |
| 203 |
|
| 204 |
template <> |
| 205 |
struct Mock<MetricQueryPool> : public MetricQueryPool { |
| 206 |
Mock(); |
| 207 |
~Mock() override; |
| 208 |
|
| 209 |
ADDMETHOD_NOBASE(metricQueryCreate, ze_result_t, ZE_RESULT_SUCCESS, (uint32_t, zet_metric_query_handle_t *)); |
| 210 |
ADDMETHOD_NOBASE(destroy, ze_result_t, ZE_RESULT_SUCCESS, ()); |
| 211 |
}; |
| 212 |
|
| 213 |
template <> |
| 214 |
struct Mock<MetricQuery> : public MetricQuery { |
| 215 |
Mock(); |
| 216 |
~Mock() override; |
| 217 |
|
| 218 |
ADDMETHOD_NOBASE(getData, ze_result_t, ZE_RESULT_SUCCESS, (size_t *, uint8_t *)); |
| 219 |
ADDMETHOD_NOBASE(reset, ze_result_t, ZE_RESULT_SUCCESS, ()); |
| 220 |
ADDMETHOD_NOBASE(destroy, ze_result_t, ZE_RESULT_SUCCESS, ()); |
| 221 |
}; |
| 222 |
|
| 223 |
class MockIpSamplingOsInterface : public MetricIpSamplingOsInterface { |
| 224 |
|
| 225 |
public: |
| 226 |
ze_result_t getMetricsTimerResolutionReturn = ZE_RESULT_SUCCESS; |
| 227 |
~MockIpSamplingOsInterface() override = default; |
| 228 |
ze_result_t startMeasurement(uint32_t ¬ifyEveryNReports, uint32_t &samplingPeriodNs) override { |
| 229 |
return ZE_RESULT_ERROR_UNKNOWN; |
| 230 |
} |
| 231 |
ze_result_t stopMeasurement() override { return ZE_RESULT_ERROR_UNKNOWN; } |
| 232 |
ze_result_t readData(uint8_t *pRawData, size_t *pRawDataSize) override { return ZE_RESULT_ERROR_UNKNOWN; } |
| 233 |
uint32_t getRequiredBufferSize(const uint32_t maxReportCount) override { return 0; } |
| 234 |
uint32_t getUnitReportSize() override { return 0; } |
| 235 |
bool isNReportsAvailable() override { return false; } |
| 236 |
bool isDependencyAvailable() override { return false; } |
| 237 |
ze_result_t getMetricsTimerResolution(uint64_t &timerResolution) override { |
| 238 |
timerResolution = 12500000UL; |
| 239 |
return getMetricsTimerResolutionReturn; |
| 240 |
} |
| 241 |
}; |
| 242 |
|
| 243 |
class MetricContextFixture : public DeviceFixture { |
| 244 |
|
| 245 |
protected: |
| 246 |
void setUp(); |
| 247 |
void tearDown(); |
| 248 |
void openMetricsAdapter(); |
| 249 |
void openMetricsAdapterGroup(); |
| 250 |
DebugManagerStateRestore restorer; |
| 251 |
|
| 252 |
public: |
| 253 |
// Mocked objects. |
| 254 |
std::unique_ptr<Mock<MetricEnumeration>> mockMetricEnumeration = nullptr; |
| 255 |
std::unique_ptr<Mock<MetricsLibrary>> mockMetricsLibrary = nullptr; |
| 256 |
|
| 257 |
// Mocked metrics library/discovery APIs. |
| 258 |
MockMetricsLibraryApi mockMetricsLibraryApi = {}; |
| 259 |
MockMetricsDiscoveryApi mockMetricsDiscoveryApi = {}; |
| 260 |
|
| 261 |
// Metrics discovery device |
| 262 |
Mock<IAdapterGroup_1_13> adapterGroup; |
| 263 |
Mock<IAdapter_1_13> adapter; |
| 264 |
Mock<IMetricsDevice_1_13> metricsDevice; |
| 265 |
MetricsDiscovery::TMetricsDeviceParams_1_2 metricsDeviceParams = {}; |
| 266 |
MetricsDiscovery::TTypedValue_1_0 defaultMaximumOaBufferSize = {}; |
| 267 |
void setupDefaultMocksForMetricDevice(Mock<IMetricsDevice_1_13> &metricDevice); |
| 268 |
MockIpSamplingOsInterface *mockIpSamplingOsInterface; |
| 269 |
}; |
| 270 |
|
| 271 |
class MetricMultiDeviceFixture : public MultiDeviceFixture { |
| 272 |
|
| 273 |
protected: |
| 274 |
void setUp(); |
| 275 |
void tearDown(); |
| 276 |
void openMetricsAdapter(); |
| 277 |
void openMetricsAdapterSubDevice(uint32_t subDeviceIndex); |
| 278 |
void openMetricsAdapterDeviceAndSubDeviceNoCountVerify(uint32_t subDeviceIndex); |
| 279 |
void openMetricsAdapterGroup(); |
| 280 |
DebugManagerStateRestore restorer; |
| 281 |
|
| 282 |
public: |
| 283 |
std::vector<L0::Device *> devices; |
| 284 |
|
| 285 |
// Mocked objects. |
| 286 |
std::unique_ptr<Mock<MetricEnumeration>> mockMetricEnumeration = nullptr; |
| 287 |
std::unique_ptr<Mock<MetricsLibrary>> mockMetricsLibrary = nullptr; |
| 288 |
|
| 289 |
std::vector<std::unique_ptr<Mock<MetricEnumeration>>> mockMetricEnumerationSubDevices; |
| 290 |
std::vector<std::unique_ptr<Mock<MetricsLibrary>>> mockMetricsLibrarySubDevices; |
| 291 |
|
| 292 |
// Mocked metrics library/discovery APIs. |
| 293 |
MockMetricsLibraryApi mockMetricsLibraryApi = {}; |
| 294 |
MockMetricsDiscoveryApi mockMetricsDiscoveryApi = {}; |
| 295 |
|
| 296 |
// Metrics discovery device |
| 297 |
Mock<IAdapterGroup_1_13> adapterGroup; |
| 298 |
Mock<IAdapter_1_13> adapter; |
| 299 |
Mock<IMetricsDevice_1_13> metricsDevice; |
| 300 |
MetricsDiscovery::TMetricsDeviceParams_1_2 metricsDeviceParams = {}; |
| 301 |
// MetricsDiscovery::TTypedValue_1_0 defaultMaximumOaBufferSize = {}; |
| 302 |
void setupDefaultMocksForMetricDevice(Mock<IMetricsDevice_1_13> &metricDevice); |
| 303 |
}; |
| 304 |
|
| 305 |
class MetricStreamerMultiDeviceFixture : public MetricMultiDeviceFixture { |
| 306 |
public: |
| 307 |
void cleanup(zet_device_handle_t &hDevice, zet_metric_streamer_handle_t &hStreamer); |
| 308 |
}; |
| 309 |
|
| 310 |
} // namespace ult |
| 311 |
} // namespace L0 |