Move performance counters to shared

Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2021-09-24 09:02:44 +00:00
committed by Compute-Runtime-Automation
parent 6f0baaeab9
commit 69390c7eca
19 changed files with 20 additions and 17 deletions

View File

@@ -9,8 +9,6 @@ set(RUNTIME_SRCS_OS_INTERFACE_BASE
${CMAKE_CURRENT_SOURCE_DIR}/metrics_library.cpp
${CMAKE_CURRENT_SOURCE_DIR}/metrics_library.h
${CMAKE_CURRENT_SOURCE_DIR}/ocl_reg_path.h
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters.cpp
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters.h
)
target_sources(${NEO_STATIC_LIB_NAME} PRIVATE ${RUNTIME_SRCS_OS_INTERFACE_BASE})

View File

@@ -11,8 +11,6 @@ set(RUNTIME_SRCS_OS_INTERFACE_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/device_caps_init_linux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ocl_reg_path.cpp
${CMAKE_CURRENT_SOURCE_DIR}/os_metrics_library.cpp
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_linux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_linux.h
)
if(UNIX)

View File

@@ -1,89 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "performance_counters_linux.h"
#include "shared/source/device/device.h"
#include "shared/source/device/sub_device.h"
#include "shared/source/helpers/hw_helper.h"
#include "shared/source/os_interface/linux/os_time_linux.h"
#include "shared/source/os_interface/os_interface.h"
namespace NEO {
////////////////////////////////////////////////////
// PerformanceCounters::create
////////////////////////////////////////////////////
std::unique_ptr<PerformanceCounters> PerformanceCounters::create(Device *device) {
auto counter = std::make_unique<PerformanceCountersLinux>();
auto drm = device->getOSTime()->getOSInterface()->getDriverModel()->as<Drm>();
auto gen = device->getHardwareInfo().platform.eRenderCoreFamily;
auto &hwHelper = HwHelper::get(gen);
UNRECOVERABLE_IF(counter == nullptr);
if (!device->isSubDevice()) {
// Root device.
counter->subDevice.Enabled = false;
counter->subDeviceIndex.Index = 0;
counter->subDeviceCount.Count = std::max(device->getNumSubDevices(), 1u);
} else {
// Sub device.
counter->subDevice.Enabled = true;
counter->subDeviceIndex.Index = static_cast<NEO::SubDevice *>(device)->getSubDeviceIndex();
counter->subDeviceCount.Count = std::max(device->getRootDevice()->getNumSubDevices(), 1u);
}
// Adapter data.
counter->adapter.Type = LinuxAdapterType::DrmFileDescriptor;
counter->adapter.DrmFileDescriptor = drm->getFileDescriptor();
counter->clientData.Linux.Adapter = &(counter->adapter);
// Gen data.
counter->clientType.Gen = static_cast<MetricsLibraryApi::ClientGen>(hwHelper.getMetricsLibraryGenId());
return counter;
}
//////////////////////////////////////////////////////
// PerformanceCountersLinux::enableCountersConfiguration
//////////////////////////////////////////////////////
bool PerformanceCountersLinux::enableCountersConfiguration() {
// Release previous counters configuration so the user
// can change configuration between kernels.
releaseCountersConfiguration();
// Create oa configuration.
if (!metricsLibrary->oaConfigurationCreate(
context,
oaConfiguration)) {
DEBUG_BREAK_IF(true);
return false;
}
// Enable oa configuration.
if (!metricsLibrary->oaConfigurationActivate(
oaConfiguration)) {
DEBUG_BREAK_IF(true);
return false;
}
return true;
}
//////////////////////////////////////////////////////
// PerformanceCountersLinux::releaseCountersConfiguration
//////////////////////////////////////////////////////
void PerformanceCountersLinux::releaseCountersConfiguration() {
// Oa configuration.
if (oaConfiguration.IsValid()) {
metricsLibrary->oaConfigurationDeactivate(oaConfiguration);
metricsLibrary->oaConfigurationDelete(oaConfiguration);
oaConfiguration.data = nullptr;
}
}
} // namespace NEO

View File

