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 * ctx->op_metrics array currently
74 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
76 unsigned timer_begun_id;
78 /* GL_TIME_ELAPSED queries for which glEndQuery has been
79 * called, (but results have not yet been queried). */
80 timer_query_t *timer_head;
81 timer_query_t *timer_tail;
83 /* Performance monitor for which glEndPerfMonitorAMD has not
85 unsigned monitor_begun_id;
87 /* Performance monitors for which glEndPerfMonitorAMD has
88 * been called, (but results have not yet been queried). */
89 monitor_t *monitor_head;
90 monitor_t *monitor_tail;
92 int monitors_in_flight;
94 unsigned num_op_metrics;
95 op_metrics_t *op_metrics;
99 metrics_op_string (metrics_op_t op)
101 if (op >= METRICS_OP_SHADER)
102 return "Shader program";
106 case METRICS_OP_ACCUM:
107 return "glAccum*(+)";
108 case METRICS_OP_BUFFER_DATA:
109 return "glBufferData(+)";
110 case METRICS_OP_BUFFER_SUB_DATA:
111 return "glCopyBufferSubData*";
112 case METRICS_OP_BITMAP:
114 case METRICS_OP_BLIT_FRAMEBUFFER:
115 return "glBlitFramebuffer*";
116 case METRICS_OP_CLEAR:
118 case METRICS_OP_CLEAR_BUFFER_DATA:
119 return "glCearBufferData(+)";
120 case METRICS_OP_CLEAR_TEX_IMAGE:
121 return "glClearTexImage(+)";
122 case METRICS_OP_COPY_PIXELS:
123 return "glCopyPixels";
124 case METRICS_OP_COPY_TEX_IMAGE:
125 return "glCopyTexImage(+)";
126 case METRICS_OP_DRAW_PIXELS:
127 return "glDrawPixels";
128 case METRICS_OP_GET_TEX_IMAGE:
129 return "glGetTexImage(+)";
130 case METRICS_OP_READ_PIXELS:
131 return "glReadPixels*";
132 case METRICS_OP_TEX_IMAGE:
133 return "glTexImage*(+)";
135 fprintf (stderr, "fips: Internal error: "
136 "Unknown metrics op value: %d\n", op);
144 metrics_counter_start (void)
146 context_t *ctx = context_get_current ();
147 metrics_t *metrics = ctx->metrics;
150 /* Initialize the timer_query and monitor objects */
151 glGenQueries (1, &metrics->timer_begun_id);
153 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
155 for (i = 0; i < ctx->metrics_info.num_groups; i++)
157 metrics_group_info_t *group;
160 group = &ctx->metrics_info.groups[i];
162 num_counters = group->num_counters;
163 if (group->max_active_counters < group->num_counters)
165 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
166 group->max_active_counters,
167 group->num_counters, i);
168 num_counters = group->max_active_counters;
172 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
178 /* Start the queries */
179 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
181 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
185 metrics_counter_stop (void)
187 context_t *ctx = context_get_current ();
188 metrics_t *metrics = ctx->metrics;
189 timer_query_t *timer;
192 /* Stop the current timer and monitor. */
193 glEndQuery (GL_TIME_ELAPSED);
194 glEndPerfMonitorAMD (metrics->monitor_begun_id);
196 /* Add these IDs to our lists of outstanding queries and
197 * monitors so the results can be collected later. */
198 timer = xmalloc (sizeof (timer_query_t));
200 timer->op = metrics->op;
201 timer->id = metrics->timer_begun_id;
204 if (metrics->timer_tail) {
205 metrics->timer_tail->next = timer;
206 metrics->timer_tail = timer;
208 metrics->timer_tail = timer;
209 metrics->timer_head = timer;
212 /* Create a new performance-monitor query */
213 monitor = xmalloc (sizeof (monitor_t));
215 monitor->op = metrics->op;
216 monitor->id = metrics->monitor_begun_id;
217 monitor->next = NULL;
219 if (metrics->monitor_tail) {
220 metrics->monitor_tail->next = monitor;
221 metrics->monitor_tail = monitor;
223 metrics->monitor_tail = monitor;
224 metrics->monitor_head = monitor;
227 metrics->monitors_in_flight++;
229 /* Avoid being a resource hog and collect outstanding results
230 * once we have sent off a large number of
231 * queries. (Presumably, many of the outstanding queries are
234 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
235 metrics_collect_available ();
239 metrics_set_current_op (metrics_op_t op)
241 context_t *ctx = context_get_current ();
242 metrics_t *metrics = ctx->metrics;
248 metrics_get_current_op (void)
250 context_t *ctx = context_get_current ();
251 metrics_t *metrics = ctx->metrics;
257 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
259 metrics_info_t *info = &ctx->metrics_info;
263 metrics->time_ns = 0.0;
265 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
267 for (i = 0; i < info->num_groups; i++) {
268 metrics->counters[i] = xmalloc (sizeof (double) *
269 info->groups[i].num_counters);
270 for (j = 0; j < info->groups[i].num_counters; j++)
271 metrics->counters[i][j] = 0.0;
275 static op_metrics_t *
276 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
278 metrics_t *metrics = ctx->metrics;
281 if (op >= metrics->num_op_metrics)
283 metrics->op_metrics = realloc (metrics->op_metrics,
284 (op + 1) * sizeof (op_metrics_t));
285 for (i = metrics->num_op_metrics; i < op + 1; i++)
286 op_metrics_init (ctx, &metrics->op_metrics[i], i);
288 metrics->num_op_metrics = op + 1;
291 return &metrics->op_metrics[op];
295 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
297 #define CONSUME(var) \
298 if (p + sizeof(var) > ((unsigned char *) result) + size) \
300 fprintf (stderr, "Unexpected end-of-buffer while " \
301 "parsing results\n"); \
304 (var) = *((typeof(var) *) p); \
307 context_t *ctx = context_get_current ();
308 metrics_info_t *info = &ctx->metrics_info;
309 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
310 unsigned char *p = (unsigned char *) result;
312 while (p < ((unsigned char *) result) + size)
314 GLuint group_id, group_index;
315 GLuint counter_id, counter_index;
316 metrics_group_info_t *group;
321 CONSUME (counter_id);
323 for (i = 0; i < info->num_groups; i++) {
324 if (info->groups[i].id == group_id)
328 assert (group_index < info->num_groups);
329 group = &info->groups[group_index];
331 for (i = 0; i < group->num_counters; i++) {
332 if (group->counter_ids[i] == counter_id)
336 assert (counter_index < group->num_counters);
338 switch (group->counter_types[counter_index])
341 uint64_t uint64_value;
343 case GL_UNSIGNED_INT:
344 CONSUME (uint_value);
347 case GL_UNSIGNED_INT64_AMD:
348 CONSUME (uint64_value);
349 value = uint64_value;
351 case GL_PERCENTAGE_AMD:
353 CONSUME (float_value);
357 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
358 group->counter_types[counter_index]);
363 metrics->counters[group_index][counter_index] += value;
368 accumulate_program_time (metrics_op_t op, unsigned time_ns)
370 context_t *ctx = context_get_current ();
371 op_metrics_t *metrics;
373 metrics = ctx_get_op_metrics (ctx, op);
375 metrics->time_ns += time_ns;
378 typedef struct per_stage_metrics
380 op_metrics_t *metrics;
381 shader_stage_info_t *stage;
384 } per_stage_metrics_t;
387 _is_shader_stage_counter (metrics_info_t *info,
388 unsigned group_index,
389 unsigned counter_index)
391 shader_stage_info_t *stage;
394 for (i = 0; i < info->num_shader_stages; i++) {
395 stage = &info->stages[i];
397 if (stage->active_group_index == group_index &&
398 stage->active_counter_index == counter_index)
403 if (stage->stall_group_index == group_index &&
404 stage->stall_counter_index == counter_index)
414 print_per_stage_metrics (context_t *ctx,
415 per_stage_metrics_t *per_stage,
418 metrics_info_t *info = &ctx->metrics_info;
419 op_metrics_t *metric = per_stage->metrics;
420 metrics_group_info_t *group;
421 const char *op_string;
422 unsigned group_index, counter;
425 /* Don't print anything for stages with no alloted time. */
426 if (per_stage->time_ns == 0.0)
429 op_string = metrics_op_string (metric->op);
431 printf ("%21s", op_string);
433 if (metric->op >= METRICS_OP_SHADER) {
434 printf (" %3d", metric->op - METRICS_OP_SHADER);
440 if (per_stage->stage)
441 printf (" %cS:", per_stage->stage->name[0]);
445 printf ("\t%7.2f ms (%4.1f%%)",
446 per_stage->time_ns / 1e6,
447 per_stage->time_ns / total * 100);
449 if (per_stage->active)
450 printf (", %4.1f%% active", per_stage->active * 100);
454 /* I'm not seeing a lot of value printing the rest of these
455 * performance counters by default yet. Use --verbose to get
461 for (group_index = 0; group_index < info->num_groups; group_index++) {
462 group = &info->groups[group_index];
463 for (counter = 0; counter < group->num_counters; counter++) {
465 /* Don't print this counter value if it's a
466 * per-stage cycle counter, (which we have
467 * already accounted for). */
468 if (_is_shader_stage_counter (info, group_index, counter))
471 value = metric->counters[group_index][counter];
474 printf ("%s: %.2f ", group->counter_names[counter],
482 time_compare(const void *in_a, const void *in_b, void *arg unused)
484 const per_stage_metrics_t *a = in_a;
485 const per_stage_metrics_t *b = in_b;
488 if (a->time_ns < b->time_ns)
490 if (a->time_ns > b->time_ns)
496 print_program_metrics (void)
498 context_t *ctx = context_get_current ();
499 metrics_t *metrics = ctx->metrics;
500 metrics_info_t *info = &ctx->metrics_info;
501 unsigned num_shader_stages = info->num_shader_stages;
502 per_stage_metrics_t *sorted, *per_stage;
503 double total_time, op_cycles;
505 unsigned group_index, counter_index;
506 unsigned i, j, num_sorted;
508 /* Make a sorted list of the per-stage operations by time
509 * used, and figure out the total so we can print percentages.
511 num_sorted = metrics->num_op_metrics * num_shader_stages;
513 sorted = xmalloc (sizeof (*sorted) * num_sorted);
517 for (i = 0; i < metrics->num_op_metrics; i++) {
519 op = &metrics->op_metrics[i];
521 /* Accumulate total time across all ops. */
522 total_time += op->time_ns;
524 /* Also, find total cycles in all stages of this op. */
527 for (j = 0; j < num_shader_stages; j++) {
529 group_index = info->stages[j].active_group_index;
530 counter_index = info->stages[j].active_counter_index;
531 op_cycles += op->counters[group_index][counter_index];
534 group_index = info->stages[j].stall_group_index;
535 counter_index = info->stages[j].stall_counter_index;
536 op_cycles += op->counters[group_index][counter_index];
539 for (j = 0; j < num_shader_stages; j++) {
540 double active_cycles, stall_cycles, stage_cycles;
543 group_index = info->stages[j].active_group_index;
544 counter_index = info->stages[j].active_counter_index;
545 active_cycles = op->counters[group_index][counter_index];
548 group_index = info->stages[j].stall_group_index;
549 counter_index = info->stages[j].stall_counter_index;
550 stall_cycles = op->counters[group_index][counter_index];
552 stage_cycles = active_cycles + stall_cycles;
554 per_stage = &sorted[i * num_shader_stages + j];
555 per_stage->metrics = op;
558 per_stage->stage = &info->stages[j];
559 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
561 /* If we don't have any per-stage cycle counts
562 * for this operation, then use the first
563 * stage as a placeholder for all the time,
564 * but NULL-ify the stage info so that the
565 * report doesn't lie about this time being
566 * from any particular stage. */
567 per_stage->stage = NULL;
569 per_stage->time_ns = op->time_ns;
571 per_stage->time_ns = 0.0;
576 per_stage->active = active_cycles / stage_cycles;
578 per_stage->active = 0.0;
583 qsort_r (sorted, num_sorted, sizeof (*sorted),
584 time_compare, metrics->op_metrics);
586 for (i = 0; i < num_sorted; i++)
587 print_per_stage_metrics (ctx, &sorted[i], total_time);
592 /* Called at program exit.
594 * This is similar to metrics_info_fini, but only frees any used
595 * memory. Notably, it does not call any OpenGL functions, (since the
596 * OpenGL context no longer exists at program exit).
601 context_t *ctx = context_get_current ();
602 metrics_t *metrics = ctx->metrics;
603 metrics_info_t *info = &ctx->metrics_info;
605 timer_query_t *timer, *timer_next;
606 monitor_t *monitor, *monitor_next;
609 printf ("fips: terminating\n");
611 if (! info->initialized)
614 for (timer = metrics->timer_head;
618 timer_next = timer->next;
622 for (monitor = metrics->monitor_head;
624 monitor = monitor_next)
626 monitor_next = monitor->next;
630 for (i = 0; i < info->num_groups; i++) {
631 metrics_group_info_t *group = &info->groups[i];
633 for (j = 0; j < group->num_counters; i++)
634 free (group->counter_names[j]);
636 free (group->counter_types);
637 free (group->counter_names);
638 free (group->counter_ids);
645 for (i = 0; i < info->num_shader_stages; i++)
646 free (info->stages[i].name);
652 metrics_collect_available (void)
654 context_t *ctx = context_get_current ();
655 metrics_t *metrics = ctx->metrics;
657 /* Consume all timer queries that are ready. */
658 timer_query_t *timer = metrics->timer_head;
661 GLuint available, elapsed;
663 glGetQueryObjectuiv (timer->id,
664 GL_QUERY_RESULT_AVAILABLE, &available);
668 glGetQueryObjectuiv (timer->id,
669 GL_QUERY_RESULT, &elapsed);
671 accumulate_program_time (timer->op, elapsed);
673 metrics->timer_head = timer->next;
674 if (metrics->timer_head == NULL)
675 metrics->timer_tail = NULL;
677 glDeleteQueries (1, &timer->id);
680 timer = metrics->timer_head;
683 /* And similarly for all performance monitors that are ready. */
684 monitor_t *monitor = metrics->monitor_head;
687 GLuint available, result_size, *result;
690 glGetPerfMonitorCounterDataAMD (monitor->id,
691 GL_PERFMON_RESULT_AVAILABLE_AMD,
692 sizeof (available), &available,
697 glGetPerfMonitorCounterDataAMD (monitor->id,
698 GL_PERFMON_RESULT_SIZE_AMD,
699 sizeof (result_size),
702 result = xmalloc (result_size);
704 glGetPerfMonitorCounterDataAMD (monitor->id,
705 GL_PERFMON_RESULT_AMD,
709 accumulate_program_metrics (monitor->op, result, result_size);
713 metrics->monitor_head = monitor->next;
714 if (metrics->monitor_head == NULL)
715 metrics->monitor_tail = NULL;
717 glDeletePerfMonitorsAMD (1, &monitor->id);
721 metrics->monitors_in_flight--;
723 monitor = metrics->monitor_head;
729 metrics_end_frame (void)
731 static int initialized = 0;
732 static struct timeval tv_start, tv_now;
735 gettimeofday (&tv_start, NULL);
736 atexit (metrics_exit);
737 if (getenv ("FIPS_VERBOSE"))
744 metrics_collect_available ();
746 if (frames % 15 == 0) {
749 gettimeofday (&tv_now, NULL);
751 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
752 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
754 printf("FPS: %.3f\n", fps);
756 print_program_metrics ();
761 metrics_create (void)
765 metrics = xmalloc (sizeof (metrics_t));
769 metrics->timer_begun_id = 0;
771 metrics->timer_head = NULL;
772 metrics->timer_tail = NULL;
774 metrics->monitor_begun_id = 0;
776 metrics->monitor_head = NULL;
777 metrics->monitor_tail = NULL;
779 metrics->monitors_in_flight = 0;
781 metrics->num_op_metrics = 0;
782 metrics->op_metrics = NULL;
788 metrics_fini (metrics_t *metrics)
790 timer_query_t *timer, *timer_next;
791 monitor_t *monitor, *monitor_next;
793 /* Discard and cleanup any outstanding queries. */
794 if (metrics->timer_begun_id) {
795 glEndQuery (GL_TIME_ELAPSED);
796 glDeleteQueries (1, &metrics->timer_begun_id);
797 metrics->timer_begun_id = 0;
800 for (timer = metrics->timer_head;
804 glDeleteQueries (1, &timer->id);
805 timer_next = timer->next;
808 metrics->timer_head = NULL;
809 metrics->timer_tail = NULL;
811 if (metrics->monitor_begun_id) {
812 glEndPerfMonitorAMD (metrics->monitor_begun_id);
813 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
814 metrics->monitor_begun_id = 0;
817 for (monitor = metrics->monitor_head;
819 monitor = monitor_next)
821 glDeletePerfMonitorsAMD (1, &monitor->id);
822 monitor_next = monitor->next;
825 metrics->monitor_head = NULL;
826 metrics->monitor_tail = NULL;
828 metrics->monitors_in_flight = 0;
832 metrics_destroy (metrics_t *metrics)
834 metrics_fini (metrics);