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 * metrics->op_metrics array currently
72 /* Description of all available peformance counters, counter
73 * groups, their names and IDs, etc. */
76 /* The current operation being measured. */
79 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
81 unsigned timer_begun_id;
83 /* GL_TIME_ELAPSED queries for which glEndQuery has been
84 * called, (but results have not yet been queried). */
85 timer_query_t *timer_head;
86 timer_query_t *timer_tail;
88 /* Performance monitor for which glEndPerfMonitorAMD has not
90 unsigned monitor_begun_id;
92 /* Performance monitors for which glEndPerfMonitorAMD has
93 * been called, (but results have not yet been queried). */
94 monitor_t *monitor_head;
95 monitor_t *monitor_tail;
97 int monitors_in_flight;
99 unsigned num_op_metrics;
100 op_metrics_t *op_metrics;
104 metrics_create (metrics_info_t *info)
108 metrics = xmalloc (sizeof (metrics_t));
110 metrics->info = info;
114 metrics->timer_begun_id = 0;
116 metrics->timer_head = NULL;
117 metrics->timer_tail = NULL;
119 metrics->monitor_begun_id = 0;
121 metrics->monitor_head = NULL;
122 metrics->monitor_tail = NULL;
124 metrics->monitors_in_flight = 0;
126 metrics->num_op_metrics = 0;
127 metrics->op_metrics = NULL;
133 metrics_fini (metrics_t *metrics)
135 timer_query_t *timer, *timer_next;
136 monitor_t *monitor, *monitor_next;
138 /* Discard and cleanup any outstanding queries. */
139 if (metrics->timer_begun_id) {
140 glEndQuery (GL_TIME_ELAPSED);
141 glDeleteQueries (1, &metrics->timer_begun_id);
142 metrics->timer_begun_id = 0;
145 for (timer = metrics->timer_head;
149 glDeleteQueries (1, &timer->id);
150 timer_next = timer->next;
153 metrics->timer_head = NULL;
154 metrics->timer_tail = NULL;
156 if (metrics->monitor_begun_id) {
157 glEndPerfMonitorAMD (metrics->monitor_begun_id);
158 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
159 metrics->monitor_begun_id = 0;
162 for (monitor = metrics->monitor_head;
164 monitor = monitor_next)
166 glDeletePerfMonitorsAMD (1, &monitor->id);
167 monitor_next = monitor->next;
170 metrics->monitor_head = NULL;
171 metrics->monitor_tail = NULL;
173 metrics->monitors_in_flight = 0;
177 metrics_destroy (metrics_t *metrics)
179 metrics_fini (metrics);
185 metrics_op_string (metrics_op_t op)
187 if (op >= METRICS_OP_SHADER)
188 return "Shader program";
192 case METRICS_OP_ACCUM:
193 return "glAccum*(+)";
194 case METRICS_OP_BUFFER_DATA:
195 return "glBufferData(+)";
196 case METRICS_OP_BUFFER_SUB_DATA:
197 return "glCopyBufferSubData*";
198 case METRICS_OP_BITMAP:
200 case METRICS_OP_BLIT_FRAMEBUFFER:
201 return "glBlitFramebuffer*";
202 case METRICS_OP_CLEAR:
204 case METRICS_OP_CLEAR_BUFFER_DATA:
205 return "glCearBufferData(+)";
206 case METRICS_OP_CLEAR_TEX_IMAGE:
207 return "glClearTexImage(+)";
208 case METRICS_OP_COPY_PIXELS:
209 return "glCopyPixels";
210 case METRICS_OP_COPY_TEX_IMAGE:
211 return "glCopyTexImage(+)";
212 case METRICS_OP_DRAW_PIXELS:
213 return "glDrawPixels";
214 case METRICS_OP_GET_TEX_IMAGE:
215 return "glGetTexImage(+)";
216 case METRICS_OP_READ_PIXELS:
217 return "glReadPixels*";
218 case METRICS_OP_TEX_IMAGE:
219 return "glTexImage*(+)";
221 fprintf (stderr, "fips: Internal error: "
222 "Unknown metrics op value: %d\n", op);
230 metrics_counter_start (metrics_t *metrics)
234 /* Initialize the timer_query and monitor objects */
235 glGenQueries (1, &metrics->timer_begun_id);
237 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
239 for (i = 0; i < metrics->info->num_groups; i++)
241 metrics_group_info_t *group;
244 group = &metrics->info->groups[i];
246 num_counters = group->num_counters;
247 if (group->max_active_counters < group->num_counters)
249 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
250 group->max_active_counters,
251 group->num_counters, i);
252 num_counters = group->max_active_counters;
256 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
262 /* Start the queries */
263 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
265 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
269 metrics_counter_stop (metrics_t *metrics)
271 timer_query_t *timer;
274 /* Stop the current timer and monitor. */
275 glEndQuery (GL_TIME_ELAPSED);
276 glEndPerfMonitorAMD (metrics->monitor_begun_id);
278 /* Add these IDs to our lists of outstanding queries and
279 * monitors so the results can be collected later. */
280 timer = xmalloc (sizeof (timer_query_t));
282 timer->op = metrics->op;
283 timer->id = metrics->timer_begun_id;
286 if (metrics->timer_tail) {
287 metrics->timer_tail->next = timer;
288 metrics->timer_tail = timer;
290 metrics->timer_tail = timer;
291 metrics->timer_head = timer;
294 /* Create a new performance-monitor query */
295 monitor = xmalloc (sizeof (monitor_t));
297 monitor->op = metrics->op;
298 monitor->id = metrics->monitor_begun_id;
299 monitor->next = NULL;
301 if (metrics->monitor_tail) {
302 metrics->monitor_tail->next = monitor;
303 metrics->monitor_tail = monitor;
305 metrics->monitor_tail = monitor;
306 metrics->monitor_head = monitor;
309 metrics->monitors_in_flight++;
311 /* Avoid being a resource hog and collect outstanding results
312 * once we have sent off a large number of
313 * queries. (Presumably, many of the outstanding queries are
316 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
317 metrics_collect_available (metrics);
321 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
327 metrics_get_current_op (metrics_t *metrics)
333 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
338 metrics->time_ns = 0.0;
340 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
342 for (i = 0; i < info->num_groups; i++) {
343 metrics->counters[i] = xmalloc (sizeof (double) *
344 info->groups[i].num_counters);
345 for (j = 0; j < info->groups[i].num_counters; j++)
346 metrics->counters[i][j] = 0.0;
350 static op_metrics_t *
351 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
355 if (op >= metrics->num_op_metrics)
357 metrics->op_metrics = realloc (metrics->op_metrics,
358 (op + 1) * sizeof (op_metrics_t));
359 for (i = metrics->num_op_metrics; i < op + 1; i++)
360 op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
362 metrics->num_op_metrics = op + 1;
365 return &metrics->op_metrics[op];
369 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
370 GLuint *result, GLuint size)
372 #define CONSUME(var) \
373 if (p + sizeof(var) > ((unsigned char *) result) + size) \
375 fprintf (stderr, "Unexpected end-of-buffer while " \
376 "parsing results\n"); \
379 (var) = *((typeof(var) *) p); \
382 metrics_info_t *info = metrics->info;
383 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
384 unsigned char *p = (unsigned char *) result;
386 while (p < ((unsigned char *) result) + size)
388 GLuint group_id, group_index;
389 GLuint counter_id, counter_index;
390 metrics_group_info_t *group;
395 CONSUME (counter_id);
397 for (i = 0; i < info->num_groups; i++) {
398 if (info->groups[i].id == group_id)
402 assert (group_index < info->num_groups);
403 group = &info->groups[group_index];
405 for (i = 0; i < group->num_counters; i++) {
406 if (group->counter_ids[i] == counter_id)
410 assert (counter_index < group->num_counters);
412 switch (group->counter_types[counter_index])
415 uint64_t uint64_value;
417 case GL_UNSIGNED_INT:
418 CONSUME (uint_value);
421 case GL_UNSIGNED_INT64_AMD:
422 CONSUME (uint64_value);
423 value = uint64_value;
425 case GL_PERCENTAGE_AMD:
427 CONSUME (float_value);
431 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
432 group->counter_types[counter_index]);
437 op_metrics->counters[group_index][counter_index] += value;
442 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
444 op_metrics_t *op_metrics;
446 op_metrics = _get_op_metrics (metrics, op);
448 op_metrics->time_ns += time_ns;
451 typedef struct per_stage_metrics
453 op_metrics_t *metrics;
454 shader_stage_info_t *stage;
457 } per_stage_metrics_t;
460 _is_shader_stage_counter (metrics_info_t *info,
461 unsigned group_index,
462 unsigned counter_index)
464 shader_stage_info_t *stage;
467 for (i = 0; i < info->num_shader_stages; i++) {
468 stage = &info->stages[i];
470 if (stage->active_group_index == group_index &&
471 stage->active_counter_index == counter_index)
476 if (stage->stall_group_index == group_index &&
477 stage->stall_counter_index == counter_index)
487 print_per_stage_metrics (metrics_t *metrics,
488 per_stage_metrics_t *per_stage,
491 metrics_info_t *info = metrics->info;
492 op_metrics_t *op_metrics = per_stage->metrics;
493 metrics_group_info_t *group;
494 const char *op_string;
495 unsigned group_index, counter;
498 /* Don't print anything for stages with no alloted time. */
499 if (per_stage->time_ns == 0.0)
502 op_string = metrics_op_string (op_metrics->op);
504 printf ("%21s", op_string);
506 if (op_metrics->op >= METRICS_OP_SHADER) {
507 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
513 if (per_stage->stage)
514 printf (" %cS:", per_stage->stage->name[0]);
518 printf ("\t%7.2f ms (%4.1f%%)",
519 per_stage->time_ns / 1e6,
520 per_stage->time_ns / total * 100);
522 if (per_stage->active)
523 printf (", %4.1f%% active", per_stage->active * 100);
527 /* I'm not seeing a lot of value printing the rest of these
528 * performance counters by default yet. Use --verbose to get
534 for (group_index = 0; group_index < info->num_groups; group_index++) {
535 group = &info->groups[group_index];
536 for (counter = 0; counter < group->num_counters; counter++) {
538 /* Don't print this counter value if it's a
539 * per-stage cycle counter, (which we have
540 * already accounted for). */
541 if (_is_shader_stage_counter (info, group_index, counter))
544 value = op_metrics->counters[group_index][counter];
547 printf ("%s: %.2f ", group->counter_names[counter],
555 time_compare(const void *in_a, const void *in_b, void *arg unused)
557 const per_stage_metrics_t *a = in_a;
558 const per_stage_metrics_t *b = in_b;
561 if (a->time_ns < b->time_ns)
563 if (a->time_ns > b->time_ns)
569 print_program_metrics (metrics_t *metrics)
571 metrics_info_t *info = metrics->info;
572 unsigned num_shader_stages = info->num_shader_stages;
573 per_stage_metrics_t *sorted, *per_stage;
574 double total_time, op_cycles;
576 unsigned group_index, counter_index;
577 unsigned i, j, num_sorted;
579 /* Make a sorted list of the per-stage operations by time
580 * used, and figure out the total so we can print percentages.
582 num_sorted = metrics->num_op_metrics * num_shader_stages;
584 sorted = xmalloc (sizeof (*sorted) * num_sorted);
588 for (i = 0; i < metrics->num_op_metrics; i++) {
590 op = &metrics->op_metrics[i];
592 /* Accumulate total time across all ops. */
593 total_time += op->time_ns;
595 /* Also, find total cycles in all stages of this op. */
598 for (j = 0; j < num_shader_stages; j++) {
600 group_index = info->stages[j].active_group_index;
601 counter_index = info->stages[j].active_counter_index;
602 op_cycles += op->counters[group_index][counter_index];
605 group_index = info->stages[j].stall_group_index;
606 counter_index = info->stages[j].stall_counter_index;
607 op_cycles += op->counters[group_index][counter_index];
610 for (j = 0; j < num_shader_stages; j++) {
611 double active_cycles, stall_cycles, stage_cycles;
614 group_index = info->stages[j].active_group_index;
615 counter_index = info->stages[j].active_counter_index;
616 active_cycles = op->counters[group_index][counter_index];
619 group_index = info->stages[j].stall_group_index;
620 counter_index = info->stages[j].stall_counter_index;
621 stall_cycles = op->counters[group_index][counter_index];
623 stage_cycles = active_cycles + stall_cycles;
625 per_stage = &sorted[i * num_shader_stages + j];
626 per_stage->metrics = op;
629 per_stage->stage = &info->stages[j];
630 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
632 /* If we don't have any per-stage cycle counts
633 * for this operation, then use the first
634 * stage as a placeholder for all the time,
635 * but NULL-ify the stage info so that the
636 * report doesn't lie about this time being
637 * from any particular stage. */
638 per_stage->stage = NULL;
640 per_stage->time_ns = op->time_ns;
642 per_stage->time_ns = 0.0;
647 per_stage->active = active_cycles / stage_cycles;
649 per_stage->active = 0.0;
654 qsort_r (sorted, num_sorted, sizeof (*sorted),
655 time_compare, metrics->op_metrics);
657 for (i = 0; i < num_sorted; i++)
658 print_per_stage_metrics (metrics, &sorted[i], total_time);
664 metrics_collect_available (metrics_t *metrics)
666 /* Consume all timer queries that are ready. */
667 timer_query_t *timer = metrics->timer_head;
670 GLuint available, elapsed;
672 glGetQueryObjectuiv (timer->id,
673 GL_QUERY_RESULT_AVAILABLE, &available);
677 glGetQueryObjectuiv (timer->id,
678 GL_QUERY_RESULT, &elapsed);
680 accumulate_program_time (metrics, timer->op, elapsed);
682 metrics->timer_head = timer->next;
683 if (metrics->timer_head == NULL)
684 metrics->timer_tail = NULL;
686 glDeleteQueries (1, &timer->id);
689 timer = metrics->timer_head;
692 /* And similarly for all performance monitors that are ready. */
693 monitor_t *monitor = metrics->monitor_head;
696 GLuint available, result_size, *result;
699 glGetPerfMonitorCounterDataAMD (monitor->id,
700 GL_PERFMON_RESULT_AVAILABLE_AMD,
701 sizeof (available), &available,
706 glGetPerfMonitorCounterDataAMD (monitor->id,
707 GL_PERFMON_RESULT_SIZE_AMD,
708 sizeof (result_size),
711 result = xmalloc (result_size);
713 glGetPerfMonitorCounterDataAMD (monitor->id,
714 GL_PERFMON_RESULT_AMD,
718 accumulate_program_metrics (metrics, monitor->op, result, result_size);
722 metrics->monitor_head = monitor->next;
723 if (metrics->monitor_head == NULL)
724 metrics->monitor_tail = NULL;
726 glDeletePerfMonitorsAMD (1, &monitor->id);
730 metrics->monitors_in_flight--;
732 monitor = metrics->monitor_head;
738 metrics_end_frame (metrics_t *metrics)
740 static int initialized = 0;
741 static struct timeval tv_start, tv_now;
744 gettimeofday (&tv_start, NULL);
745 if (getenv ("FIPS_VERBOSE"))
752 metrics_collect_available (metrics);
754 if (frames % 15 == 0) {
757 gettimeofday (&tv_now, NULL);
759 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
760 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
762 printf("FPS: %.3f\n", fps);
764 print_program_metrics (metrics);