@@ -1,32 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "opencl/source/os_interface/performance_counters.h"
namespace NEO {
using MetricsLibraryApi::ClientDataLinuxAdapter_1_0;
using MetricsLibraryApi::LinuxAdapterType;
class PerformanceCountersLinux : public PerformanceCounters {
public:
PerformanceCountersLinux() = default;
~PerformanceCountersLinux() override = default;
/////////////////////////////////////////////////////
// Gpu oa/mmio configuration.
/////////////////////////////////////////////////////
bool enableCountersConfiguration() override;
void releaseCountersConfiguration() override;
/////////////////////////////////////////////////////
// Metrics Library client adapter data.
/////////////////////////////////////////////////////
ClientDataLinuxAdapter_1_0 adapter = {};
};
} // namespace NEO

View File

@@ -1,289 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "opencl/source/os_interface/performance_counters.h"
#include "shared/source/helpers/engine_node_helper.h"
#include "shared/source/os_interface/os_context.h"
#include "shared/source/utilities/tag_allocator.h"
#include "opencl/source/command_queue/command_queue.h"
using namespace MetricsLibraryApi;
namespace NEO {
//////////////////////////////////////////////////////
// PerformanceCounters constructor.
//////////////////////////////////////////////////////
PerformanceCounters::PerformanceCounters() {
metricsLibrary = std::make_unique<MetricsLibrary>();
UNRECOVERABLE_IF(metricsLibrary == nullptr);
}
//////////////////////////////////////////////////////
// PerformanceCounters::getReferenceNumber
//////////////////////////////////////////////////////
uint32_t PerformanceCounters::getReferenceNumber() {
std::lock_guard<std::mutex> lockMutex(mutex);
return referenceCounter;
}
//////////////////////////////////////////////////////
// PerformanceCounters::enable
//////////////////////////////////////////////////////
bool PerformanceCounters::enable(bool ccsEngine) {
std::lock_guard<std::mutex> lockMutex(mutex);
if (referenceCounter == 0) {
available = openMetricsLibrary();
this->usingCcsEngine = ccsEngine;
}
referenceCounter++;
return available && (this->usingCcsEngine == ccsEngine);
}
//////////////////////////////////////////////////////
// PerformanceCounters::shutdown
//////////////////////////////////////////////////////
void PerformanceCounters::shutdown() {
std::lock_guard<std::mutex> lockMutex(mutex);
if (referenceCounter >= 1) {
if (referenceCounter == 1) {
available = false;
closeMetricsLibrary();
}
referenceCounter--;
}
}
//////////////////////////////////////////////////////
// PerformanceCounters::getMetricsLibraryInterface
//////////////////////////////////////////////////////
MetricsLibrary *PerformanceCounters::getMetricsLibraryInterface() {
return metricsLibrary.get();
}
//////////////////////////////////////////////////////
// PerformanceCounters::setMetricsLibraryInterface
//////////////////////////////////////////////////////
void PerformanceCounters::setMetricsLibraryInterface(std::unique_ptr<MetricsLibrary> newMetricsLibrary) {
metricsLibrary = std::move(newMetricsLibrary);
}
//////////////////////////////////////////////////////
// PerformanceCounters::getMetricsLibraryContext
//////////////////////////////////////////////////////
ContextHandle_1_0 PerformanceCounters::getMetricsLibraryContext() {
return context;
}
//////////////////////////////////////////////////////
// PerformanceCounters::openMetricsLibrary
//////////////////////////////////////////////////////
bool PerformanceCounters::openMetricsLibrary() {
// Open metrics library.
bool result = metricsLibrary->open();
DEBUG_BREAK_IF(!result);
// Create metrics library context.
if (result) {
result = metricsLibrary->contextCreate(
clientType,
subDevice,
subDeviceIndex,
subDeviceCount,
clientData,
contextData,
context);
// Validate gpu report size.
DEBUG_BREAK_IF(!metricsLibrary->hwCountersGetGpuReportSize());
}
// Error handling.
if (!result) {
closeMetricsLibrary();
}
return result;
}
//////////////////////////////////////////////////////
// PerformanceCounters::closeMetricsLibrary
//////////////////////////////////////////////////////
void PerformanceCounters::closeMetricsLibrary() {
// Destroy oa configuration.
releaseCountersConfiguration();
// Destroy hw counters query.
if (query.IsValid()) {
metricsLibrary->hwCountersDelete(query);
query = {};
}
// Destroy metrics library context.
if (context.IsValid()) {
metricsLibrary->contextDelete(context);
context = {};
}
}
//////////////////////////////////////////////////////
// PerformanceCounters::getQueryHandle
//////////////////////////////////////////////////////
void PerformanceCounters::getQueryHandleRef(QueryHandle_1_0 &handle) {
if (!handle.IsValid()) {
metricsLibrary->hwCountersCreate(
context,
1,
nullptr,
handle);
}
DEBUG_BREAK_IF(!handle.IsValid());
}
//////////////////////////////////////////////////////
// PerformanceCounters::getQueryHandle
//////////////////////////////////////////////////////
void PerformanceCounters::deleteQuery(QueryHandle_1_0 &handle) {
metricsLibrary->hwCountersDelete(handle);
}
//////////////////////////////////////////////////////
// PerformanceCounters::getGpuCommandsSize
//////////////////////////////////////////////////////
uint32_t PerformanceCounters::getGpuCommandsSize(CommandQueue &commandQueue, const bool reservePerfCounters) {
uint32_t size = 0;
if (reservePerfCounters) {
const auto performanceCounters = commandQueue.getPerfCounters();
const auto commandBufferType = EngineHelpers::isCcs(commandQueue.getGpgpuEngine().osContext->getEngineType())
? MetricsLibraryApi::GpuCommandBufferType::Compute
: MetricsLibraryApi::GpuCommandBufferType::Render;
size += performanceCounters->getGpuCommandsSize(commandBufferType, true);
size += performanceCounters->getGpuCommandsSize(commandBufferType, false);
}
return size;
}
//////////////////////////////////////////////////////
// PerformanceCounters::getGpuCommandsSize
//////////////////////////////////////////////////////
uint32_t PerformanceCounters::getGpuCommandsSize(
const MetricsLibraryApi::GpuCommandBufferType commandBufferType,
const bool begin) {
CommandBufferData_1_0 bufferData = {};
CommandBufferSize_1_0 bufferSize = {};
if (begin) {
// Load currently activated (through metrics discovery) oa configuration and use it.
// It will allow to change counters configuration between subsequent clEnqueueNDCommandRange calls.
if (!enableCountersConfiguration()) {
return 0;
}
}
bufferData.HandleContext = context;
bufferData.CommandsType = ObjectType::QueryHwCounters;
bufferData.Type = commandBufferType;
getQueryHandleRef(query);
bufferData.QueryHwCounters.Begin = begin;
bufferData.QueryHwCounters.Handle = query;
return metricsLibrary->commandBufferGetSize(bufferData, bufferSize)
? bufferSize.GpuMemorySize
: 0;
}
//////////////////////////////////////////////////////
// PerformanceCounters::getGpuCommands
//////////////////////////////////////////////////////
bool PerformanceCounters::getGpuCommands(
const MetricsLibraryApi::GpuCommandBufferType commandBufferType,
TagNodeBase &performanceCounters,
const bool begin,
const uint32_t bufferSize,
void *pBuffer) {
// Command Buffer data.
CommandBufferData_1_0 bufferData = {};
bufferData.HandleContext = context;
bufferData.CommandsType = ObjectType::QueryHwCounters;
bufferData.Data = pBuffer;
bufferData.Size = bufferSize;
bufferData.Type = commandBufferType;
// Gpu memory allocation for query hw counters.
const uint32_t allocationOffset = offsetof(HwPerfCounter, report);
bufferData.Allocation.CpuAddress = reinterpret_cast<uint8_t *>(performanceCounters.getCpuBase()) + allocationOffset;
bufferData.Allocation.GpuAddress = performanceCounters.getGpuAddress() + allocationOffset;
// Allocate query handle for cl_event if not exists.
getQueryHandleRef(performanceCounters.getQueryHandleRef());
// Query hw counters specific data.
bufferData.QueryHwCounters.Begin = begin;
bufferData.QueryHwCounters.Handle = performanceCounters.getQueryHandleRef();
return metricsLibrary->commandBufferGet(bufferData);
}
//////////////////////////////////////////////////////
// PerformanceCounters::getApiReportSize
//////////////////////////////////////////////////////
uint32_t PerformanceCounters::getApiReportSize() {
return metricsLibrary->hwCountersGetApiReportSize();
}
//////////////////////////////////////////////////////
// PerformanceCounters::getGpuReportSize
//////////////////////////////////////////////////////
uint32_t PerformanceCounters::getGpuReportSize() {
return metricsLibrary->hwCountersGetGpuReportSize();
}
//////////////////////////////////////////////////////
// PerformanceCounters::getApiReport
//////////////////////////////////////////////////////
bool PerformanceCounters::getApiReport(const TagNodeBase *performanceCounters, const size_t inputParamSize, void *pInputParam, size_t *pOutputParamSize, bool isEventComplete) {
const uint32_t outputSize = metricsLibrary->hwCountersGetApiReportSize();
if (pOutputParamSize) {
*pOutputParamSize = outputSize;
}
if (!performanceCounters) {
return false;
}
if (pInputParam == nullptr && inputParamSize == 0 && pOutputParamSize) {
return true;
}
if (pInputParam == nullptr || isEventComplete == false) {
return false;
}
if (inputParamSize < outputSize) {
return false;
}
return metricsLibrary->hwCountersGetReport(performanceCounters->getQueryHandleRef(), 0, 1, outputSize, pInputParam);
}
} // namespace NEO

