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
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->monitor_begun_id) {
157 glEndPerfMonitorAMD (metrics->monitor_begun_id);
158 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
159 metrics->monitor_begun_id = 0;
162 for (monitor = metrics->monitor_head;
164 monitor = monitor_next)
166 glDeletePerfMonitorsAMD (1, &monitor->id);
167 monitor_next = monitor->next;
170 metrics->monitor_head = NULL;
171 metrics->monitor_tail = NULL;
173 metrics->monitors_in_flight = 0;
177 metrics_destroy (metrics_t *metrics)
179 metrics_fini (metrics);
185 metrics_op_string (metrics_op_t op)
187 if (op >= METRICS_OP_SHADER)
188 return "Shader program";
192 case METRICS_OP_ACCUM:
193 return "glAccum*(+)";
194 case METRICS_OP_BUFFER_DATA:
195 return "glBufferData(+)";
196 case METRICS_OP_BUFFER_SUB_DATA:
197 return "glCopyBufferSubData*";
198 case METRICS_OP_BITMAP:
200 case METRICS_OP_BLIT_FRAMEBUFFER:
201 return "glBlitFramebuffer*";
202 case METRICS_OP_CLEAR:
204 case METRICS_OP_CLEAR_BUFFER_DATA:
205 return "glCearBufferData(+)";
206 case METRICS_OP_CLEAR_TEX_IMAGE:
207 return "glClearTexImage(+)";
208 case METRICS_OP_COPY_PIXELS:
209 return "glCopyPixels";
210 case METRICS_OP_COPY_TEX_IMAGE:
211 return "glCopyTexImage(+)";
212 case METRICS_OP_DRAW_PIXELS:
213 return "glDrawPixels";
214 case METRICS_OP_GET_TEX_IMAGE:
215 return "glGetTexImage(+)";
216 case METRICS_OP_READ_PIXELS:
217 return "glReadPixels*";
218 case METRICS_OP_TEX_IMAGE:
219 return "glTexImage*(+)";
221 fprintf (stderr, "fips: Internal error: "
222 "Unknown metrics op value: %d\n", op);
230 metrics_counter_start (void)
232 context_t *ctx = context_get_current ();
233 metrics_t *metrics = ctx->metrics;
236 /* Initialize the timer_query and monitor objects */
237 glGenQueries (1, &metrics->timer_begun_id);
239 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
241 for (i = 0; i < metrics->info->num_groups; i++)
243 metrics_group_info_t *group;
246 group = &metrics->info->groups[i];
248 num_counters = group->num_counters;
249 if (group->max_active_counters < group->num_counters)
251 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
252 group->max_active_counters,
253 group->num_counters, i);
254 num_counters = group->max_active_counters;
258 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
264 /* Start the queries */
265 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
267 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
271 metrics_counter_stop (void)
273 context_t *ctx = context_get_current ();
274 metrics_t *metrics = ctx->metrics;
275 timer_query_t *timer;
278 /* Stop the current timer and monitor. */
279 glEndQuery (GL_TIME_ELAPSED);
280 glEndPerfMonitorAMD (metrics->monitor_begun_id);
282 /* Add these IDs to our lists of outstanding queries and
283 * monitors so the results can be collected later. */
284 timer = xmalloc (sizeof (timer_query_t));
286 timer->op = metrics->op;
287 timer->id = metrics->timer_begun_id;
290 if (metrics->timer_tail) {
291 metrics->timer_tail->next = timer;
292 metrics->timer_tail = timer;
294 metrics->timer_tail = timer;
295 metrics->timer_head = timer;
298 /* Create a new performance-monitor query */
299 monitor = xmalloc (sizeof (monitor_t));
301 monitor->op = metrics->op;
302 monitor->id = metrics->monitor_begun_id;
303 monitor->next = NULL;
305 if (metrics->monitor_tail) {
306 metrics->monitor_tail->next = monitor;
307 metrics->monitor_tail = monitor;
309 metrics->monitor_tail = monitor;
310 metrics->monitor_head = monitor;
313 metrics->monitors_in_flight++;
315 /* Avoid being a resource hog and collect outstanding results
316 * once we have sent off a large number of
317 * queries. (Presumably, many of the outstanding queries are
320 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
321 metrics_collect_available ();
325 metrics_set_current_op (metrics_op_t op)
327 context_t *ctx = context_get_current ();
328 metrics_t *metrics = ctx->metrics;
334 metrics_get_current_op (void)
336 context_t *ctx = context_get_current ();
337 metrics_t *metrics = ctx->metrics;
343 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
345 metrics_info_t *info = ctx->metrics->info;
349 metrics->time_ns = 0.0;
351 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
353 for (i = 0; i < info->num_groups; i++) {
354 metrics->counters[i] = xmalloc (sizeof (double) *
355 info->groups[i].num_counters);
356 for (j = 0; j < info->groups[i].num_counters; j++)
357 metrics->counters[i][j] = 0.0;
361 static op_metrics_t *
362 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
364 metrics_t *metrics = ctx->metrics;
367 if (op >= metrics->num_op_metrics)
369 metrics->op_metrics = realloc (metrics->op_metrics,
370 (op + 1) * sizeof (op_metrics_t));
371 for (i = metrics->num_op_metrics; i < op + 1; i++)
372 op_metrics_init (ctx, &metrics->op_metrics[i], i);
374 metrics->num_op_metrics = op + 1;
377 return &metrics->op_metrics[op];
381 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
383 #define CONSUME(var) \
384 if (p + sizeof(var) > ((unsigned char *) result) + size) \
386 fprintf (stderr, "Unexpected end-of-buffer while " \
387 "parsing results\n"); \
390 (var) = *((typeof(var) *) p); \
393 context_t *ctx = context_get_current ();
394 metrics_info_t *info = ctx->metrics->info;
395 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
396 unsigned char *p = (unsigned char *) result;
398 while (p < ((unsigned char *) result) + size)
400 GLuint group_id, group_index;
401 GLuint counter_id, counter_index;
402 metrics_group_info_t *group;
407 CONSUME (counter_id);
409 for (i = 0; i < info->num_groups; i++) {
410 if (info->groups[i].id == group_id)
414 assert (group_index < info->num_groups);
415 group = &info->groups[group_index];
417 for (i = 0; i < group->num_counters; i++) {
418 if (group->counter_ids[i] == counter_id)
422 assert (counter_index < group->num_counters);
424 switch (group->counter_types[counter_index])
427 uint64_t uint64_value;
429 case GL_UNSIGNED_INT:
430 CONSUME (uint_value);
433 case GL_UNSIGNED_INT64_AMD:
434 CONSUME (uint64_value);
435 value = uint64_value;
437 case GL_PERCENTAGE_AMD:
439 CONSUME (float_value);
443 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
444 group->counter_types[counter_index]);
449 metrics->counters[group_index][counter_index] += value;
454 accumulate_program_time (metrics_op_t op, unsigned time_ns)
456 context_t *ctx = context_get_current ();
457 op_metrics_t *metrics;
459 metrics = ctx_get_op_metrics (ctx, op);
461 metrics->time_ns += time_ns;
464 typedef struct per_stage_metrics
466 op_metrics_t *metrics;
467 shader_stage_info_t *stage;
470 } per_stage_metrics_t;
473 _is_shader_stage_counter (metrics_info_t *info,
474 unsigned group_index,
475 unsigned counter_index)
477 shader_stage_info_t *stage;
480 for (i = 0; i < info->num_shader_stages; i++) {
481 stage = &info->stages[i];
483 if (stage->active_group_index == group_index &&
484 stage->active_counter_index == counter_index)
489 if (stage->stall_group_index == group_index &&
490 stage->stall_counter_index == counter_index)
500 print_per_stage_metrics (context_t *ctx,
501 per_stage_metrics_t *per_stage,
504 metrics_info_t *info = ctx->metrics->info;
505 op_metrics_t *metric = per_stage->metrics;
506 metrics_group_info_t *group;
507 const char *op_string;
508 unsigned group_index, counter;
511 /* Don't print anything for stages with no alloted time. */
512 if (per_stage->time_ns == 0.0)
515 op_string = metrics_op_string (metric->op);
517 printf ("%21s", op_string);
519 if (metric->op >= METRICS_OP_SHADER) {
520 printf (" %3d", metric->op - METRICS_OP_SHADER);
526 if (per_stage->stage)
527 printf (" %cS:", per_stage->stage->name[0]);
531 printf ("\t%7.2f ms (%4.1f%%)",
532 per_stage->time_ns / 1e6,
533 per_stage->time_ns / total * 100);
535 if (per_stage->active)
536 printf (", %4.1f%% active", per_stage->active * 100);
540 /* I'm not seeing a lot of value printing the rest of these
541 * performance counters by default yet. Use --verbose to get
547 for (group_index = 0; group_index < info->num_groups; group_index++) {
548 group = &info->groups[group_index];
549 for (counter = 0; counter < group->num_counters; counter++) {
551 /* Don't print this counter value if it's a
552 * per-stage cycle counter, (which we have
553 * already accounted for). */
554 if (_is_shader_stage_counter (info, group_index, counter))
557 value = metric->counters[group_index][counter];
560 printf ("%s: %.2f ", group->counter_names[counter],
568 time_compare(const void *in_a, const void *in_b, void *arg unused)
570 const per_stage_metrics_t *a = in_a;
571 const per_stage_metrics_t *b = in_b;
574 if (a->time_ns < b->time_ns)
576 if (a->time_ns > b->time_ns)
582 print_program_metrics (void)
584 context_t *ctx = context_get_current ();
585 metrics_t *metrics = ctx->metrics;
586 metrics_info_t *info = metrics->info;
587 unsigned num_shader_stages = info->num_shader_stages;
588 per_stage_metrics_t *sorted, *per_stage;
589 double total_time, op_cycles;
591 unsigned group_index, counter_index;
592 unsigned i, j, num_sorted;
594 /* Make a sorted list of the per-stage operations by time
595 * used, and figure out the total so we can print percentages.
597 num_sorted = metrics->num_op_metrics * num_shader_stages;
599 sorted = xmalloc (sizeof (*sorted) * num_sorted);
603 for (i = 0; i < metrics->num_op_metrics; i++) {
605 op = &metrics->op_metrics[i];
607 /* Accumulate total time across all ops. */
608 total_time += op->time_ns;
610 /* Also, find total cycles in all stages of this op. */
613 for (j = 0; j < num_shader_stages; j++) {
615 group_index = info->stages[j].active_group_index;
616 counter_index = info->stages[j].active_counter_index;
617 op_cycles += op->counters[group_index][counter_index];
620 group_index = info->stages[j].stall_group_index;
621 counter_index = info->stages[j].stall_counter_index;
622 op_cycles += op->counters[group_index][counter_index];
625 for (j = 0; j < num_shader_stages; j++) {
626 double active_cycles, stall_cycles, stage_cycles;
629 group_index = info->stages[j].active_group_index;
630 counter_index = info->stages[j].active_counter_index;
631 active_cycles = op->counters[group_index][counter_index];
634 group_index = info->stages[j].stall_group_index;
635 counter_index = info->stages[j].stall_counter_index;
636 stall_cycles = op->counters[group_index][counter_index];
638 stage_cycles = active_cycles + stall_cycles;
640 per_stage = &sorted[i * num_shader_stages + j];
641 per_stage->metrics = op;
644 per_stage->stage = &info->stages[j];
645 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
647 /* If we don't have any per-stage cycle counts
648 * for this operation, then use the first
649 * stage as a placeholder for all the time,
650 * but NULL-ify the stage info so that the
651 * report doesn't lie about this time being
652 * from any particular stage. */
653 per_stage->stage = NULL;
655 per_stage->time_ns = op->time_ns;
657 per_stage->time_ns = 0.0;
662 per_stage->active = active_cycles / stage_cycles;
664 per_stage->active = 0.0;
669 qsort_r (sorted, num_sorted, sizeof (*sorted),
670 time_compare, metrics->op_metrics);
672 for (i = 0; i < num_sorted; i++)
673 print_per_stage_metrics (ctx, &sorted[i], total_time);
678 /* Called at program exit.
680 * This is similar to metrics_info_fini, but only frees any used
681 * memory. Notably, it does not call any OpenGL functions, (since the
682 * OpenGL context no longer exists at program exit).
687 context_t *ctx = context_get_current ();
688 metrics_t *metrics = ctx->metrics;
689 metrics_info_t *info = metrics->info;
691 timer_query_t *timer, *timer_next;
692 monitor_t *monitor, *monitor_next;
695 printf ("fips: terminating\n");
697 if (! info->initialized)
700 for (timer = metrics->timer_head;
704 timer_next = timer->next;
708 for (monitor = metrics->monitor_head;
710 monitor = monitor_next)
712 monitor_next = monitor->next;
716 for (i = 0; i < info->num_groups; i++) {
717 metrics_group_info_t *group = &info->groups[i];
719 for (j = 0; j < group->num_counters; i++)
720 free (group->counter_names[j]);
722 free (group->counter_types);
723 free (group->counter_names);
724 free (group->counter_ids);
731 for (i = 0; i < info->num_shader_stages; i++)
732 free (info->stages[i].name);
738 metrics_collect_available (void)
740 context_t *ctx = context_get_current ();
741 metrics_t *metrics = ctx->metrics;
743 /* Consume all timer queries that are ready. */
744 timer_query_t *timer = metrics->timer_head;
747 GLuint available, elapsed;
749 glGetQueryObjectuiv (timer->id,
750 GL_QUERY_RESULT_AVAILABLE, &available);
754 glGetQueryObjectuiv (timer->id,
755 GL_QUERY_RESULT, &elapsed);
757 accumulate_program_time (timer->op, elapsed);
759 metrics->timer_head = timer->next;
760 if (metrics->timer_head == NULL)
761 metrics->timer_tail = NULL;
763 glDeleteQueries (1, &timer->id);
766 timer = metrics->timer_head;
769 /* And similarly for all performance monitors that are ready. */
770 monitor_t *monitor = metrics->monitor_head;
773 GLuint available, result_size, *result;
776 glGetPerfMonitorCounterDataAMD (monitor->id,
777 GL_PERFMON_RESULT_AVAILABLE_AMD,
778 sizeof (available), &available,
783 glGetPerfMonitorCounterDataAMD (monitor->id,
784 GL_PERFMON_RESULT_SIZE_AMD,
785 sizeof (result_size),
788 result = xmalloc (result_size);
790 glGetPerfMonitorCounterDataAMD (monitor->id,
791 GL_PERFMON_RESULT_AMD,
795 accumulate_program_metrics (monitor->op, result, result_size);
799 metrics->monitor_head = monitor->next;
800 if (metrics->monitor_head == NULL)
801 metrics->monitor_tail = NULL;
803 glDeletePerfMonitorsAMD (1, &monitor->id);
807 metrics->monitors_in_flight--;
809 monitor = metrics->monitor_head;
815 metrics_end_frame (void)
817 static int initialized = 0;
818 static struct timeval tv_start, tv_now;
821 gettimeofday (&tv_start, NULL);
822 atexit (metrics_exit);
823 if (getenv ("FIPS_VERBOSE"))
830 metrics_collect_available ();
832 if (frames % 15 == 0) {
835 gettimeofday (&tv_now, NULL);
837 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
838 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
840 printf("FPS: %.3f\n", fps);
842 print_program_metrics ();