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 /* We assume that all peformance counters are made
167 * available as uint32 values. The code calling
168 * CONSUME in accumulate_program_metrics will need to
169 * be extended to accomodate other counter values. */
170 if (group->counter_types[i] != GL_UNSIGNED_INT) {
171 fprintf (stderr, "fips: Internal error: No support for non-uint counter values\n");
175 glGetPerfMonitorCounterStringAMD (group->id,
176 group->counter_ids[i],
179 group->counter_names[i] = xmalloc (length + 1);
181 glGetPerfMonitorCounterStringAMD (group->id,
182 group->counter_ids[i],
184 group->counter_names[i]);
189 metrics_group_info_fini (metrics_group_info_t *group)
193 for (i = 0; i < group->num_counters; i++)
194 free (group->counter_names[i]);
196 free (group->counter_types);
197 free (group->counter_names);
198 free (group->counter_ids);
204 metrics_info_fini (metrics_info_t *info);
206 /* A helper function, part of metrics_info_init below. */
214 _add_shader_stage (metrics_info_t *info, const char *name,
215 GLuint group_index, GLuint counter_index,
216 shader_phase_t phase)
218 shader_stage_info_t *stage;
219 char *stage_name, *space;
222 stage_name = xstrdup (name);
224 /* Terminate the stage name at the first space.
226 * This is valid for counter names such as:
228 * "Vertex Shader Active Time"
230 * "Vertex Shader Stall Time - Core Stall"
232 space = strchr (stage_name, ' ');
236 /* Look for an existing stage of the given name. */
239 for (i = 0; i < info->num_shader_stages; i++) {
240 if (strcmp (info->stages[i].name, stage_name) == 0) {
241 stage = &info->stages[i];
247 info->num_shader_stages++;
248 info->stages = xrealloc (info->stages,
249 info->num_shader_stages *
250 sizeof (shader_stage_info_t));
251 stage = &info->stages[info->num_shader_stages - 1];
252 stage->name = xstrdup (stage_name);
253 stage->active_group_index = 0;
254 stage->active_counter_index = 0;
255 stage->stall_group_index = 0;
256 stage->stall_counter_index = 0;
259 if (phase == SHADER_ACTIVE) {
260 stage->active_group_index = group_index;
261 stage->active_counter_index = counter_index;
263 stage->stall_group_index = group_index;
264 stage->stall_counter_index = counter_index;
271 metrics_info_init (void)
275 metrics_info_t *info = ¤t_context.metrics_info;
277 if (info->initialized)
278 metrics_info_fini (info);
280 glGetPerfMonitorGroupsAMD ((int *) &info->num_groups, 0, NULL);
282 group_ids = xmalloc (info->num_groups * sizeof (GLuint));
284 glGetPerfMonitorGroupsAMD (NULL, info->num_groups, group_ids);
286 info->groups = xmalloc (info->num_groups * sizeof (metrics_group_info_t));
288 for (i = 0; i < info->num_groups; i++)
289 metrics_group_info_init (&info->groups[i], group_ids[i]);
293 /* Identify each shader stage (by looking at
294 * performance-counter names for specific patterns) and
295 * initialize structures referring to the corresponding
296 * counter numbers for each stage. */
297 info->num_shader_stages = 0;
300 for (i = 0; i < info->num_groups; i++) {
301 metrics_group_info_t *group = &info->groups[i];
302 for (j = 0; j < group->num_counters; j++) {
303 char *name = group->counter_names[j];
304 if (strstr (name, "Shader Active Time")) {
305 _add_shader_stage (info, name, i, j,
308 if (strstr (name, "Shader Stall Time")) {
309 _add_shader_stage (info, name, i, j,
315 info->initialized = 1;
319 metrics_info_fini (metrics_info_t *info)
323 for (i = 0; i < info->num_groups; i++)
324 metrics_group_info_fini (&info->groups[i]);
328 for (i = 0; i < info->num_shader_stages; i++)
329 free (info->stages[i].name);
335 metrics_op_string (metrics_op_t op)
337 if (op >= METRICS_OP_SHADER)
338 return "Shader program";
342 case METRICS_OP_ACCUM:
343 return "glAccum*(+)";
344 case METRICS_OP_BUFFER_DATA:
345 return "glBufferData(+)";
346 case METRICS_OP_BUFFER_SUB_DATA:
347 return "glCopyBufferSubData*";
348 case METRICS_OP_BITMAP:
350 case METRICS_OP_BLIT_FRAMEBUFFER:
351 return "glBlitFramebuffer*";
352 case METRICS_OP_CLEAR:
354 case METRICS_OP_CLEAR_BUFFER_DATA:
355 return "glCearBufferData(+)";
356 case METRICS_OP_CLEAR_TEX_IMAGE:
357 return "glClearTexImage(+)";
358 case METRICS_OP_COPY_PIXELS:
359 return "glCopyPixels";
360 case METRICS_OP_COPY_TEX_IMAGE:
361 return "glCopyTexImage(+)";
362 case METRICS_OP_DRAW_PIXELS:
363 return "glDrawPixels";
364 case METRICS_OP_GET_TEX_IMAGE:
365 return "glGetTexImage(+)";
366 case METRICS_OP_READ_PIXELS:
367 return "glReadPixels*";
368 case METRICS_OP_TEX_IMAGE:
369 return "glTexImage*(+)";
371 fprintf (stderr, "fips: Internal error: "
372 "Unknown metrics op value: %d\n", op);
380 metrics_counter_start (void)
382 context_t *ctx = ¤t_context;
383 timer_query_t *timer;
387 /* Create new timer query, add to list */
388 timer = xmalloc (sizeof (timer_query_t));
393 if (ctx->timer_tail) {
394 ctx->timer_tail->next = timer;
395 ctx->timer_tail = timer;
397 ctx->timer_tail = timer;
398 ctx->timer_head = timer;
401 /* Create a new performance-monitor query */
402 monitor = xmalloc (sizeof (monitor_t));
404 monitor->op = ctx->op;
405 monitor->next = NULL;
407 if (ctx->monitor_tail) {
408 ctx->monitor_tail->next = monitor;
409 ctx->monitor_tail = monitor;
411 ctx->monitor_tail = monitor;
412 ctx->monitor_head = monitor;
415 /* Initialize the timer_query and monitor objects */
416 glGenQueries (1, &timer->id);
418 glGenPerfMonitorsAMD (1, &monitor->id);
420 for (i = 0; i < ctx->metrics_info.num_groups; i++)
422 metrics_group_info_t *group;
425 group = &ctx->metrics_info.groups[i];
427 num_counters = group->num_counters;
428 if (group->max_active_counters < group->num_counters)
430 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
431 group->max_active_counters,
432 group->num_counters, i);
433 num_counters = group->max_active_counters;
437 glSelectPerfMonitorCountersAMD(monitor->id,
443 /* Start the queries */
444 glBeginQuery (GL_TIME_ELAPSED, timer->id);
446 glBeginPerfMonitorAMD (monitor->id);
450 metrics_counter_stop (void)
452 glEndQuery (GL_TIME_ELAPSED);
453 glEndPerfMonitorAMD (current_context.monitor_tail->id);
457 metrics_set_current_op (metrics_op_t op)
459 current_context.op = op;
463 metrics_get_current_op (void)
465 return current_context.op;
469 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
471 metrics_info_t *info = &ctx->metrics_info;
475 metrics->time_ns = 0.0;
477 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
479 for (i = 0; i < info->num_groups; i++) {
480 metrics->counters[i] = xmalloc (sizeof (double) *
481 info->groups[i].num_counters);
482 for (j = 0; j < info->groups[i].num_counters; j++)
483 metrics->counters[i][j] = 0.0;
487 static op_metrics_t *
488 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
492 if (op >= ctx->num_op_metrics)
494 ctx->op_metrics = realloc (ctx->op_metrics,
495 (op + 1) * sizeof (op_metrics_t));
496 for (i = ctx->num_op_metrics; i < op + 1; i++)
497 op_metrics_init (ctx, &ctx->op_metrics[i], i);
499 ctx->num_op_metrics = op + 1;
502 return &ctx->op_metrics[op];
506 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
508 #define CONSUME(var) \
509 if (p + sizeof(var) > ((unsigned char *) result) + size) \
511 fprintf (stderr, "Unexpected end-of-buffer while " \
512 "parsing results\n"); \
515 (var) = *((typeof(var) *) p); \
518 context_t *ctx = ¤t_context;
519 metrics_info_t *info = &ctx->metrics_info;
520 unsigned char *p = (unsigned char *) result;
522 while (p < ((unsigned char *) result) + size)
524 GLuint group_id, group_index;
525 GLuint counter_id, counter_index;
526 metrics_group_info_t *group;
531 CONSUME (counter_id);
534 for (i = 0; i < info->num_groups; i++) {
535 if (info->groups[i].id == group_id)
539 assert (group_index < info->num_groups);
540 group = &info->groups[group_index];
542 for (i = 0; i < group->num_counters; i++) {
543 if (group->counter_ids[i] == counter_id)
547 assert (counter_index < group->num_counters);
549 ctx->op_metrics[op].counters[group_index][counter_index] += value;
554 accumulate_program_time (metrics_op_t op, unsigned time_ns)
556 op_metrics_t *metrics;
558 metrics = ctx_get_op_metrics (¤t_context, op);
560 metrics->time_ns += time_ns;
563 typedef struct per_stage_metrics
565 op_metrics_t *metrics;
566 shader_stage_info_t *stage;
569 } per_stage_metrics_t;
572 _is_shader_stage_counter (metrics_info_t *info,
573 unsigned group_index,
574 unsigned counter_index)
576 shader_stage_info_t *stage;
579 for (i = 0; i < info->num_shader_stages; i++) {
580 stage = &info->stages[i];
582 if (stage->active_group_index == group_index &&
583 stage->active_counter_index == counter_index)
588 if (stage->stall_group_index == group_index &&
589 stage->stall_counter_index == counter_index)
599 print_per_stage_metrics (context_t *ctx,
600 per_stage_metrics_t *per_stage,
603 metrics_info_t *info = &ctx->metrics_info;
604 op_metrics_t *metric = per_stage->metrics;
605 metrics_group_info_t *group;
606 const char *op_string;
607 unsigned group_index, counter;
610 /* Don't print anything for stages with no alloted time. */
611 if (per_stage->time_ns == 0.0)
614 op_string = metrics_op_string (metric->op);
616 printf ("%21s", op_string);
618 if (metric->op >= METRICS_OP_SHADER) {
619 printf (" %3d", metric->op - METRICS_OP_SHADER);
624 printf (" %cS:", per_stage->stage->name[0]);
626 printf ("\t%7.2f ms (%4.1f%%)",
627 per_stage->time_ns / 1e6,
628 per_stage->time_ns / total * 100);
630 if (per_stage->active)
631 printf (", %4.1f%% active", per_stage->active * 100);
635 /* I'm not seeing a lot of value printing the rest of these
636 * performance counters by default yet. Use --verbose to get
642 for (group_index = 0; group_index < info->num_groups; group_index++) {
643 group = &info->groups[group_index];
644 for (counter = 0; counter < group->num_counters; counter++) {
646 /* Don't print this counter value if it's a
647 * per-stage cycle counter, (which we have
648 * already accounted for). */
649 if (_is_shader_stage_counter (info, group_index, counter))
652 value = metric->counters[group_index][counter];
655 printf ("%s: %.2f ", group->counter_names[counter],
663 time_compare(const void *in_a, const void *in_b, void *arg unused)
665 const per_stage_metrics_t *a = in_a;
666 const per_stage_metrics_t *b = in_b;
669 if (a->time_ns < b->time_ns)
671 if (a->time_ns > b->time_ns)
677 print_program_metrics (void)
679 context_t *ctx = ¤t_context;
680 metrics_info_t *info = &ctx->metrics_info;
681 unsigned num_shader_stages = info->num_shader_stages;
682 per_stage_metrics_t *sorted, *per_stage;
683 double total_time, op_cycles;
685 unsigned group_index, counter_index;
686 unsigned i, j, num_sorted;
688 /* Make a sorted list of the per-stage operations by time
689 * used, and figure out the total so we can print percentages.
691 num_sorted = ctx->num_op_metrics * num_shader_stages;
693 sorted = xmalloc (sizeof (*sorted) * num_sorted);
697 for (i = 0; i < ctx->num_op_metrics; i++) {
699 op = &ctx->op_metrics[i];
701 /* Accumulate total time across all ops. */
702 total_time += op->time_ns;
704 /* Also, find total cycles in all stages of this op. */
707 for (j = 0; j < num_shader_stages; j++) {
709 group_index = info->stages[j].active_group_index;
710 counter_index = info->stages[j].active_counter_index;
711 op_cycles += op->counters[group_index][counter_index];
714 group_index = info->stages[j].stall_group_index;
715 counter_index = info->stages[j].stall_counter_index;
716 op_cycles += op->counters[group_index][counter_index];
719 for (j = 0; j < num_shader_stages; j++) {
720 double active_cycles, stall_cycles, stage_cycles;
723 group_index = info->stages[j].active_group_index;
724 counter_index = info->stages[j].active_counter_index;
725 active_cycles = op->counters[group_index][counter_index];
728 group_index = info->stages[j].stall_group_index;
729 counter_index = info->stages[j].stall_counter_index;
730 stall_cycles = op->counters[group_index][counter_index];
732 stage_cycles = active_cycles + stall_cycles;
734 per_stage = &sorted[i * num_shader_stages + j];
735 per_stage->metrics = op;
736 per_stage->stage = &info->stages[j];
738 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
740 per_stage->time_ns = 0.0;
742 per_stage->active = active_cycles / stage_cycles;
744 per_stage->active = 0.0;
748 qsort_r (sorted, num_sorted, sizeof (*sorted),
749 time_compare, ctx->op_metrics);
751 for (i = 0; i < num_sorted; i++)
752 print_per_stage_metrics (ctx, &sorted[i], total_time);
757 /* Called at program exit */
762 printf ("fips: terminating\n");
764 metrics_info_fini (¤t_context.metrics_info);
769 metrics_end_frame (void)
771 static int initialized = 0;
772 static struct timeval tv_start, tv_now;
775 gettimeofday (&tv_start, NULL);
776 atexit (metrics_exit);
777 if (getenv ("FIPS_VERBOSE"))
783 gettimeofday (&tv_now, NULL);
785 /* Consume all timer queries that are ready. */
786 timer_query_t *timer = current_context.timer_head;
789 GLuint available, elapsed;
791 glGetQueryObjectuiv (timer->id,
792 GL_QUERY_RESULT_AVAILABLE, &available);
796 glGetQueryObjectuiv (timer->id,
797 GL_QUERY_RESULT, &elapsed);
799 accumulate_program_time (timer->op, elapsed);
801 current_context.timer_head = timer->next;
802 if (current_context.timer_head == NULL)
803 current_context.timer_tail = NULL;
805 glDeleteQueries (1, &timer->id);
808 timer = current_context.timer_head;
811 /* And similarly for all performance monitors that are ready. */
812 monitor_t *monitor = current_context.monitor_head;
815 GLuint available, result_size, *result;
818 glGetPerfMonitorCounterDataAMD (monitor->id,
819 GL_PERFMON_RESULT_AVAILABLE_AMD,
820 sizeof (available), &available,
825 glGetPerfMonitorCounterDataAMD (monitor->id,
826 GL_PERFMON_RESULT_SIZE_AMD,
827 sizeof (result_size),
830 result = xmalloc (result_size);
832 glGetPerfMonitorCounterDataAMD (monitor->id,
833 GL_PERFMON_RESULT_AMD,
837 accumulate_program_metrics (monitor->op, result, result_size);
841 current_context.monitor_head = monitor->next;
842 if (current_context.monitor_head == NULL)
843 current_context.monitor_tail = NULL;
845 glDeletePerfMonitorsAMD (1, &monitor->id);
848 monitor = current_context.monitor_head;
851 if (frames % 60 == 0) {
854 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
855 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
857 printf("FPS: %.3f\n", fps);
859 print_program_metrics ();