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 metrics_op_string (metrics_op_t op)
44 if (op >= METRICS_OP_SHADER)
45 return "Shader program";
49 case METRICS_OP_ACCUM:
51 case METRICS_OP_BUFFER_DATA:
52 return "glBufferData(+)";
53 case METRICS_OP_BUFFER_SUB_DATA:
54 return "glCopyBufferSubData*";
55 case METRICS_OP_BITMAP:
57 case METRICS_OP_BLIT_FRAMEBUFFER:
58 return "glBlitFramebuffer*";
59 case METRICS_OP_CLEAR:
61 case METRICS_OP_CLEAR_BUFFER_DATA:
62 return "glCearBufferData(+)";
63 case METRICS_OP_CLEAR_TEX_IMAGE:
64 return "glClearTexImage(+)";
65 case METRICS_OP_COPY_PIXELS:
66 return "glCopyPixels";
67 case METRICS_OP_COPY_TEX_IMAGE:
68 return "glCopyTexImage(+)";
69 case METRICS_OP_DRAW_PIXELS:
70 return "glDrawPixels";
71 case METRICS_OP_GET_TEX_IMAGE:
72 return "glGetTexImage(+)";
73 case METRICS_OP_READ_PIXELS:
74 return "glReadPixels*";
75 case METRICS_OP_TEX_IMAGE:
76 return "glTexImage*(+)";
78 fprintf (stderr, "fips: Internal error: "
79 "Unknown metrics op value: %d\n", op);
87 metrics_counter_start (void)
89 context_t *ctx = context_get_current ();
90 metrics_t *metrics = &ctx->metrics;
93 /* Initialize the timer_query and monitor objects */
94 glGenQueries (1, &metrics->timer_begun_id);
96 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
98 for (i = 0; i < ctx->metrics_info.num_groups; i++)
100 metrics_group_info_t *group;
103 group = &ctx->metrics_info.groups[i];
105 num_counters = group->num_counters;
106 if (group->max_active_counters < group->num_counters)
108 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
109 group->max_active_counters,
110 group->num_counters, i);
111 num_counters = group->max_active_counters;
115 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
121 /* Start the queries */
122 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
124 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
128 metrics_counter_stop (void)
130 context_t *ctx = context_get_current ();
131 metrics_t *metrics = &ctx->metrics;
132 timer_query_t *timer;
135 /* Stop the current timer and monitor. */
136 glEndQuery (GL_TIME_ELAPSED);
137 glEndPerfMonitorAMD (metrics->monitor_begun_id);
139 /* Add these IDs to our lists of outstanding queries and
140 * monitors so the results can be collected later. */
141 timer = xmalloc (sizeof (timer_query_t));
143 timer->op = metrics->op;
144 timer->id = metrics->timer_begun_id;
147 if (metrics->timer_tail) {
148 metrics->timer_tail->next = timer;
149 metrics->timer_tail = timer;
151 metrics->timer_tail = timer;
152 metrics->timer_head = timer;
155 /* Create a new performance-monitor query */
156 monitor = xmalloc (sizeof (monitor_t));
158 monitor->op = metrics->op;
159 monitor->id = metrics->monitor_begun_id;
160 monitor->next = NULL;
162 if (metrics->monitor_tail) {
163 metrics->monitor_tail->next = monitor;
164 metrics->monitor_tail = monitor;
166 metrics->monitor_tail = monitor;
167 metrics->monitor_head = monitor;
170 metrics->monitors_in_flight++;
172 /* Avoid being a resource hog and collect outstanding results
173 * once we have sent off a large number of
174 * queries. (Presumably, many of the outstanding queries are
177 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
178 metrics_collect_available ();
182 metrics_set_current_op (metrics_op_t op)
184 context_t *ctx = context_get_current ();
185 metrics_t *metrics = &ctx->metrics;
191 metrics_get_current_op (void)
193 context_t *ctx = context_get_current ();
194 metrics_t *metrics = &ctx->metrics;
200 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
202 metrics_info_t *info = &ctx->metrics_info;
206 metrics->time_ns = 0.0;
208 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
210 for (i = 0; i < info->num_groups; i++) {
211 metrics->counters[i] = xmalloc (sizeof (double) *
212 info->groups[i].num_counters);
213 for (j = 0; j < info->groups[i].num_counters; j++)
214 metrics->counters[i][j] = 0.0;
218 static op_metrics_t *
219 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
221 metrics_t *metrics = &ctx->metrics;
224 if (op >= metrics->num_op_metrics)
226 metrics->op_metrics = realloc (metrics->op_metrics,
227 (op + 1) * sizeof (op_metrics_t));
228 for (i = metrics->num_op_metrics; i < op + 1; i++)
229 op_metrics_init (ctx, &metrics->op_metrics[i], i);
231 metrics->num_op_metrics = op + 1;
234 return &metrics->op_metrics[op];
238 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
240 #define CONSUME(var) \
241 if (p + sizeof(var) > ((unsigned char *) result) + size) \
243 fprintf (stderr, "Unexpected end-of-buffer while " \
244 "parsing results\n"); \
247 (var) = *((typeof(var) *) p); \
250 context_t *ctx = context_get_current ();
251 metrics_info_t *info = &ctx->metrics_info;
252 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
253 unsigned char *p = (unsigned char *) result;
255 while (p < ((unsigned char *) result) + size)
257 GLuint group_id, group_index;
258 GLuint counter_id, counter_index;
259 metrics_group_info_t *group;
264 CONSUME (counter_id);
266 for (i = 0; i < info->num_groups; i++) {
267 if (info->groups[i].id == group_id)
271 assert (group_index < info->num_groups);
272 group = &info->groups[group_index];
274 for (i = 0; i < group->num_counters; i++) {
275 if (group->counter_ids[i] == counter_id)
279 assert (counter_index < group->num_counters);
281 switch (group->counter_types[counter_index])
284 uint64_t uint64_value;
286 case GL_UNSIGNED_INT:
287 CONSUME (uint_value);
290 case GL_UNSIGNED_INT64_AMD:
291 CONSUME (uint64_value);
292 value = uint64_value;
294 case GL_PERCENTAGE_AMD:
296 CONSUME (float_value);
300 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
301 group->counter_types[counter_index]);
306 metrics->counters[group_index][counter_index] += value;
311 accumulate_program_time (metrics_op_t op, unsigned time_ns)
313 context_t *ctx = context_get_current ();
314 op_metrics_t *metrics;
316 metrics = ctx_get_op_metrics (ctx, op);
318 metrics->time_ns += time_ns;
321 typedef struct per_stage_metrics
323 op_metrics_t *metrics;
324 shader_stage_info_t *stage;
327 } per_stage_metrics_t;
330 _is_shader_stage_counter (metrics_info_t *info,
331 unsigned group_index,
332 unsigned counter_index)
334 shader_stage_info_t *stage;
337 for (i = 0; i < info->num_shader_stages; i++) {
338 stage = &info->stages[i];
340 if (stage->active_group_index == group_index &&
341 stage->active_counter_index == counter_index)
346 if (stage->stall_group_index == group_index &&
347 stage->stall_counter_index == counter_index)
357 print_per_stage_metrics (context_t *ctx,
358 per_stage_metrics_t *per_stage,
361 metrics_info_t *info = &ctx->metrics_info;
362 op_metrics_t *metric = per_stage->metrics;
363 metrics_group_info_t *group;
364 const char *op_string;
365 unsigned group_index, counter;
368 /* Don't print anything for stages with no alloted time. */
369 if (per_stage->time_ns == 0.0)
372 op_string = metrics_op_string (metric->op);
374 printf ("%21s", op_string);
376 if (metric->op >= METRICS_OP_SHADER) {
377 printf (" %3d", metric->op - METRICS_OP_SHADER);
383 if (per_stage->stage)
384 printf (" %cS:", per_stage->stage->name[0]);
388 printf ("\t%7.2f ms (%4.1f%%)",
389 per_stage->time_ns / 1e6,
390 per_stage->time_ns / total * 100);
392 if (per_stage->active)
393 printf (", %4.1f%% active", per_stage->active * 100);
397 /* I'm not seeing a lot of value printing the rest of these
398 * performance counters by default yet. Use --verbose to get
404 for (group_index = 0; group_index < info->num_groups; group_index++) {
405 group = &info->groups[group_index];
406 for (counter = 0; counter < group->num_counters; counter++) {
408 /* Don't print this counter value if it's a
409 * per-stage cycle counter, (which we have
410 * already accounted for). */
411 if (_is_shader_stage_counter (info, group_index, counter))
414 value = metric->counters[group_index][counter];
417 printf ("%s: %.2f ", group->counter_names[counter],
425 time_compare(const void *in_a, const void *in_b, void *arg unused)
427 const per_stage_metrics_t *a = in_a;
428 const per_stage_metrics_t *b = in_b;
431 if (a->time_ns < b->time_ns)
433 if (a->time_ns > b->time_ns)
439 print_program_metrics (void)
441 context_t *ctx = context_get_current ();
442 metrics_t *metrics = &ctx->metrics;
443 metrics_info_t *info = &ctx->metrics_info;
444 unsigned num_shader_stages = info->num_shader_stages;
445 per_stage_metrics_t *sorted, *per_stage;
446 double total_time, op_cycles;
448 unsigned group_index, counter_index;
449 unsigned i, j, num_sorted;
451 /* Make a sorted list of the per-stage operations by time
452 * used, and figure out the total so we can print percentages.
454 num_sorted = metrics->num_op_metrics * num_shader_stages;
456 sorted = xmalloc (sizeof (*sorted) * num_sorted);
460 for (i = 0; i < metrics->num_op_metrics; i++) {
462 op = &metrics->op_metrics[i];
464 /* Accumulate total time across all ops. */
465 total_time += op->time_ns;
467 /* Also, find total cycles in all stages of this op. */
470 for (j = 0; j < num_shader_stages; j++) {
472 group_index = info->stages[j].active_group_index;
473 counter_index = info->stages[j].active_counter_index;
474 op_cycles += op->counters[group_index][counter_index];
477 group_index = info->stages[j].stall_group_index;
478 counter_index = info->stages[j].stall_counter_index;
479 op_cycles += op->counters[group_index][counter_index];
482 for (j = 0; j < num_shader_stages; j++) {
483 double active_cycles, stall_cycles, stage_cycles;
486 group_index = info->stages[j].active_group_index;
487 counter_index = info->stages[j].active_counter_index;
488 active_cycles = op->counters[group_index][counter_index];
491 group_index = info->stages[j].stall_group_index;
492 counter_index = info->stages[j].stall_counter_index;
493 stall_cycles = op->counters[group_index][counter_index];
495 stage_cycles = active_cycles + stall_cycles;
497 per_stage = &sorted[i * num_shader_stages + j];
498 per_stage->metrics = op;
501 per_stage->stage = &info->stages[j];
502 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
504 /* If we don't have any per-stage cycle counts
505 * for this operation, then use the first
506 * stage as a placeholder for all the time,
507 * but NULL-ify the stage info so that the
508 * report doesn't lie about this time being
509 * from any particular stage. */
510 per_stage->stage = NULL;
512 per_stage->time_ns = op->time_ns;
514 per_stage->time_ns = 0.0;
519 per_stage->active = active_cycles / stage_cycles;
521 per_stage->active = 0.0;
526 qsort_r (sorted, num_sorted, sizeof (*sorted),
527 time_compare, metrics->op_metrics);
529 for (i = 0; i < num_sorted; i++)
530 print_per_stage_metrics (ctx, &sorted[i], total_time);
535 /* Called at program exit.
537 * This is similar to metrics_info_fini, but only frees any used
538 * memory. Notably, it does not call any OpenGL functions, (since the
539 * OpenGL context no longer exists at program exit).
544 context_t *ctx = context_get_current ();
545 metrics_t *metrics = &ctx->metrics;
546 metrics_info_t *info = &ctx->metrics_info;
548 timer_query_t *timer, *timer_next;
549 monitor_t *monitor, *monitor_next;
552 printf ("fips: terminating\n");
554 if (! info->initialized)
557 for (timer = metrics->timer_head;
561 timer_next = timer->next;
565 for (monitor = metrics->monitor_head;
567 monitor = monitor_next)
569 monitor_next = monitor->next;
573 for (i = 0; i < info->num_groups; i++) {
574 metrics_group_info_t *group = &info->groups[i];
576 for (j = 0; j < group->num_counters; i++)
577 free (group->counter_names[j]);
579 free (group->counter_types);
580 free (group->counter_names);
581 free (group->counter_ids);
588 for (i = 0; i < info->num_shader_stages; i++)
589 free (info->stages[i].name);
595 metrics_collect_available (void)
597 context_t *ctx = context_get_current ();
598 metrics_t *metrics = &ctx->metrics;
600 /* Consume all timer queries that are ready. */
601 timer_query_t *timer = metrics->timer_head;
604 GLuint available, elapsed;
606 glGetQueryObjectuiv (timer->id,
607 GL_QUERY_RESULT_AVAILABLE, &available);
611 glGetQueryObjectuiv (timer->id,
612 GL_QUERY_RESULT, &elapsed);
614 accumulate_program_time (timer->op, elapsed);
616 metrics->timer_head = timer->next;
617 if (metrics->timer_head == NULL)
618 metrics->timer_tail = NULL;
620 glDeleteQueries (1, &timer->id);
623 timer = metrics->timer_head;
626 /* And similarly for all performance monitors that are ready. */
627 monitor_t *monitor = metrics->monitor_head;
630 GLuint available, result_size, *result;
633 glGetPerfMonitorCounterDataAMD (monitor->id,
634 GL_PERFMON_RESULT_AVAILABLE_AMD,
635 sizeof (available), &available,
640 glGetPerfMonitorCounterDataAMD (monitor->id,
641 GL_PERFMON_RESULT_SIZE_AMD,
642 sizeof (result_size),
645 result = xmalloc (result_size);
647 glGetPerfMonitorCounterDataAMD (monitor->id,
648 GL_PERFMON_RESULT_AMD,
652 accumulate_program_metrics (monitor->op, result, result_size);
656 metrics->monitor_head = monitor->next;
657 if (metrics->monitor_head == NULL)
658 metrics->monitor_tail = NULL;
660 glDeletePerfMonitorsAMD (1, &monitor->id);
664 metrics->monitors_in_flight--;
666 monitor = metrics->monitor_head;
672 metrics_end_frame (void)
674 static int initialized = 0;
675 static struct timeval tv_start, tv_now;
678 gettimeofday (&tv_start, NULL);
679 atexit (metrics_exit);
680 if (getenv ("FIPS_VERBOSE"))
687 metrics_collect_available ();
689 if (frames % 15 == 0) {
692 gettimeofday (&tv_now, NULL);
694 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
695 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
697 printf("FPS: %.3f\n", fps);
699 print_program_metrics ();
704 metrics_init (metrics_t *metrics)
708 metrics->timer_begun_id = 0;
710 metrics->timer_head = NULL;
711 metrics->timer_tail = NULL;
713 metrics->monitor_begun_id = 0;
715 metrics->monitor_head = NULL;
716 metrics->monitor_tail = NULL;
718 metrics->monitors_in_flight = 0;
720 metrics->num_op_metrics = 0;
721 metrics->op_metrics = NULL;
725 metrics_fini (metrics_t *metrics)
727 timer_query_t *timer, *timer_next;
728 monitor_t *monitor, *monitor_next;
730 /* Since these metrics are going away, let's first collect
731 * whatever results might already be available. */
732 metrics_collect_available ();
734 /* If, after collection, any queries are still outstanding,
735 * just give up and clean them up. */
736 if (metrics->timer_begun_id) {
737 glEndQuery (GL_TIME_ELAPSED);
738 glDeleteQueries (1, &metrics->timer_begun_id);
739 metrics->timer_begun_id = 0;
742 for (timer = metrics->timer_head;
746 glDeleteQueries (1, &timer->id);
747 timer_next = timer->next;
750 metrics->timer_head = NULL;
751 metrics->timer_tail = NULL;
753 if (metrics->monitor_begun_id) {
754 glEndPerfMonitorAMD (metrics->monitor_begun_id);
755 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
756 metrics->monitor_begun_id = 0;
759 for (monitor = metrics->monitor_head;
761 monitor = monitor_next)
763 glDeletePerfMonitorsAMD (1, &monitor->id);
764 monitor_next = monitor->next;
767 metrics->monitor_head = NULL;
768 metrics->monitor_tail = NULL;
770 metrics->monitors_in_flight = 0;