View File

@@ -1,115 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "opencl/source/event/perf_counter.h"
#include "opencl/source/os_interface/metrics_library.h"
#include <mutex>
namespace NEO {
//////////////////////////////////////////////////////
// Forward declaration.
//////////////////////////////////////////////////////
class TagNodeBase;
class CommandQueue;
//////////////////////////////////////////////////////
// Performance counters implementation.
//////////////////////////////////////////////////////
class PerformanceCounters {
public:
//////////////////////////////////////////////////////
// Constructor/destructor.
//////////////////////////////////////////////////////
PerformanceCounters();
virtual ~PerformanceCounters() = default;
//////////////////////////////////////////////////////
// Performance counters creation.
//////////////////////////////////////////////////////
static std::unique_ptr<PerformanceCounters> create(class Device *device);
bool enable(bool ccsEngine);
void shutdown();
uint32_t getReferenceNumber();
/////////////////////////////////////////////////////
// Gpu oa/mmio configuration.
/////////////////////////////////////////////////////
virtual bool enableCountersConfiguration() = 0;
virtual void releaseCountersConfiguration() = 0;
//////////////////////////////////////////////////////
// Gpu commands.
//////////////////////////////////////////////////////
static uint32_t getGpuCommandsSize(CommandQueue &commandQueue, const bool reservePerfCounters);
uint32_t getGpuCommandsSize(const MetricsLibraryApi::GpuCommandBufferType commandBufferType, const bool begin);
bool getGpuCommands(const MetricsLibraryApi::GpuCommandBufferType commandBufferType, TagNodeBase &performanceCounters, const bool begin, const uint32_t bufferSize, void *pBuffer);
/////////////////////////////////////////////////////
// Gpu/Api reports.
/////////////////////////////////////////////////////
uint32_t getApiReportSize();
uint32_t getGpuReportSize();
bool getApiReport(const TagNodeBase *performanceCounters, const size_t inputParamSize, void *pClientData, size_t *pOutputSize, bool isEventComplete);
/////////////////////////////////////////////////////
// Metrics Library interface.
/////////////////////////////////////////////////////
MetricsLibrary *getMetricsLibraryInterface();
void setMetricsLibraryInterface(std::unique_ptr<MetricsLibrary> newMetricsLibrary);
bool openMetricsLibrary();
void closeMetricsLibrary();
/////////////////////////////////////////////////////
// Metrics Library context/query handles.
/////////////////////////////////////////////////////
ContextHandle_1_0 getMetricsLibraryContext();
void getQueryHandleRef(QueryHandle_1_0 &handle);
void deleteQuery(QueryHandle_1_0 &handle);
protected:
/////////////////////////////////////////////////////
// Common members.
/////////////////////////////////////////////////////
std::mutex mutex;
uint32_t referenceCounter = 0;
bool available = false;
bool usingCcsEngine = false;
/////////////////////////////////////////////////////
// Metrics Library interface.
/////////////////////////////////////////////////////
std::unique_ptr<MetricsLibrary> metricsLibrary = {};
/////////////////////////////////////////////////////
// Metrics Library client data.
/////////////////////////////////////////////////////
ClientData_1_0 clientData = {};
ClientType_1_0 clientType = {ClientApi::OpenCL, ClientGen::Unknown};
ClientOptionsSubDeviceData_1_0 subDevice = {};
ClientOptionsSubDeviceIndexData_1_0 subDeviceIndex = {};
ClientOptionsSubDeviceCountData_1_0 subDeviceCount = {};
/////////////////////////////////////////////////////
// Metrics Library context.
/////////////////////////////////////////////////////
ContextCreateData_1_0 contextData = {};
ContextHandle_1_0 context = {};
/////////////////////////////////////////////////////
// Metrics Library oa counters configuration.
/////////////////////////////////////////////////////
ConfigurationHandle_1_0 oaConfiguration = {};
/////////////////////////////////////////////////////
// Metrics Library query object.
/////////////////////////////////////////////////////
QueryHandle_1_0 query = {};
};
} // namespace NEO

