forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_metric_collector.h
352 lines (286 loc) · 10.7 KB
/
cl_metric_collector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_SAMPLES_CL_GPU_METRICS_CL_METRIC_COLLECTOR_H_
#define PTI_SAMPLES_CL_GPU_METRICS_CL_METRIC_COLLECTOR_H_
#include <algorithm>
#include <atomic>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "cl_api_tracer.h"
#include "cl_utils.h"
#include "metric_device.h"
enum CollectorState {
COLLECTOR_STATE_IDLE = 0,
COLLECTOR_STATE_ENABLED = 1,
COLLECTOR_STATE_DISABLED = 2
};
class ClMetricCollector {
public:
static ClMetricCollector* Create(
cl_device_id device, const char* set_name) {
PTI_ASSERT(device != nullptr);
PTI_ASSERT(set_name != nullptr);
std::string device_string = utils::GetEnv("PTI_DEVICE_ID");
uint32_t device_id =
device_string.empty() ? 0 : std::stoul(device_string);
std::string sub_device_string = utils::GetEnv("PTI_SUB_DEVICE_ID");
uint32_t sub_device_id =
sub_device_string.empty() ? 0 : std::stoul(sub_device_string);
MetricDevice* metric_device =
MetricDevice::Create(device_id, sub_device_id);
if (metric_device == nullptr) {
std::cerr << "[WARNING] Unable to find MD library" << std::endl;
return nullptr;
}
md::IConcurrentGroup_1_5* group =
metric_device->FindMetricGroup(set_name);
md::IMetricSet_1_5* set = metric_device->FindMetricSet(set_name);
if (group == nullptr || set == nullptr) {
std::cerr << "[WARNING] Metric set is not found: " <<
set_name << std::endl;
delete metric_device;
return nullptr;
}
ClMetricCollector* collector =
new ClMetricCollector(metric_device, group, set);
PTI_ASSERT(collector != nullptr);
ClApiTracer* tracer = new ClApiTracer(device, Callback, collector);
if (tracer == nullptr || !tracer->IsValid()) {
std::cerr << "[WARNING] Unable to create OpenCL tracer " <<
"for target device" << std::endl;
if (tracer != nullptr) {
delete tracer;
delete collector;
}
return nullptr;
}
collector->EnableTracing(tracer);
return collector;
}
~ClMetricCollector() {
PTI_ASSERT(collector_thread_ == nullptr);
PTI_ASSERT(collector_state_ == COLLECTOR_STATE_IDLE);
if (tracer_ != nullptr) {
delete tracer_;
}
PTI_ASSERT(device_ != nullptr);
delete device_;
}
void DisableTracing() {
PTI_ASSERT(tracer_ != nullptr);
bool disabled = tracer_->Disable();
PTI_ASSERT(disabled);
}
uint64_t GetKernelTimestamp(uint64_t report_timestamp) const {
md::TCompletionCode status = md::CC_OK;
uint64_t gpu_snap_point = 0, cpu_snap_point = 0;
const MetricDevice& device = *device_;
status = device->GetGpuCpuTimestamps(
&gpu_snap_point, &cpu_snap_point, nullptr);
PTI_ASSERT(status == md::CC_OK);
uint64_t cpu_timestamp = cpu_snap_point -
(gpu_snap_point - report_timestamp);
#if defined(__gnu_linux__)
cpu_timestamp = utils::ConvertClockMonotonicToRaw(cpu_timestamp);
#endif
return cpu_timestamp;
}
int GetMetricId(const char* name) const {
PTI_ASSERT(name != nullptr);
PTI_ASSERT(set_ != nullptr);
for (uint32_t mid = 0; mid < set_->GetParams()->MetricsCount; ++mid) {
md::IMetric_1_0* metric = set_->GetMetric(mid);
PTI_ASSERT(metric != nullptr);
if (strcmp(metric->GetParams()->SymbolName, name) == 0) {
return mid;
}
}
for (uint32_t iid = 0; iid < set_->GetParams()->InformationCount; ++iid) {
md::IInformation_1_0* info = set_->GetInformation(iid);
PTI_ASSERT(info != nullptr);
if (strcmp(info->GetParams()->SymbolName, name) == 0) {
return iid + set_->GetParams()->MetricsCount;
}
}
return -1;
}
uint32_t GetReportSize() const {
PTI_ASSERT(set_ != nullptr);
return set_->GetParams()->MetricsCount +
set_->GetParams()->InformationCount;
}
std::vector<md::TTypedValue_1_0> GetReportList() const {
PTI_ASSERT(set_ != nullptr);
if (metric_storage_.size() == 0) {
return std::vector<md::TTypedValue_1_0>();
}
size_t raw_report_count =
metric_storage_.size() / set_->GetParams()->RawReportSize;
PTI_ASSERT(
metric_storage_.size() ==
raw_report_count * set_->GetParams()->RawReportSize);
uint32_t calculated_report_size = GetReportSize();
std::vector<md::TTypedValue_1_0> calculated_reports(
calculated_report_size * raw_report_count);
uint32_t calculated_report_count = 0;
md::TCompletionCode status = md::CC_OK;
PTI_ASSERT(
metric_storage_.size() < (std::numeric_limits<uint32_t>::max)());
PTI_ASSERT(
calculated_reports.size() * sizeof(md::TTypedValue_1_0) <
(std::numeric_limits<uint32_t>::max)());
status = set_->CalculateMetrics(
metric_storage_.data(),
static_cast<uint32_t>(metric_storage_.size()),
calculated_reports.data(),
static_cast<uint32_t>(
calculated_reports.size() * sizeof(md::TTypedValue_1_0)),
&calculated_report_count, nullptr, 0);
PTI_ASSERT(status == md::CC_OK);
calculated_reports.resize(
calculated_report_count * calculated_report_size);
return calculated_reports;
}
ClMetricCollector(const ClMetricCollector& copy) = delete;
ClMetricCollector& operator=(const ClMetricCollector& copy) = delete;
private: // Implementation Details
ClMetricCollector(
MetricDevice* device, md::IConcurrentGroup_1_5* group,
md::IMetricSet_1_5* set)
: device_(device), group_(group), set_(set) {
PTI_ASSERT(device_ != nullptr);
PTI_ASSERT(group_ != nullptr);
PTI_ASSERT(set_ != nullptr);
}
void EnableTracing(ClApiTracer* tracer) {
PTI_ASSERT(tracer != nullptr);
tracer_ = tracer;
bool set = true;
set = set && tracer->SetTracingFunction(
CL_FUNCTION_clCreateCommandQueueWithProperties);
set = set && tracer->SetTracingFunction(
CL_FUNCTION_clCreateCommandQueue);
set = set && tracer->SetTracingFunction(
CL_FUNCTION_clReleaseCommandQueue);
PTI_ASSERT(set);
bool enabled = tracer_->Enable();
PTI_ASSERT(enabled);
}
void EnableMetrics() {
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(queue_count_ >= 0);
if (queue_count_ == 0) {
PTI_ASSERT(collector_thread_ == nullptr);
PTI_ASSERT(collector_state_ == COLLECTOR_STATE_IDLE);
collector_thread_ = new std::thread(Collect, this);
PTI_ASSERT(collector_thread_ != nullptr);
while (collector_state_.load(std::memory_order_acquire) !=
COLLECTOR_STATE_ENABLED) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
++queue_count_;
}
void DisableMetrics() {
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(queue_count_ > 0);
--queue_count_;
if (queue_count_ == 0) {
PTI_ASSERT(collector_thread_ != nullptr);
PTI_ASSERT(collector_state_ == COLLECTOR_STATE_ENABLED);
collector_state_.store(
COLLECTOR_STATE_DISABLED, std::memory_order_release);
collector_thread_->join();
delete collector_thread_;
collector_thread_ = nullptr;
collector_state_.store(COLLECTOR_STATE_IDLE, std::memory_order_release);
}
}
void AppendMetrics(const std::vector<uint8_t>& storage) {
PTI_ASSERT(storage.size() > 0);
size_t intial_size = metric_storage_.size();
metric_storage_.resize(intial_size + storage.size());
std::copy(storage.begin(), storage.end(),
metric_storage_.begin() + intial_size);
}
static void Collect(ClMetricCollector* collector) {
PTI_ASSERT(collector != nullptr);
PTI_ASSERT(collector->group_ != nullptr);
PTI_ASSERT(collector->set_ != nullptr);
md::IConcurrentGroup_1_5* group = collector->group_;
md::IMetricSet_1_5* set = collector->set_;
uint32_t sampling_interval = 100000; // nanoseconds
uint32_t buffer_size = 0; // defined by MDAPI
md::TCompletionCode status = md::CC_OK;
status = set->SetApiFiltering(md::API_TYPE_IOSTREAM);
PTI_ASSERT(status == md::CC_OK);
status = group->OpenIoStream(set, 0, &sampling_interval, &buffer_size);
PTI_ASSERT(status == md::CC_OK);
PTI_ASSERT(buffer_size > 0);
uint32_t max_report_count = buffer_size / set->GetParams()->RawReportSize;
uint32_t wait_time = 500; // milliseconds
collector->collector_state_.store(
COLLECTOR_STATE_ENABLED, std::memory_order_release);
std::vector<uint8_t> storage;
while (collector->collector_state_.load(std::memory_order_acquire) !=
COLLECTOR_STATE_DISABLED) {
status = group->WaitForReports(wait_time);
PTI_ASSERT(status == md::CC_OK || status == md::CC_WAIT_TIMEOUT ||
status == md::CC_INTERRUPTED);
storage.resize(max_report_count * set->GetParams()->RawReportSize);
uint32_t report_count = max_report_count;
status = group->ReadIoStream(
&report_count, reinterpret_cast<char*>(storage.data()),
md::IO_READ_FLAG_DROP_OLD_REPORTS);
PTI_ASSERT(status == md::CC_OK || status == md::CC_READ_PENDING);
PTI_ASSERT(report_count <= max_report_count);
storage.resize(report_count * set->GetParams()->RawReportSize);
collector->AppendMetrics(storage);
}
status = group->CloseIoStream();
PTI_ASSERT(status == md::CC_OK);
}
private: // Callbacks
static void Callback(
cl_function_id function,
cl_callback_data* callback_data,
void* user_data) {
if (callback_data->site == CL_CALLBACK_SITE_ENTER) {
return;
}
ClMetricCollector* collector =
reinterpret_cast<ClMetricCollector*>(user_data);
PTI_ASSERT(collector != nullptr);
if (function == CL_FUNCTION_clCreateCommandQueue ||
function == CL_FUNCTION_clCreateCommandQueueWithProperties) {
cl_command_queue queue =
*reinterpret_cast<cl_command_queue*>(
callback_data->functionReturnValue);
if (queue != nullptr) {
collector->EnableMetrics();
}
} else if (function == CL_FUNCTION_clReleaseCommandQueue) {
cl_int status =
*reinterpret_cast<cl_int*>(callback_data->functionReturnValue);
if (status == CL_SUCCESS) {
collector->DisableMetrics();
}
}
}
private: // Data
ClApiTracer* tracer_ = nullptr;
MetricDevice* device_ = nullptr;
md::IConcurrentGroup_1_5* group_ = nullptr;
md::IMetricSet_1_5* set_ = nullptr;
std::atomic<CollectorState> collector_state_{COLLECTOR_STATE_IDLE};
std::thread* collector_thread_ = nullptr;
std::mutex lock_;
int queue_count_ = 0;
std::vector<uint8_t> metric_storage_;
};
#endif // PTI_SAMPLES_CL_GPU_METRICS_CL_METRIC_COLLECTOR_H_