L0 Metrics Api: multi adapter support

Switching to Metrics Discovery adapter interface.
This commit is contained in:
Piotr Maciejewski
2020-12-11 13:11:00 +00:00
committed by Compute-Runtime-Automation
parent 09f9b2896e
commit 4a5599b1ee
16 changed files with 1488 additions and 1153 deletions

View File

@@ -1,14 +1,78 @@
/*
* Copyright (C) 2019-2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/os_interface.h"
#include "level_zero/tools/source/metrics/metric_enumeration_imp.h"
#include <sys/stat.h>
#include <sys/sysmacros.h>
namespace L0 {
const char *MetricEnumeration::getMetricsDiscoveryFilename() { return "libmd.so"; }
bool MetricEnumeration::getAdapterId(uint32_t &adapterMajor, uint32_t &adapterMinor) {
auto &device = metricContext.getDevice();
auto osInterface = device.getOsInterface().get();
auto drm = osInterface->getDrm();
auto drmFile = drm->getFileDescriptor();
struct stat drmStat = {};
int32_t result = fstat(drmFile, &drmStat);
adapterMajor = major(drmStat.st_rdev);
adapterMinor = minor(drmStat.st_rdev);
return result == 0;
}
MetricsDiscovery::IAdapter_1_8 *MetricEnumeration::getMetricsAdapter() {
UNRECOVERABLE_IF(pAdapterGroup == nullptr);
// Obtain drm minor / major version.
uint32_t drmMajor = 0;
uint32_t drmMinor = 0;
UNRECOVERABLE_IF(getAdapterId(drmMajor, drmMinor) == false);
// Driver drm major/minor version.
const int32_t drmNodePrimary = 0; // From xf86drm.h
const int32_t drmNodeRender = 2; // From xf86drm.h
const int32_t drmMaxDevices = 64; // From drm_drv.c#110
const int32_t drmMinorRender = drmMinor - (drmNodeRender * drmMaxDevices);
const int32_t drmMinorPrimary = drmMinor - (drmNodePrimary * drmMaxDevices);
// Enumerate metrics discovery adapters.
for (uint32_t index = 0, count = pAdapterGroup->GetParams()->AdapterCount;
index < count;
++index) {
UNRECOVERABLE_IF(pAdapterGroup->GetAdapter(index) == nullptr);
UNRECOVERABLE_IF(pAdapterGroup->GetAdapter(index)->GetParams() == nullptr);
auto adapter = pAdapterGroup->GetAdapter(index);
auto adapterParams = adapter->GetParams();
const bool validAdapterType = adapterParams->SystemId.Type == MetricsDiscovery::ADAPTER_ID_TYPE_MAJOR_MINOR;
const bool validAdapterMajor = adapterParams->SystemId.MajorMinor.Major == static_cast<int32_t>(drmMajor);
const bool validAdapterMinor = (adapterParams->SystemId.MajorMinor.Minor == drmMinorRender) ||
(adapterParams->SystemId.MajorMinor.Minor == drmMinorPrimary);
if (validAdapterType && validAdapterMajor && validAdapterMinor) {
return adapter;
}
}
return nullptr;
}
} // namespace L0

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -80,20 +80,14 @@ ze_result_t MetricEnumeration::loadMetricsDiscovery() {
// Load exported functions.
if (hMetricsDiscovery) {
openMetricsDevice = reinterpret_cast<MetricsDiscovery::OpenMetricsDevice_fn>(
hMetricsDiscovery->getProcAddress("OpenMetricsDevice"));
closeMetricsDevice = reinterpret_cast<MetricsDiscovery::CloseMetricsDevice_fn>(
hMetricsDiscovery->getProcAddress("CloseMetricsDevice"));
openMetricsDeviceFromFile =
reinterpret_cast<MetricsDiscovery::OpenMetricsDeviceFromFile_fn>(
hMetricsDiscovery->getProcAddress("OpenMetricsDeviceFromFile"));
openAdapterGroup = reinterpret_cast<MetricsDiscovery::OpenAdapterGroup_fn>(
hMetricsDiscovery->getProcAddress("OpenAdapterGroup"));
}
if (openMetricsDevice == nullptr || closeMetricsDevice == nullptr ||
openMetricsDeviceFromFile == nullptr) {
PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "cannot load %s exported functions\n", MetricEnumeration::getMetricsDiscoveryFilename());
if (openAdapterGroup == nullptr) {
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "cannot load %s exported functions\n", MetricEnumeration::getMetricsDiscoveryFilename());
cleanupMetricsDiscovery();
return ZE_RESULT_ERROR_UNKNOWN;
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
// Return success if exported functions have been loaded.
@@ -101,14 +95,36 @@ ze_result_t MetricEnumeration::loadMetricsDiscovery() {
}
ze_result_t MetricEnumeration::openMetricsDiscovery() {
UNRECOVERABLE_IF(openMetricsDevice == nullptr);
UNRECOVERABLE_IF(closeMetricsDevice == nullptr);
UNRECOVERABLE_IF(openAdapterGroup == nullptr);
auto openResult = openMetricsDevice(&pMetricsDevice);
if (openResult != MetricsDiscovery::CC_OK) {
// Clean up members.
pAdapterGroup = nullptr;
pAdapter = nullptr;
pMetricsDevice = nullptr;
// Open adapter group.
openAdapterGroup(&pAdapterGroup);
if (pAdapterGroup == nullptr) {
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "unable to open metrics adapter groups %s\n", " ");
cleanupMetricsDiscovery();
return ZE_RESULT_ERROR_UNKNOWN;
}
// Obtain metrics adapter that matches adapter used by l0.
pAdapter = getMetricsAdapter();
if (pAdapter == nullptr) {
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "unable to open metrics adapter %s\n", " ");
cleanupMetricsDiscovery();
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
pAdapter->OpenMetricsDevice(&pMetricsDevice);
if (pMetricsDevice == nullptr) {
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "unable to open metrics device %s\n", " ");
cleanupMetricsDiscovery();
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
return ZE_RESULT_SUCCESS;
}
@@ -119,16 +135,13 @@ ze_result_t MetricEnumeration::cleanupMetricsDiscovery() {
metricGroups.clear();
if (pMetricsDevice) {
closeMetricsDevice(pMetricsDevice);
if (pAdapter && pMetricsDevice) {
pAdapter->CloseMetricsDevice(pMetricsDevice);
pMetricsDevice = nullptr;
}
if (hMetricsDiscovery != nullptr) {
openMetricsDevice = nullptr;
closeMetricsDevice = nullptr;
openMetricsDeviceFromFile = nullptr;
openAdapterGroup = nullptr;
hMetricsDiscovery.reset();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -32,6 +32,8 @@ struct MetricEnumeration {
ze_result_t initialize();
virtual ze_result_t openMetricsDiscovery();
virtual bool getAdapterId(uint32_t &major, uint32_t &minor);
virtual MetricsDiscovery::IAdapter_1_8 *getMetricsAdapter();
ze_result_t cleanupMetricsDiscovery();
ze_result_t cacheMetricInformation();
@@ -57,9 +59,9 @@ struct MetricEnumeration {
// Metrics Discovery API.
std::unique_ptr<NEO::OsLibrary> hMetricsDiscovery = nullptr;
MetricsDiscovery::OpenMetricsDevice_fn openMetricsDevice = nullptr;
MetricsDiscovery::CloseMetricsDevice_fn closeMetricsDevice = nullptr;
MetricsDiscovery::OpenMetricsDeviceFromFile_fn openMetricsDeviceFromFile = nullptr;
MetricsDiscovery::OpenAdapterGroup_fn openAdapterGroup = nullptr;
MetricsDiscovery::IAdapterGroup_1_8 *pAdapterGroup = nullptr;
MetricsDiscovery::IAdapter_1_8 *pAdapter = nullptr;
MetricsDiscovery::IMetricsDevice_1_5 *pMetricsDevice = nullptr;
public:

View File

@@ -1,10 +1,12 @@
/*
* Copyright (C) 2019-2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/windows/os_interface.h"
#include "level_zero/tools/source/metrics/metric_enumeration_imp.h"
#if defined(_WIN64)
@@ -19,4 +21,47 @@ namespace L0 {
const char *MetricEnumeration::getMetricsDiscoveryFilename() { return METRICS_DISCOVERY_NAME; }
bool MetricEnumeration::getAdapterId(uint32_t &major, uint32_t &minor) {
auto &device = metricContext.getDevice();
auto osInterface = device.getOsInterface().get();
auto luid = osInterface->getWddm()->getAdapterLuid();
major = luid.HighPart;
minor = luid.LowPart;
return true;
}
MetricsDiscovery::IAdapter_1_8 *MetricEnumeration::getMetricsAdapter() {
uint32_t major = 0;
uint32_t minor = 0;
UNRECOVERABLE_IF(pAdapterGroup == nullptr);
UNRECOVERABLE_IF(getAdapterId(major, minor) == false);
// Enumerate metrics discovery adapters.
for (uint32_t index = 0, count = pAdapterGroup->GetParams()->AdapterCount;
index < count;
++index) {
UNRECOVERABLE_IF(pAdapterGroup->GetAdapter(index) == nullptr);
UNRECOVERABLE_IF(pAdapterGroup->GetAdapter(index)->GetParams() == nullptr);
auto adapter = pAdapterGroup->GetAdapter(index);
auto adapterParams = adapter->GetParams();
const bool validAdapterInfo = adapterParams->SystemId.Type == MetricsDiscovery::ADAPTER_ID_TYPE_LUID;
const bool validAdapterMatch = (adapterParams->SystemId.Luid.HighPart == major) &&
(adapterParams->SystemId.Luid.LowPart == minor);
if (validAdapterInfo && validAdapterMatch) {
return adapter;
}
}
return nullptr;
}
} // namespace L0