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"
35 typedef struct timer_query
40 struct timer_query *next;
43 /* Performance-monitor query */
44 typedef struct monitor
53 typedef struct op_metrics
55 /* This happens to also be the index into the
56 * ctx->op_metrics array currently
64 typedef struct metrics_group_info
70 GLuint max_active_counters;
74 GLuint *counter_types;
76 } metrics_group_info_t;
78 typedef struct shader_stage_info
82 GLuint active_group_index;
83 GLuint active_counter_index;
85 GLuint stall_group_index;
86 GLuint stall_counter_index;
88 } shader_stage_info_t;
90 typedef struct metrics_info
95 metrics_group_info_t *groups;
97 unsigned num_shader_stages;
98 shader_stage_info_t *stages;
102 typedef struct context
104 metrics_info_t metrics_info;
108 timer_query_t *timer_head;
109 timer_query_t *timer_tail;
111 monitor_t *monitor_head;
112 monitor_t *monitor_tail;
114 unsigned num_op_metrics;
115 op_metrics_t *op_metrics;
118 /* FIXME: Need a map from integers to context objects and track the
119 * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
121 context_t current_context;
127 metrics_group_info_init (metrics_group_info_t *group, GLuint id)
135 glGetPerfMonitorGroupStringAMD (id, 0, &length, NULL);
137 group->name = xmalloc (length + 1);
139 glGetPerfMonitorGroupStringAMD (id, length + 1, NULL, group->name);
141 /* Get number of counters */
142 group->num_counters = 0;
143 group->max_active_counters = 0;
144 glGetPerfMonitorCountersAMD (group->id,
145 (int *) &group->num_counters,
146 (int *) &group->max_active_counters,
149 /* Get counter numbers */
150 group->counter_ids = xmalloc (group->num_counters * sizeof (GLuint));
152 glGetPerfMonitorCountersAMD (group->id, NULL, NULL,
156 /* Get counter names */
157 group->counter_names = xmalloc (group->num_counters * sizeof (char *));
158 group->counter_types = xmalloc (group->num_counters * sizeof (GLuint));
160 for (i = 0; i < group->num_counters; i++) {
161 glGetPerfMonitorCounterInfoAMD (group->id,
162 group->counter_ids[i],
164 &group->counter_types[i]);
166 glGetPerfMonitorCounterStringAMD (group->id,
167 group->counter_ids[i],
170 group->counter_names[i] = xmalloc (length + 1);
172 glGetPerfMonitorCounterStringAMD (group->id,
173 group->counter_ids[i],
175 group->counter_names[i]);
180 metrics_group_info_fini (metrics_group_info_t *group)
184 for (i = 0; i < group->num_counters; i++)
185 free (group->counter_names[i]);
187 free (group->counter_types);
188 free (group->counter_names);
189 free (group->counter_ids);
194 /* A helper function, part of metrics_info_init below. */
202 _add_shader_stage (metrics_info_t *info, const char *name,
203 GLuint group_index, GLuint counter_index,
204 shader_phase_t phase)
206 shader_stage_info_t *stage;
207 char *stage_name, *space;
210 stage_name = xstrdup (name);
212 /* Terminate the stage name at the first space.
214 * This is valid for counter names such as:
216 * "Vertex Shader Active Time"
218 * "Vertex Shader Stall Time - Core Stall"
220 space = strchr (stage_name, ' ');
224 /* Look for an existing stage of the given name. */
227 for (i = 0; i < info->num_shader_stages; i++) {
228 if (strcmp (info->stages[i].name, stage_name) == 0) {
229 stage = &info->stages[i];
235 info->num_shader_stages++;
236 info->stages = xrealloc (info->stages,
237 info->num_shader_stages *
238 sizeof (shader_stage_info_t));
239 stage = &info->stages[info->num_shader_stages - 1];
240 stage->name = xstrdup (stage_name);
241 stage->active_group_index = 0;
242 stage->active_counter_index = 0;
243 stage->stall_group_index = 0;
244 stage->stall_counter_index = 0;
247 if (phase == SHADER_ACTIVE) {
248 stage->active_group_index = group_index;
249 stage->active_counter_index = counter_index;
251 stage->stall_group_index = group_index;
252 stage->stall_counter_index = counter_index;
259 metrics_info_init (void)
263 metrics_info_t *info = ¤t_context.metrics_info;
265 glGetPerfMonitorGroupsAMD ((int *) &info->num_groups, 0, NULL);
267 group_ids = xmalloc (info->num_groups * sizeof (GLuint));
269 glGetPerfMonitorGroupsAMD (NULL, info->num_groups, group_ids);
271 info->groups = xmalloc (info->num_groups * sizeof (metrics_group_info_t));
273 for (i = 0; i < info->num_groups; i++)
274 metrics_group_info_init (&info->groups[i], group_ids[i]);
278 /* Identify each shader stage (by looking at
279 * performance-counter names for specific patterns) and
280 * initialize structures referring to the corresponding
281 * counter numbers for each stage. */
282 info->num_shader_stages = 0;
285 for (i = 0; i < info->num_groups; i++) {
286 metrics_group_info_t *group = &info->groups[i];
287 for (j = 0; j < group->num_counters; j++) {
288 char *name = group->counter_names[j];
289 if (strstr (name, "Shader Active Time")) {
290 _add_shader_stage (info, name, i, j,
293 if (strstr (name, "Shader Stall Time")) {
294 _add_shader_stage (info, name, i, j,
300 info->initialized = 1;
304 metrics_info_fini (void)
306 metrics_info_t *info = ¤t_context.metrics_info;
308 timer_query_t *timer, *timer_next;
309 monitor_t *monitor, *monitor_next;
311 if (! info->initialized)
314 for (timer = current_context.timer_head;
318 timer_next = timer->next;
321 current_context.timer_head = NULL;
322 current_context.timer_tail = NULL;
324 for (monitor = current_context.monitor_head;
326 monitor = monitor_next)
328 monitor_next = monitor->next;
331 current_context.monitor_head = NULL;
332 current_context.monitor_tail = NULL;
334 for (i = 0; i < info->num_groups; i++)
335 metrics_group_info_fini (&info->groups[i]);
340 for (i = 0; i < info->num_shader_stages; i++)
341 free (info->stages[i].name);
346 info->initialized = 0;
350 metrics_op_string (metrics_op_t op)
352 if (op >= METRICS_OP_SHADER)
353 return "Shader program";
357 case METRICS_OP_ACCUM:
358 return "glAccum*(+)";
359 case METRICS_OP_BUFFER_DATA:
360 return "glBufferData(+)";
361 case METRICS_OP_BUFFER_SUB_DATA:
362 return "glCopyBufferSubData*";
363 case METRICS_OP_BITMAP:
365 case METRICS_OP_BLIT_FRAMEBUFFER:
366 return "glBlitFramebuffer*";
367 case METRICS_OP_CLEAR:
369 case METRICS_OP_CLEAR_BUFFER_DATA:
370 return "glCearBufferData(+)";
371 case METRICS_OP_CLEAR_TEX_IMAGE:
372 return "glClearTexImage(+)";
373 case METRICS_OP_COPY_PIXELS:
374 return "glCopyPixels";
375 case METRICS_OP_COPY_TEX_IMAGE:
376 return "glCopyTexImage(+)";
377 case METRICS_OP_DRAW_PIXELS:
378 return "glDrawPixels";
379 case METRICS_OP_GET_TEX_IMAGE:
380 return "glGetTexImage(+)";
381 case METRICS_OP_READ_PIXELS:
382 return "glReadPixels*";
383 case METRICS_OP_TEX_IMAGE:
384 return "glTexImage*(+)";
386 fprintf (stderr, "fips: Internal error: "
387 "Unknown metrics op value: %d\n", op);
395 metrics_counter_start (void)
397 context_t *ctx = ¤t_context;
398 timer_query_t *timer;
402 /* Create new timer query, add to list */
403 timer = xmalloc (sizeof (timer_query_t));
408 if (ctx->timer_tail) {
409 ctx->timer_tail->next = timer;
410 ctx->timer_tail = timer;
412 ctx->timer_tail = timer;
413 ctx->timer_head = timer;
416 /* Create a new performance-monitor query */
417 monitor = xmalloc (sizeof (monitor_t));
419 monitor->op = ctx->op;
420 monitor->next = NULL;
422 if (ctx->monitor_tail) {
423 ctx->monitor_tail->next = monitor;
424 ctx->monitor_tail = monitor;
426 ctx->monitor_tail = monitor;
427 ctx->monitor_head = monitor;
430 /* Initialize the timer_query and monitor objects */
431 glGenQueries (1, &timer->id);
433 glGenPerfMonitorsAMD (1, &monitor->id);
435 for (i = 0; i < ctx->metrics_info.num_groups; i++)
437 metrics_group_info_t *group;
440 group = &ctx->metrics_info.groups[i];
442 num_counters = group->num_counters;
443 if (group->max_active_counters < group->num_counters)
445 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
446 group->max_active_counters,
447 group->num_counters, i);
448 num_counters = group->max_active_counters;
452 glSelectPerfMonitorCountersAMD(monitor->id,
458 /* Start the queries */
459 glBeginQuery (GL_TIME_ELAPSED, timer->id);
461 glBeginPerfMonitorAMD (monitor->id);
465 metrics_counter_stop (void)
467 glEndQuery (GL_TIME_ELAPSED);
468 glEndPerfMonitorAMD (current_context.monitor_tail->id);
472 metrics_set_current_op (metrics_op_t op)
474 current_context.op = op;
478 metrics_get_current_op (void)
480 return current_context.op;
484 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
486 metrics_info_t *info = &ctx->metrics_info;
490 metrics->time_ns = 0.0;
492 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
494 for (i = 0; i < info->num_groups; i++) {
495 metrics->counters[i] = xmalloc (sizeof (double) *
496 info->groups[i].num_counters);
497 for (j = 0; j < info->groups[i].num_counters; j++)
498 metrics->counters[i][j] = 0.0;
502 static op_metrics_t *
503 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
507 if (op >= ctx->num_op_metrics)
509 ctx->op_metrics = realloc (ctx->op_metrics,
510 (op + 1) * sizeof (op_metrics_t));
511 for (i = ctx->num_op_metrics; i < op + 1; i++)
512 op_metrics_init (ctx, &ctx->op_metrics[i], i);
514 ctx->num_op_metrics = op + 1;
517 return &ctx->op_metrics[op];
521 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
523 #define CONSUME(var) \
524 if (p + sizeof(var) > ((unsigned char *) result) + size) \
526 fprintf (stderr, "Unexpected end-of-buffer while " \
527 "parsing results\n"); \
530 (var) = *((typeof(var) *) p); \
533 context_t *ctx = ¤t_context;
534 metrics_info_t *info = &ctx->metrics_info;
535 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
536 unsigned char *p = (unsigned char *) result;
538 while (p < ((unsigned char *) result) + size)
540 GLuint group_id, group_index;
541 GLuint counter_id, counter_index;
542 metrics_group_info_t *group;
547 CONSUME (counter_id);
549 for (i = 0; i < info->num_groups; i++) {
550 if (info->groups[i].id == group_id)
554 assert (group_index < info->num_groups);
555 group = &info->groups[group_index];
557 for (i = 0; i < group->num_counters; i++) {
558 if (group->counter_ids[i] == counter_id)
562 assert (counter_index < group->num_counters);
564 switch (group->counter_types[counter_index])
567 uint64_t uint64_value;
569 case GL_UNSIGNED_INT:
570 CONSUME (uint_value);
573 case GL_UNSIGNED_INT64_AMD:
574 CONSUME (uint64_value);
575 value = uint64_value;
577 case GL_PERCENTAGE_AMD:
579 CONSUME (float_value);
583 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
584 group->counter_types[counter_index]);
589 metrics->counters[group_index][counter_index] += value;
594 accumulate_program_time (metrics_op_t op, unsigned time_ns)
596 op_metrics_t *metrics;
598 metrics = ctx_get_op_metrics (¤t_context, op);
600 metrics->time_ns += time_ns;
603 typedef struct per_stage_metrics
605 op_metrics_t *metrics;
606 shader_stage_info_t *stage;
609 } per_stage_metrics_t;
612 _is_shader_stage_counter (metrics_info_t *info,
613 unsigned group_index,
614 unsigned counter_index)
616 shader_stage_info_t *stage;
619 for (i = 0; i < info->num_shader_stages; i++) {
620 stage = &info->stages[i];
622 if (stage->active_group_index == group_index &&
623 stage->active_counter_index == counter_index)
628 if (stage->stall_group_index == group_index &&
629 stage->stall_counter_index == counter_index)
639 print_per_stage_metrics (context_t *ctx,
640 per_stage_metrics_t *per_stage,
643 metrics_info_t *info = &ctx->metrics_info;
644 op_metrics_t *metric = per_stage->metrics;
645 metrics_group_info_t *group;
646 const char *op_string;
647 unsigned group_index, counter;
650 /* Don't print anything for stages with no alloted time. */
651 if (per_stage->time_ns == 0.0)
654 op_string = metrics_op_string (metric->op);
656 printf ("%21s", op_string);
658 if (metric->op >= METRICS_OP_SHADER) {
659 printf (" %3d", metric->op - METRICS_OP_SHADER);
664 printf (" %cS:", per_stage->stage->name[0]);
666 printf ("\t%7.2f ms (%4.1f%%)",
667 per_stage->time_ns / 1e6,
668 per_stage->time_ns / total * 100);
670 if (per_stage->active)
671 printf (", %4.1f%% active", per_stage->active * 100);
675 /* I'm not seeing a lot of value printing the rest of these
676 * performance counters by default yet. Use --verbose to get
682 for (group_index = 0; group_index < info->num_groups; group_index++) {
683 group = &info->groups[group_index];
684 for (counter = 0; counter < group->num_counters; counter++) {
686 /* Don't print this counter value if it's a
687 * per-stage cycle counter, (which we have
688 * already accounted for). */
689 if (_is_shader_stage_counter (info, group_index, counter))
692 value = metric->counters[group_index][counter];
695 printf ("%s: %.2f ", group->counter_names[counter],
703 time_compare(const void *in_a, const void *in_b, void *arg unused)
705 const per_stage_metrics_t *a = in_a;
706 const per_stage_metrics_t *b = in_b;
709 if (a->time_ns < b->time_ns)
711 if (a->time_ns > b->time_ns)
717 print_program_metrics (void)
719 context_t *ctx = ¤t_context;
720 metrics_info_t *info = &ctx->metrics_info;
721 unsigned num_shader_stages = info->num_shader_stages;
722 per_stage_metrics_t *sorted, *per_stage;
723 double total_time, op_cycles;
725 unsigned group_index, counter_index;
726 unsigned i, j, num_sorted;
728 /* Make a sorted list of the per-stage operations by time
729 * used, and figure out the total so we can print percentages.
731 num_sorted = ctx->num_op_metrics * num_shader_stages;
733 sorted = xmalloc (sizeof (*sorted) * num_sorted);
737 for (i = 0; i < ctx->num_op_metrics; i++) {
739 op = &ctx->op_metrics[i];
741 /* Accumulate total time across all ops. */
742 total_time += op->time_ns;
744 /* Also, find total cycles in all stages of this op. */
747 for (j = 0; j < num_shader_stages; j++) {
749 group_index = info->stages[j].active_group_index;
750 counter_index = info->stages[j].active_counter_index;
751 op_cycles += op->counters[group_index][counter_index];
754 group_index = info->stages[j].stall_group_index;
755 counter_index = info->stages[j].stall_counter_index;
756 op_cycles += op->counters[group_index][counter_index];
759 for (j = 0; j < num_shader_stages; j++) {
760 double active_cycles, stall_cycles, stage_cycles;
763 group_index = info->stages[j].active_group_index;
764 counter_index = info->stages[j].active_counter_index;
765 active_cycles = op->counters[group_index][counter_index];
768 group_index = info->stages[j].stall_group_index;
769 counter_index = info->stages[j].stall_counter_index;
770 stall_cycles = op->counters[group_index][counter_index];
772 stage_cycles = active_cycles + stall_cycles;
774 per_stage = &sorted[i * num_shader_stages + j];
775 per_stage->metrics = op;
776 per_stage->stage = &info->stages[j];
778 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
780 per_stage->time_ns = 0.0;
782 per_stage->active = active_cycles / stage_cycles;
784 per_stage->active = 0.0;
788 qsort_r (sorted, num_sorted, sizeof (*sorted),
789 time_compare, ctx->op_metrics);
791 for (i = 0; i < num_sorted; i++)
792 print_per_stage_metrics (ctx, &sorted[i], total_time);
797 /* Called at program exit */
802 printf ("fips: terminating\n");
804 metrics_info_fini ();
809 metrics_end_frame (void)
811 static int initialized = 0;
812 static struct timeval tv_start, tv_now;
815 gettimeofday (&tv_start, NULL);
816 atexit (metrics_exit);
817 if (getenv ("FIPS_VERBOSE"))
823 gettimeofday (&tv_now, NULL);
825 /* Consume all timer queries that are ready. */
826 timer_query_t *timer = current_context.timer_head;
829 GLuint available, elapsed;
831 glGetQueryObjectuiv (timer->id,
832 GL_QUERY_RESULT_AVAILABLE, &available);
836 glGetQueryObjectuiv (timer->id,
837 GL_QUERY_RESULT, &elapsed);
839 accumulate_program_time (timer->op, elapsed);
841 current_context.timer_head = timer->next;
842 if (current_context.timer_head == NULL)
843 current_context.timer_tail = NULL;
845 glDeleteQueries (1, &timer->id);
848 timer = current_context.timer_head;
851 /* And similarly for all performance monitors that are ready. */
852 monitor_t *monitor = current_context.monitor_head;
855 GLuint available, result_size, *result;
858 glGetPerfMonitorCounterDataAMD (monitor->id,
859 GL_PERFMON_RESULT_AVAILABLE_AMD,
860 sizeof (available), &available,
865 glGetPerfMonitorCounterDataAMD (monitor->id,
866 GL_PERFMON_RESULT_SIZE_AMD,
867 sizeof (result_size),
870 result = xmalloc (result_size);
872 glGetPerfMonitorCounterDataAMD (monitor->id,
873 GL_PERFMON_RESULT_AMD,
877 accumulate_program_metrics (monitor->op, result, result_size);
881 current_context.monitor_head = monitor->next;
882 if (current_context.monitor_head == NULL)
883 current_context.monitor_tail = NULL;
885 glDeletePerfMonitorsAMD (1, &monitor->id);
888 monitor = current_context.monitor_head;
891 if (frames % 60 == 0) {
894 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
895 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
897 printf("FPS: %.3f\n", fps);
899 print_program_metrics ();