-
Notifications
You must be signed in to change notification settings - Fork 56
/
tool.cc
278 lines (251 loc) · 8.02 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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#include <iostream>
#include "cl_tracer.h"
#include "cl_utils.h"
static ClTracer* tracer = nullptr;
extern "C" PTI_EXPORT
void Usage() {
std::cout <<
"Usage: ./cl_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 queued, 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 <<
"--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" PTI_EXPORT
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("CLT_CallLogging", "1");
++app_index;
} else if (strcmp(argv[i], "--host-timing") == 0 ||
strcmp(argv[i], "-h") == 0) {
utils::SetEnv("CLT_HostTiming", "1");
++app_index;
} else if (strcmp(argv[i], "--device-timing") == 0 ||
strcmp(argv[i], "-d") == 0) {
utils::SetEnv("CLT_DeviceTiming", "1");
++app_index;
} else if (strcmp(argv[i], "--kernel-submission") == 0 ||
strcmp(argv[i], "-s") == 0) {
utils::SetEnv("CLT_KernelSubmission", "1");
++app_index;
} else if (strcmp(argv[i], "--device-timeline") == 0 ||
strcmp(argv[i], "-t") == 0) {
utils::SetEnv("CLT_DeviceTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-call-logging") == 0) {
utils::SetEnv("CLT_ChromeCallLogging", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-device-timeline") == 0) {
utils::SetEnv("CLT_ChromeDeviceTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-kernel-timeline") == 0) {
utils::SetEnv("CLT_ChromeKernelTimeline", "1");
++app_index;
} else if (strcmp(argv[i], "--chrome-device-stages") == 0) {
utils::SetEnv("CLT_ChromeDeviceStages", "1");
++app_index;
} else if (strcmp(argv[i], "--verbose") == 0 ||
strcmp(argv[i], "-v") == 0) {
utils::SetEnv("CLT_Verbose", "1");
++app_index;
} else if (strcmp(argv[i], "--demangle") == 0) {
utils::SetEnv("CLT_Demangle", "1");
++app_index;
} else if (strcmp(argv[i], "--tid") == 0) {
utils::SetEnv("CLT_Tid", "1");
++app_index;
} else if (strcmp(argv[i], "--pid") == 0) {
utils::SetEnv("CLT_Pid", "1");
++app_index;
} else if (strcmp(argv[i], "--output") == 0 ||
strcmp(argv[i], "-o") == 0) {
utils::SetEnv("CLT_LogToFile", "1");
++i;
if (i >= argc) {
std::cout << "[ERROR] Log file name is not specified" << std::endl;
return -1;
}
utils::SetEnv("CLT_LogFilename", argv[i]);
app_index += 2;
} else if (strcmp(argv[i], "--conditional-collection") == 0) {
utils::SetEnv("CLT_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("CLT_ChromeDeviceTimeline") == "1" &&
utils::GetEnv("CLT_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("CLT_ChromeDeviceTimeline") == "1" &&
utils::GetEnv("CLT_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" PTI_EXPORT
void SetToolEnv() {
// tool itself does not need this
// this is for target application to check if tool is present
utils::SetEnv("CL_ENABLE_TRACING_LAYER", "1");
}
static TraceOptions ReadArgs() {
std::string value;
uint32_t flags = 0;
std::string log_file;
value = utils::GetEnv("CLT_CallLogging");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CALL_LOGGING);
}
value = utils::GetEnv("CLT_HostTiming");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_HOST_TIMING);
}
value = utils::GetEnv("CLT_DeviceTiming");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEVICE_TIMING);
}
value = utils::GetEnv("CLT_KernelSubmission");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_KERNEL_SUBMITTING);
}
value = utils::GetEnv("CLT_DeviceTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEVICE_TIMELINE);
}
value = utils::GetEnv("CLT_ChromeCallLogging");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_CALL_LOGGING);
}
value = utils::GetEnv("CLT_ChromeDeviceTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_DEVICE_TIMELINE);
}
value = utils::GetEnv("CLT_ChromeKernelTimeline");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_KERNEL_TIMELINE);
}
value = utils::GetEnv("CLT_ChromeDeviceStages");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CHROME_DEVICE_STAGES);
}
value = utils::GetEnv("CLT_Verbose");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_VERBOSE);
}
value = utils::GetEnv("CLT_Demangle");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_DEMANGLE);
}
value = utils::GetEnv("CLT_Tid");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_TID);
}
value = utils::GetEnv("CLT_Pid");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_PID);
}
value = utils::GetEnv("CLT_LogToFile");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_LOG_TO_FILE);
log_file = utils::GetEnv("CLT_LogFilename");
PTI_ASSERT(!log_file.empty());
}
value = utils::GetEnv("CLT_ConditionalCollection");
if (!value.empty() && value == "1") {
flags |= (1 << TRACE_CONDITIONAL_COLLECTION);
}
return TraceOptions(flags, log_file);
}
void EnableProfiling() {
tracer = ClTracer::Create(ReadArgs());
}
void DisableProfiling() {
if (tracer != nullptr) {
delete tracer;
}
}