Files
compute-runtime/level_zero/tools/source/metrics/metric_tracer_imp.cpp
Piotr Maciejewski 8f70ba916a Disable Metrics Library after Metric Tracer usage.
Metric Tracer can use Metrics Library to generate gpu commands 
for Metric Tracer marker. Closing Metrics Library will force it to reintialize,
so Metric Query can use Linux time based sampling stream exclusively 
(needed to activate metric set or to obtain context switch/triggered reports).

Change-Id: I3105febfbbd78b537ef0688d1b9c08ab54f9a875
2020-05-15 22:13:18 +02:00

199 lines
6.7 KiB
C++

/*
* Copyright (C) 2019-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/tools/source/metrics/metric_tracer_imp.h"
#include "shared/source/helpers/debug_helpers.h"
#include "level_zero/core/source/device/device.h"
#include "level_zero/tools/source/metrics/metric_query_imp.h"
namespace L0 {
ze_result_t MetricTracerImp::readData(uint32_t maxReportCount, size_t *pRawDataSize,
uint8_t *pRawData) {
DEBUG_BREAK_IF(rawReportSize == 0);
auto metricGroup = MetricGroup::fromHandle(hMetricGroup);
// Return required size if requested.
if (*pRawDataSize == 0) {
*pRawDataSize = getRequiredBufferSize(maxReportCount);
return ZE_RESULT_SUCCESS;
}
// User is expected to allocate space.
if (pRawData == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
// Retrieve the number of reports that fit into the buffer.
uint32_t reportCount = static_cast<uint32_t>(*pRawDataSize / rawReportSize);
// Read tracer data.
const ze_result_t result = metricGroup->readIoStream(reportCount, *pRawData);
if (result == ZE_RESULT_SUCCESS) {
*pRawDataSize = reportCount * rawReportSize;
}
return result;
}
ze_result_t MetricTracerImp::close() {
const auto result = stopMeasurements();
if (result == ZE_RESULT_SUCCESS) {
auto device = Device::fromHandle(hDevice);
auto &metricContext = device->getMetricContext();
auto &metricsLibrary = metricContext.getMetricsLibrary();
// Clear metric tracer reference in context.
// Another metric tracer instance or query can be used.
metricContext.setMetricTracer(nullptr);
// Close metrics library (if was used to generate tracer's marker gpu commands).
// It will allow metric query to use Linux Tbs stream exclusively
// (to activate metric sets and to read context switch reports).
metricsLibrary.release();
// Release notification event.
if (pNotificationEvent != nullptr) {
pNotificationEvent->metricTracer = nullptr;
}
// Delete metric tracer.
delete this;
}
return result;
}
ze_result_t MetricTracerImp::initialize(ze_device_handle_t hDevice,
zet_metric_group_handle_t hMetricGroup) {
this->hDevice = hDevice;
this->hMetricGroup = hMetricGroup;
auto metricGroup = MetricGroup::fromHandle(this->hMetricGroup);
rawReportSize = metricGroup->getRawReportSize();
return ZE_RESULT_SUCCESS;
}
ze_result_t MetricTracerImp::startMeasurements(uint32_t &notifyEveryNReports,
uint32_t &samplingPeriodNs,
ze_event_handle_t hNotificationEvent) {
auto metricGroup = MetricGroup::fromHandle(hMetricGroup);
uint32_t requestedOaBufferSize = getOaBufferSize(notifyEveryNReports);
const ze_result_t result = metricGroup->openIoStream(samplingPeriodNs, requestedOaBufferSize);
// Return oa buffer size and notification event aligned to gpu capabilities.
if (result == ZE_RESULT_SUCCESS) {
oaBufferSize = requestedOaBufferSize;
notifyEveryNReports = getNotifyEveryNReports(requestedOaBufferSize);
}
// Associate notification event with metric tracer.
pNotificationEvent = Event::fromHandle(hNotificationEvent);
if (pNotificationEvent != nullptr) {
pNotificationEvent->metricTracer = this;
}
return result;
}
ze_result_t MetricTracerImp::stopMeasurements() {
auto metricGroup = MetricGroup::fromHandle(hMetricGroup);
const ze_result_t result = metricGroup->closeIoStream();
if (result == ZE_RESULT_SUCCESS) {
oaBufferSize = 0;
}
return result;
}
uint32_t MetricTracerImp::getOaBufferSize(const uint32_t notifyEveryNReports) const {
// Notification is on half full buffer, hence multiplication by 2.
return notifyEveryNReports * rawReportSize * 2;
}
uint32_t MetricTracerImp::getNotifyEveryNReports(const uint32_t oaBufferSize) const {
// Notification is on half full buffer, hence division by 2.
return rawReportSize
? oaBufferSize / (rawReportSize * 2)
: 0;
}
Event::State MetricTracerImp::getNotificationState() {
auto metricGroup = MetricGroup::fromHandle(hMetricGroup);
bool reportsReady = metricGroup->waitForReports(0) == ZE_RESULT_SUCCESS;
return reportsReady
? Event::State::STATE_SIGNALED
: Event::State::STATE_INITIAL;
}
uint32_t MetricTracerImp::getRequiredBufferSize(const uint32_t maxReportCount) const {
DEBUG_BREAK_IF(rawReportSize == 0);
uint32_t maxOaBufferReportCount = oaBufferSize / rawReportSize;
// Trim to OA buffer size if needed.
return maxReportCount > maxOaBufferReportCount ? oaBufferSize
: maxReportCount * rawReportSize;
}
ze_result_t MetricTracer::open(zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
zet_metric_tracer_desc_t &desc, ze_event_handle_t hNotificationEvent,
zet_metric_tracer_handle_t *phMetricTracer) {
auto pDevice = Device::fromHandle(hDevice);
auto &metricContext = pDevice->getMetricContext();
*phMetricTracer = nullptr;
// Check whether metric tracer is already open.
if (metricContext.getMetricTracer() != nullptr) {
return ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE;
}
// Metric tracer cannot be used with query simultaneously
// (oa buffer cannot be shared).
if (metricContext.getMetricsLibrary().getMetricQueryCount() > 0) {
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
// Check metric group sampling type.
auto metricGroupProperties = MetricGroup::getProperties(hMetricGroup);
if (metricGroupProperties.samplingType != ZET_METRIC_GROUP_SAMPLING_TYPE_TIME_BASED) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
// Check whether metric group is activated.
if (!metricContext.isMetricGroupActivated(hMetricGroup)) {
return ZE_RESULT_NOT_READY;
}
auto pMetricTracer = new MetricTracerImp();
UNRECOVERABLE_IF(pMetricTracer == nullptr);
pMetricTracer->initialize(hDevice, hMetricGroup);
const ze_result_t result = pMetricTracer->startMeasurements(
desc.notifyEveryNReports, desc.samplingPeriod, hNotificationEvent);
if (result == ZE_RESULT_SUCCESS) {
metricContext.setMetricTracer(pMetricTracer);
} else {
delete pMetricTracer;
pMetricTracer = nullptr;
return ZE_RESULT_ERROR_UNKNOWN;
}
*phMetricTracer = pMetricTracer->toHandle();
return ZE_RESULT_SUCCESS;
}
} // namespace L0