-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprofiler.cpp
598 lines (485 loc) · 16.4 KB
/
profiler.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// profiler.cpp
#include "profiler.h"
#ifdef ENABLE_PROFILER
#include "hp_timer.h"
#include "drawer2D.h"
#include "thread.h"
#include <limits.h>
#include <stdio.h>
Profiler profiler;
// Unit: percentage of the screen dimensions
#define MARGIN_X 0.02f // left and right margin
#define MARGIN_Y 0.02f // bottom margin
#define LINE_HEIGHT 0.01f // height of a line representing a thread
//#define TIME_DRAWN_MS 30.0 // the width of the profiler corresponds to TIME_DRAWN_MS milliseconds
//#define TIME_DRAWN_MS 60.0 // the width of the profiler corresponds to TIME_DRAWN_MS milliseconds
#define TIME_DRAWN_MS 120.0 // the width of the profiler corresponds to TIME_DRAWN_MS milliseconds
#define GPU_COUNT 1 // TODO: multiple GPUs are not supported
// -----
#define PROFILER_WIDTH (1.0f - 2.0f*MARGIN_X)
#define X_OFFSET MARGIN_X
#define Y_OFFSET (MARGIN_Y + LINE_HEIGHT)
#define X_FACTOR ( (float)(PROFILER_WIDTH / (TIME_DRAWN_MS * 1000000.0)) )
#define Y_SCALE_OFFSET 0.002f // By how much do we reduce the height when displaying
// a marker that is lower in the hierarchy
#define NB_MAX_TEXT_LINES 20
#define Y_TEXT_MARGIN 0.05f // size between 2 lines of text
#define COLOR_FROZEN Color(0xD0, 0xD0, 0xD0)
//-----------------------------------------------------------------------------
void Profiler::init(int win_w, int win_h, int mouse_x, int mouse_y)
{
m_cur_frame = 0;
m_freeze_state = UNFROZEN;
m_visible = true;
updateBackgroundRect();
m_win_w = win_w;
m_win_h = win_h;
m_mouse_x = mouse_x;
m_mouse_y = mouse_y;
mutexCreate(&m_cpu_mutex);
for(size_t i=0 ; i < NB_RECORDED_FRAMES ; i++)
{
m_frame_info[i].frame = -1;
m_frame_info[i].time_sync_start = INVALID_TIME;
m_frame_info[i].time_sync_end = INVALID_TIME;
}
}
//-----------------------------------------------------------------------------
void Profiler::shut()
{
// Release GPU timer queries
for(size_t i=0 ; i < NB_GPU_MARKERS ; i++)
{
GpuMarker& marker = m_gpu_thread_info.markers[i];
if(marker.id_query_start != INVALID_QUERY)
{
glDeleteQueries(1, &marker.id_query_start);
marker.id_query_start = INVALID_QUERY;
}
if(marker.id_query_end != INVALID_QUERY)
{
glDeleteQueries(1, &marker.id_query_end);
marker.id_query_end = INVALID_QUERY;
}
}
mutexDestroy(&m_cpu_mutex);
}
//-----------------------------------------------------------------------------
/// Push a new marker that starts now
void Profiler::pushCpuMarker(const char* name, const Color& color)
{
// Don't do anything when frozen
if(isFrozen())
return;
CpuThreadInfo& ti = getOrAddCpuThreadInfo();
Marker& marker = ti.markers[ti.cur_write_id];
assert(marker.frame != m_cur_frame && "looping: too many markers, no free slots available");
marker.start = getTimeNs();
marker.end = INVALID_TIME;
marker.layer = ti.nb_pushed_markers;
strncpy(marker.name, name, MARKER_NAME_MAX_LENGTH);
marker.color = color;
marker.frame = m_cur_frame;
incrementCycle(&ti.cur_write_id, NB_MARKERS_PER_CPU_THREAD);
ti.nb_pushed_markers++;
}
//-----------------------------------------------------------------------------
/// Stop the last pushed marker
void Profiler::popCpuMarker()
{
// Don't do anything when frozen
if(isFrozen())
return;
CpuThreadInfo& ti = getOrAddCpuThreadInfo();
assert(ti.nb_pushed_markers != 0);
// Get the most recent marker that has not been closed yet
int index = ti.cur_write_id-1;
while(ti.markers[index].end != INVALID_TIME) // skip closed markers
decrementCycle(&index, NB_MARKERS_PER_CPU_THREAD);
Marker& marker = ti.markers[index];
assert(marker.end == INVALID_TIME);
marker.end = getTimeNs();
ti.nb_pushed_markers--;
}
//-----------------------------------------------------------------------------
/// Push a new GPU marker that starts when the previously issued commands are processed
void Profiler::pushGpuMarker(const char* name, const Color& color)
{
// Don't do anything when frozen
if(isFrozen())
return;
GpuThreadInfo& ti = m_gpu_thread_info;
GpuMarker& marker = ti.markers[ti.cur_write_id];
assert(marker.frame != m_cur_frame && "looping: too many markers, no free slots available");
// Issue timer query
if(marker.id_query_start == INVALID_QUERY)
glGenQueries(1, &marker.id_query_start);
glQueryCounter(marker.id_query_start, GL_TIMESTAMP);
// Fill in marker
marker.start = INVALID_TIME;
marker.end = INVALID_TIME;
marker.layer = ti.nb_pushed_markers;
strncpy(marker.name, name, MARKER_NAME_MAX_LENGTH);
marker.color = color;
marker.frame = m_cur_frame;
incrementCycle(&ti.cur_write_id, NB_GPU_MARKERS);
ti.nb_pushed_markers++;
}
//-----------------------------------------------------------------------------
/// Stop the last pushed GPU marker when the previously issued commands are processed
void Profiler::popGpuMarker()
{
// Don't do anything when frozen
if(isFrozen())
return;
GpuThreadInfo& ti = m_gpu_thread_info;
// Get the most recent marker that has not been closed yet
int index = ti.cur_write_id-1;
while(ti.markers[index].end != INVALID_TIME) // skip closed markers
decrementCycle(&index, NB_GPU_MARKERS);
GpuMarker& marker = ti.markers[index];
// Issue timer query
if(marker.id_query_end == INVALID_QUERY)
glGenQueries(1, &marker.id_query_end);
glQueryCounter(marker.id_query_end, GL_TIMESTAMP);
ti.nb_pushed_markers--;
}
//-----------------------------------------------------------------------------
/// Update frame information and frame counter
void Profiler::synchronizeFrame()
{
// Freeze/unfreeze as needed
if(m_freeze_state == WAITING_FOR_FREEZE)
m_freeze_state = FROZEN;
else if(m_freeze_state == WAITING_FOR_UNFREEZE)
m_freeze_state = UNFROZEN;
// Don't do anything when frozen
if(isFrozen())
return;
// Next frame
m_cur_frame++;
// Copy: cur_read_id <- next_read_id
// -> GPU:
m_gpu_thread_info.cur_read_id = m_gpu_thread_info.next_read_id;
// -> CPUs:
for(size_t i=m_cpu_thread_infos.begin() ;
i != m_cpu_thread_infos.getMaxSize() ;
i = m_cpu_thread_infos.next(i))
{
CpuThreadInfo &ti = m_cpu_thread_infos.get(i);
ti.cur_read_id = ti.next_read_id;
}
// Frame time information
uint64_t now = getTimeNs();
size_t index_oldest = 0;
for(size_t i=0 ; i < NB_RECORDED_FRAMES ; i++)
{
if(m_frame_info[i].frame < m_frame_info[index_oldest].frame)
index_oldest = i;
if(m_frame_info[i].frame == m_cur_frame-1)
m_frame_info[i].time_sync_end = now;
}
FrameInfo &new_frame = m_frame_info[index_oldest];
new_frame.time_sync_start = now;
new_frame.time_sync_end = INVALID_TIME;
new_frame.frame = m_cur_frame;
}
//-----------------------------------------------------------------------------
/// Draw the markers
void Profiler::draw()
{
if(m_visible)
drawBackground();
int displayed_frame = m_cur_frame - int(NB_RECORDED_FRAMES-1);
if(displayed_frame < 0) // don't draw anything during the first frames
return;
// Used when drawing information on the markers hovered by the mouse
int read_indices[GPU_COUNT + NB_MAX_CPU_THREADS]; // indices of the first drawn markers, per thread
int thread_index = 0; // read_indices[thread_index]
// --- Find the FrameInfo (start and end times) for the frame we want to display ---
FrameInfo* frame_info = NULL;
for(int index_frame_info = 0 ; index_frame_info < int(NB_RECORDED_FRAMES) ; index_frame_info++)
{
if(m_frame_info[index_frame_info].frame == displayed_frame)
{
frame_info = &m_frame_info[index_frame_info];
break;
}
}
if(!frame_info || frame_info->time_sync_end == INVALID_TIME)
return;
const uint64_t frame_delta_time = frame_info->time_sync_end - frame_info->time_sync_start;
// --- Draw the end of the frame ---
{
Rect rect_end;
rect_end.x = X_OFFSET + X_FACTOR*frame_delta_time;
rect_end.y = m_back_rect.y;
rect_end.w = 0.003f;
rect_end.h = m_back_rect.h;
if(m_visible)
drawer2D.drawRect(rect_end, COLOR_BLACK);
}
// ---- Draw the GPU markers ----
{
GpuThreadInfo& ti = m_gpu_thread_info;
int read_id = ti.cur_read_id;
read_indices[thread_index++] = read_id;
// Get the times and draw the markers
uint64_t first_start = INVALID_TIME;
// Select only the markers that belong to this frame.
// As GPU times are not synchronized with CPU times, we can't cleanly handle markers that started
// in the previous frame and finish in this one, so we just display the GPU markers that belong
// to the displayed frame.
while(ti.markers[read_id].frame == displayed_frame)
{
GpuMarker& marker = ti.markers[read_id];
bool ok = true;
if(marker.id_query_start == INVALID_QUERY ||
marker.id_query_end == INVALID_QUERY)
ok = false;
if(ok)
{
GLint start_ok = 0;
GLint end_ok = 0;
glGetQueryObjectiv(marker.id_query_start, GL_QUERY_RESULT_AVAILABLE, &start_ok);
glGetQueryObjectiv(marker.id_query_end, GL_QUERY_RESULT_AVAILABLE, &end_ok);
ok = (bool)(start_ok && end_ok);
}
if(ok)
{
uint64_t start, end;
glGetQueryObjectui64v(marker.id_query_start, GL_QUERY_RESULT, &marker.start);
glGetQueryObjectui64v(marker.id_query_end, GL_QUERY_RESULT, &marker.end);
start = marker.start;
end = marker.end;
if(first_start == INVALID_TIME)
first_start = start;
start -= first_start;
end -= first_start;
Rect rect;
rect.x = X_OFFSET + X_FACTOR * (float)(start);
rect.y = Y_OFFSET;
rect.w = X_FACTOR * (float)(end - start);
rect.h = LINE_HEIGHT;
// Reduce vertically the size of the markers according to their layer
rect.y += Y_SCALE_OFFSET *marker.layer;
rect.h -= (2.0f*Y_SCALE_OFFSET) *marker.layer;
if(m_visible)
drawer2D.drawRect(rect, marker.color);
}
incrementCycle(&read_id, NB_GPU_MARKERS);
}
ti.next_read_id = read_id;
}
// ---- Draw the CPU markers ----
// For each thread:
for(size_t i=m_cpu_thread_infos.begin() ;
i != m_cpu_thread_infos.getMaxSize() ;
i = m_cpu_thread_infos.next(i))
{
CpuThreadInfo &ti = m_cpu_thread_infos.get(i);
// Jump back to the last marker that ends after the start of this frame.
// Avoid going to a frame older than displayed_frame-1.
// -> handle markers that overlap the previous and the displayed frame
int read_id = ti.cur_read_id;
while(true)
{
int candidate_id = read_id;
decrementCycle(&candidate_id, NB_MARKERS_PER_CPU_THREAD);
if(ti.markers[candidate_id].frame >= displayed_frame-1 &&
ti.markers[candidate_id].end > frame_info->time_sync_start)
{
read_id = candidate_id;
continue;
}
break;
}
// In the worst case, we try to draw a marker that is out of this frame:
// it just gets clamped and nothing is visible
read_indices[thread_index++] = read_id;
// Draw the markers
while(ti.markers[read_id].frame >= displayed_frame-1 && // - for markers that started in the previous frame and finished
// in this frame
ti.markers[read_id].frame <= displayed_frame) // - for "regular" markers, that started in this frame
{
CpuMarker &marker = ti.markers[read_id];
uint64_t start, end;
start = clamp(marker.start, frame_info->time_sync_start, frame_info->time_sync_end);
end = clamp(marker.end, frame_info->time_sync_start, frame_info->time_sync_end);
start -= frame_info->time_sync_start;
end -= frame_info->time_sync_start;
Rect rect;
rect.x = X_OFFSET + X_FACTOR * (float)(start);
rect.y = Y_OFFSET + (i+GPU_COUNT)*LINE_HEIGHT;
rect.w = X_FACTOR * (float)(end - start);
rect.h = LINE_HEIGHT;
// Reduce vertically the size of the markers according to their layer
rect.y += Y_SCALE_OFFSET*marker.layer;
rect.h -= (2.0f*Y_SCALE_OFFSET)*marker.layer;
if(m_visible)
drawer2D.drawRect(rect, marker.color);
incrementCycle(&read_id, NB_MARKERS_PER_CPU_THREAD);
}
ti.next_read_id = read_id;
}
if(m_visible)
drawHoveredMarkersText(read_indices, frame_info);
}
//-----------------------------------------------------------------------------
/// Handle freeze/unfreeze
void Profiler::onLeftClick()
{
if(!m_visible)
return;
float fx = float(m_mouse_x) / float(m_win_w);
float fy = float(m_win_h-1 - m_mouse_y) / float(m_win_h);
if(m_back_rect.isPointInside(fx, fy))
{
switch(m_freeze_state)
{
case UNFROZEN:
m_freeze_state = WAITING_FOR_FREEZE;
break;
case FROZEN:
m_freeze_state = WAITING_FOR_UNFREEZE;
break;
case WAITING_FOR_FREEZE:
case WAITING_FOR_UNFREEZE:
assert(false && "should not happen - synchronizeFrame() should be called between 2 calls to onLeftClick()");
break;
}
}
}
//-----------------------------------------------------------------------------
// Get the CpuThreadInfo corresponding to the calling thread
Profiler::CpuThreadInfo& Profiler::getOrAddCpuThreadInfo()
{
ThreadId thread_id = threadGetCurrentId();
for(size_t i = m_cpu_thread_infos.begin();
i != m_cpu_thread_infos.getMaxSize() ;
i = m_cpu_thread_infos.next(i))
{
if(m_cpu_thread_infos.isUsed(i) &&
m_cpu_thread_infos.get(i).thread_id == thread_id)
{
return m_cpu_thread_infos.get(i);
}
}
mutexLock(&m_cpu_mutex);
size_t i = m_cpu_thread_infos.add();
mutexUnlock(&m_cpu_mutex);
CpuThreadInfo &ti = m_cpu_thread_infos.get(i);
ti.init(thread_id);
updateBackgroundRect();
return ti;
}
//-----------------------------------------------------------------------------
void Profiler::drawBackground()
{
drawer2D.drawRect(m_back_rect, isFrozen() ? COLOR_FROZEN : COLOR_WHITE);
}
/// Draw text information for the markers that are hovered by the mouse pointer
void Profiler::drawHoveredMarkersText(const int *read_indices, const FrameInfo* frame_info)
{
// Compute some values for drawing
float fx = float(m_mouse_x) / float(m_win_w);
float fy = float(m_win_h-1 - m_mouse_y) / float(m_win_h);
size_t nb_max_markers = 0;
const Marker *markers = NULL;
int read_id = -1;
uint64_t start_time = 0;
size_t sizeof_marker = 0;
#define GET_MARKER(__i) (const Marker*)(((const uint8_t*)markers) + (__i)*sizeof_marker)
Rect rect;
rect.x = X_OFFSET;
rect.y = Y_OFFSET;
rect.w = PROFILER_WIDTH;
rect.h = LINE_HEIGHT;
// --- Which list of markers is hovered by the mouse pointer? ---
if(rect.isPointInside(fx, fy))
{
// Hovering the GPU line
nb_max_markers = NB_GPU_MARKERS;
markers = &m_gpu_thread_info.markers[0];
read_id = read_indices[0];
start_time = m_gpu_thread_info.markers[read_id].start;
sizeof_marker = sizeof(GpuMarker);
}
else
{
// CPUs
for(size_t i=m_cpu_thread_infos.begin() ;
i != m_cpu_thread_infos.getMaxSize() ;
i = m_cpu_thread_infos.next(i))
{
rect.y += LINE_HEIGHT;
if(rect.isPointInside(fx, fy))
{
// Hovering a CPU line
nb_max_markers = NB_MARKERS_PER_CPU_THREAD;
markers = &m_cpu_thread_infos[i].markers[0];
read_id = read_indices[i+GPU_COUNT];
start_time = frame_info->time_sync_start;
sizeof_marker = sizeof(CpuMarker);
break;
}
}
}
if(!markers)
return; // mouse pointer doesn't hover any line
// --- Choose the markers that are to be displayed ---
const Marker *chosen_markers[NB_MAX_TEXT_LINES];
int nb_chosen_markers = 0;
{
int counter = 0;
const Marker* m = GET_MARKER(read_id);
while( m->frame >= frame_info->frame-1 &&
m->frame <= frame_info->frame &&
nb_chosen_markers < NB_MAX_TEXT_LINES)
{
// Get the relative start and end of the marker
uint64_t start = m->start - start_time;
uint64_t end = m->end - start_time;
rect.x = X_OFFSET + X_FACTOR * (float)(start);
rect.w = X_FACTOR * (float)(end - start);
if(rect.isPointInside(fx, fy))
{
chosen_markers[nb_chosen_markers++] = m;
counter++;
}
// Next
incrementCycle(&read_id, nb_max_markers);
m = GET_MARKER(read_id);
}
}
// --- Draw information on the chosen markers ---
{
char str[256];
float y_text = m_back_rect.y + m_back_rect.h + Y_TEXT_MARGIN;
for(int i=nb_chosen_markers-1 ; i >= 0 ; i--)
{
const Marker* m = chosen_markers[i];
uint64_t marker_time_us = (m->end - m->start) / (uint64_t)(1000);
double marker_time_ms = double(marker_time_us) / 1000.0;
sprintf(str, "[%2.1lfms] ", marker_time_ms);
size_t len=strlen(str);
for(size_t layer=0 ; layer < m->layer ; layer++)
str[len++] = '+';
str[len] = '\0';
strcat(str, m->name);
drawer2D.drawString(str, 0.01f, y_text, m->color);
y_text += Y_TEXT_MARGIN;
}
}
#undef GET_MARKER
}
void Profiler::updateBackgroundRect()
{
size_t nb_threads = m_cpu_thread_infos.getSize();
nb_threads += GPU_COUNT;
m_back_rect.x = MARGIN_X;
m_back_rect.y = MARGIN_Y;
m_back_rect.w = 1.0f-2.0f*MARGIN_X;
m_back_rect.h = ((float)(nb_threads) + 2.0f)*LINE_HEIGHT;
}
#endif // defined(ENABLE_PROFILER)