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);
195 metrics_info_fini (metrics_info_t *info);
197 /* A helper function, part of metrics_info_init below. */
205 _add_shader_stage (metrics_info_t *info, const char *name,
206 GLuint group_index, GLuint counter_index,
207 shader_phase_t phase)
209 shader_stage_info_t *stage;
210 char *stage_name, *space;
213 stage_name = xstrdup (name);
215 /* Terminate the stage name at the first space.
217 * This is valid for counter names such as:
219 * "Vertex Shader Active Time"
221 * "Vertex Shader Stall Time - Core Stall"
223 space = strchr (stage_name, ' ');
227 /* Look for an existing stage of the given name. */
230 for (i = 0; i < info->num_shader_stages; i++) {
231 if (strcmp (info->stages[i].name, stage_name) == 0) {
232 stage = &info->stages[i];
238 info->num_shader_stages++;
239 info->stages = xrealloc (info->stages,
240 info->num_shader_stages *
241 sizeof (shader_stage_info_t));
242 stage = &info->stages[info->num_shader_stages - 1];
243 stage->name = xstrdup (stage_name);
244 stage->active_group_index = 0;
245 stage->active_counter_index = 0;
246 stage->stall_group_index = 0;
247 stage->stall_counter_index = 0;
250 if (phase == SHADER_ACTIVE) {
251 stage->active_group_index = group_index;
252 stage->active_counter_index = counter_index;
254 stage->stall_group_index = group_index;
255 stage->stall_counter_index = counter_index;
262 metrics_info_init (void)
266 metrics_info_t *info = ¤t_context.metrics_info;
268 if (info->initialized)
269 metrics_info_fini (info);
271 glGetPerfMonitorGroupsAMD ((int *) &info->num_groups, 0, NULL);
273 group_ids = xmalloc (info->num_groups * sizeof (GLuint));
275 glGetPerfMonitorGroupsAMD (NULL, info->num_groups, group_ids);
277 info->groups = xmalloc (info->num_groups * sizeof (metrics_group_info_t));
279 for (i = 0; i < info->num_groups; i++)
280 metrics_group_info_init (&info->groups[i], group_ids[i]);
284 /* Identify each shader stage (by looking at
285 * performance-counter names for specific patterns) and
286 * initialize structures referring to the corresponding
287 * counter numbers for each stage. */
288 info->num_shader_stages = 0;
291 for (i = 0; i < info->num_groups; i++) {
292 metrics_group_info_t *group = &info->groups[i];
293 for (j = 0; j < group->num_counters; j++) {
294 char *name = group->counter_names[j];
295 if (strstr (name, "Shader Active Time")) {
296 _add_shader_stage (info, name, i, j,
299 if (strstr (name, "Shader Stall Time")) {
300 _add_shader_stage (info, name, i, j,
306 info->initialized = 1;
310 metrics_info_fini (metrics_info_t *info)
314 for (i = 0; i < info->num_groups; i++)
315 metrics_group_info_fini (&info->groups[i]);
319 for (i = 0; i < info->num_shader_stages; i++)
320 free (info->stages[i].name);
326 metrics_op_string (metrics_op_t op)
328 if (op >= METRICS_OP_SHADER)
329 return "Shader program";
333 case METRICS_OP_ACCUM:
334 return "glAccum*(+)";
335 case METRICS_OP_BUFFER_DATA:
336 return "glBufferData(+)";
337 case METRICS_OP_BUFFER_SUB_DATA:
338 return "glCopyBufferSubData*";
339 case METRICS_OP_BITMAP:
341 case METRICS_OP_BLIT_FRAMEBUFFER:
342 return "glBlitFramebuffer*";
343 case METRICS_OP_CLEAR:
345 case METRICS_OP_CLEAR_BUFFER_DATA:
346 return "glCearBufferData(+)";
347 case METRICS_OP_CLEAR_TEX_IMAGE:
348 return "glClearTexImage(+)";
349 case METRICS_OP_COPY_PIXELS:
350 return "glCopyPixels";
351 case METRICS_OP_COPY_TEX_IMAGE:
352 return "glCopyTexImage(+)";
353 case METRICS_OP_DRAW_PIXELS:
354 return "glDrawPixels";
355 case METRICS_OP_GET_TEX_IMAGE:
356 return "glGetTexImage(+)";
357 case METRICS_OP_READ_PIXELS:
358 return "glReadPixels*";
359 case METRICS_OP_TEX_IMAGE:
360 return "glTexImage*(+)";
362 fprintf (stderr, "fips: Internal error: "
363 "Unknown metrics op value: %d\n", op);
371 metrics_counter_start (void)
373 context_t *ctx = ¤t_context;
374 timer_query_t *timer;
378 /* Create new timer query, add to list */
379 timer = xmalloc (sizeof (timer_query_t));
384 if (ctx->timer_tail) {
385 ctx->timer_tail->next = timer;
386 ctx->timer_tail = timer;
388 ctx->timer_tail = timer;
389 ctx->timer_head = timer;
392 /* Create a new performance-monitor query */
393 monitor = xmalloc (sizeof (monitor_t));
395 monitor->op = ctx->op;
396 monitor->next = NULL;
398 if (ctx->monitor_tail) {
399 ctx->monitor_tail->next = monitor;
400 ctx->monitor_tail = monitor;
402 ctx->monitor_tail = monitor;
403 ctx->monitor_head = monitor;
406 /* Initialize the timer_query and monitor objects */
407 glGenQueries (1, &timer->id);
409 glGenPerfMonitorsAMD (1, &monitor->id);
411 for (i = 0; i < ctx->metrics_info.num_groups; i++)
413 metrics_group_info_t *group;
416 group = &ctx->metrics_info.groups[i];
418 num_counters = group->num_counters;
419 if (group->max_active_counters < group->num_counters)
421 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
422 group->max_active_counters,
423 group->num_counters, i);
424 num_counters = group->max_active_counters;
428 glSelectPerfMonitorCountersAMD(monitor->id,
434 /* Start the queries */
435 glBeginQuery (GL_TIME_ELAPSED, timer->id);
437 glBeginPerfMonitorAMD (monitor->id);
441 metrics_counter_stop (void)
443 glEndQuery (GL_TIME_ELAPSED);
444 glEndPerfMonitorAMD (current_context.monitor_tail->id);
448 metrics_set_current_op (metrics_op_t op)
450 current_context.op = op;
454 metrics_get_current_op (void)
456 return current_context.op;
460 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
462 metrics_info_t *info = &ctx->metrics_info;
466 metrics->time_ns = 0.0;
468 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
470 for (i = 0; i < info->num_groups; i++) {
471 metrics->counters[i] = xmalloc (sizeof (double) *
472 info->groups[i].num_counters);
473 for (j = 0; j < info->groups[i].num_counters; j++)
474 metrics->counters[i][j] = 0.0;
478 static op_metrics_t *
479 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
483 if (op >= ctx->num_op_metrics)
485 ctx->op_metrics = realloc (ctx->op_metrics,
486 (op + 1) * sizeof (op_metrics_t));
487 for (i = ctx->num_op_metrics; i < op + 1; i++)
488 op_metrics_init (ctx, &ctx->op_metrics[i], i);
490 ctx->num_op_metrics = op + 1;
493 return &ctx->op_metrics[op];
497 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
499 #define CONSUME(var) \
500 if (p + sizeof(var) > ((unsigned char *) result) + size) \
502 fprintf (stderr, "Unexpected end-of-buffer while " \
503 "parsing results\n"); \
506 (var) = *((typeof(var) *) p); \
509 context_t *ctx = ¤t_context;
510 metrics_info_t *info = &ctx->metrics_info;
511 unsigned char *p = (unsigned char *) result;
513 while (p < ((unsigned char *) result) + size)
515 GLuint group_id, group_index;
516 GLuint counter_id, counter_index;
517 metrics_group_info_t *group;
522 CONSUME (counter_id);
524 for (i = 0; i < info->num_groups; i++) {
525 if (info->groups[i].id == group_id)
529 assert (group_index < info->num_groups);
530 group = &info->groups[group_index];
532 for (i = 0; i < group->num_counters; i++) {
533 if (group->counter_ids[i] == counter_id)
537 assert (counter_index < group->num_counters);
539 switch (group->counter_types[counter_index])
542 uint64_t uint64_value;
544 case GL_UNSIGNED_INT:
545 CONSUME (uint_value);
548 case GL_UNSIGNED_INT64_AMD:
549 CONSUME (uint64_value);
550 value = uint64_value;
552 case GL_PERCENTAGE_AMD:
554 CONSUME (float_value);
558 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
559 group->counter_types[counter_index]);
564 ctx->op_metrics[op].counters[group_index][counter_index] += value;
569 accumulate_program_time (metrics_op_t op, unsigned time_ns)
571 op_metrics_t *metrics;
573 metrics = ctx_get_op_metrics (¤t_context, op);
575 metrics->time_ns += time_ns;
578 typedef struct per_stage_metrics
580 op_metrics_t *metrics;
581 shader_stage_info_t *stage;
584 } per_stage_metrics_t;
587 _is_shader_stage_counter (metrics_info_t *info,
588 unsigned group_index,
589 unsigned counter_index)
591 shader_stage_info_t *stage;
594 for (i = 0; i < info->num_shader_stages; i++) {
595 stage = &info->stages[i];
597 if (stage->active_group_index == group_index &&
598 stage->active_counter_index == counter_index)
603 if (stage->stall_group_index == group_index &&
604 stage->stall_counter_index == counter_index)
614 print_per_stage_metrics (context_t *ctx,
615 per_stage_metrics_t *per_stage,
618 metrics_info_t *info = &ctx->metrics_info;
619 op_metrics_t *metric = per_stage->metrics;
620 metrics_group_info_t *group;
621 const char *op_string;
622 unsigned group_index, counter;
625 /* Don't print anything for stages with no alloted time. */
626 if (per_stage->time_ns == 0.0)
629 op_string = metrics_op_string (metric->op);
631 printf ("%21s", op_string);
633 if (metric->op >= METRICS_OP_SHADER) {
634 printf (" %3d", metric->op - METRICS_OP_SHADER);
639 printf (" %cS:", per_stage->stage->name[0]);
641 printf ("\t%7.2f ms (%4.1f%%)",
642 per_stage->time_ns / 1e6,
643 per_stage->time_ns / total * 100);
645 if (per_stage->active)
646 printf (", %4.1f%% active", per_stage->active * 100);
650 /* I'm not seeing a lot of value printing the rest of these
651 * performance counters by default yet. Use --verbose to get
657 for (group_index = 0; group_index < info->num_groups; group_index++) {
658 group = &info->groups[group_index];
659 for (counter = 0; counter < group->num_counters; counter++) {
661 /* Don't print this counter value if it's a
662 * per-stage cycle counter, (which we have
663 * already accounted for). */
664 if (_is_shader_stage_counter (info, group_index, counter))
667 value = metric->counters[group_index][counter];
670 printf ("%s: %.2f ", group->counter_names[counter],
678 time_compare(const void *in_a, const void *in_b, void *arg unused)
680 const per_stage_metrics_t *a = in_a;
681 const per_stage_metrics_t *b = in_b;
684 if (a->time_ns < b->time_ns)
686 if (a->time_ns > b->time_ns)
692 print_program_metrics (void)
694 context_t *ctx = ¤t_context;
695 metrics_info_t *info = &ctx->metrics_info;
696 unsigned num_shader_stages = info->num_shader_stages;
697 per_stage_metrics_t *sorted, *per_stage;
698 double total_time, op_cycles;
700 unsigned group_index, counter_index;
701 unsigned i, j, num_sorted;
703 /* Make a sorted list of the per-stage operations by time
704 * used, and figure out the total so we can print percentages.
706 num_sorted = ctx->num_op_metrics * num_shader_stages;
708 sorted = xmalloc (sizeof (*sorted) * num_sorted);
712 for (i = 0; i < ctx->num_op_metrics; i++) {
714 op = &ctx->op_metrics[i];
716 /* Accumulate total time across all ops. */
717 total_time += op->time_ns;
719 /* Also, find total cycles in all stages of this op. */
722 for (j = 0; j < num_shader_stages; j++) {
724 group_index = info->stages[j].active_group_index;
725 counter_index = info->stages[j].active_counter_index;
726 op_cycles += op->counters[group_index][counter_index];
729 group_index = info->stages[j].stall_group_index;
730 counter_index = info->stages[j].stall_counter_index;
731 op_cycles += op->counters[group_index][counter_index];
734 for (j = 0; j < num_shader_stages; j++) {
735 double active_cycles, stall_cycles, stage_cycles;
738 group_index = info->stages[j].active_group_index;
739 counter_index = info->stages[j].active_counter_index;
740 active_cycles = op->counters[group_index][counter_index];
743 group_index = info->stages[j].stall_group_index;
744 counter_index = info->stages[j].stall_counter_index;
745 stall_cycles = op->counters[group_index][counter_index];
747 stage_cycles = active_cycles + stall_cycles;
749 per_stage = &sorted[i * num_shader_stages + j];
750 per_stage->metrics = op;
751 per_stage->stage = &info->stages[j];
753 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
755 per_stage->time_ns = 0.0;
757 per_stage->active = active_cycles / stage_cycles;
759 per_stage->active = 0.0;
763 qsort_r (sorted, num_sorted, sizeof (*sorted),
764 time_compare, ctx->op_metrics);
766 for (i = 0; i < num_sorted; i++)
767 print_per_stage_metrics (ctx, &sorted[i], total_time);
772 /* Called at program exit */
777 printf ("fips: terminating\n");
779 metrics_info_fini (¤t_context.metrics_info);
784 metrics_end_frame (void)
786 static int initialized = 0;
787 static struct timeval tv_start, tv_now;
790 gettimeofday (&tv_start, NULL);
791 atexit (metrics_exit);
792 if (getenv ("FIPS_VERBOSE"))
798 gettimeofday (&tv_now, NULL);
800 /* Consume all timer queries that are ready. */
801 timer_query_t *timer = current_context.timer_head;
804 GLuint available, elapsed;
806 glGetQueryObjectuiv (timer->id,
807 GL_QUERY_RESULT_AVAILABLE, &available);
811 glGetQueryObjectuiv (timer->id,
812 GL_QUERY_RESULT, &elapsed);
814 accumulate_program_time (timer->op, elapsed);
816 current_context.timer_head = timer->next;
817 if (current_context.timer_head == NULL)
818 current_context.timer_tail = NULL;
820 glDeleteQueries (1, &timer->id);
823 timer = current_context.timer_head;
826 /* And similarly for all performance monitors that are ready. */
827 monitor_t *monitor = current_context.monitor_head;
830 GLuint available, result_size, *result;
833 glGetPerfMonitorCounterDataAMD (monitor->id,
834 GL_PERFMON_RESULT_AVAILABLE_AMD,
835 sizeof (available), &available,
840 glGetPerfMonitorCounterDataAMD (monitor->id,
841 GL_PERFMON_RESULT_SIZE_AMD,
842 sizeof (result_size),
845 result = xmalloc (result_size);
847 glGetPerfMonitorCounterDataAMD (monitor->id,
848 GL_PERFMON_RESULT_AMD,
852 accumulate_program_metrics (monitor->op, result, result_size);
856 current_context.monitor_head = monitor->next;
857 if (current_context.monitor_head == NULL)
858 current_context.monitor_tail = NULL;
860 glDeletePerfMonitorsAMD (1, &monitor->id);
863 monitor = current_context.monitor_head;
866 if (frames % 60 == 0) {
869 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
870 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
872 printf("FPS: %.3f\n", fps);
874 print_program_metrics ();