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
34 #include "fips-dispatch-gl.h"
38 #include "metrics-info.h"
44 #define MAX_MONITORS_IN_FLIGHT 1000
47 typedef struct timer_query
52 struct timer_query *next;
55 /* Performance-monitor query */
56 typedef struct monitor
64 typedef struct op_metrics
66 /* This happens to also be the index into the
67 * metrics->op_metrics array currently
77 /* Description of all available peformance counters, counter
78 * groups, their names and IDs, etc. */
81 /* The current operation being measured. */
84 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
86 unsigned timer_begun_id;
88 /* GL_TIME_ELAPSED queries for which glEndQuery has been
89 * called, (but results have not yet been queried). */
90 timer_query_t *timer_head;
91 timer_query_t *timer_tail;
93 /* Performance monitor for which glEndPerfMonitorAMD has not
95 unsigned monitor_begun_id;
97 /* Performance monitors for which glEndPerfMonitorAMD has
98 * been called, (but results have not yet been queried). */
99 monitor_t *monitor_head;
100 monitor_t *monitor_tail;
102 int monitors_in_flight;
104 unsigned num_op_metrics;
105 op_metrics_t *op_metrics;
107 /* Per-frame time and latency measurement. */
108 GLint64 swap_begin_timestamp;
109 unsigned swap_end_timestamp_id;
110 GLint64 previous_swap_end_timestamp;
112 struct timespec previous_cpu_time_ts;
113 struct timespec cpu_time_ts;
117 metrics_create (metrics_info_t *info)
121 metrics = xmalloc (sizeof (metrics_t));
123 metrics->info = info;
127 metrics->timer_begun_id = 0;
129 metrics->timer_head = NULL;
130 metrics->timer_tail = NULL;
132 metrics->monitor_begun_id = 0;
134 metrics->monitor_head = NULL;
135 metrics->monitor_tail = NULL;
137 metrics->monitors_in_flight = 0;
139 metrics->num_op_metrics = 0;
140 metrics->op_metrics = NULL;
142 glGenQueries (1, &metrics->swap_end_timestamp_id);
144 /* Get the first frame timestamp started immediately. */
145 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
151 metrics_fini (metrics_t *metrics)
153 timer_query_t *timer, *timer_next;
154 monitor_t *monitor, *monitor_next;
156 /* Discard and cleanup any outstanding queries. */
157 if (metrics->timer_begun_id) {
158 glEndQuery (GL_TIME_ELAPSED);
159 glDeleteQueries (1, &metrics->timer_begun_id);
160 metrics->timer_begun_id = 0;
163 for (timer = metrics->timer_head;
167 glDeleteQueries (1, &timer->id);
168 timer_next = timer->next;
171 metrics->timer_head = NULL;
172 metrics->timer_tail = NULL;
174 if (metrics->info->have_perfmon) {
176 if (metrics->monitor_begun_id) {
177 glEndPerfMonitorAMD (metrics->monitor_begun_id);
178 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
179 metrics->monitor_begun_id = 0;
182 for (monitor = metrics->monitor_head;
184 monitor = monitor_next)
186 glDeletePerfMonitorsAMD (1, &monitor->id);
187 monitor_next = monitor->next;
190 metrics->monitor_head = NULL;
191 metrics->monitor_tail = NULL;
195 metrics->monitors_in_flight = 0;
199 metrics_destroy (metrics_t *metrics)
201 metrics_fini (metrics);
207 metrics_op_string (metrics_op_t op)
209 if (op >= METRICS_OP_SHADER)
210 return "Shader program";
214 case METRICS_OP_ACCUM:
215 return "glAccum*(+)";
216 case METRICS_OP_BUFFER_DATA:
217 return "glBufferData(+)";
218 case METRICS_OP_BUFFER_SUB_DATA:
219 return "glCopyBufferSubData*";
220 case METRICS_OP_BITMAP:
222 case METRICS_OP_BLIT_FRAMEBUFFER:
223 return "glBlitFramebuffer*";
224 case METRICS_OP_CLEAR:
226 case METRICS_OP_CLEAR_BUFFER_DATA:
227 return "glCearBufferData(+)";
228 case METRICS_OP_CLEAR_TEX_IMAGE:
229 return "glClearTexImage(+)";
230 case METRICS_OP_COPY_PIXELS:
231 return "glCopyPixels";
232 case METRICS_OP_COPY_TEX_IMAGE:
233 return "glCopyTexImage(+)";
234 case METRICS_OP_DRAW_PIXELS:
235 return "glDrawPixels";
236 case METRICS_OP_GET_TEX_IMAGE:
237 return "glGetTexImage(+)";
238 case METRICS_OP_READ_PIXELS:
239 return "glReadPixels*";
240 case METRICS_OP_TEX_IMAGE:
241 return "glTexImage*(+)";
243 fprintf (stderr, "fips: Internal error: "
244 "Unknown metrics op value: %d\n", op);
252 metrics_counter_start (metrics_t *metrics)
256 /* Initialize the timer_query object. */
257 glGenQueries (1, &metrics->timer_begun_id);
259 /* Most everything else in this function is
260 * performance-monitor related. If we don't have that
261 * extension, just start the timer query and be done. */
262 if (! metrics->info->have_perfmon) {
263 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
267 /* Initialize the performance-monitor object */
268 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
270 for (i = 0; i < metrics->info->num_groups; i++)
272 metrics_group_info_t *group;
275 group = &metrics->info->groups[i];
277 num_counters = group->num_counters;
278 if (group->max_active_counters < group->num_counters)
280 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
281 group->max_active_counters,
282 group->num_counters, i);
283 num_counters = group->max_active_counters;
287 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
293 /* Start the queries */
294 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
296 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
300 metrics_counter_stop (metrics_t *metrics)
302 timer_query_t *timer;
305 /* Stop the current timer and monitor. */
306 glEndQuery (GL_TIME_ELAPSED);
308 if (metrics->info->have_perfmon)
309 glEndPerfMonitorAMD (metrics->monitor_begun_id);
311 /* Add these IDs to our lists of outstanding queries and
312 * monitors so the results can be collected later. */
313 timer = xmalloc (sizeof (timer_query_t));
315 timer->op = metrics->op;
316 timer->id = metrics->timer_begun_id;
319 if (metrics->timer_tail) {
320 metrics->timer_tail->next = timer;
321 metrics->timer_tail = timer;
323 metrics->timer_tail = timer;
324 metrics->timer_head = timer;
327 if (metrics->info->have_perfmon) {
328 /* Create a new performance-monitor query */
329 monitor = xmalloc (sizeof (monitor_t));
331 monitor->op = metrics->op;
332 monitor->id = metrics->monitor_begun_id;
333 monitor->next = NULL;
335 if (metrics->monitor_tail) {
336 metrics->monitor_tail->next = monitor;
337 metrics->monitor_tail = monitor;
339 metrics->monitor_tail = monitor;
340 metrics->monitor_head = monitor;
344 metrics->monitors_in_flight++;
346 /* Avoid being a resource hog and collect outstanding results
347 * once we have sent off a large number of
348 * queries. (Presumably, many of the outstanding queries are
351 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
352 metrics_collect_available (metrics);
356 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
362 metrics_get_current_op (metrics_t *metrics)
368 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
373 metrics->time_ns = 0.0;
375 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
377 for (i = 0; i < info->num_groups; i++) {
378 metrics->counters[i] = xmalloc (sizeof (double) *
379 info->groups[i].num_counters);
380 for (j = 0; j < info->groups[i].num_counters; j++)
381 metrics->counters[i][j] = 0.0;
385 static op_metrics_t *
386 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
390 if (op >= metrics->num_op_metrics)
392 metrics->op_metrics = realloc (metrics->op_metrics,
393 (op + 1) * sizeof (op_metrics_t));
394 for (i = metrics->num_op_metrics; i < op + 1; i++)
395 op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
397 metrics->num_op_metrics = op + 1;
400 return &metrics->op_metrics[op];
404 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
405 GLuint *result, GLuint size)
407 #define CONSUME(var) \
408 if (p + sizeof(var) > ((unsigned char *) result) + size) \
410 fprintf (stderr, "Unexpected end-of-buffer while " \
411 "parsing results\n"); \
415 (var) = *((typeof(var) *) p); \
418 metrics_info_t *info = metrics->info;
419 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
420 unsigned char *p = (unsigned char *) result;
422 while (p < ((unsigned char *) result) + size)
424 GLuint group_id, group_index;
425 GLuint counter_id, counter_index;
426 metrics_group_info_t *group;
431 CONSUME (counter_id);
433 for (i = 0; i < info->num_groups; i++) {
434 if (info->groups[i].id == group_id)
438 assert (group_index < info->num_groups);
439 group = &info->groups[group_index];
441 for (i = 0; i < group->num_counters; i++) {
442 if (group->counter_ids[i] == counter_id)
446 assert (counter_index < group->num_counters);
448 switch (group->counter_types[counter_index])
451 uint64_t uint64_value;
453 case GL_UNSIGNED_INT:
454 CONSUME (uint_value);
457 case GL_UNSIGNED_INT64_AMD:
458 CONSUME (uint64_value);
459 value = uint64_value;
461 case GL_PERCENTAGE_AMD:
463 CONSUME (float_value);
467 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
468 group->counter_types[counter_index]);
473 op_metrics->counters[group_index][counter_index] += value;
478 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
480 op_metrics_t *op_metrics;
482 op_metrics = _get_op_metrics (metrics, op);
484 op_metrics->time_ns += time_ns;
487 typedef struct per_stage_metrics
489 op_metrics_t *metrics;
490 shader_stage_info_t *stage;
493 } per_stage_metrics_t;
496 _is_shader_stage_counter (metrics_info_t *info,
497 unsigned group_index,
498 unsigned counter_index)
500 shader_stage_info_t *stage;
503 for (i = 0; i < info->num_shader_stages; i++) {
504 stage = &info->stages[i];
506 if (stage->active_group_index == group_index &&
507 stage->active_counter_index == counter_index)
512 if (stage->stall_group_index == group_index &&
513 stage->stall_counter_index == counter_index)
523 print_per_stage_metrics (metrics_t *metrics,
524 per_stage_metrics_t *per_stage,
527 metrics_info_t *info = metrics->info;
528 op_metrics_t *op_metrics = per_stage->metrics;
529 metrics_group_info_t *group;
530 const char *op_string;
531 unsigned group_index, counter;
534 /* Don't print anything for stages with no alloted time. */
535 if (per_stage->time_ns == 0.0)
538 op_string = metrics_op_string (op_metrics->op);
540 printf ("%21s", op_string);
542 if (op_metrics->op >= METRICS_OP_SHADER) {
543 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
549 if (per_stage->stage)
550 printf (" %cS:", per_stage->stage->name[0]);
554 printf ("\t%7.2f ms (%4.1f%%)",
555 per_stage->time_ns / 1e6,
556 per_stage->time_ns / total * 100);
558 if (per_stage->active)
559 printf (", %4.1f%% active", per_stage->active * 100);
563 /* I'm not seeing a lot of value printing the rest of these
564 * performance counters by default yet. Use --verbose to get
570 for (group_index = 0; group_index < info->num_groups; group_index++) {
571 group = &info->groups[group_index];
572 for (counter = 0; counter < group->num_counters; counter++) {
574 /* Don't print this counter value if it's a
575 * per-stage cycle counter, (which we have
576 * already accounted for). */
577 if (_is_shader_stage_counter (info, group_index, counter))
580 value = op_metrics->counters[group_index][counter];
583 printf ("%s: %.2f ", group->counter_names[counter],
591 time_compare(const void *in_a, const void *in_b, void *arg unused)
593 const per_stage_metrics_t *a = in_a;
594 const per_stage_metrics_t *b = in_b;
597 if (a->time_ns < b->time_ns)
599 if (a->time_ns > b->time_ns)
605 print_program_metrics (metrics_t *metrics)
607 metrics_info_t *info = metrics->info;
608 unsigned num_shader_stages = info->num_shader_stages;
609 per_stage_metrics_t *sorted, *per_stage;
610 double total_time, op_cycles;
612 unsigned group_index, counter_index;
613 unsigned i, j, num_sorted;
615 if (! info->have_perfmon &&
616 ! info->printed_missing_perfmon_warning)
619 "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
620 " Some fips features, (such as per-shader-stage timings),\n"
621 " will not be available.\n");
623 info->printed_missing_perfmon_warning = true;
626 /* Make a sorted list of the per-stage operations by time
627 * used, and figure out the total so we can print percentages.
629 if (num_shader_stages)
630 num_sorted = metrics->num_op_metrics * num_shader_stages;
632 num_sorted = metrics->num_op_metrics;
634 sorted = xmalloc (sizeof (*sorted) * num_sorted);
638 for (i = 0; i < metrics->num_op_metrics; i++) {
640 op = &metrics->op_metrics[i];
642 /* Accumulate total time across all ops. */
643 total_time += op->time_ns;
645 /* Also, find total cycles in all stages of this op. */
648 if (num_shader_stages == 0) {
649 per_stage = &sorted[i];
650 per_stage->metrics = op;
651 per_stage->stage = NULL;
652 per_stage->time_ns = op->time_ns;
653 per_stage->active = 0.0;
656 for (j = 0; j < num_shader_stages; j++) {
658 group_index = info->stages[j].active_group_index;
659 counter_index = info->stages[j].active_counter_index;
660 op_cycles += op->counters[group_index][counter_index];
663 group_index = info->stages[j].stall_group_index;
664 counter_index = info->stages[j].stall_counter_index;
665 op_cycles += op->counters[group_index][counter_index];
668 for (j = 0; j < num_shader_stages; j++) {
669 double active_cycles, stall_cycles, stage_cycles;
672 group_index = info->stages[j].active_group_index;
673 counter_index = info->stages[j].active_counter_index;
674 active_cycles = op->counters[group_index][counter_index];
677 group_index = info->stages[j].stall_group_index;
678 counter_index = info->stages[j].stall_counter_index;
679 stall_cycles = op->counters[group_index][counter_index];
681 stage_cycles = active_cycles + stall_cycles;
683 per_stage = &sorted[i * num_shader_stages + j];
684 per_stage->metrics = op;
687 per_stage->stage = &info->stages[j];
688 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
690 /* If we don't have any per-stage cycle counts
691 * for this operation, then use the first
692 * stage as a placeholder for all the time,
693 * but NULL-ify the stage info so that the
694 * report doesn't lie about this time being
695 * from any particular stage. */
696 per_stage->stage = NULL;
698 per_stage->time_ns = op->time_ns;
700 per_stage->time_ns = 0.0;
705 per_stage->active = active_cycles / stage_cycles;
707 per_stage->active = 0.0;
712 qsort_r (sorted, num_sorted, sizeof (*sorted),
713 time_compare, metrics->op_metrics);
715 for (i = 0; i < num_sorted; i++)
716 print_per_stage_metrics (metrics, &sorted[i], total_time);
722 metrics_collect_available (metrics_t *metrics)
724 /* Consume all timer queries that are ready. */
725 timer_query_t *timer = metrics->timer_head;
728 GLuint available, elapsed;
730 glGetQueryObjectuiv (timer->id,
731 GL_QUERY_RESULT_AVAILABLE, &available);
735 glGetQueryObjectuiv (timer->id,
736 GL_QUERY_RESULT, &elapsed);
738 accumulate_program_time (metrics, timer->op, elapsed);
740 metrics->timer_head = timer->next;
741 if (metrics->timer_head == NULL)
742 metrics->timer_tail = NULL;
744 glDeleteQueries (1, &timer->id);
747 timer = metrics->timer_head;
750 if (! metrics->info->have_perfmon)
753 /* And similarly for all performance monitors that are ready. */
754 monitor_t *monitor = metrics->monitor_head;
757 GLuint available, result_size, *result;
760 glGetPerfMonitorCounterDataAMD (monitor->id,
761 GL_PERFMON_RESULT_AVAILABLE_AMD,
762 sizeof (available), &available,
767 glGetPerfMonitorCounterDataAMD (monitor->id,
768 GL_PERFMON_RESULT_SIZE_AMD,
769 sizeof (result_size),
772 result = xmalloc (result_size);
774 glGetPerfMonitorCounterDataAMD (monitor->id,
775 GL_PERFMON_RESULT_AMD,
779 accumulate_program_metrics (metrics, monitor->op, result, result_size);
783 metrics->monitor_head = monitor->next;
784 if (metrics->monitor_head == NULL)
785 metrics->monitor_tail = NULL;
787 glDeletePerfMonitorsAMD (1, &monitor->id);
791 metrics->monitors_in_flight--;
793 monitor = metrics->monitor_head;
797 /* Return the difference from ts0 to ts1 as floating-point seconds. */
798 #define TIMESPEC_DIFF(ts0, ts1) (double) (ts1.tv_sec - ts0.tv_sec + \
799 (ts1.tv_nsec - ts0.tv_nsec) / 1e9)
802 metrics_end_frame_pre_swap (metrics_t *metrics)
804 GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
807 /* Don't leave any counters running over the end_frame work we
808 * do here. The counters will be started again at the end of
809 * metrics_end_frame_post_swap. */
811 metrics_counter_stop (metrics);
813 /* Now that an entire frame's worth of content has gone by, we
814 * can be sure that the timer quiery for the previous frame's
817 * Note: The only case in which this query isn't immediately
818 * available would be if there was effectively nothing in this
819 * frame. In that case, triggering a block on this query
820 * result is not a concern (since an empty frame is not
821 * interesting, and all of our counters are stopped anyway).
823 * Accepting this one block is much better than adding a
824 * linked list for these results that would reaslisticly never
825 * have more than one entry anyway.
828 glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
829 GL_QUERY_RESULT, &swap_end_timestamp);
833 printf ("# frame: Frame_Number Frame_Time_milliseconds Frame_latency_milliseconds CPU_load GPU_load\n");
835 /* Subtract previous frame's times to get frame times. */
836 frame_time_ns = swap_end_timestamp - metrics->previous_swap_end_timestamp;
838 latency_ns = swap_end_timestamp - metrics->swap_begin_timestamp;
840 cpu_time = TIMESPEC_DIFF (metrics->previous_cpu_time_ts, metrics->cpu_time_ts);
842 /* We've waited one frame to ensure we have a timestamp
843 * result. So the time we've actually measured here is
844 * for the previous frame. */
845 printf ("frame: %d %g %g %g %g\n",
847 (double) frame_time_ns / 1e6,
848 (double) latency_ns / 1e6,
849 cpu_time / (frame_time_ns / 1e9),
853 metrics->previous_swap_end_timestamp = swap_end_timestamp;
854 metrics->previous_cpu_time_ts = metrics->cpu_time_ts;
856 /* Before the actual swap call, we get the current timestamp
857 * value. This is a synchronous get so we can use this as the
858 * baseline for a frame latency measurment. */
860 glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
864 metrics_end_frame_post_swap (metrics_t *metrics)
866 static int initialized = 0;
867 static struct timeval tv_start, tv_now;
870 /* Now that the swap command has been queued, we issue an
871 * asynchronous query of the timestamp value. Comparing this
872 * to the synchronous get we just sent in
873 * metrics_end_frame_pre_swap allows us to measure the
874 * per-frame swap latency. */
876 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
878 /* Query the total amount of CPU time spent by this process. */
879 err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
881 fprintf (stderr, "Failed to query CPU time with clock_gettime: %s\n",
887 gettimeofday (&tv_start, NULL);
888 if (getenv ("FIPS_VERBOSE"))
893 metrics_collect_available (metrics);
897 if (frames % 15 == 0) {
900 gettimeofday (&tv_now, NULL);
902 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
903 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
905 printf("FPS: %.3f\n", fps);
907 print_program_metrics (metrics);
910 /* Start the counter up again now that we're done. */
911 metrics_counter_start (metrics);