forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.cc
299 lines (271 loc) · 8.49 KB
/
tool.cc
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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#include <iostream>
#include "ze_tracer.h"
#include "ze_utils.h"
static ZeTracer* tracer = nullptr;
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
void Usage() {
std::cout <<
"Usage: ./ze_tracer[.exe] [options] <application> <args>" <<
std::endl;
std::cout << "Options:" << std::endl;
std::cout <<
"--call-logging [-c] " <<
"Trace host API calls" <<
std::endl;
std::cout <<
"--host-timing [-h] " <<
"Report host API execution time" <<
std::endl;
std::cout <<
"--device-timing [-d] " <<
"Report kernels execution time" <<
std::endl;
std::cout <<
"--kernel-submission [-s] " <<
"Report append, submit and execute intervals for kernels" <<
std::endl;
std::cout <<
"--device-timeline [-t] " <<
"Trace device activities" <<
std::endl;
std::cout <<
"--chrome-call-logging " <<
"Dump host API calls to JSON file" <<
std::endl;
std::cout <<
"--chrome-device-timeline " <<
"Dump device activities to JSON file per command queue" <<
std::endl;
std::cout <<
"--chrome-kernel-timeline " <<
"Dump device activities to JSON file per kernel name" <<
std::endl;
std::cout <<
"--chrome-device-stages " <<
"Dump device activities by stages to JSON file" <<
std::endl;
std::cout <<
"--verbose [-v] " <<
"Enable verbose mode to show more kernel information" <<
std::endl;
std::cout <<
"--demangle " <<
"Demangle DPC++ kernel names" <<
std::endl;
std::cout <<
"--kernels-per-tile " <<
"Dump kernel information per tile" <<
std::endl;
std::cout <<
"--tid " <<
"Print thread ID into host API trace" <<
std::endl;
std::cout <<
"--pid " <<
"Print process ID into host API and device activity trace" <<
std::endl;
std::cout <<
"--output [-o] <filename> " <<
"Print console logs into the file" <<
std::endl;
std::cout <<
"--conditional-collection " <<
"Enable conditional collection mode" <<
std::endl;
std::cout <<
"--version " <<
"Print version" <<
std::endl;
}
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
int ParseArgs(int argc, char* argv[]) {
int app_index = 1;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--call-logging") == 0 ||
strcmp(argv[i], "-c") == 0) {
utils::SetEnv("ZET_CallLogging", "1");
++app_index;
} else if (strcmp(argv[i], "--host-timing") == 0 ||
strcmp(argv[i], "-h") == 0) {
utils::SetEnv("ZET_HostTiming", "1");
++app_index;
} else if (strcmp(argv[i], "--device-timing") == 0 ||
strcmp(argv[i], "-d") == 0) {
utils::SetEnv("ZET_DeviceTiming", "1");
++app_index;
} else if (strcmp(argv[i], "--kernel-submission") == 0 ||
strcmp(argv[i], "-s") == 0) {
utils::SetEnv("ZET_KernelSubmission", "1");
++app_index;
} else if (strcmp(argv[i], "--device-timeline") == 0 ||
strcmp(argv[i], "-t") == 0) {
utils::SetEnv("ZET_DeviceTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-call-logging") == 0) {
utils::SetEnv("ZET_ChromeCallLogging", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-device-timeline") == 0) {
utils::SetEnv("ZET_ChromeDeviceTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-kernel-timeline") == 0) {
utils::SetEnv("ZET_ChromeKernelTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-device-stages") == 0) {
utils::SetEnv("ZET_ChromeDeviceStages", "1");
++app_index;
} else if (strcmp(argv[i], "--verbose") == 0 ||
strcmp(argv[i], "-v") == 0) {
utils::SetEnv("ZET_Verbose", "1");
++app_index;
} else if (strcmp(argv[i], "--demangle") == 0) {
utils::SetEnv("ZET_Demangle", "1");
++app_index;
} else if (strcmp(argv[i], "--kernels-per-tile") == 0) {
utils::SetEnv("ZET_KernelsPerTile", "1");
++app_index;
} else if (strcmp(argv[i], "--tid") == 0) {
utils::SetEnv("ZET_Tid", "1");
++app_index;
} else if (strcmp(argv[i], "--pid") == 0) {
utils::SetEnv("ZET_Pid", "1");
++app_index;
} else if (strcmp(argv[i], "--output") == 0 ||
strcmp(argv[i], "-o") == 0) {
utils::SetEnv("ZET_LogToFile", "1");
++i;
if (i >= argc) {
std::cout << "[ERROR] Log file name is not specified" << std::endl;
return -1;
}
utils::SetEnv("ZET_LogFilename", argv[i]);
app_index += 2;
} else if (strcmp(argv[i], "--conditional-collection") == 0) {
utils::SetEnv("ZET_ConditionalCollection", "1");
++app_index;
} else if (strcmp(argv[i], "--version") == 0) {
#ifdef PTI_VERSION
std::cout << TOSTRING(PTI_VERSION) << std::endl;
#endif
return 0;
} else {
break;
}
}
if (utils::GetEnv("ZET_ChromeDeviceTimeline") == "1" &&
utils::GetEnv("ZET_ChromeDeviceStages") == "1") {
std::cout <<
"[ERROR] Options --chrome-device-timeline and " <<
"--chrome-device-stages can't be used together," <<
" choose one of them" << std::endl;
return -1;
}
if (utils::GetEnv("ZET_ChromeDeviceTimeline") == "1" &&
utils::GetEnv("ZET_ChromeKernelTimeline") == "1") {
std::cout <<
"[ERROR] Options --chrome-device-timeline and " <<
"--chrome-kernel-timeline can't be used together, " <<
"choose one of them" << std::endl;
return -1;
}
return app_index;
}
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
void SetToolEnv() {
utils::SetEnv("ZE_ENABLE_TRACING_LAYER", "1");
}
static TraceOptions ReadArgs() {
std::string value;
uint32_t flags = 0;
std::string log_file;
value = utils::GetEnv("ZET_CallLogging");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CALL_LOGGING);
}
value = utils::GetEnv("ZET_HostTiming");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_HOST_TIMING);
}
value = utils::GetEnv("ZET_DeviceTiming");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEVICE_TIMING);
}
value = utils::GetEnv("ZET_KernelSubmission");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_KERNEL_SUBMITTING);
}
value = utils::GetEnv("ZET_DeviceTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEVICE_TIMELINE);
}
value = utils::GetEnv("ZET_ChromeCallLogging");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_CALL_LOGGING);
}
value = utils::GetEnv("ZET_ChromeDeviceTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_DEVICE_TIMELINE);
}
value = utils::GetEnv("ZET_ChromeKernelTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_KERNEL_TIMELINE);
}
value = utils::GetEnv("ZET_ChromeDeviceStages");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_DEVICE_STAGES);
}
value = utils::GetEnv("ZET_Verbose");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_VERBOSE);
}
value = utils::GetEnv("ZET_Demangle");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEMANGLE);
}
value = utils::GetEnv("ZET_KernelsPerTile");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_KERNELS_PER_TILE);
}
value = utils::GetEnv("ZET_Tid");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_TID);
}
value = utils::GetEnv("ZET_Pid");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_PID);
}
value = utils::GetEnv("ZET_LogToFile");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_LOG_TO_FILE);
log_file = utils::GetEnv("ZET_LogFilename");
PTI_ASSERT(!log_file.empty());
}
value = utils::GetEnv("ZET_ConditionalCollection");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CONDITIONAL_COLLECTION);
}
return TraceOptions(flags, log_file);
}
void EnableProfiling() {
ze_result_t status = zeInit(ZE_INIT_FLAG_GPU_ONLY);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
tracer = ZeTracer::Create(ReadArgs());
}
void DisableProfiling() {
if (tracer != nullptr) {
delete tracer;
}
}