forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_storage.h
351 lines (278 loc) · 9.8 KB
/
result_storage.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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_TOOLS_ONEPROF_RESULT_STORAGE_H_
#define PTI_TOOLS_ONEPROF_RESULT_STORAGE_H_
#include <fstream>
#include <vector>
#include "ze_utils.h"
struct DeviceProps {
uint64_t freq;
uint64_t mask;
};
struct DeviceInterval {
uint64_t start;
uint64_t end;
uint32_t sub_device_id;
};
struct KernelInterval {
uint32_t kernel_id;
std::vector<DeviceInterval> device_interval_list;
};
struct ResultData {
uint32_t pid;
uint32_t device_id;
uint64_t execution_time;
std::string metric_group;
std::vector<DeviceProps> device_props_list;
std::vector<std::string> kernel_name_list;
std::vector<KernelInterval> kernel_interval_list;
};
class ResultStorage {
public:
static std::string GetResultFileName(const std::string& path, uint32_t pid) {
std::string filename =
std::string("result.") + std::to_string(pid) + ".bin";
if (!path.empty()) {
filename = path + "/" + filename;
}
return filename;
}
static ResultStorage* Create(const std::string& path, uint32_t pid) {
std::string filename = GetResultFileName(path, pid);
PTI_ASSERT(!filename.empty());
ResultStorage* storage = new ResultStorage(filename);
PTI_ASSERT(storage != nullptr);
if (!storage->file_.is_open()) {
delete storage;
return nullptr;
}
return storage;
}
~ResultStorage() {
file_.close();
}
void Dump(const ResultData* data) {
PTI_ASSERT(file_.is_open());
file_.write( // PID
reinterpret_cast<const char*>(&data->pid),
sizeof(uint32_t));
file_.write( // Device ID
reinterpret_cast<const char*>(&data->device_id),
sizeof(uint32_t));
file_.write( // Execution Time
reinterpret_cast<const char*>(&data->execution_time),
sizeof(uint64_t));
DumpMetricGroup(data->metric_group);
DumpDeviceProps(data->device_props_list);
DumpKernelNames(data->kernel_name_list);
DumpKernelIntervals(data->kernel_interval_list);
}
private:
ResultStorage(const std::string& filename)
: file_(filename, std::ios::out | std::ios::binary) {}
void DumpMetricGroup(const std::string& metric_group) {
PTI_ASSERT(!metric_group.empty());
PTI_ASSERT(file_.is_open());
size_t metric_group_size = metric_group.size();
file_.write( // Metic Group Name Size
reinterpret_cast<const char*>(&metric_group_size),
sizeof(size_t));
file_.write( // Metric Group Name
metric_group.c_str(),
metric_group_size * sizeof(char));
}
void DumpDeviceProps(const std::vector<DeviceProps>& device_props_list) {
PTI_ASSERT(file_.is_open());
size_t device_props_list_size = device_props_list.size();
file_.write( // Device Properties List Size
reinterpret_cast<const char*>(&device_props_list_size),
sizeof(size_t));
for (size_t i = 0; i < device_props_list_size; ++i) {
const DeviceProps& device_props = device_props_list[i];
file_.write( // Frequency
reinterpret_cast<const char*>(&device_props.freq),
sizeof(uint64_t));
file_.write( // Mask
reinterpret_cast<const char*>(&device_props.mask),
sizeof(uint64_t));
}
}
void DumpKernelNames(
const std::vector<std::string>& kernel_name_list) {
PTI_ASSERT(file_.is_open());
size_t kernel_name_list_size = kernel_name_list.size();
file_.write( // Kernel Name List Size
reinterpret_cast<const char*>(&kernel_name_list_size),
sizeof(size_t));
for (const auto& kernel_name : kernel_name_list) {
size_t kernel_name_size = kernel_name.size();
PTI_ASSERT(kernel_name_size > 0);
file_.write( // Kernel Name Size
reinterpret_cast<const char*>(&kernel_name_size),
sizeof(size_t));
file_.write( // Kernel Name
kernel_name.c_str(),
kernel_name_size * sizeof(char));
}
}
void DumpKernelIntervals(
const std::vector<KernelInterval>& kernel_interval_list) {
PTI_ASSERT(file_.is_open());
size_t kernel_interval_list_size = kernel_interval_list.size();
file_.write( // Kernel Interval List Size
reinterpret_cast<const char*>(&kernel_interval_list_size),
sizeof(size_t));
for (const auto& kernel_interval : kernel_interval_list) {
file_.write( // Kernel ID
reinterpret_cast<const char*>(&kernel_interval.kernel_id),
sizeof(uint32_t));
const auto& device_interval_list =
kernel_interval.device_interval_list;
size_t device_interval_list_size = device_interval_list.size();
file_.write( // Device Interval List Size
reinterpret_cast<const char*>(&device_interval_list_size),
sizeof(size_t));
for (const auto& device_interval : device_interval_list) {
file_.write( // Start Time
reinterpret_cast<const char*>(&device_interval.start),
sizeof(uint64_t));
file_.write( // End Time
reinterpret_cast<const char*>(&device_interval.end),
sizeof(uint64_t));
file_.write( // Sub-device ID
reinterpret_cast<const char*>(&device_interval.sub_device_id),
sizeof(uint32_t));
}
}
}
private:
std::ofstream file_;
};
class ResultReader {
public:
static ResultReader* Create(const std::string& filename) {
ResultReader* reader = new ResultReader(filename);
PTI_ASSERT(reader != nullptr);
if (!reader->file_.is_open()) {
delete reader;
return nullptr;
}
return reader;
}
~ResultReader() {
file_.close();
}
ResultData* Read() {
PTI_ASSERT(file_.is_open());
ResultData* data = new ResultData{};
PTI_ASSERT(data != nullptr);
file_.read( // PID
reinterpret_cast<char*>(&data->pid),
sizeof(uint32_t));
file_.read( // Device ID
reinterpret_cast<char*>(&data->device_id),
sizeof(uint32_t));
file_.read( // Execution Time
reinterpret_cast<char*>(&data->execution_time),
sizeof(uint64_t));
data->metric_group = ReadMetricGroup();
data->device_props_list = ReadDeviceProps();
data->kernel_name_list = ReadKernelNames();
data->kernel_interval_list = ReadKernelIntervals();
return data;
}
private:
ResultReader(const std::string& filename)
: file_(filename, std::ios::in | std::ios::binary) {}
std::string ReadMetricGroup() {
PTI_ASSERT(file_.is_open());
size_t metric_group_size = 0;
file_.read( // Metic Group Name Size
reinterpret_cast<char*>(&metric_group_size),
sizeof(size_t));
std::vector<char> metric_group(metric_group_size);
file_.read( // Metric Group Name
metric_group.data(),
metric_group_size * sizeof(char));
return std::string(metric_group.begin(), metric_group.end());
}
std::vector<DeviceProps> ReadDeviceProps() {
PTI_ASSERT(file_.is_open());
size_t device_props_list_size = 0;
file_.read( // Device Properties List Size
reinterpret_cast<char*>(&device_props_list_size),
sizeof(size_t));
std::vector<DeviceProps> device_props_list;
for (size_t i = 0; i < device_props_list_size; ++i) {
DeviceProps device_props{};
file_.read( // Frequency
reinterpret_cast<char*>(&device_props.freq),
sizeof(uint64_t));
file_.read( // Mask
reinterpret_cast<char*>(&device_props.mask),
sizeof(uint64_t));
device_props_list.push_back(device_props);
}
return device_props_list;
}
std::vector<std::string> ReadKernelNames() {
PTI_ASSERT(file_.is_open());
size_t kernel_name_list_size = 0;
file_.read( // Kernel Name List Size
reinterpret_cast<char*>(&kernel_name_list_size),
sizeof(size_t));
std::vector<std::string> kernel_name_list;
for (size_t i = 0; i < kernel_name_list_size; ++i) {
size_t kernel_name_size = 0;
file_.read( // Kernel Name Size
reinterpret_cast<char*>(&kernel_name_size),
sizeof(size_t));
std::vector<char> kernel_name(kernel_name_size);
file_.read( // Kernel Name
kernel_name.data(),
kernel_name_size * sizeof(char));
kernel_name_list.push_back(std::string(
kernel_name.begin(), kernel_name.end()));
}
return kernel_name_list;
}
std::vector<KernelInterval> ReadKernelIntervals() {
PTI_ASSERT(file_.is_open());
size_t kernel_interval_list_size = 0;
file_.read( // Kernel Interval List Size
reinterpret_cast<char*>(&kernel_interval_list_size),
sizeof(size_t));
std::vector<KernelInterval> kernel_interval_list;
for (size_t i = 0; i < kernel_interval_list_size; ++i) {
KernelInterval kernel_interval{};
file_.read( // Kernel ID
reinterpret_cast<char*>(&kernel_interval.kernel_id),
sizeof(uint32_t));
size_t device_interval_list_size = 0;
file_.read( // Device Interval List Size
reinterpret_cast<char*>(&device_interval_list_size),
sizeof(size_t));
for (size_t j = 0; j < device_interval_list_size; ++j) {
DeviceInterval device_interval{};
file_.read( // Start Time
reinterpret_cast<char*>(&device_interval.start),
sizeof(uint64_t));
file_.read( // End Time
reinterpret_cast<char*>(&device_interval.end),
sizeof(uint64_t));
file_.read( // Sub-device ID
reinterpret_cast<char*>(&device_interval.sub_device_id),
sizeof(uint32_t));
kernel_interval.device_interval_list.push_back(device_interval);
}
kernel_interval_list.push_back(kernel_interval);
}
return kernel_interval_list;
}
private:
std::ifstream file_;
};
#endif // PTI_TOOLS_ONEPROF_RESULT_STORAGE_H_