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 ();
92 /* Initialize the timer_query and monitor objects */
93 glGenQueries (1, &ctx->timer_begun_id);
95 glGenPerfMonitorsAMD (1, &ctx->monitor_begun_id);
97 for (i = 0; i < ctx->metrics_info.num_groups; i++)
99 metrics_group_info_t *group;
102 group = &ctx->metrics_info.groups[i];
104 num_counters = group->num_counters;
105 if (group->max_active_counters < group->num_counters)
107 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
108 group->max_active_counters,
109 group->num_counters, i);
110 num_counters = group->max_active_counters;
114 glSelectPerfMonitorCountersAMD(ctx->monitor_begun_id,
120 /* Start the queries */
121 glBeginQuery (GL_TIME_ELAPSED, ctx->timer_begun_id);
123 glBeginPerfMonitorAMD (ctx->monitor_begun_id);
127 metrics_counter_stop (void)
129 context_t *ctx = context_get_current ();
130 timer_query_t *timer;
133 /* Stop the current timer and monitor. */
134 glEndQuery (GL_TIME_ELAPSED);
135 glEndPerfMonitorAMD (ctx->monitor_begun_id);
137 /* Add these IDs to our lists of outstanding queries and
138 * monitors so the results can be collected later. */
139 timer = xmalloc (sizeof (timer_query_t));
142 timer->id = ctx->timer_begun_id;
145 if (ctx->timer_tail) {
146 ctx->timer_tail->next = timer;
147 ctx->timer_tail = timer;
149 ctx->timer_tail = timer;
150 ctx->timer_head = timer;
153 /* Create a new performance-monitor query */
154 monitor = xmalloc (sizeof (monitor_t));
156 monitor->op = ctx->op;
157 monitor->id = ctx->monitor_begun_id;
158 monitor->next = NULL;
160 if (ctx->monitor_tail) {
161 ctx->monitor_tail->next = monitor;
162 ctx->monitor_tail = monitor;
164 ctx->monitor_tail = monitor;
165 ctx->monitor_head = monitor;
168 ctx->monitors_in_flight++;
170 /* Avoid being a resource hog and collect outstanding results
171 * once we have sent off a large number of
172 * queries. (Presumably, many of the outstanding queries are
175 if (ctx->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
176 metrics_collect_available ();
180 metrics_set_current_op (metrics_op_t op)
182 context_t *ctx = context_get_current ();
188 metrics_get_current_op (void)
190 context_t *ctx = context_get_current ();
196 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
198 metrics_info_t *info = &ctx->metrics_info;
202 metrics->time_ns = 0.0;
204 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
206 for (i = 0; i < info->num_groups; i++) {
207 metrics->counters[i] = xmalloc (sizeof (double) *
208 info->groups[i].num_counters);
209 for (j = 0; j < info->groups[i].num_counters; j++)
210 metrics->counters[i][j] = 0.0;
214 static op_metrics_t *
215 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
219 if (op >= ctx->num_op_metrics)
221 ctx->op_metrics = realloc (ctx->op_metrics,
222 (op + 1) * sizeof (op_metrics_t));
223 for (i = ctx->num_op_metrics; i < op + 1; i++)
224 op_metrics_init (ctx, &ctx->op_metrics[i], i);
226 ctx->num_op_metrics = op + 1;
229 return &ctx->op_metrics[op];
233 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
235 #define CONSUME(var) \
236 if (p + sizeof(var) > ((unsigned char *) result) + size) \
238 fprintf (stderr, "Unexpected end-of-buffer while " \
239 "parsing results\n"); \
242 (var) = *((typeof(var) *) p); \
245 context_t *ctx = context_get_current ();
246 metrics_info_t *info = &ctx->metrics_info;
247 op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
248 unsigned char *p = (unsigned char *) result;
250 while (p < ((unsigned char *) result) + size)
252 GLuint group_id, group_index;
253 GLuint counter_id, counter_index;
254 metrics_group_info_t *group;
259 CONSUME (counter_id);
261 for (i = 0; i < info->num_groups; i++) {
262 if (info->groups[i].id == group_id)
266 assert (group_index < info->num_groups);
267 group = &info->groups[group_index];
269 for (i = 0; i < group->num_counters; i++) {
270 if (group->counter_ids[i] == counter_id)
274 assert (counter_index < group->num_counters);
276 switch (group->counter_types[counter_index])
279 uint64_t uint64_value;
281 case GL_UNSIGNED_INT:
282 CONSUME (uint_value);
285 case GL_UNSIGNED_INT64_AMD:
286 CONSUME (uint64_value);
287 value = uint64_value;
289 case GL_PERCENTAGE_AMD:
291 CONSUME (float_value);
295 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
296 group->counter_types[counter_index]);
301 metrics->counters[group_index][counter_index] += value;
306 accumulate_program_time (metrics_op_t op, unsigned time_ns)
308 context_t *ctx = context_get_current ();
309 op_metrics_t *metrics;
311 metrics = ctx_get_op_metrics (ctx, op);
313 metrics->time_ns += time_ns;
316 typedef struct per_stage_metrics
318 op_metrics_t *metrics;
319 shader_stage_info_t *stage;
322 } per_stage_metrics_t;
325 _is_shader_stage_counter (metrics_info_t *info,
326 unsigned group_index,
327 unsigned counter_index)
329 shader_stage_info_t *stage;
332 for (i = 0; i < info->num_shader_stages; i++) {
333 stage = &info->stages[i];
335 if (stage->active_group_index == group_index &&
336 stage->active_counter_index == counter_index)
341 if (stage->stall_group_index == group_index &&
342 stage->stall_counter_index == counter_index)
352 print_per_stage_metrics (context_t *ctx,
353 per_stage_metrics_t *per_stage,
356 metrics_info_t *info = &ctx->metrics_info;
357 op_metrics_t *metric = per_stage->metrics;
358 metrics_group_info_t *group;
359 const char *op_string;
360 unsigned group_index, counter;
363 /* Don't print anything for stages with no alloted time. */
364 if (per_stage->time_ns == 0.0)
367 op_string = metrics_op_string (metric->op);
369 printf ("%21s", op_string);
371 if (metric->op >= METRICS_OP_SHADER) {
372 printf (" %3d", metric->op - METRICS_OP_SHADER);
378 if (per_stage->stage)
379 printf (" %cS:", per_stage->stage->name[0]);
383 printf ("\t%7.2f ms (%4.1f%%)",
384 per_stage->time_ns / 1e6,
385 per_stage->time_ns / total * 100);
387 if (per_stage->active)
388 printf (", %4.1f%% active", per_stage->active * 100);
392 /* I'm not seeing a lot of value printing the rest of these
393 * performance counters by default yet. Use --verbose to get
399 for (group_index = 0; group_index < info->num_groups; group_index++) {
400 group = &info->groups[group_index];
401 for (counter = 0; counter < group->num_counters; counter++) {
403 /* Don't print this counter value if it's a
404 * per-stage cycle counter, (which we have
405 * already accounted for). */
406 if (_is_shader_stage_counter (info, group_index, counter))
409 value = metric->counters[group_index][counter];
412 printf ("%s: %.2f ", group->counter_names[counter],
420 time_compare(const void *in_a, const void *in_b, void *arg unused)
422 const per_stage_metrics_t *a = in_a;
423 const per_stage_metrics_t *b = in_b;
426 if (a->time_ns < b->time_ns)
428 if (a->time_ns > b->time_ns)
434 print_program_metrics (void)
436 context_t *ctx = context_get_current ();
437 metrics_info_t *info = &ctx->metrics_info;
438 unsigned num_shader_stages = info->num_shader_stages;
439 per_stage_metrics_t *sorted, *per_stage;
440 double total_time, op_cycles;
442 unsigned group_index, counter_index;
443 unsigned i, j, num_sorted;
445 /* Make a sorted list of the per-stage operations by time
446 * used, and figure out the total so we can print percentages.
448 num_sorted = ctx->num_op_metrics * num_shader_stages;
450 sorted = xmalloc (sizeof (*sorted) * num_sorted);
454 for (i = 0; i < ctx->num_op_metrics; i++) {
456 op = &ctx->op_metrics[i];
458 /* Accumulate total time across all ops. */
459 total_time += op->time_ns;
461 /* Also, find total cycles in all stages of this op. */
464 for (j = 0; j < num_shader_stages; j++) {
466 group_index = info->stages[j].active_group_index;
467 counter_index = info->stages[j].active_counter_index;
468 op_cycles += op->counters[group_index][counter_index];
471 group_index = info->stages[j].stall_group_index;
472 counter_index = info->stages[j].stall_counter_index;
473 op_cycles += op->counters[group_index][counter_index];
476 for (j = 0; j < num_shader_stages; j++) {
477 double active_cycles, stall_cycles, stage_cycles;
480 group_index = info->stages[j].active_group_index;
481 counter_index = info->stages[j].active_counter_index;
482 active_cycles = op->counters[group_index][counter_index];
485 group_index = info->stages[j].stall_group_index;
486 counter_index = info->stages[j].stall_counter_index;
487 stall_cycles = op->counters[group_index][counter_index];
489 stage_cycles = active_cycles + stall_cycles;
491 per_stage = &sorted[i * num_shader_stages + j];
492 per_stage->metrics = op;
495 per_stage->stage = &info->stages[j];
496 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
498 /* If we don't have any per-stage cycle counts
499 * for this operation, then use the first
500 * stage as a placeholder for all the time,
501 * but NULL-ify the stage info so that the
502 * report doesn't lie about this time being
503 * from any particular stage. */
504 per_stage->stage = NULL;
506 per_stage->time_ns = op->time_ns;
508 per_stage->time_ns = 0.0;
513 per_stage->active = active_cycles / stage_cycles;
515 per_stage->active = 0.0;
520 qsort_r (sorted, num_sorted, sizeof (*sorted),
521 time_compare, ctx->op_metrics);
523 for (i = 0; i < num_sorted; i++)
524 print_per_stage_metrics (ctx, &sorted[i], total_time);
529 /* Called at program exit.
531 * This is similar to metrics_info_fini, but only frees any used
532 * memory. Notably, it does not call any OpenGL functions, (since the
533 * OpenGL context no longer exists at program exit).
538 context_t *ctx = context_get_current ();
539 metrics_info_t *info = &ctx->metrics_info;
541 timer_query_t *timer, *timer_next;
542 monitor_t *monitor, *monitor_next;
545 printf ("fips: terminating\n");
547 if (! info->initialized)
550 for (timer = ctx->timer_head;
554 timer_next = timer->next;
558 for (monitor = ctx->monitor_head;
560 monitor = monitor_next)
562 monitor_next = monitor->next;
566 for (i = 0; i < info->num_groups; i++) {
567 metrics_group_info_t *group = &info->groups[i];
569 for (j = 0; j < group->num_counters; i++)
570 free (group->counter_names[j]);
572 free (group->counter_types);
573 free (group->counter_names);
574 free (group->counter_ids);
581 for (i = 0; i < info->num_shader_stages; i++)
582 free (info->stages[i].name);
588 metrics_collect_available (void)
590 context_t *ctx = context_get_current ();
592 /* Consume all timer queries that are ready. */
593 timer_query_t *timer = ctx->timer_head;
596 GLuint available, elapsed;
598 glGetQueryObjectuiv (timer->id,
599 GL_QUERY_RESULT_AVAILABLE, &available);
603 glGetQueryObjectuiv (timer->id,
604 GL_QUERY_RESULT, &elapsed);
606 accumulate_program_time (timer->op, elapsed);
608 ctx->timer_head = timer->next;
609 if (ctx->timer_head == NULL)
610 ctx->timer_tail = NULL;
612 glDeleteQueries (1, &timer->id);
615 timer = ctx->timer_head;
618 /* And similarly for all performance monitors that are ready. */
619 monitor_t *monitor = ctx->monitor_head;
622 GLuint available, result_size, *result;
625 glGetPerfMonitorCounterDataAMD (monitor->id,
626 GL_PERFMON_RESULT_AVAILABLE_AMD,
627 sizeof (available), &available,
632 glGetPerfMonitorCounterDataAMD (monitor->id,
633 GL_PERFMON_RESULT_SIZE_AMD,
634 sizeof (result_size),
637 result = xmalloc (result_size);
639 glGetPerfMonitorCounterDataAMD (monitor->id,
640 GL_PERFMON_RESULT_AMD,
644 accumulate_program_metrics (monitor->op, result, result_size);
648 ctx->monitor_head = monitor->next;
649 if (ctx->monitor_head == NULL)
650 ctx->monitor_tail = NULL;
652 glDeletePerfMonitorsAMD (1, &monitor->id);
656 ctx->monitors_in_flight--;
658 monitor = ctx->monitor_head;
664 metrics_end_frame (void)
666 static int initialized = 0;
667 static struct timeval tv_start, tv_now;
670 gettimeofday (&tv_start, NULL);
671 atexit (metrics_exit);
672 if (getenv ("FIPS_VERBOSE"))
679 metrics_collect_available ();
681 if (frames % 15 == 0) {
684 gettimeofday (&tv_now, NULL);
686 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
687 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
689 printf("FPS: %.3f\n", fps);
691 print_program_metrics ();