Skip to content

Commit

Permalink
Metrics Refactor Metric Group
Browse files Browse the repository at this point in the history
Refactor Metric Group Implementation to move OA specific
implementation to OA specific classes.
This is so that stall sampling specific Metric Group
implementation could be done seamlessly.

Related-To: LOCI-2753

Signed-off-by: Joshua Santosh Ranjan <[email protected]>
  • Loading branch information
joshuaranjan authored and Compute-Runtime-Automation committed Feb 4, 2022
1 parent 10e4e62 commit 93e117f
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 75 deletions.
4 changes: 2 additions & 2 deletions level_zero/api/tools/zet_metric.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2021 Intel Corporation
* Copyright (C) 2020-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -30,7 +30,7 @@ zetMetricGet(
zet_metric_group_handle_t hMetricGroup,
uint32_t *pCount,
zet_metric_handle_t *phMetrics) {
return L0::MetricGroup::fromHandle(hMetricGroup)->getMetric(pCount, phMetrics);
return L0::MetricGroup::fromHandle(hMetricGroup)->metricGet(pCount, phMetrics);
}

ZE_DLLEXPORT ze_result_t ZE_APICALL
Expand Down
7 changes: 4 additions & 3 deletions level_zero/tools/source/metrics/metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ ze_result_t MetricDeviceContext::activateMetricGroupsDeferred(uint32_t count, ze
for (auto index = 0u; index < count; index++) {

zet_metric_group_handle_t hMetricGroup = MetricGroup::fromHandle(phMetricGroups[index])->getMetricGroupForSubDevice(subDeviceIndex);
auto domain = MetricGroup::getProperties(hMetricGroup).domain;
zet_metric_group_properties_t properties = {ZET_STRUCTURE_TYPE_METRIC_GROUP_PROPERTIES};
MetricGroup::fromHandle(hMetricGroup)->getProperties(&properties);
auto domain = properties.domain;
// Domain already associated with the same handle.
if (domains[domain].first == hMetricGroup) {
continue;
Expand Down Expand Up @@ -370,8 +372,7 @@ ze_result_t metricGroupGet(zet_device_handle_t hDevice, uint32_t *pCount, zet_me
ze_result_t metricStreamerOpen(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
zet_metric_streamer_desc_t *pDesc, ze_event_handle_t hNotificationEvent,
zet_metric_streamer_handle_t *phMetricStreamer) {

return MetricStreamer::open(hContext, hDevice, hMetricGroup, *pDesc, hNotificationEvent, phMetricStreamer);
return MetricGroup::fromHandle(hMetricGroup)->streamerOpen(hContext, hDevice, pDesc, hNotificationEvent, phMetricStreamer);
}

template <>
Expand Down
36 changes: 12 additions & 24 deletions level_zero/tools/source/metrics/metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,32 @@ struct MetricGroup : _zet_metric_group_handle_t {
virtual ~MetricGroup() = default;

virtual ze_result_t getProperties(zet_metric_group_properties_t *pProperties) = 0;
virtual ze_result_t getMetric(uint32_t *pCount, zet_metric_handle_t *phMetrics) = 0;
virtual ze_result_t metricGet(uint32_t *pCount, zet_metric_handle_t *phMetrics) = 0;
virtual ze_result_t calculateMetricValues(const zet_metric_group_calculation_type_t type, size_t rawDataSize,
const uint8_t *pRawData, uint32_t *pMetricValueCount,
zet_typed_value_t *pMetricValues) = 0;
virtual ze_result_t calculateMetricValuesExp(const zet_metric_group_calculation_type_t type, size_t rawDataSize,
const uint8_t *pRawData, uint32_t *pSetCount,
uint32_t *pTotalMetricValueCount, uint32_t *pMetricCounts,
zet_typed_value_t *pMetricValues) = 0;

static MetricGroup *create(zet_metric_group_properties_t &properties,
MetricsDiscovery::IMetricSet_1_5 &metricSet,
MetricsDiscovery::IConcurrentGroup_1_5 &concurrentGroup,
const std::vector<Metric *> &metrics,
MetricSource &metricSource);
static MetricGroup *fromHandle(zet_metric_group_handle_t handle) {
return static_cast<MetricGroup *>(handle);
}
static zet_metric_group_properties_t getProperties(const zet_metric_group_handle_t handle);

zet_metric_group_handle_t toHandle() { return this; }

virtual uint32_t getRawReportSize() = 0;

virtual bool activate() = 0;
virtual bool deactivate() = 0;
virtual zet_metric_group_handle_t getMetricGroupForSubDevice(const uint32_t subDeviceIndex) = 0;

virtual ze_result_t openIoStream(uint32_t &timerPeriodNs, uint32_t &oaBufferSize) = 0;
virtual ze_result_t waitForReports(const uint32_t timeoutMs) = 0;
virtual ze_result_t readIoStream(uint32_t &reportCount, uint8_t &reportData) = 0;
virtual ze_result_t closeIoStream() = 0;
virtual ze_result_t streamerOpen(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
zet_metric_streamer_desc_t *desc,
ze_event_handle_t hNotificationEvent,
zet_metric_streamer_handle_t *phMetricStreamer) = 0;
virtual ze_result_t metricQueryPoolCreate(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
const zet_metric_query_pool_desc_t *desc,
zet_metric_query_pool_handle_t *phMetricQueryPool) = 0;
};

struct MetricGroupCalculateHeader {
Expand All @@ -126,19 +121,12 @@ struct MetricGroupCalculateHeader {

struct MetricStreamer : _zet_metric_streamer_handle_t {
virtual ~MetricStreamer() = default;

virtual ze_result_t readData(uint32_t maxReportCount, size_t *pRawDataSize,
uint8_t *pRawData) = 0;
virtual ze_result_t close() = 0;
static ze_result_t openForDevice(Device *pDevice, zet_metric_group_handle_t hMetricGroup,
zet_metric_streamer_desc_t &desc,
zet_metric_streamer_handle_t *phMetricStreamer);
static ze_result_t open(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
zet_metric_streamer_desc_t &desc, ze_event_handle_t hNotificationEvent, zet_metric_streamer_handle_t *phMetricStreamer);
static MetricStreamer *fromHandle(zet_metric_streamer_handle_t handle) {
return static_cast<MetricStreamer *>(handle);
}

virtual Event::State getNotificationState() = 0;
inline zet_metric_streamer_handle_t toHandle() { return this; }
};
Expand Down
20 changes: 10 additions & 10 deletions level_zero/tools/source/metrics/metric_enumeration_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ MetricEnumeration::cacheMetricGroup(MetricsDiscovery::IMetricSet_1_5 &metricSet,
std::vector<Metric *> metrics;
createMetrics(metricSet, metrics);

auto pMetricGroup = MetricGroup::create(properties, metricSet, concurrentGroup, metrics, metricSource);
auto pMetricGroup = OaMetricGroupImp::create(properties, metricSet, concurrentGroup, metrics, metricSource);
DEBUG_BREAK_IF(pMetricGroup == nullptr);

metricGroups.push_back(pMetricGroup);
Expand Down Expand Up @@ -485,14 +485,14 @@ OaMetricGroupImp ::~OaMetricGroupImp() {

ze_result_t OaMetricGroupImp::getProperties(zet_metric_group_properties_t *pProperties) {
if (metricGroups.size() > 0) {
*pProperties = MetricGroup::getProperties(metricGroups[0]);
*pProperties = OaMetricGroupImp::getProperties(metricGroups[0]);
} else {
copyProperties(properties, *pProperties);
}
return ZE_RESULT_SUCCESS;
}

zet_metric_group_properties_t MetricGroup::getProperties(const zet_metric_group_handle_t handle) {
zet_metric_group_properties_t OaMetricGroupImp::getProperties(const zet_metric_group_handle_t handle) {
auto metricGroup = MetricGroup::fromHandle(handle);
UNRECOVERABLE_IF(!metricGroup);

Expand All @@ -502,11 +502,11 @@ zet_metric_group_properties_t MetricGroup::getProperties(const zet_metric_group_
return properties;
}

ze_result_t OaMetricGroupImp::getMetric(uint32_t *pCount, zet_metric_handle_t *phMetrics) {
ze_result_t OaMetricGroupImp::metricGet(uint32_t *pCount, zet_metric_handle_t *phMetrics) {

if (metricGroups.size() > 0) {
auto metricGroupSubDevice = MetricGroup::fromHandle(metricGroups[0]);
return metricGroupSubDevice->getMetric(pCount, phMetrics);
return metricGroupSubDevice->metricGet(pCount, phMetrics);
}

if (*pCount == 0) {
Expand Down Expand Up @@ -916,11 +916,11 @@ void OaMetricImp::copyProperties(const zet_metric_properties_t &source,
source.resultUnits, sizeof(destination.resultUnits));
}

MetricGroup *MetricGroup::create(zet_metric_group_properties_t &properties,
MetricsDiscovery::IMetricSet_1_5 &metricSet,
MetricsDiscovery::IConcurrentGroup_1_5 &concurrentGroup,
const std::vector<Metric *> &metrics,
MetricSource &metricSource) {
MetricGroup *OaMetricGroupImp::create(zet_metric_group_properties_t &properties,
MetricsDiscovery::IMetricSet_1_5 &metricSet,
MetricsDiscovery::IConcurrentGroup_1_5 &concurrentGroup,
const std::vector<Metric *> &metrics,
MetricSource &metricSource) {
auto pMetricGroup = new OaMetricGroupImp();
UNRECOVERABLE_IF(pMetricGroup == nullptr);
pMetricGroup->initialize(properties, metricSet, concurrentGroup, metrics, static_cast<OaMetricSourceImp &>(metricSource));
Expand Down
35 changes: 28 additions & 7 deletions level_zero/tools/source/metrics/metric_enumeration_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct OaMetricGroupImp : MetricGroup {
~OaMetricGroupImp() override;

ze_result_t getProperties(zet_metric_group_properties_t *pProperties) override;
ze_result_t getMetric(uint32_t *pCount, zet_metric_handle_t *phMetrics) override;
ze_result_t metricGet(uint32_t *pCount, zet_metric_handle_t *phMetrics) override;
ze_result_t calculateMetricValues(const zet_metric_group_calculation_type_t type, size_t rawDataSize, const uint8_t *pRawData,
uint32_t *pMetricValueCount,
zet_typed_value_t *pCalculatedData) override;
Expand All @@ -91,8 +91,6 @@ struct OaMetricGroupImp : MetricGroup {
const std::vector<Metric *> &groupMetrics,
OaMetricSourceImp &metricSource);

uint32_t getRawReportSize() override;

bool activate() override;
bool deactivate() override;

Expand All @@ -103,12 +101,31 @@ struct OaMetricGroupImp : MetricGroup {
zet_metric_group_handle_t getMetricGroupForSubDevice(const uint32_t subDeviceIndex) override;

// Time based measurements.
ze_result_t openIoStream(uint32_t &timerPeriodNs, uint32_t &oaBufferSize) override;
ze_result_t waitForReports(const uint32_t timeoutMs) override;
ze_result_t readIoStream(uint32_t &reportCount, uint8_t &reportData) override;
ze_result_t closeIoStream() override;
ze_result_t openIoStream(uint32_t &timerPeriodNs, uint32_t &oaBufferSize);
ze_result_t waitForReports(const uint32_t timeoutMs);
ze_result_t readIoStream(uint32_t &reportCount, uint8_t &reportData);
ze_result_t closeIoStream();

std::vector<zet_metric_group_handle_t> &getMetricGroups();
ze_result_t streamerOpen(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
zet_metric_streamer_desc_t *desc,
ze_event_handle_t hNotificationEvent,
zet_metric_streamer_handle_t *phMetricStreamer) override;

ze_result_t metricQueryPoolCreate(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
const zet_metric_query_pool_desc_t *desc,
zet_metric_query_pool_handle_t *phMetricQueryPool) override;
static MetricGroup *create(zet_metric_group_properties_t &properties,
MetricsDiscovery::IMetricSet_1_5 &metricSet,
MetricsDiscovery::IConcurrentGroup_1_5 &concurrentGroup,
const std::vector<Metric *> &metrics,
MetricSource &metricSource);
static zet_metric_group_properties_t getProperties(const zet_metric_group_handle_t handle);
uint32_t getRawReportSize();

protected:
void copyProperties(const zet_metric_group_properties_t &source,
Expand All @@ -134,6 +151,10 @@ struct OaMetricGroupImp : MetricGroup {
std::vector<zet_metric_group_handle_t> metricGroups;

OaMetricSourceImp *metricSource;

private:
ze_result_t openForDevice(Device *pDevice, zet_metric_streamer_desc_t &desc,
zet_metric_streamer_handle_t *phMetricStreamer);
};

struct OaMetricImp : Metric {
Expand Down
28 changes: 24 additions & 4 deletions level_zero/tools/source/metrics/metric_query_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ ConfigurationHandle_1_0 MetricsLibrary::addConfiguration(zet_metric_group_handle

// Create metrics library configuration.
auto metricGroup = MetricGroup::fromHandle(handle);
auto properties = MetricGroup::getProperties(handle);
auto properties = OaMetricGroupImp::getProperties(handle);
auto configuration = createConfiguration(metricGroup, properties);

// Cache configuration if valid.
Expand All @@ -421,9 +421,17 @@ void MetricsLibrary::deleteAllConfigurations() {
configurations.clear();
}

ze_result_t metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
const zet_metric_query_pool_desc_t *pDesc, zet_metric_query_pool_handle_t *phMetricQueryPool) {
ze_result_t OaMetricGroupImp::metricQueryPoolCreate(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
const zet_metric_query_pool_desc_t *desc,
zet_metric_query_pool_handle_t *phMetricQueryPool) {

return OaMetricQueryPoolImp::metricQueryPoolCreate(hContext, hDevice, toHandle(), desc, phMetricQueryPool);
}

ze_result_t OaMetricQueryPoolImp::metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
const zet_metric_query_pool_desc_t *pDesc, zet_metric_query_pool_handle_t *phMetricQueryPool) {
auto device = Device::fromHandle(hDevice);
auto &metricSource = device->getMetricDeviceContext().getMetricSource<OaMetricSourceImp>();

Expand All @@ -440,6 +448,7 @@ ze_result_t metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_hand
if (metricSource.isImplicitScalingCapable()) {

auto emptyMetricGroups = std::vector<zet_metric_group_handle_t>();

auto &metricGroups = hMetricGroup
? static_cast<OaMetricGroupImp *>(MetricGroup::fromHandle(hMetricGroup))->getMetricGroups()
: emptyMetricGroups;
Expand Down Expand Up @@ -498,6 +507,17 @@ ze_result_t metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_hand
return ZE_RESULT_SUCCESS;
}

ze_result_t metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
const zet_metric_query_pool_desc_t *pDesc, zet_metric_query_pool_handle_t *phMetricQueryPool) {

if (pDesc->type == ZET_METRIC_QUERY_POOL_TYPE_EXECUTION) {
return OaMetricQueryPoolImp::metricQueryPoolCreate(hContext, hDevice, hMetricGroup, pDesc, phMetricQueryPool);
} else {
UNRECOVERABLE_IF(hMetricGroup == nullptr);
return MetricGroup::fromHandle(hMetricGroup)->metricQueryPoolCreate(hContext, hDevice, pDesc, phMetricQueryPool);
}
}

OaMetricQueryPoolImp::OaMetricQueryPoolImp(OaMetricSourceImp &metricSourceInput,
zet_metric_group_handle_t hEventMetricGroupInput,
const zet_metric_query_pool_desc_t &poolDescription)
Expand Down Expand Up @@ -584,7 +604,7 @@ bool OaMetricQueryPoolImp::allocateGpuMemory() {

bool OaMetricQueryPoolImp::createMetricQueryPool() {
// Validate metric group query - only event based is supported.
auto metricGroupProperites = MetricGroup::getProperties(hMetricGroup);
auto metricGroupProperites = OaMetricGroupImp::getProperties(hMetricGroup);
const bool validMetricGroup = metricGroupProperites.samplingType == ZET_METRIC_GROUP_SAMPLING_TYPE_FLAG_EVENT_BASED;

if (!validMetricGroup) {
Expand Down
3 changes: 3 additions & 0 deletions level_zero/tools/source/metrics/metric_query_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ struct OaMetricQueryPoolImp : MetricQueryPool {

std::vector<zet_metric_query_pool_handle_t> &getMetricQueryPools();

static ze_result_t metricQueryPoolCreate(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
const zet_metric_query_pool_desc_t *pDesc, zet_metric_query_pool_handle_t *phMetricQueryPool);

protected:
bool createMetricQueryPool();
bool createSkipExecutionQueryPool();
Expand Down
Loading

0 comments on commit 93e117f

Please sign in to comment.