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
74 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
76 unsigned timer_begun_id;
78 /* GL_TIME_ELAPSED queries for which glEndQuery has been
79 * called, (but results have not yet been queried). */
80 timer_query_t *timer_head;
81 timer_query_t *timer_tail;
83 /* Performance monitor for which glEndPerfMonitorAMD has not
85 unsigned monitor_begun_id;
87 /* Performance monitors for which glEndPerfMonitorAMD has
88 * been called, (but results have not yet been queried). */
89 monitor_t *monitor_head;
90 monitor_t *monitor_tail;
92 int monitors_in_flight;
94 unsigned num_op_metrics;
95 op_metrics_t *op_metrics;
103 metrics = xmalloc (sizeof (metrics_t));
107 metrics->timer_begun_id = 0;
109 metrics->timer_head = NULL;
110 metrics->timer_tail = NULL;
112 metrics->monitor_begun_id = 0;
114 metrics->monitor_head = NULL;
115 metrics->monitor_tail = NULL;
117 metrics->monitors_in_flight = 0;
119 metrics->num_op_metrics = 0;
120 metrics->op_metrics = NULL;
126 metrics_fini (metrics_t *metrics)
128 timer_query_t *timer, *timer_next;
129 monitor_t *monitor, *monitor_next;
131 /* Discard and cleanup any outstanding queries. */
132 if (metrics->timer_begun_id) {
133 glEndQuery (GL_TIME_ELAPSED);
134 glDeleteQueries (1, &metrics->timer_begun_id);
135 metrics->timer_begun_id = 0;
138 for (timer = metrics->timer_head;
142 glDeleteQueries (1, &timer->id);
143 timer_next = timer->next;
146 metrics->timer_head = NULL;
147 metrics->timer_tail = NULL;
149 if (metrics->monitor_begun_id) {
150 glEndPerfMonitorAMD (metrics->monitor_begun_id);
151 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
152 metrics->monitor_begun_id = 0;
155 for (monitor = metrics->monitor_head;
157 monitor = monitor_next)
159 glDeletePerfMonitorsAMD (1, &monitor->id);
160 monitor_next = monitor->next;
163 metrics->monitor_head = NULL;
164 metrics->monitor_tail = NULL;
166 metrics->monitors_in_flight = 0;
170 metrics_destroy (metrics_t *metrics)
172 metrics_fini (metrics);
178 metrics_op_string (metrics_op_t op)
180 if (op >= METRICS_OP_SHADER)
181 return "Shader program";
185 case METRICS_OP_ACCUM:
186 return "glAccum*(+)";
187 case METRICS_OP_BUFFER_DATA:
188 return "glBufferData(+)";
189 case METRICS_OP_BUFFER_SUB_DATA:
190 return "glCopyBufferSubData*";
191 case METRICS_OP_BITMAP:
193 case METRICS_OP_BLIT_FRAMEBUFFER:
194 return "glBlitFramebuffer*";
195 case METRICS_OP_CLEAR:
197 case METRICS_OP_CLEAR_BUFFER_DATA:
198 return "glCearBufferData(+)";
199 case METRICS_OP_CLEAR_TEX_IMAGE:
200 return "glClearTexImage(+)";
201 case METRICS_OP_COPY_PIXELS:
202 return "glCopyPixels";
203 case METRICS_OP_COPY_TEX_IMAGE:
204 return "glCopyTexImage(+)";
205 case METRICS_OP_DRAW_PIXELS:
206 return "glDrawPixels";
207 case METRICS_OP_GET_TEX_IMAGE:
208 return "glGetTexImage(+)";
209 case METRICS_OP_READ_PIXELS:
210 return "glReadPixels*";
211 case METRICS_OP_TEX_IMAGE:
212 return "glTexImage*(+)";
214 fprintf (stderr, "fips: Internal error: "
215 "Unknown metrics op value: %d\n", op);
223 metrics_counter_start (void)
225 context_t *ctx = context_get_current ();
226 metrics_t *metrics = ctx->metrics;
229 /* Initialize the timer_query and monitor objects */
230 glGenQueries (1, &metrics->timer_begun_id);
232 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
234 for (i = 0; i < ctx->metrics_info.num_groups; i++)
236 metrics_group_info_t *group;
239 group = &ctx->metrics_info.groups[i];
241 num_counters = group->num_counters;
242 if (group->max_active_counters < group->num_counters)
244 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
245 group->max_active_counters,
246 group->num_counters, i);
247 num_counters = group->max_active_counters;
251 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
257 /* Start the queries */
258 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
260 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
264 metrics_counter_stop (void)
266 context_t *ctx = context_get_current ();
267 metrics_t *metrics = ctx->metrics;
268 timer_query_t *timer;
271 /* Stop the current timer and monitor. */
272 glEndQuery (GL_TIME_ELAPSED);
273 glEndPerfMonitorAMD (metrics->monitor_begun_id);
275 /* Add these IDs to our lists of outstanding queries and
276 * monitors so the results can be collected later. */
277 timer = xmalloc (sizeof (timer_query_t));
279 timer->op = metrics->op;
280 timer->id = metrics->timer_begun_id;
283 if (metrics->timer_tail) {
284 metrics->timer_tail->next = timer;
285 metrics->timer_tail = timer;
287 metrics->timer_tail = timer;
288 metrics->timer_head = timer;
291 /* Create a new performance-monitor query */
292 monitor = xmalloc (sizeof (monitor_t));
294 monitor->op = metrics->op;
295 monitor->id = metrics->monitor_begun_id;
296 monitor->next = NULL;
298 if (metrics->monitor_tail) {
299 metrics->monitor_tail->next = monitor;
300 metrics->monitor_tail = monitor;
302 metrics->monitor_tail = monitor;
303 metrics->monitor_head = monitor;
306 metrics->monitors_in_flight++;
308 /* Avoid being a resource hog and collect outstanding results
309 * once we have sent off a large number of
310 * queries. (Presumably, many of the outstanding queries are
313 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
314 metrics_collect_available ();
318 metrics_set_current_op (metrics_op_t op)
320 context_t *ctx = context_get_current ();
321 metrics_t *metrics = ctx->metrics;
327 metrics_get_current_op (void)
329 context_t *ctx = context_get_current ();
330 metrics_t *metrics = ctx->metrics;
336 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
338 metrics_info_t *info = &ctx->metrics_info;
342 metrics->time_ns = 0.0;
344 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
346 for (i = 0; i < info->num_groups; i++) {
347 metrics->counters[i] = xmalloc (sizeof (double) *
348 info->groups[i].num_counters);
349 for (j = 0; j < info->groups[i].num_counters; j++)
350 metrics->counters[i][j] = 0.0;
354 static op_metrics_t *
355 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
357 metrics_t *metrics = ctx->metrics;
360 if (op >= metrics->num_op_metrics)
362 metrics->op_metrics = realloc (metrics->op_metrics,
363 (op + 1) * sizeof (op_metrics_t));
364 for (i = metrics->num_op_metrics; i < op + 1; i++)
365 op_metrics_init (ctx, &metrics->op_metrics[i], i);
367 metrics->num_op_metrics = op + 1;
370 return &metrics->op_metrics[op];
374 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
376 #define CONSUME(var) \
377 if (p + sizeof(var) > ((unsigned char *) result) + size) \
379 fprintf (stderr, "Unexpected end-of-buffer while " \
380 "parsing results\n"); \
383 (var) = *((typeof(var) *) p); \
386 context_t *ctx = context_get_current ();
387 metrics_info_t *info = &ctx->metrics_info;
388 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
389 unsigned char *p = (unsigned char *) result;
391 while (p < ((unsigned char *) result) + size)
393 GLuint group_id, group_index;
394 GLuint counter_id, counter_index;
395 metrics_group_info_t *group;
400 CONSUME (counter_id);
402 for (i = 0; i < info->num_groups; i++) {
403 if (info->groups[i].id == group_id)
407 assert (group_index < info->num_groups);
408 group = &info->groups[group_index];
410 for (i = 0; i < group->num_counters; i++) {
411 if (group->counter_ids[i] == counter_id)
415 assert (counter_index < group->num_counters);
417 switch (group->counter_types[counter_index])
420 uint64_t uint64_value;
422 case GL_UNSIGNED_INT:
423 CONSUME (uint_value);
426 case GL_UNSIGNED_INT64_AMD:
427 CONSUME (uint64_value);
428 value = uint64_value;
430 case GL_PERCENTAGE_AMD:
432 CONSUME (float_value);
436 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
437 group->counter_types[counter_index]);
442 metrics->counters[group_index][counter_index] += value;
447 accumulate_program_time (metrics_op_t op, unsigned time_ns)
449 context_t *ctx = context_get_current ();
450 op_metrics_t *metrics;
452 metrics = ctx_get_op_metrics (ctx, op);
454 metrics->time_ns += time_ns;
457 typedef struct per_stage_metrics
459 op_metrics_t *metrics;
460 shader_stage_info_t *stage;
463 } per_stage_metrics_t;
466 _is_shader_stage_counter (metrics_info_t *info,
467 unsigned group_index,
468 unsigned counter_index)
470 shader_stage_info_t *stage;
473 for (i = 0; i < info->num_shader_stages; i++) {
474 stage = &info->stages[i];
476 if (stage->active_group_index == group_index &&
477 stage->active_counter_index == counter_index)
482 if (stage->stall_group_index == group_index &&
483 stage->stall_counter_index == counter_index)
493 print_per_stage_metrics (context_t *ctx,
494 per_stage_metrics_t *per_stage,
497 metrics_info_t *info = &ctx->metrics_info;
498 op_metrics_t *metric = per_stage->metrics;
499 metrics_group_info_t *group;
500 const char *op_string;
501 unsigned group_index, counter;
504 /* Don't print anything for stages with no alloted time. */
505 if (per_stage->time_ns == 0.0)
508 op_string = metrics_op_string (metric->op);
510 printf ("%21s", op_string);
512 if (metric->op >= METRICS_OP_SHADER) {
513 printf (" %3d", metric->op - METRICS_OP_SHADER);
519 if (per_stage->stage)
520 printf (" %cS:", per_stage->stage->name[0]);
524 printf ("\t%7.2f ms (%4.1f%%)",
525 per_stage->time_ns / 1e6,
526 per_stage->time_ns / total * 100);
528 if (per_stage->active)
529 printf (", %4.1f%% active", per_stage->active * 100);
533 /* I'm not seeing a lot of value printing the rest of these
534 * performance counters by default yet. Use --verbose to get
540 for (group_index = 0; group_index < info->num_groups; group_index++) {
541 group = &info->groups[group_index];
542 for (counter = 0; counter < group->num_counters; counter++) {
544 /* Don't print this counter value if it's a
545 * per-stage cycle counter, (which we have
546 * already accounted for). */
547 if (_is_shader_stage_counter (info, group_index, counter))
550 value = metric->counters[group_index][counter];
553 printf ("%s: %.2f ", group->counter_names[counter],
561 time_compare(const void *in_a, const void *in_b, void *arg unused)
563 const per_stage_metrics_t *a = in_a;
564 const per_stage_metrics_t *b = in_b;
567 if (a->time_ns < b->time_ns)
569 if (a->time_ns > b->time_ns)
575 print_program_metrics (void)
577 context_t *ctx = context_get_current ();
578 metrics_t *metrics = ctx->metrics;
579 metrics_info_t *info = &ctx->metrics_info;
580 unsigned num_shader_stages = info->num_shader_stages;
581 per_stage_metrics_t *sorted, *per_stage;
582 double total_time, op_cycles;
584 unsigned group_index, counter_index;
585 unsigned i, j, num_sorted;
587 /* Make a sorted list of the per-stage operations by time
588 * used, and figure out the total so we can print percentages.
590 num_sorted = metrics->num_op_metrics * num_shader_stages;
592 sorted = xmalloc (sizeof (*sorted) * num_sorted);
596 for (i = 0; i < metrics->num_op_metrics; i++) {
598 op = &metrics->op_metrics[i];
600 /* Accumulate total time across all ops. */
601 total_time += op->time_ns;
603 /* Also, find total cycles in all stages of this op. */
606 for (j = 0; j < num_shader_stages; j++) {
608 group_index = info->stages[j].active_group_index;
609 counter_index = info->stages[j].active_counter_index;
610 op_cycles += op->counters[group_index][counter_index];
613 group_index = info->stages[j].stall_group_index;
614 counter_index = info->stages[j].stall_counter_index;
615 op_cycles += op->counters[group_index][counter_index];
618 for (j = 0; j < num_shader_stages; j++) {
619 double active_cycles, stall_cycles, stage_cycles;
622 group_index = info->stages[j].active_group_index;
623 counter_index = info->stages[j].active_counter_index;
624 active_cycles = op->counters[group_index][counter_index];
627 group_index = info->stages[j].stall_group_index;
628 counter_index = info->stages[j].stall_counter_index;
629 stall_cycles = op->counters[group_index][counter_index];
631 stage_cycles = active_cycles + stall_cycles;
633 per_stage = &sorted[i * num_shader_stages + j];
634 per_stage->metrics = op;
637 per_stage->stage = &info->stages[j];
638 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
640 /* If we don't have any per-stage cycle counts
641 * for this operation, then use the first
642 * stage as a placeholder for all the time,
643 * but NULL-ify the stage info so that the
644 * report doesn't lie about this time being
645 * from any particular stage. */
646 per_stage->stage = NULL;
648 per_stage->time_ns = op->time_ns;
650 per_stage->time_ns = 0.0;
655 per_stage->active = active_cycles / stage_cycles;
657 per_stage->active = 0.0;
662 qsort_r (sorted, num_sorted, sizeof (*sorted),
663 time_compare, metrics->op_metrics);
665 for (i = 0; i < num_sorted; i++)
666 print_per_stage_metrics (ctx, &sorted[i], total_time);
671 /* Called at program exit.
673 * This is similar to metrics_info_fini, but only frees any used
674 * memory. Notably, it does not call any OpenGL functions, (since the
675 * OpenGL context no longer exists at program exit).
680 context_t *ctx = context_get_current ();
681 metrics_t *metrics = ctx->metrics;
682 metrics_info_t *info = &ctx->metrics_info;
684 timer_query_t *timer, *timer_next;
685 monitor_t *monitor, *monitor_next;
688 printf ("fips: terminating\n");
690 if (! info->initialized)
693 for (timer = metrics->timer_head;
697 timer_next = timer->next;
701 for (monitor = metrics->monitor_head;
703 monitor = monitor_next)
705 monitor_next = monitor->next;
709 for (i = 0; i < info->num_groups; i++) {
710 metrics_group_info_t *group = &info->groups[i];
712 for (j = 0; j < group->num_counters; i++)
713 free (group->counter_names[j]);
715 free (group->counter_types);
716 free (group->counter_names);
717 free (group->counter_ids);
724 for (i = 0; i < info->num_shader_stages; i++)
725 free (info->stages[i].name);
731 metrics_collect_available (void)
733 context_t *ctx = context_get_current ();
734 metrics_t *metrics = ctx->metrics;
736 /* Consume all timer queries that are ready. */
737 timer_query_t *timer = metrics->timer_head;
740 GLuint available, elapsed;
742 glGetQueryObjectuiv (timer->id,
743 GL_QUERY_RESULT_AVAILABLE, &available);
747 glGetQueryObjectuiv (timer->id,
748 GL_QUERY_RESULT, &elapsed);
750 accumulate_program_time (timer->op, elapsed);
752 metrics->timer_head = timer->next;
753 if (metrics->timer_head == NULL)
754 metrics->timer_tail = NULL;
756 glDeleteQueries (1, &timer->id);
759 timer = metrics->timer_head;
762 /* And similarly for all performance monitors that are ready. */
763 monitor_t *monitor = metrics->monitor_head;
766 GLuint available, result_size, *result;
769 glGetPerfMonitorCounterDataAMD (monitor->id,
770 GL_PERFMON_RESULT_AVAILABLE_AMD,
771 sizeof (available), &available,
776 glGetPerfMonitorCounterDataAMD (monitor->id,
777 GL_PERFMON_RESULT_SIZE_AMD,
778 sizeof (result_size),
781 result = xmalloc (result_size);
783 glGetPerfMonitorCounterDataAMD (monitor->id,
784 GL_PERFMON_RESULT_AMD,
788 accumulate_program_metrics (monitor->op, result, result_size);
792 metrics->monitor_head = monitor->next;
793 if (metrics->monitor_head == NULL)
794 metrics->monitor_tail = NULL;
796 glDeletePerfMonitorsAMD (1, &monitor->id);
800 metrics->monitors_in_flight--;
802 monitor = metrics->monitor_head;
808 metrics_end_frame (void)
810 static int initialized = 0;
811 static struct timeval tv_start, tv_now;
814 gettimeofday (&tv_start, NULL);
815 atexit (metrics_exit);
816 if (getenv ("FIPS_VERBOSE"))
823 metrics_collect_available ();
825 if (frames % 15 == 0) {
828 gettimeofday (&tv_now, NULL);
830 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
831 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
833 printf("FPS: %.3f\n", fps);
835 print_program_metrics ();