1 /* Copyright © 2013, Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "fips-dispatch-gl.h"
33 #include "metrics-info.h"
39 #define MAX_MONITORS_IN_FLIGHT 1000
42 typedef struct timer_query
47 struct timer_query *next;
50 /* Performance-monitor query */
51 typedef struct monitor
59 typedef struct op_metrics
61 /* This happens to also be the index into the
62 * metrics->op_metrics array currently
72 /* Description of all available peformance counters, counter
73 * groups, their names and IDs, etc. */
76 /* The current operation being measured. */
79 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
81 unsigned timer_begun_id;
83 /* GL_TIME_ELAPSED queries for which glEndQuery has been
84 * called, (but results have not yet been queried). */
85 timer_query_t *timer_head;
86 timer_query_t *timer_tail;
88 /* Performance monitor for which glEndPerfMonitorAMD has not
90 unsigned monitor_begun_id;
92 /* Performance monitors for which glEndPerfMonitorAMD has
93 * been called, (but results have not yet been queried). */
94 monitor_t *monitor_head;
95 monitor_t *monitor_tail;
97 int monitors_in_flight;
99 unsigned num_op_metrics;
100 op_metrics_t *op_metrics;
104 metrics_create (metrics_info_t *info)
108 metrics = xmalloc (sizeof (metrics_t));
110 metrics->info = info;
114 metrics->timer_begun_id = 0;
116 metrics->timer_head = NULL;
117 metrics->timer_tail = NULL;
119 metrics->monitor_begun_id = 0;
121 metrics->monitor_head = NULL;
122 metrics->monitor_tail = NULL;
124 metrics->monitors_in_flight = 0;
126 metrics->num_op_metrics = 0;
127 metrics->op_metrics = NULL;
133 metrics_fini (metrics_t *metrics)
135 timer_query_t *timer, *timer_next;
136 monitor_t *monitor, *monitor_next;
138 /* Discard and cleanup any outstanding queries. */
139 if (metrics->timer_begun_id) {
140 glEndQuery (GL_TIME_ELAPSED);
141 glDeleteQueries (1, &metrics->timer_begun_id);
142 metrics->timer_begun_id = 0;
145 for (timer = metrics->timer_head;
149 glDeleteQueries (1, &timer->id);
150 timer_next = timer->next;
153 metrics->timer_head = NULL;
154 metrics->timer_tail = NULL;
156 if (metrics->info->have_perfmon) {
158 if (metrics->monitor_begun_id) {
159 glEndPerfMonitorAMD (metrics->monitor_begun_id);
160 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
161 metrics->monitor_begun_id = 0;
164 for (monitor = metrics->monitor_head;
166 monitor = monitor_next)
168 glDeletePerfMonitorsAMD (1, &monitor->id);
169 monitor_next = monitor->next;
172 metrics->monitor_head = NULL;
173 metrics->monitor_tail = NULL;
177 metrics->monitors_in_flight = 0;
181 metrics_destroy (metrics_t *metrics)
183 metrics_fini (metrics);
189 metrics_op_string (metrics_op_t op)
191 if (op >= METRICS_OP_SHADER)
192 return "Shader program";
196 case METRICS_OP_ACCUM:
197 return "glAccum*(+)";
198 case METRICS_OP_BUFFER_DATA:
199 return "glBufferData(+)";
200 case METRICS_OP_BUFFER_SUB_DATA:
201 return "glCopyBufferSubData*";
202 case METRICS_OP_BITMAP:
204 case METRICS_OP_BLIT_FRAMEBUFFER:
205 return "glBlitFramebuffer*";
206 case METRICS_OP_CLEAR:
208 case METRICS_OP_CLEAR_BUFFER_DATA:
209 return "glCearBufferData(+)";
210 case METRICS_OP_CLEAR_TEX_IMAGE:
211 return "glClearTexImage(+)";
212 case METRICS_OP_COPY_PIXELS:
213 return "glCopyPixels";
214 case METRICS_OP_COPY_TEX_IMAGE:
215 return "glCopyTexImage(+)";
216 case METRICS_OP_DRAW_PIXELS:
217 return "glDrawPixels";
218 case METRICS_OP_GET_TEX_IMAGE:
219 return "glGetTexImage(+)";
220 case METRICS_OP_READ_PIXELS:
221 return "glReadPixels*";
222 case METRICS_OP_TEX_IMAGE:
223 return "glTexImage*(+)";
225 fprintf (stderr, "fips: Internal error: "
226 "Unknown metrics op value: %d\n", op);
234 metrics_counter_start (metrics_t *metrics)
238 /* Initialize the timer_query object. */
239 glGenQueries (1, &metrics->timer_begun_id);
241 /* Most everything else in this function is
242 * performance-monitor related. If we don't have that
243 * extension, just start the timer query and be done. */
244 if (! metrics->info->have_perfmon) {
245 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
249 /* Initialize the performance-monitor object */
250 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
252 for (i = 0; i < metrics->info->num_groups; i++)
254 metrics_group_info_t *group;
257 group = &metrics->info->groups[i];
259 num_counters = group->num_counters;
260 if (group->max_active_counters < group->num_counters)
262 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
263 group->max_active_counters,
264 group->num_counters, i);
265 num_counters = group->max_active_counters;
269 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
275 /* Start the queries */
276 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
278 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
282 metrics_counter_stop (metrics_t *metrics)
284 timer_query_t *timer;
287 /* Stop the current timer and monitor. */
288 glEndQuery (GL_TIME_ELAPSED);
290 if (metrics->info->have_perfmon)
291 glEndPerfMonitorAMD (metrics->monitor_begun_id);
293 /* Add these IDs to our lists of outstanding queries and
294 * monitors so the results can be collected later. */
295 timer = xmalloc (sizeof (timer_query_t));
297 timer->op = metrics->op;
298 timer->id = metrics->timer_begun_id;
301 if (metrics->timer_tail) {
302 metrics->timer_tail->next = timer;
303 metrics->timer_tail = timer;
305 metrics->timer_tail = timer;
306 metrics->timer_head = timer;
309 if (metrics->info->have_perfmon) {
310 /* Create a new performance-monitor query */
311 monitor = xmalloc (sizeof (monitor_t));
313 monitor->op = metrics->op;
314 monitor->id = metrics->monitor_begun_id;
315 monitor->next = NULL;
317 if (metrics->monitor_tail) {
318 metrics->monitor_tail->next = monitor;
319 metrics->monitor_tail = monitor;
321 metrics->monitor_tail = monitor;
322 metrics->monitor_head = monitor;
326 metrics->monitors_in_flight++;
328 /* Avoid being a resource hog and collect outstanding results
329 * once we have sent off a large number of
330 * queries. (Presumably, many of the outstanding queries are
333 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
334 metrics_collect_available (metrics);
338 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
344 metrics_get_current_op (metrics_t *metrics)
350 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
355 metrics->time_ns = 0.0;
357 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
359 for (i = 0; i < info->num_groups; i++) {
360 metrics->counters[i] = xmalloc (sizeof (double) *
361 info->groups[i].num_counters);
362 for (j = 0; j < info->groups[i].num_counters; j++)
363 metrics->counters[i][j] = 0.0;
367 static op_metrics_t *
368 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
372 if (op >= metrics->num_op_metrics)
374 metrics->op_metrics = realloc (metrics->op_metrics,
375 (op + 1) * sizeof (op_metrics_t));
376 for (i = metrics->num_op_metrics; i < op + 1; i++)
377 op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
379 metrics->num_op_metrics = op + 1;
382 return &metrics->op_metrics[op];
386 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
387 GLuint *result, GLuint size)
389 #define CONSUME(var) \
390 if (p + sizeof(var) > ((unsigned char *) result) + size) \
392 fprintf (stderr, "Unexpected end-of-buffer while " \
393 "parsing results\n"); \
396 (var) = *((typeof(var) *) p); \
399 metrics_info_t *info = metrics->info;
400 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
401 unsigned char *p = (unsigned char *) result;
403 while (p < ((unsigned char *) result) + size)
405 GLuint group_id, group_index;
406 GLuint counter_id, counter_index;
407 metrics_group_info_t *group;
412 CONSUME (counter_id);
414 for (i = 0; i < info->num_groups; i++) {
415 if (info->groups[i].id == group_id)
419 assert (group_index < info->num_groups);
420 group = &info->groups[group_index];
422 for (i = 0; i < group->num_counters; i++) {
423 if (group->counter_ids[i] == counter_id)
427 assert (counter_index < group->num_counters);
429 switch (group->counter_types[counter_index])
432 uint64_t uint64_value;
434 case GL_UNSIGNED_INT:
435 CONSUME (uint_value);
438 case GL_UNSIGNED_INT64_AMD:
439 CONSUME (uint64_value);
440 value = uint64_value;
442 case GL_PERCENTAGE_AMD:
444 CONSUME (float_value);
448 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
449 group->counter_types[counter_index]);
454 op_metrics->counters[group_index][counter_index] += value;
459 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
461 op_metrics_t *op_metrics;
463 op_metrics = _get_op_metrics (metrics, op);
465 op_metrics->time_ns += time_ns;
468 typedef struct per_stage_metrics
470 op_metrics_t *metrics;
471 shader_stage_info_t *stage;
474 } per_stage_metrics_t;
477 _is_shader_stage_counter (metrics_info_t *info,
478 unsigned group_index,
479 unsigned counter_index)
481 shader_stage_info_t *stage;
484 for (i = 0; i < info->num_shader_stages; i++) {
485 stage = &info->stages[i];
487 if (stage->active_group_index == group_index &&
488 stage->active_counter_index == counter_index)
493 if (stage->stall_group_index == group_index &&
494 stage->stall_counter_index == counter_index)
504 print_per_stage_metrics (metrics_t *metrics,
505 per_stage_metrics_t *per_stage,
508 metrics_info_t *info = metrics->info;
509 op_metrics_t *op_metrics = per_stage->metrics;
510 metrics_group_info_t *group;
511 const char *op_string;
512 unsigned group_index, counter;
515 /* Don't print anything for stages with no alloted time. */
516 if (per_stage->time_ns == 0.0)
519 op_string = metrics_op_string (op_metrics->op);
521 printf ("%21s", op_string);
523 if (op_metrics->op >= METRICS_OP_SHADER) {
524 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
530 if (per_stage->stage)
531 printf (" %cS:", per_stage->stage->name[0]);
535 printf ("\t%7.2f ms (%4.1f%%)",
536 per_stage->time_ns / 1e6,
537 per_stage->time_ns / total * 100);
539 if (per_stage->active)
540 printf (", %4.1f%% active", per_stage->active * 100);
544 /* I'm not seeing a lot of value printing the rest of these
545 * performance counters by default yet. Use --verbose to get
551 for (group_index = 0; group_index < info->num_groups; group_index++) {
552 group = &info->groups[group_index];
553 for (counter = 0; counter < group->num_counters; counter++) {
555 /* Don't print this counter value if it's a
556 * per-stage cycle counter, (which we have
557 * already accounted for). */
558 if (_is_shader_stage_counter (info, group_index, counter))
561 value = op_metrics->counters[group_index][counter];
564 printf ("%s: %.2f ", group->counter_names[counter],
572 time_compare(const void *in_a, const void *in_b, void *arg unused)
574 const per_stage_metrics_t *a = in_a;
575 const per_stage_metrics_t *b = in_b;
578 if (a->time_ns < b->time_ns)
580 if (a->time_ns > b->time_ns)
586 print_program_metrics (metrics_t *metrics)
588 metrics_info_t *info = metrics->info;
589 unsigned num_shader_stages = info->num_shader_stages;
590 per_stage_metrics_t *sorted, *per_stage;
591 double total_time, op_cycles;
593 unsigned group_index, counter_index;
594 unsigned i, j, num_sorted;
596 if (! info->have_perfmon &&
597 ! info->printed_missing_perfmon_warning)
600 "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
601 " Some fips features, (such as per-shader-stage timings),\n"
602 " will not be available.\n");
604 info->printed_missing_perfmon_warning = true;
607 /* Make a sorted list of the per-stage operations by time
608 * used, and figure out the total so we can print percentages.
610 if (num_shader_stages)
611 num_sorted = metrics->num_op_metrics * num_shader_stages;
613 num_sorted = metrics->num_op_metrics;
615 sorted = xmalloc (sizeof (*sorted) * num_sorted);
619 for (i = 0; i < metrics->num_op_metrics; i++) {
621 op = &metrics->op_metrics[i];
623 /* Accumulate total time across all ops. */
624 total_time += op->time_ns;
626 /* Also, find total cycles in all stages of this op. */
629 if (num_shader_stages == 0) {
630 per_stage = &sorted[i];
631 per_stage->metrics = op;
632 per_stage->stage = NULL;
633 per_stage->time_ns = op->time_ns;
634 per_stage->active = 0.0;
637 for (j = 0; j < num_shader_stages; j++) {
639 group_index = info->stages[j].active_group_index;
640 counter_index = info->stages[j].active_counter_index;
641 op_cycles += op->counters[group_index][counter_index];
644 group_index = info->stages[j].stall_group_index;
645 counter_index = info->stages[j].stall_counter_index;
646 op_cycles += op->counters[group_index][counter_index];
649 for (j = 0; j < num_shader_stages; j++) {
650 double active_cycles, stall_cycles, stage_cycles;
653 group_index = info->stages[j].active_group_index;
654 counter_index = info->stages[j].active_counter_index;
655 active_cycles = op->counters[group_index][counter_index];
658 group_index = info->stages[j].stall_group_index;
659 counter_index = info->stages[j].stall_counter_index;
660 stall_cycles = op->counters[group_index][counter_index];
662 stage_cycles = active_cycles + stall_cycles;
664 per_stage = &sorted[i * num_shader_stages + j];
665 per_stage->metrics = op;
668 per_stage->stage = &info->stages[j];
669 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
671 /* If we don't have any per-stage cycle counts
672 * for this operation, then use the first
673 * stage as a placeholder for all the time,
674 * but NULL-ify the stage info so that the
675 * report doesn't lie about this time being
676 * from any particular stage. */
677 per_stage->stage = NULL;
679 per_stage->time_ns = op->time_ns;
681 per_stage->time_ns = 0.0;
686 per_stage->active = active_cycles / stage_cycles;
688 per_stage->active = 0.0;
693 qsort_r (sorted, num_sorted, sizeof (*sorted),
694 time_compare, metrics->op_metrics);
696 for (i = 0; i < num_sorted; i++)
697 print_per_stage_metrics (metrics, &sorted[i], total_time);
703 metrics_collect_available (metrics_t *metrics)
705 /* Consume all timer queries that are ready. */
706 timer_query_t *timer = metrics->timer_head;
709 GLuint available, elapsed;
711 glGetQueryObjectuiv (timer->id,
712 GL_QUERY_RESULT_AVAILABLE, &available);
716 glGetQueryObjectuiv (timer->id,
717 GL_QUERY_RESULT, &elapsed);
719 accumulate_program_time (metrics, timer->op, elapsed);
721 metrics->timer_head = timer->next;
722 if (metrics->timer_head == NULL)
723 metrics->timer_tail = NULL;
725 glDeleteQueries (1, &timer->id);
728 timer = metrics->timer_head;
731 if (! metrics->info->have_perfmon)
734 /* And similarly for all performance monitors that are ready. */
735 monitor_t *monitor = metrics->monitor_head;
738 GLuint available, result_size, *result;
741 glGetPerfMonitorCounterDataAMD (monitor->id,
742 GL_PERFMON_RESULT_AVAILABLE_AMD,
743 sizeof (available), &available,
748 glGetPerfMonitorCounterDataAMD (monitor->id,
749 GL_PERFMON_RESULT_SIZE_AMD,
750 sizeof (result_size),
753 result = xmalloc (result_size);
755 glGetPerfMonitorCounterDataAMD (monitor->id,
756 GL_PERFMON_RESULT_AMD,
760 accumulate_program_metrics (metrics, monitor->op, result, result_size);
764 metrics->monitor_head = monitor->next;
765 if (metrics->monitor_head == NULL)
766 metrics->monitor_tail = NULL;
768 glDeletePerfMonitorsAMD (1, &monitor->id);
772 metrics->monitors_in_flight--;
774 monitor = metrics->monitor_head;
780 metrics_end_frame (metrics_t *metrics)
782 static int initialized = 0;
783 static struct timeval tv_start, tv_now;
785 /* Don't leave any counters running over work we do here. */
786 metrics_counter_stop (metrics);
789 gettimeofday (&tv_start, NULL);
790 if (getenv ("FIPS_VERBOSE"))
797 metrics_collect_available (metrics);
799 if (frames % 15 == 0) {
802 gettimeofday (&tv_now, NULL);
804 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
805 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
807 printf("FPS: %.3f\n", fps);
809 print_program_metrics (metrics);
812 /* Start the counter up again now that we're done. */
813 metrics_counter_start (metrics);