forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbTimer.cpp
421 lines (364 loc) · 12.6 KB
/
wbTimer.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <wb.h>
#include <time.h>
#ifdef _WIN32
uint64_t _hrtime_frequency = 0;
#endif /* _WIN32 */
wbTimer_t _timer = NULL;
#ifdef __APPLE__
static double o_timebase = 0;
static uint64_t o_timestart = 0;
#endif /* __APPLE__ */
uint64_t _hrtime(void) {
#define NANOSEC ((uint64_t)1e9)
#ifdef _MSC_VER
LARGE_INTEGER counter;
if (!QueryPerformanceCounter(&counter)) {
return 0;
}
return ((uint64_t)counter.LowPart * NANOSEC / _hrtime_frequency) +
(((uint64_t)counter.HighPart * NANOSEC / _hrtime_frequency) << 32);
#else
struct timespec ts;
#ifdef __APPLE__
#define O_NANOSEC (+1.0E-9)
#define O_GIGA UINT64_C(1000000000)
if (!o_timestart) {
mach_timebase_info_data_t tb = {0};
mach_timebase_info(&tb);
o_timebase = tb.numer;
o_timebase /= tb.denom;
o_timestart = mach_absolute_time();
}
double diff = (mach_absolute_time() - o_timestart) * o_timebase;
ts.tv_sec = diff * O_NANOSEC;
ts.tv_nsec = diff - (ts.tv_sec * O_GIGA);
#undef O_NANOSEC
#undef O_GIGA
#else /* __APPLE__ */
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif /* __APPLE__ */
return (((uint64_t)ts.tv_sec) * NANOSEC + ts.tv_nsec);
#endif /* _MSC_VER */
#undef NANOSEC
}
static inline uint64_t getTime(void) {
#ifdef WB_USE_CUDA
cudaDeviceSynchronize();
#endif /* WB_USE_CUDA */
return _hrtime();
}
static inline wbTimerNode_t wbTimerNode_new(int id, wbTimerKind_t kind,
const char *file, const char *fun,
int startLine) {
wbTimerNode_t node = wbNew(struct st_wbTimerNode_t);
wbTimerNode_setId(node, id);
wbTimerNode_setLevel(node, 0);
wbTimerNode_setStoppedQ(node, wbFalse);
wbTimerNode_setKind(node, kind);
wbTimerNode_setStartTime(node, 0);
wbTimerNode_setEndTime(node, 0);
wbTimerNode_setElapsedTime(node, 0);
wbTimerNode_setStartLine(node, startLine);
wbTimerNode_setEndLine(node, 0);
wbTimerNode_setStartFunction(node, fun);
wbTimerNode_setEndFunction(node, NULL);
wbTimerNode_setStartFile(node, file);
wbTimerNode_setEndFile(node, NULL);
wbTimerNode_setNext(node, NULL);
wbTimerNode_setPrevious(node, NULL);
wbTimerNode_setParent(node, NULL);
wbTimerNode_setMessage(node, NULL);
return node;
}
static inline void wbTimerNode_delete(wbTimerNode_t node) {
if (node != NULL) {
if (wbTimerNode_getMessage(node)) {
wbDelete(wbTimerNode_getMessage(node));
}
wbDelete(node);
}
}
static inline const char *_nodeKind(wbTimerKind_t kind) {
switch (kind) {
case wbTimerKind_Generic:
return "Generic";
case wbTimerKind_IO:
return "IO";
case wbTimerKind_GPU:
return "GPU";
case wbTimerKind_Copy:
return "Copy";
case wbTimerKind_Driver:
return "Driver";
case wbTimerKind_CopyAsync:
return "CopyAsync";
case wbTimerKind_Compute:
return "Compute";
case wbTimerKind_CPUGPUOverlap:
return "CPUGPUOverlap";
}
return "Undefined";
}
static inline string wbTimerNode_toJSON(wbTimerNode_t node) {
if (node == NULL) {
return "";
} else {
stringstream ss;
ss << "{\n";
ss << wbString_quote("id") << ":" << wbTimerNode_getId(node) << ",\n";
ss << wbString_quote("stopped") << ":"
<< wbString(wbTimerNode_stoppedQ(node) ? "true" : "false") << ",\n";
ss << wbString_quote("kind") << ":"
<< wbString_quote(_nodeKind(wbTimerNode_getKind(node))) << ",\n";
ss << wbString_quote("start_time") << ":" << wbTimerNode_getStartTime(node)
<< ",\n";
ss << wbString_quote("end_time") << ":" << wbTimerNode_getEndTime(node)
<< ",\n";
ss << wbString_quote("elapsed_time") << ":"
<< wbTimerNode_getElapsedTime(node) << ",\n";
ss << wbString_quote("start_line") << ":" << wbTimerNode_getStartLine(node)
<< ",\n";
ss << wbString_quote("end_line") << ":" << wbTimerNode_getEndLine(node)
<< ",\n";
ss << wbString_quote("start_function") << ":"
<< wbString_quote(wbTimerNode_getStartFunction(node)) << ",\n";
ss << wbString_quote("end_function") << ":"
<< wbString_quote(wbTimerNode_getEndFunction(node)) << ",\n";
ss << wbString_quote("start_file") << ":"
<< wbString_quote(wbTimerNode_getStartFile(node)) << ",\n";
ss << wbString_quote("end_file") << ":"
<< wbString_quote(wbTimerNode_getEndFile(node)) << ",\n";
ss << wbString_quote("parent_id") << ":"
<< wbString(wbTimerNode_hasParent(node)
? wbTimerNode_getId(wbTimerNode_getParent(node))
: -1) << ",\n";
ss << wbString_quote("message") << ":"
<< wbString_quote(wbTimerNode_getMessage(node)) << "\n";
ss << "}";
return ss.str();
}
}
static inline string wbTimerNode_toXML(wbTimerNode_t node) {
if (node == NULL) {
return "";
} else {
stringstream ss;
ss << "<node>\n";
ss << "<id>" << wbTimerNode_getId(node) << "</id>\n";
ss << "<stoppedQ>"
<< wbString(wbTimerNode_stoppedQ(node) ? "true" : "false")
<< "</stoppedQ>\n";
ss << "<kind>" << _nodeKind(wbTimerNode_getKind(node)) << "</kind>\n";
ss << "<start_time>" << wbTimerNode_getStartTime(node) << "</start_time>\n";
ss << "<end_time>" << wbTimerNode_getEndTime(node) << "</end_time>\n";
ss << "<elapsed_time>" << wbTimerNode_getElapsedTime(node)
<< "</elapsed_time>\n";
ss << "<start_line>" << wbTimerNode_getStartLine(node) << "</start_line>\n";
ss << "<end_line>" << wbTimerNode_getEndLine(node) << "</end_line>\n";
ss << "<start_function>" << wbTimerNode_getStartFunction(node)
<< "</start_function>\n";
ss << "<end_function>" << wbTimerNode_getEndFunction(node)
<< "</end_function>\n";
ss << "<start_file>" << wbTimerNode_getStartFile(node) << "</start_file>\n";
ss << "<end_file>" << wbTimerNode_getEndFile(node) << "</end_file>\n";
ss << "<parent_id>"
<< wbString(wbTimerNode_hasParent(node)
? wbTimerNode_getId(wbTimerNode_getParent(node))
: -1) << "</parent_id>\n";
ss << "<message>" << wbTimerNode_getMessage(node) << "</message>\n";
ss << "</node>\n";
return ss.str();
}
}
#define wbTimer_getLength(timer) ((timer)->length)
#define wbTimer_getHead(timer) ((timer)->head)
#define wbTimer_getTail(timer) ((timer)->tail)
#define wbTimer_getStartTime(timer) ((timer)->startTime)
#define wbTimer_getEndTime(timer) ((timer)->endTime)
#define wbTimer_getElapsedTime(timer) ((timer)->elapsedTime)
#define wbTimer_setLength(timer, val) (wbTimer_getLength(timer) = val)
#define wbTimer_setHead(timer, val) (wbTimer_getHead(timer) = val)
#define wbTimer_setTail(timer, val) (wbTimer_getTail(timer) = val)
#define wbTimer_setStartTime(node, val) (wbTimer_getStartTime(node) = val)
#define wbTimer_setEndTime(node, val) (wbTimer_getEndTime(node) = val)
#define wbTimer_setElapsedTime(node, val) (wbTimer_getElapsedTime(node) = val)
#define wbTimer_incrementLength(timer) (wbTimer_getLength(timer)++)
#define wbTimer_decrementLength(timer) (wbTimer_getLength(timer)--)
#define wbTimer_emptyQ(timer) (wbTimer_getLength(timer) == 0)
void wbTimer_delete(wbTimer_t timer) {
if (timer != NULL) {
wbTimerNode_t tmp, iter;
iter = wbTimer_getHead(timer);
while (iter) {
tmp = wbTimerNode_getNext(iter);
wbTimerNode_delete(iter);
iter = tmp;
}
wbDelete(timer);
}
}
string wbTimer_toJSON(wbTimer_t timer) {
if (timer == NULL) {
return NULL;
} else {
stringstream ss;
wbTimerNode_t iter;
uint64_t currentTime;
currentTime = getTime();
wbTimer_setEndTime(timer, currentTime);
wbTimer_setElapsedTime(timer, currentTime - wbTimer_getStartTime(timer));
ss << "{\n";
ss << wbString_quote("start_time") << ":" << wbTimer_getStartTime(timer)
<< ",\n";
ss << wbString_quote("end_time") << ":" << wbTimer_getEndTime(timer)
<< ",\n";
ss << wbString_quote("elapsed_time") << ":" << wbTimer_getElapsedTime(timer)
<< ",\n";
ss << wbString_quote("elements") << ":[\n";
for (iter = wbTimer_getHead(timer); iter != NULL;
iter = wbTimerNode_getNext(iter)) {
if (!wbTimerNode_stoppedQ(iter)) {
wbTimerNode_setEndTime(iter, currentTime);
wbTimerNode_setElapsedTime(iter, currentTime -
wbTimerNode_getStartTime(iter));
}
ss << wbTimerNode_toJSON(iter);
if (wbTimerNode_getNext(iter) != NULL) {
ss << ",\n";
}
}
ss << "]\n";
ss << "}";
return ss.str();
}
}
string wbTimer_toJSON() { return wbTimer_toJSON(_timer); }
string wbTimer_toXML(wbTimer_t timer) {
if (timer == NULL) {
return "";
} else {
stringstream ss;
wbTimerNode_t iter;
uint64_t currentTime;
currentTime = getTime();
wbTimer_setEndTime(timer, currentTime);
wbTimer_setElapsedTime(timer, currentTime - wbTimer_getStartTime(timer));
ss << "<timer>\n";
ss << "<start_time>" << wbTimer_getStartTime(timer) << "</start_time>\n";
ss << "<end_time>" << wbTimer_getEndTime(timer) << "</end_time>\n";
ss << "<elapsed_time>" << wbTimer_getElapsedTime(timer)
<< "</elapsed_time>\n";
ss << "<elements>\n";
for (iter = wbTimer_getHead(timer); iter != NULL;
iter = wbTimerNode_getNext(iter)) {
if (!wbTimerNode_stoppedQ(iter)) {
wbTimerNode_setEndTime(iter, currentTime);
wbTimerNode_setElapsedTime(iter, currentTime -
wbTimerNode_getStartTime(iter));
}
ss << wbTimerNode_toXML(iter);
}
ss << "</elements>\n";
ss << "</timer>\n";
return ss.str();
}
}
string wbTimer_toXML() { return wbTimer_toXML(_timer); }
wbTimer_t wbTimer_new(void) {
wbTimer_t timer = wbNew(struct st_wbTimer_t);
wbTimer_setLength(timer, 0);
wbTimer_setHead(timer, NULL);
wbTimer_setTail(timer, NULL);
wbTimer_setStartTime(timer, getTime());
wbTimer_setEndTime(timer, 0);
wbTimer_setElapsedTime(timer, 0);
return timer;
}
static inline wbTimerNode_t _findParent(wbTimer_t timer) {
wbTimerNode_t iter;
for (iter = wbTimer_getTail(timer); iter != NULL;
iter = wbTimerNode_getPrevious(iter)) {
if (!wbTimerNode_stoppedQ(iter)) {
return iter;
}
}
return NULL;
}
static inline void _insertIntoList(wbTimer_t timer, wbTimerNode_t node) {
if (wbTimer_emptyQ(timer)) {
wbTimer_setHead(timer, node);
wbTimer_setTail(timer, node);
} else {
wbTimerNode_t end = wbTimer_getTail(timer);
wbTimer_setTail(timer, node);
wbTimerNode_setNext(end, node);
wbTimerNode_setPrevious(node, end);
}
wbTimer_incrementLength(timer);
}
wbTimerNode_t wbTimer_start(wbTimerKind_t kind, const char *file,
const char *fun, int line) {
int id;
uint64_t currentTime;
wbTimerNode_t node;
wbTimerNode_t parent;
wb_init();
currentTime = getTime();
id = wbTimer_getLength(_timer);
node = wbTimerNode_new(id, kind, file, fun, line);
parent = _findParent(_timer);
_insertIntoList(_timer, node);
wbTimerNode_setStartTime(node, currentTime);
wbTimerNode_setParent(node, parent);
if (parent != NULL) {
wbTimerNode_setLevel(node, wbTimerNode_getLevel(parent) + 1);
}
return node;
}
wbTimerNode_t wbTimer_start(wbTimerKind_t kind, string msg, const char *file,
const char *fun, int line) {
wbTimerNode_t node = wbTimer_start(kind, file, fun, line);
wbTimerNode_setMessage(node, wbString_duplicate(msg));
return node;
}
static inline wbTimerNode_t _findNode(wbTimer_t timer, wbTimerKind_t kind,
string msg) {
wbTimerNode_t iter;
for (iter = wbTimer_getTail(timer); iter != NULL;
iter = wbTimerNode_getPrevious(iter)) {
if (msg == "") {
if (!wbTimerNode_stoppedQ(iter) && wbTimerNode_getKind(iter) == kind) {
return iter;
}
} else {
if (!wbTimerNode_stoppedQ(iter) && wbTimerNode_getKind(iter) == kind &&
msg == wbTimerNode_getMessage(iter)) {
return iter;
}
}
}
return NULL;
}
void wbTimer_stop(wbTimerKind_t kind, string msg, const char *file,
const char *fun, int line) {
uint64_t currentTime;
wbTimerNode_t node;
currentTime = getTime();
node = _findNode(_timer, kind, msg);
wbAssert(node != NULL);
if (node == NULL) {
return;
}
wbTimerNode_setEndTime(node, currentTime);
wbTimerNode_setElapsedTime(node,
currentTime - wbTimerNode_getStartTime(node));
wbTimerNode_setEndLine(node, line);
wbTimerNode_setEndFunction(node, fun);
wbTimerNode_setEndFile(node, file);
wbTimerNode_setStoppedQ(node, wbTrue);
return;
}
void wbTimer_stop(wbTimerKind_t kind, const char *file, const char *fun,
int line) {
wbTimer_stop(kind, "", file, fun, line);
}