1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #pragma once
9 #include "shared/source/utilities/metrics_library.h"
10 #include "shared/source/utilities/perf_counter.h"
11 
12 #include "engine_node.h"
13 
14 #include <mutex>
15 
16 namespace NEO {
17 
18 //////////////////////////////////////////////////////
19 // Forward declaration.
20 //////////////////////////////////////////////////////
21 class TagNodeBase;
22 
23 //////////////////////////////////////////////////////
24 // Performance counters implementation.
25 //////////////////////////////////////////////////////
26 class PerformanceCounters {
27   public:
28     //////////////////////////////////////////////////////
29     // Constructor/destructor.
30     //////////////////////////////////////////////////////
31     PerformanceCounters();
32     virtual ~PerformanceCounters() = default;
33 
34     //////////////////////////////////////////////////////
35     // Performance counters creation.
36     //////////////////////////////////////////////////////
37     static std::unique_ptr<PerformanceCounters> create(class Device *device);
38     bool enable(bool ccsEngine);
39     void shutdown();
40     uint32_t getReferenceNumber();
41 
42     /////////////////////////////////////////////////////
43     // Gpu oa/mmio configuration.
44     /////////////////////////////////////////////////////
45     virtual bool enableCountersConfiguration() = 0;
46     virtual void releaseCountersConfiguration() = 0;
47 
48     //////////////////////////////////////////////////////
49     // Gpu commands.
50     //////////////////////////////////////////////////////
51     static uint32_t getGpuCommandsSize(PerformanceCounters *performanceCounters, aub_stream::EngineType engineType, const bool reservePerfCounters);
52     uint32_t getGpuCommandsSize(const MetricsLibraryApi::GpuCommandBufferType commandBufferType, const bool begin);
53     bool getGpuCommands(const MetricsLibraryApi::GpuCommandBufferType commandBufferType, TagNodeBase &performanceCounters, const bool begin, const uint32_t bufferSize, void *pBuffer);
54 
55     /////////////////////////////////////////////////////
56     // Gpu/Api reports.
57     /////////////////////////////////////////////////////
58     uint32_t getApiReportSize();
59     uint32_t getGpuReportSize();
60     bool getApiReport(const TagNodeBase *performanceCounters, const size_t inputParamSize, void *pClientData, size_t *pOutputSize, bool isEventComplete);
61 
62     /////////////////////////////////////////////////////
63     // Metrics Library interface.
64     /////////////////////////////////////////////////////
65     MetricsLibrary *getMetricsLibraryInterface();
66     void setMetricsLibraryInterface(std::unique_ptr<MetricsLibrary> newMetricsLibrary);
67     bool openMetricsLibrary();
68     void closeMetricsLibrary();
69 
70     /////////////////////////////////////////////////////
71     // Metrics Library context/query handles.
72     /////////////////////////////////////////////////////
73     ContextHandle_1_0 getMetricsLibraryContext();
74     void getQueryHandleRef(QueryHandle_1_0 &handle);
75     void deleteQuery(QueryHandle_1_0 &handle);
76 
77   protected:
78     /////////////////////////////////////////////////////
79     // Common members.
80     /////////////////////////////////////////////////////
81     std::mutex mutex;
82     uint32_t referenceCounter = 0;
83     bool available = false;
84     bool usingCcsEngine = false;
85 
86     /////////////////////////////////////////////////////
87     // Metrics Library interface.
88     /////////////////////////////////////////////////////
89     std::unique_ptr<MetricsLibrary> metricsLibrary = {};
90 
91     /////////////////////////////////////////////////////
92     // Metrics Library client data.
93     /////////////////////////////////////////////////////
94     ClientData_1_0 clientData = {};
95     ClientType_1_0 clientType = {ClientApi::OpenCL, ClientGen::Unknown};
96     ClientOptionsSubDeviceData_1_0 subDevice = {};
97     ClientOptionsSubDeviceIndexData_1_0 subDeviceIndex = {};
98     ClientOptionsSubDeviceCountData_1_0 subDeviceCount = {};
99 
100     /////////////////////////////////////////////////////
101     // Metrics Library context.
102     /////////////////////////////////////////////////////
103     ContextCreateData_1_0 contextData = {};
104     ContextHandle_1_0 context = {};
105 
106     /////////////////////////////////////////////////////
107     // Metrics Library oa counters configuration.
108     /////////////////////////////////////////////////////
109     ConfigurationHandle_1_0 oaConfiguration = {};
110 
111     /////////////////////////////////////////////////////
112     // Metrics Library query object.
113     /////////////////////////////////////////////////////
114     QueryHandle_1_0 query = {};
115 };
116 } // namespace NEO
117