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 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
110 unsigned timer_begun_id;
112 /* GL_TIME_ELAPSED queries for which glEndQuery has been
113 * called, (but results have not yet been queried). */
114 timer_query_t *timer_head;
115 timer_query_t *timer_tail;
117 /* Performance monitor for which glEndPerfMonitorAMD has not
118 * yet been called. */
119 unsigned monitor_begun_id;
121 /* Performance monitors for which glEndPerfMonitorAMD has
122 * been called, (but results have not yet been queried). */
123 monitor_t *monitor_head;
124 monitor_t *monitor_tail;
126 int monitors_in_flight;
128 unsigned num_op_metrics;
129 op_metrics_t *op_metrics;
132 /* FIXME: Need a map from integers to context objects and track the
133 * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
135 context_t current_context;
140 #define MAX_MONITORS_IN_FLIGHT 1000
143 metrics_collect_available (void);
146 metrics_group_info_init (metrics_group_info_t *group, GLuint id)
154 glGetPerfMonitorGroupStringAMD (id, 0, &length, NULL);
156 group->name = xmalloc (length + 1);
158 glGetPerfMonitorGroupStringAMD (id, length + 1, NULL, group->name);
160 /* Get number of counters */
161 group->num_counters = 0;
162 group->max_active_counters = 0;
163 glGetPerfMonitorCountersAMD (group->id,
164 (int *) &group->num_counters,
165 (int *) &group->max_active_counters,
168 /* Get counter numbers */
169 group->counter_ids = xmalloc (group->num_counters * sizeof (GLuint));
171 glGetPerfMonitorCountersAMD (group->id, NULL, NULL,
175 /* Get counter names */
176 group->counter_names = xmalloc (group->num_counters * sizeof (char *));
177 group->counter_types = xmalloc (group->num_counters * sizeof (GLuint));
179 for (i = 0; i < group->num_counters; i++) {
180 glGetPerfMonitorCounterInfoAMD (group->id,
181 group->counter_ids[i],
183 &group->counter_types[i]);
185 glGetPerfMonitorCounterStringAMD (group->id,
186 group->counter_ids[i],
189 group->counter_names[i] = xmalloc (length + 1);
191 glGetPerfMonitorCounterStringAMD (group->id,
192 group->counter_ids[i],
194 group->counter_names[i]);
199 metrics_group_info_fini (metrics_group_info_t *group)
203 for (i = 0; i < group->num_counters; i++)
204 free (group->counter_names[i]);
206 free (group->counter_types);
207 free (group->counter_names);
208 free (group->counter_ids);
213 /* A helper function, part of metrics_info_init below. */
221 _add_shader_stage (metrics_info_t *info, const char *name,
222 GLuint group_index, GLuint counter_index,
223 shader_phase_t phase)
225 shader_stage_info_t *stage;
226 char *stage_name, *space;
229 stage_name = xstrdup (name);
231 /* Terminate the stage name at the first space.
233 * This is valid for counter names such as:
235 * "Vertex Shader Active Time"
237 * "Vertex Shader Stall Time - Core Stall"
239 space = strchr (stage_name, ' ');
243 /* Look for an existing stage of the given name. */
246 for (i = 0; i < info->num_shader_stages; i++) {
247 if (strcmp (info->stages[i].name, stage_name) == 0) {
248 stage = &info->stages[i];
254 info->num_shader_stages++;
255 info->stages = xrealloc (info->stages,
256 info->num_shader_stages *
257 sizeof (shader_stage_info_t));
258 stage = &info->stages[info->num_shader_stages - 1];
259 stage->name = xstrdup (stage_name);
260 stage->active_group_index = 0;
261 stage->active_counter_index = 0;
262 stage->stall_group_index = 0;
263 stage->stall_counter_index = 0;
266 if (phase == SHADER_ACTIVE) {
267 stage->active_group_index = group_index;
268 stage->active_counter_index = counter_index;
270 stage->stall_group_index = group_index;
271 stage->stall_counter_index = counter_index;
278 metrics_info_init (void)
282 metrics_info_t *info = ¤t_context.metrics_info;
284 glGetPerfMonitorGroupsAMD ((int *) &info->num_groups, 0, NULL);
286 group_ids = xmalloc (info->num_groups * sizeof (GLuint));
288 glGetPerfMonitorGroupsAMD (NULL, info->num_groups, group_ids);
290 info->groups = xmalloc (info->num_groups * sizeof (metrics_group_info_t));
292 for (i = 0; i < info->num_groups; i++)
293 metrics_group_info_init (&info->groups[i], group_ids[i]);
297 /* Identify each shader stage (by looking at
298 * performance-counter names for specific patterns) and
299 * initialize structures referring to the corresponding
300 * counter numbers for each stage. */
301 info->num_shader_stages = 0;
304 for (i = 0; i < info->num_groups; i++) {
305 metrics_group_info_t *group = &info->groups[i];
306 for (j = 0; j < group->num_counters; j++) {
307 char *name = group->counter_names[j];
308 if (strstr (name, "Shader Active Time")) {
309 _add_shader_stage (info, name, i, j,
312 if (strstr (name, "Shader Stall Time")) {
313 _add_shader_stage (info, name, i, j,
319 info->initialized = 1;
323 metrics_info_fini (void)
325 context_t *ctx = ¤t_context;
326 metrics_info_t *info = &ctx->metrics_info;
328 timer_query_t *timer, *timer_next;
329 monitor_t *monitor, *monitor_next;
331 if (! info->initialized)
334 if (ctx->timer_begun_id) {
335 ctx->timer_begun_id = 0;
338 for (timer = ctx->timer_head;
342 timer_next = timer->next;
345 ctx->timer_head = NULL;
346 ctx->timer_tail = NULL;
348 if (ctx->monitor_begun_id) {
349 ctx->monitor_begun_id = 0;
352 for (monitor = ctx->monitor_head;
354 monitor = monitor_next)
356 monitor_next = monitor->next;
359 ctx->monitor_head = NULL;
360 ctx->monitor_tail = NULL;
362 current_context.monitors_in_flight = 0;
364 for (i = 0; i < info->num_groups; i++)
365 metrics_group_info_fini (&info->groups[i]);
370 for (i = 0; i < info->num_shader_stages; i++)
371 free (info->stages[i].name);
376 info->initialized = 0;
380 metrics_op_string (metrics_op_t op)
382 if (op >= METRICS_OP_SHADER)
383 return "Shader program";
387 case METRICS_OP_ACCUM:
388 return "glAccum*(+)";
389 case METRICS_OP_BUFFER_DATA:
390 return "glBufferData(+)";
391 case METRICS_OP_BUFFER_SUB_DATA:
392 return "glCopyBufferSubData*";
393 case METRICS_OP_BITMAP:
395 case METRICS_OP_BLIT_FRAMEBUFFER:
396 return "glBlitFramebuffer*";
397 case METRICS_OP_CLEAR:
399 case METRICS_OP_CLEAR_BUFFER_DATA:
400 return "glCearBufferData(+)";
401 case METRICS_OP_CLEAR_TEX_IMAGE:
402 return "glClearTexImage(+)";
403 case METRICS_OP_COPY_PIXELS:
404 return "glCopyPixels";
405 case METRICS_OP_COPY_TEX_IMAGE:
406 return "glCopyTexImage(+)";
407 case METRICS_OP_DRAW_PIXELS:
408 return "glDrawPixels";
409 case METRICS_OP_GET_TEX_IMAGE:
410 return "glGetTexImage(+)";
411 case METRICS_OP_READ_PIXELS:
412 return "glReadPixels*";
413 case METRICS_OP_TEX_IMAGE:
414 return "glTexImage*(+)";
416 fprintf (stderr, "fips: Internal error: "
417 "Unknown metrics op value: %d\n", op);
425 metrics_counter_start (void)
427 context_t *ctx = ¤t_context;
430 /* Initialize the timer_query and monitor objects */
431 glGenQueries (1, &ctx->timer_begun_id);
433 glGenPerfMonitorsAMD (1, &ctx->monitor_begun_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(ctx->monitor_begun_id,
458 /* Start the queries */
459 glBeginQuery (GL_TIME_ELAPSED, ctx->timer_begun_id);
461 glBeginPerfMonitorAMD (ctx->monitor_begun_id);
465 metrics_counter_stop (void)
467 context_t *ctx = ¤t_context;
468 timer_query_t *timer;
471 /* Stop the current timer and monitor. */
472 glEndQuery (GL_TIME_ELAPSED);
473 glEndPerfMonitorAMD (ctx->monitor_begun_id);
475 /* Add these IDs to our lists of outstanding queries and
476 * monitors so the results can be collected later. */
477 timer = xmalloc (sizeof (timer_query_t));
480 timer->id = ctx->timer_begun_id;
483 if (ctx->timer_tail) {
484 ctx->timer_tail->next = timer;
485 ctx->timer_tail = timer;
487 ctx->timer_tail = timer;
488 ctx->timer_head = timer;
491 /* Create a new performance-monitor query */
492 monitor = xmalloc (sizeof (monitor_t));
494 monitor->op = ctx->op;
495 monitor->id = ctx->monitor_begun_id;
496 monitor->next = NULL;
498 if (ctx->monitor_tail) {
499 ctx->monitor_tail->next = monitor;
500 ctx->monitor_tail = monitor;
502 ctx->monitor_tail = monitor;
503 ctx->monitor_head = monitor;
506 ctx->monitors_in_flight++;
508 /* Avoid being a resource hog and collect outstanding results
509 * once we have sent off a large number of
510 * queries. (Presumably, many of the outstanding queries are
513 if (ctx->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
514 metrics_collect_available ();
518 metrics_set_current_op (metrics_op_t op)
520 current_context.op = op;
524 metrics_get_current_op (void)
526 return current_context.op;
530 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
532 metrics_info_t *info = &ctx->metrics_info;
536 metrics->time_ns = 0.0;
538 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
540 for (i = 0; i < info->num_groups; i++) {
541 metrics->counters[i] = xmalloc (sizeof (double) *
542 info->groups[i].num_counters);
543 for (j = 0; j < info->groups[i].num_counters; j++)
544 metrics->counters[i][j] = 0.0;
548 static op_metrics_t *
549 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
553 if (op >= ctx->num_op_metrics)
555 ctx->op_metrics = realloc (ctx->op_metrics,
556 (op + 1) * sizeof (op_metrics_t));
557 for (i = ctx->num_op_metrics; i < op + 1; i++)
558 op_metrics_init (ctx, &ctx->op_metrics[i], i);
560 ctx->num_op_metrics = op + 1;
563 return &ctx->op_metrics[op];
567 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
569 #define CONSUME(var) \
570 if (p + sizeof(var) > ((unsigned char *) result) + size) \
572 fprintf (stderr, "Unexpected end-of-buffer while " \
573 "parsing results\n"); \
576 (var) = *((typeof(var) *) p); \
579 context_t *ctx = ¤t_context;
580 metrics_info_t *info = &ctx->metrics_info;
581 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
582 unsigned char *p = (unsigned char *) result;
584 while (p < ((unsigned char *) result) + size)
586 GLuint group_id, group_index;
587 GLuint counter_id, counter_index;
588 metrics_group_info_t *group;
593 CONSUME (counter_id);
595 for (i = 0; i < info->num_groups; i++) {
596 if (info->groups[i].id == group_id)
600 assert (group_index < info->num_groups);
601 group = &info->groups[group_index];
603 for (i = 0; i < group->num_counters; i++) {
604 if (group->counter_ids[i] == counter_id)
608 assert (counter_index < group->num_counters);
610 switch (group->counter_types[counter_index])
613 uint64_t uint64_value;
615 case GL_UNSIGNED_INT:
616 CONSUME (uint_value);
619 case GL_UNSIGNED_INT64_AMD:
620 CONSUME (uint64_value);
621 value = uint64_value;
623 case GL_PERCENTAGE_AMD:
625 CONSUME (float_value);
629 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
630 group->counter_types[counter_index]);
635 metrics->counters[group_index][counter_index] += value;
640 accumulate_program_time (metrics_op_t op, unsigned time_ns)
642 op_metrics_t *metrics;
644 metrics = ctx_get_op_metrics (¤t_context, op);
646 metrics->time_ns += time_ns;
649 typedef struct per_stage_metrics
651 op_metrics_t *metrics;
652 shader_stage_info_t *stage;
655 } per_stage_metrics_t;
658 _is_shader_stage_counter (metrics_info_t *info,
659 unsigned group_index,
660 unsigned counter_index)
662 shader_stage_info_t *stage;
665 for (i = 0; i < info->num_shader_stages; i++) {
666 stage = &info->stages[i];
668 if (stage->active_group_index == group_index &&
669 stage->active_counter_index == counter_index)
674 if (stage->stall_group_index == group_index &&
675 stage->stall_counter_index == counter_index)
685 print_per_stage_metrics (context_t *ctx,
686 per_stage_metrics_t *per_stage,
689 metrics_info_t *info = &ctx->metrics_info;
690 op_metrics_t *metric = per_stage->metrics;
691 metrics_group_info_t *group;
692 const char *op_string;
693 unsigned group_index, counter;
696 /* Don't print anything for stages with no alloted time. */
697 if (per_stage->time_ns == 0.0)
700 op_string = metrics_op_string (metric->op);
702 printf ("%21s", op_string);
704 if (metric->op >= METRICS_OP_SHADER) {
705 printf (" %3d", metric->op - METRICS_OP_SHADER);
711 if (per_stage->stage)
712 printf (" %cS:", per_stage->stage->name[0]);
716 printf ("\t%7.2f ms (%4.1f%%)",
717 per_stage->time_ns / 1e6,
718 per_stage->time_ns / total * 100);
720 if (per_stage->active)
721 printf (", %4.1f%% active", per_stage->active * 100);
725 /* I'm not seeing a lot of value printing the rest of these
726 * performance counters by default yet. Use --verbose to get
732 for (group_index = 0; group_index < info->num_groups; group_index++) {
733 group = &info->groups[group_index];
734 for (counter = 0; counter < group->num_counters; counter++) {
736 /* Don't print this counter value if it's a
737 * per-stage cycle counter, (which we have
738 * already accounted for). */
739 if (_is_shader_stage_counter (info, group_index, counter))
742 value = metric->counters[group_index][counter];
745 printf ("%s: %.2f ", group->counter_names[counter],
753 time_compare(const void *in_a, const void *in_b, void *arg unused)
755 const per_stage_metrics_t *a = in_a;
756 const per_stage_metrics_t *b = in_b;
759 if (a->time_ns < b->time_ns)
761 if (a->time_ns > b->time_ns)
767 print_program_metrics (void)
769 context_t *ctx = ¤t_context;
770 metrics_info_t *info = &ctx->metrics_info;
771 unsigned num_shader_stages = info->num_shader_stages;
772 per_stage_metrics_t *sorted, *per_stage;
773 double total_time, op_cycles;
775 unsigned group_index, counter_index;
776 unsigned i, j, num_sorted;
778 /* Make a sorted list of the per-stage operations by time
779 * used, and figure out the total so we can print percentages.
781 num_sorted = ctx->num_op_metrics * num_shader_stages;
783 sorted = xmalloc (sizeof (*sorted) * num_sorted);
787 for (i = 0; i < ctx->num_op_metrics; i++) {
789 op = &ctx->op_metrics[i];
791 /* Accumulate total time across all ops. */
792 total_time += op->time_ns;
794 /* Also, find total cycles in all stages of this op. */
797 for (j = 0; j < num_shader_stages; j++) {
799 group_index = info->stages[j].active_group_index;
800 counter_index = info->stages[j].active_counter_index;
801 op_cycles += op->counters[group_index][counter_index];
804 group_index = info->stages[j].stall_group_index;
805 counter_index = info->stages[j].stall_counter_index;
806 op_cycles += op->counters[group_index][counter_index];
809 for (j = 0; j < num_shader_stages; j++) {
810 double active_cycles, stall_cycles, stage_cycles;
813 group_index = info->stages[j].active_group_index;
814 counter_index = info->stages[j].active_counter_index;
815 active_cycles = op->counters[group_index][counter_index];
818 group_index = info->stages[j].stall_group_index;
819 counter_index = info->stages[j].stall_counter_index;
820 stall_cycles = op->counters[group_index][counter_index];
822 stage_cycles = active_cycles + stall_cycles;
824 per_stage = &sorted[i * num_shader_stages + j];
825 per_stage->metrics = op;
828 per_stage->stage = &info->stages[j];
829 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
831 /* If we don't have any per-stage cycle counts
832 * for this operation, then use the first
833 * stage as a placeholder for all the time,
834 * but NULL-ify the stage info so that the
835 * report doesn't lie about this time being
836 * from any particular stage. */
837 per_stage->stage = NULL;
839 per_stage->time_ns = op->time_ns;
841 per_stage->time_ns = 0.0;
846 per_stage->active = active_cycles / stage_cycles;
848 per_stage->active = 0.0;
853 qsort_r (sorted, num_sorted, sizeof (*sorted),
854 time_compare, ctx->op_metrics);
856 for (i = 0; i < num_sorted; i++)
857 print_per_stage_metrics (ctx, &sorted[i], total_time);
862 /* Called at program exit */
867 printf ("fips: terminating\n");
869 metrics_info_fini ();
873 metrics_collect_available (void)
875 context_t *ctx = ¤t_context;
877 /* Consume all timer queries that are ready. */
878 timer_query_t *timer = ctx->timer_head;
881 GLuint available, elapsed;
883 glGetQueryObjectuiv (timer->id,
884 GL_QUERY_RESULT_AVAILABLE, &available);
888 glGetQueryObjectuiv (timer->id,
889 GL_QUERY_RESULT, &elapsed);
891 accumulate_program_time (timer->op, elapsed);
893 ctx->timer_head = timer->next;
894 if (ctx->timer_head == NULL)
895 ctx->timer_tail = NULL;
897 glDeleteQueries (1, &timer->id);
900 timer = ctx->timer_head;
903 /* And similarly for all performance monitors that are ready. */
904 monitor_t *monitor = ctx->monitor_head;
907 GLuint available, result_size, *result;
910 glGetPerfMonitorCounterDataAMD (monitor->id,
911 GL_PERFMON_RESULT_AVAILABLE_AMD,
912 sizeof (available), &available,
917 glGetPerfMonitorCounterDataAMD (monitor->id,
918 GL_PERFMON_RESULT_SIZE_AMD,
919 sizeof (result_size),
922 result = xmalloc (result_size);
924 glGetPerfMonitorCounterDataAMD (monitor->id,
925 GL_PERFMON_RESULT_AMD,
929 accumulate_program_metrics (monitor->op, result, result_size);
933 ctx->monitor_head = monitor->next;
934 if (ctx->monitor_head == NULL)
935 ctx->monitor_tail = NULL;
937 glDeletePerfMonitorsAMD (1, &monitor->id);
941 ctx->monitors_in_flight--;
943 monitor = ctx->monitor_head;
949 metrics_end_frame (void)
951 static int initialized = 0;
952 static struct timeval tv_start, tv_now;
955 gettimeofday (&tv_start, NULL);
956 atexit (metrics_exit);
957 if (getenv ("FIPS_VERBOSE"))
964 metrics_collect_available ();
966 if (frames % 15 == 0) {
969 gettimeofday (&tv_now, NULL);
971 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
972 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
974 printf("FPS: %.3f\n", fps);
976 print_program_metrics ();