View File

@@ -13,8 +13,6 @@ set(RUNTIME_SRCS_OS_INTERFACE_WINDOWS
${CMAKE_CURRENT_SOURCE_DIR}/device_caps_init_win.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ocl_reg_path.cpp
${CMAKE_CURRENT_SOURCE_DIR}/os_metrics_library.cpp
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_win.cpp
${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_win.h
)
if(WIN32)

View File

@@ -1,75 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "performance_counters_win.h"
#include "shared/source/device/device.h"
#include "shared/source/helpers/hw_helper.h"
#include "shared/source/os_interface/os_interface.h"
#include "shared/source/os_interface/windows/os_time_win.h"
#include "shared/source/os_interface/windows/wddm/wddm.h"
namespace NEO {
/////////////////////////////////////////////////////
// PerformanceCounters::create
/////////////////////////////////////////////////////
std::unique_ptr<PerformanceCounters> PerformanceCounters::create(Device *device) {
auto counter = std::make_unique<PerformanceCountersWin>();
auto wddm = device->getOSTime()->getOSInterface()->getDriverModel()->as<Wddm>();
auto gen = device->getHardwareInfo().platform.eRenderCoreFamily;
auto &hwHelper = HwHelper::get(gen);
UNRECOVERABLE_IF(counter == nullptr);
counter->clientData.Windows.Adapter = reinterpret_cast<void *>(static_cast<UINT_PTR>(wddm->getAdapter()));
counter->clientData.Windows.Device = reinterpret_cast<void *>(static_cast<UINT_PTR>(wddm->getDeviceHandle()));
counter->clientData.Windows.Escape = wddm->getEscapeHandle();
counter->clientData.Windows.KmdInstrumentationEnabled = device->getHardwareInfo().capabilityTable.instrumentationEnabled;
counter->contextData.ClientData = &counter->clientData;
counter->clientType.Gen = static_cast<MetricsLibraryApi::ClientGen>(hwHelper.getMetricsLibraryGenId());
return counter;
}
//////////////////////////////////////////////////////
// PerformanceCountersWin::enableCountersConfiguration
//////////////////////////////////////////////////////
bool PerformanceCountersWin::enableCountersConfiguration() {
// Release previous counters configuration so the user
// can change configuration between kernels.
releaseCountersConfiguration();
// Create oa configuration.
if (!metricsLibrary->oaConfigurationCreate(
context,
oaConfiguration)) {
DEBUG_BREAK_IF(true);
return false;
}
// Enable oa configuration.
if (!metricsLibrary->oaConfigurationActivate(
oaConfiguration)) {
DEBUG_BREAK_IF(true);
return false;
}
return true;
}
//////////////////////////////////////////////////////
// PerformanceCountersWin::releaseCountersConfiguration
//////////////////////////////////////////////////////
void PerformanceCountersWin::releaseCountersConfiguration() {
// Oa configuration.
if (oaConfiguration.IsValid()) {
metricsLibrary->oaConfigurationDeactivate(oaConfiguration);
metricsLibrary->oaConfigurationDelete(oaConfiguration);
oaConfiguration.data = nullptr;
}
}
} // namespace NEO

View File

@@ -1,24 +0,0 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "opencl/source/os_interface/performance_counters.h"
namespace NEO {
class PerformanceCountersWin : public PerformanceCounters {
public:
PerformanceCountersWin() = default;
~PerformanceCountersWin() override = default;
/////////////////////////////////////////////////////
// Gpu oa/mmio configuration.
/////////////////////////////////////////////////////
bool enableCountersConfiguration() override;
void releaseCountersConfiguration() override;
};
} // namespace NEO