-
Notifications
You must be signed in to change notification settings - Fork 56
/
metric_streamer_collector.h
289 lines (240 loc) · 9.69 KB
/
metric_streamer_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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_TOOLS_ONEPROF_METRIC_STREAMER_COLLECTOR_H_
#define PTI_TOOLS_ONEPROF_METRIC_STREAMER_COLLECTOR_H_
#include <algorithm>
#include <atomic>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "metric_storage.h"
#include "ze_utils.h"
#define WAIT_DELAY 10000000 // 10 ms
enum CollectorState {
COLLECTOR_STATE_IDLE = 0,
COLLECTOR_STATE_ENABLED = 1,
COLLECTOR_STATE_DISABLED = 2
};
class MetricStreamerCollector {
public: // Interface
static MetricStreamerCollector* Create(
ze_driver_handle_t driver,
ze_device_handle_t device,
const char* group_name,
uint32_t sampling_interval,
const std::string& raw_data_path) {
PTI_ASSERT(driver != nullptr);
PTI_ASSERT(device != nullptr);
PTI_ASSERT(group_name != nullptr);
PTI_ASSERT(sampling_interval > 0);
ze_context_handle_t context = utils::ze::GetContext(driver);
PTI_ASSERT(context != nullptr);
std::vector<ze_device_handle_t> sub_device_list =
utils::ze::GetSubDeviceList(device);
if (sub_device_list.empty()) {
sub_device_list.push_back(device);
}
std::vector<zet_metric_group_handle_t> metric_group_list;
for (auto sub_device : sub_device_list) {
zet_metric_group_handle_t group = utils::ze::FindMetricGroup(
sub_device, group_name,
ZET_METRIC_GROUP_SAMPLING_TYPE_FLAG_TIME_BASED);
if (group == nullptr) {
std::cerr << "[WARNING] Unable to find target metric group: " <<
group_name << std::endl;
return nullptr;
}
metric_group_list.push_back(group);
}
PTI_ASSERT(metric_group_list.size() == sub_device_list.size());
return new MetricStreamerCollector(
context, sub_device_list, metric_group_list,
sampling_interval, raw_data_path);
}
void DisableMetrics() {
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_;
PTI_ASSERT(metric_storage_ != nullptr);
delete metric_storage_;
metric_storage_ = nullptr;
}
~MetricStreamerCollector() {
ze_result_t status = ZE_RESULT_SUCCESS;
PTI_ASSERT(collector_state_ == COLLECTOR_STATE_DISABLED);
PTI_ASSERT(context_ != nullptr);
status = zeContextDestroy(context_);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (metric_storage_ != nullptr) {
delete metric_storage_;
PTI_ASSERT(0);
}
}
MetricStreamerCollector(const MetricStreamerCollector& copy) = delete;
MetricStreamerCollector& operator=(const MetricStreamerCollector& copy) = delete;
private: // Implementation
MetricStreamerCollector(
ze_context_handle_t context,
const std::vector<ze_device_handle_t>& sub_device_list,
const std::vector<zet_metric_group_handle_t>& metric_group_list,
uint32_t sampling_interval,
const std::string& raw_data_path)
: context_(context),
sub_device_list_(sub_device_list),
metric_group_list_(metric_group_list),
sampling_interval_(sampling_interval),
raw_data_path_(raw_data_path) {
PTI_ASSERT(context_ != nullptr);
PTI_ASSERT(!sub_device_list_.empty());
PTI_ASSERT(!metric_group_list_.empty());
PTI_ASSERT(sampling_interval_ > 0);
metric_storage_ = MetricStorage::Create(
sub_device_list_.size(), utils::GetPid(), "raw", raw_data_path_);
PTI_ASSERT(metric_storage_ != nullptr);
EnableMetrics();
}
void EnableMetrics() {
PTI_ASSERT(collector_thread_ == nullptr);
PTI_ASSERT(collector_state_ == COLLECTOR_STATE_IDLE);
collector_state_.store(COLLECTOR_STATE_IDLE, std::memory_order_release);
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));
}
}
void AppendMetrics(
const uint8_t* storage, uint32_t size, uint32_t sub_device_id) {
PTI_ASSERT(storage != nullptr);
PTI_ASSERT(size > 0);
metric_storage_->Dump(storage, size, sub_device_id);
}
static void CollectChunk(
MetricStreamerCollector* collector,
const std::vector<ze_event_handle_t>& event_list,
const std::vector<zet_metric_streamer_handle_t>& metric_streamer_list,
std::vector<uint8_t>& storage) {
PTI_ASSERT(collector != nullptr);
ze_result_t status = ZE_RESULT_SUCCESS;
for (size_t i = 0; i < event_list.size(); ++i) {
status = zeEventHostSynchronize(event_list[i], WAIT_DELAY);
PTI_ASSERT(status == ZE_RESULT_SUCCESS || status == ZE_RESULT_NOT_READY);
if (status == ZE_RESULT_SUCCESS) {
status = zeEventHostReset(event_list[i]);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
size_t data_size = 0;
status = zetMetricStreamerReadData(
metric_streamer_list[i], UINT32_MAX, &data_size, nullptr);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
PTI_ASSERT(data_size > 0);
PTI_ASSERT(data_size <= storage.size());
status = zetMetricStreamerReadData(
metric_streamer_list[i], UINT32_MAX, &data_size, storage.data());
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (data_size > 0) {
collector->AppendMetrics(storage.data(), data_size, i);
}
}
}
static void Collect(MetricStreamerCollector* collector) {
PTI_ASSERT(collector != nullptr);
PTI_ASSERT(collector->context_ != nullptr);
PTI_ASSERT(!collector->metric_group_list_.empty());
PTI_ASSERT(collector->sub_device_list_.size() ==
collector->metric_group_list_.size());
ze_result_t status = ZE_RESULT_SUCCESS;
for (size_t i = 0; i < collector->sub_device_list_.size(); ++i) {
status = zetContextActivateMetricGroups(
collector->context_, collector->sub_device_list_[i],
1, &collector->metric_group_list_[i]);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
PTI_ASSERT(collector->sub_device_list_.size() <
(std::numeric_limits<uint32_t>::max)());
uint32_t sub_device_count =
static_cast<uint32_t>(collector->sub_device_list_.size());
ze_event_pool_desc_t event_pool_desc = {
ZE_STRUCTURE_TYPE_EVENT_POOL_DESC, nullptr,
ZE_EVENT_POOL_FLAG_HOST_VISIBLE, sub_device_count};
ze_event_pool_handle_t event_pool = nullptr;
status = zeEventPoolCreate(
collector->context_, &event_pool_desc, sub_device_count,
collector->sub_device_list_.data(), &event_pool);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
std::vector<ze_event_handle_t> event_list;
std::vector<zet_metric_streamer_handle_t> metric_streamer_list;
for (uint32_t i = 0; i < sub_device_count; ++i) {
ze_event_desc_t event_desc = {
ZE_STRUCTURE_TYPE_EVENT_DESC, nullptr, i,
ZE_EVENT_SCOPE_FLAG_HOST, ZE_EVENT_SCOPE_FLAG_HOST};
ze_event_handle_t event = nullptr;
status = zeEventCreate(event_pool, &event_desc, &event);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
event_list.push_back(event);
zet_metric_streamer_desc_t metric_streamer_desc = {
ZET_STRUCTURE_TYPE_METRIC_STREAMER_DESC,
nullptr,
MAX_REPORT_COUNT,
collector->sampling_interval_};
zet_metric_streamer_handle_t metric_streamer = nullptr;
status = zetMetricStreamerOpen(
collector->context_, collector->sub_device_list_[i],
collector->metric_group_list_[i], &metric_streamer_desc,
event, &metric_streamer);
if (status != ZE_RESULT_SUCCESS) {
std::cerr <<
"[WARNING] Sampling interval is not supported" << std::endl;
collector->collector_state_.store(
COLLECTOR_STATE_ENABLED, std::memory_order_release);
return;
}
PTI_ASSERT(metric_streamer_desc.notifyEveryNReports == MAX_REPORT_COUNT);
metric_streamer_list.push_back(metric_streamer);
}
std::vector<uint8_t> storage(MAX_BUFFER_SIZE);
collector->collector_state_.store(
COLLECTOR_STATE_ENABLED, std::memory_order_release);
if (metric_streamer_list.size() == sub_device_count) {
while (collector->collector_state_.load(std::memory_order_acquire) !=
COLLECTOR_STATE_DISABLED) {
CollectChunk(collector, event_list, metric_streamer_list, storage);
}
CollectChunk(collector, event_list, metric_streamer_list, storage);
}
for (auto metric_streamer : metric_streamer_list) {
status = zetMetricStreamerClose(metric_streamer);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
for (auto event : event_list) {
status = zeEventDestroy(event);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
status = zeEventPoolDestroy(event_pool);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
for (auto sub_device : collector->sub_device_list_) {
status = zetContextActivateMetricGroups(
collector->context_,sub_device, 0, nullptr);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
}
private: // Data
std::vector<ze_device_handle_t> sub_device_list_;
ze_context_handle_t context_ = nullptr;
std::thread* collector_thread_ = nullptr;
std::atomic<CollectorState> collector_state_{COLLECTOR_STATE_IDLE};
std::vector<zet_metric_group_handle_t> metric_group_list_;
MetricStorage* metric_storage_ = nullptr;
uint32_t sampling_interval_ = 0;
std::string raw_data_path_;
};
#endif // PTI_TOOLS_ONEPROF_METRIC_STREAMER_COLLECTOR_H_