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;
116 /* Return (a - b) while accounting for the fact that between a and b,
117 * the counter may have wrapped around (with a maximum number of bits
118 * in the counter of GL_QUERY_COUNTER_BITS). */
120 subtract_timestamp (uint64_t a, uint64_t b)
122 GLint timestamp_bits;
124 glGetQueryiv (GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, ×tamp_bits);
126 /* Note: "<<" isn't defined for the number of bits in the
127 * type, and the compiler will punish you.
129 if (timestamp_bits == 64)
133 return (1ull << timestamp_bits) + a - b;
139 metrics_create (metrics_info_t *info)
143 metrics = xmalloc (sizeof (metrics_t));
145 metrics->info = info;
149 metrics->timer_begun_id = 0;
151 metrics->timer_head = NULL;
152 metrics->timer_tail = NULL;
154 metrics->monitor_begun_id = 0;
156 metrics->monitor_head = NULL;
157 metrics->monitor_tail = NULL;
159 metrics->monitors_in_flight = 0;
161 metrics->num_op_metrics = 0;
162 metrics->op_metrics = NULL;
164 glGenQueries (1, &metrics->swap_end_timestamp_id);
166 /* Get the first frame timestamp started immediately. */
167 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
169 /* As well as the initial CPU time. */
170 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
176 metrics_fini (metrics_t *metrics)
178 timer_query_t *timer, *timer_next;
179 monitor_t *monitor, *monitor_next;
181 /* Discard and cleanup any outstanding queries. */
182 if (metrics->timer_begun_id) {
183 glEndQuery (GL_TIME_ELAPSED);
184 glDeleteQueries (1, &metrics->timer_begun_id);
185 metrics->timer_begun_id = 0;
188 for (timer = metrics->timer_head;
192 glDeleteQueries (1, &timer->id);
193 timer_next = timer->next;
196 metrics->timer_head = NULL;
197 metrics->timer_tail = NULL;
199 if (metrics->info->have_perfmon) {
201 if (metrics->monitor_begun_id) {
202 glEndPerfMonitorAMD (metrics->monitor_begun_id);
203 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
204 metrics->monitor_begun_id = 0;
207 for (monitor = metrics->monitor_head;
209 monitor = monitor_next)
211 glDeletePerfMonitorsAMD (1, &monitor->id);
212 monitor_next = monitor->next;
215 metrics->monitor_head = NULL;
216 metrics->monitor_tail = NULL;
220 metrics->monitors_in_flight = 0;
224 metrics_destroy (metrics_t *metrics)
226 metrics_fini (metrics);
232 metrics_op_string (metrics_op_t op)
234 if (op >= METRICS_OP_SHADER)
235 return "Shader program";
239 case METRICS_OP_ACCUM:
240 return "glAccum*(+)";
241 case METRICS_OP_BUFFER_DATA:
242 return "glBufferData(+)";
243 case METRICS_OP_BUFFER_SUB_DATA:
244 return "glCopyBufferSubData*";
245 case METRICS_OP_BITMAP:
247 case METRICS_OP_BLIT_FRAMEBUFFER:
248 return "glBlitFramebuffer*";
249 case METRICS_OP_CLEAR:
251 case METRICS_OP_CLEAR_BUFFER_DATA:
252 return "glCearBufferData(+)";
253 case METRICS_OP_CLEAR_TEX_IMAGE:
254 return "glClearTexImage(+)";
255 case METRICS_OP_COPY_PIXELS:
256 return "glCopyPixels";
257 case METRICS_OP_COPY_TEX_IMAGE:
258 return "glCopyTexImage(+)";
259 case METRICS_OP_DRAW_PIXELS:
260 return "glDrawPixels";
261 case METRICS_OP_GET_TEX_IMAGE:
262 return "glGetTexImage(+)";
263 case METRICS_OP_READ_PIXELS:
264 return "glReadPixels*";
265 case METRICS_OP_TEX_IMAGE:
266 return "glTexImage*(+)";
268 fprintf (stderr, "fips: Internal error: "
269 "Unknown metrics op value: %d\n", op);
277 metrics_counter_start (metrics_t *metrics)
281 /* Initialize the timer_query object. */
282 glGenQueries (1, &metrics->timer_begun_id);
284 /* Most everything else in this function is
285 * performance-monitor related. If we don't have that
286 * extension, just start the timer query and be done. */
287 if (! metrics->info->have_perfmon) {
288 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
292 /* Initialize the performance-monitor object */
293 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
295 for (i = 0; i < metrics->info->num_groups; i++)
297 metrics_group_info_t *group;
300 group = &metrics->info->groups[i];
302 num_counters = group->num_counters;
303 if (group->max_active_counters < group->num_counters)
305 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
306 group->max_active_counters,
307 group->num_counters, i);
308 num_counters = group->max_active_counters;
312 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
318 /* Start the queries */
319 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
321 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
325 metrics_counter_stop (metrics_t *metrics)
327 timer_query_t *timer;
330 /* Stop the current timer and monitor. */
331 glEndQuery (GL_TIME_ELAPSED);
333 if (metrics->info->have_perfmon)
334 glEndPerfMonitorAMD (metrics->monitor_begun_id);
336 /* Add these IDs to our lists of outstanding queries and
337 * monitors so the results can be collected later. */
338 timer = xmalloc (sizeof (timer_query_t));
340 timer->op = metrics->op;
341 timer->id = metrics->timer_begun_id;
344 if (metrics->timer_tail) {
345 metrics->timer_tail->next = timer;
346 metrics->timer_tail = timer;
348 metrics->timer_tail = timer;
349 metrics->timer_head = timer;
352 if (metrics->info->have_perfmon) {
353 /* Create a new performance-monitor query */
354 monitor = xmalloc (sizeof (monitor_t));
356 monitor->op = metrics->op;
357 monitor->id = metrics->monitor_begun_id;
358 monitor->next = NULL;
360 if (metrics->monitor_tail) {
361 metrics->monitor_tail->next = monitor;
362 metrics->monitor_tail = monitor;
364 metrics->monitor_tail = monitor;
365 metrics->monitor_head = monitor;
369 metrics->monitors_in_flight++;
371 /* Avoid being a resource hog and collect outstanding results
372 * once we have sent off a large number of
373 * queries. (Presumably, many of the outstanding queries are
376 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
377 metrics_collect_available (metrics);
381 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
387 metrics_get_current_op (metrics_t *metrics)
393 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
398 metrics->time_ns = 0.0;
400 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
402 for (i = 0; i < info->num_groups; i++) {
403 metrics->counters[i] = xmalloc (sizeof (double) *
404 info->groups[i].num_counters);
405 for (j = 0; j < info->groups[i].num_counters; j++)
406 metrics->counters[i][j] = 0.0;
410 static op_metrics_t *
411 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
415 if (op >= metrics->num_op_metrics)
417 metrics->op_metrics = realloc (metrics->op_metrics,
418 (op + 1) * sizeof (op_metrics_t));
419 for (i = metrics->num_op_metrics; i < op + 1; i++)
420 op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
422 metrics->num_op_metrics = op + 1;
425 return &metrics->op_metrics[op];
429 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
430 GLuint *result, GLuint size)
432 #define CONSUME(var) \
433 if (p + sizeof(var) > ((unsigned char *) result) + size) \
435 fprintf (stderr, "Unexpected end-of-buffer while " \
436 "parsing results\n"); \
440 (var) = *((typeof(var) *) p); \
443 metrics_info_t *info = metrics->info;
444 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
445 unsigned char *p = (unsigned char *) result;
447 while (p < ((unsigned char *) result) + size)
449 GLuint group_id, group_index;
450 GLuint counter_id, counter_index;
451 metrics_group_info_t *group;
456 CONSUME (counter_id);
458 for (i = 0; i < info->num_groups; i++) {
459 if (info->groups[i].id == group_id)
463 assert (group_index < info->num_groups);
464 group = &info->groups[group_index];
466 for (i = 0; i < group->num_counters; i++) {
467 if (group->counter_ids[i] == counter_id)
471 assert (counter_index < group->num_counters);
473 switch (group->counter_types[counter_index])
476 uint64_t uint64_value;
478 case GL_UNSIGNED_INT:
479 CONSUME (uint_value);
482 case GL_UNSIGNED_INT64_AMD:
483 CONSUME (uint64_value);
484 value = uint64_value;
486 case GL_PERCENTAGE_AMD:
488 CONSUME (float_value);
492 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
493 group->counter_types[counter_index]);
498 op_metrics->counters[group_index][counter_index] += value;
503 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
505 op_metrics_t *op_metrics;
507 op_metrics = _get_op_metrics (metrics, op);
509 op_metrics->time_ns += time_ns;
512 typedef struct per_stage_metrics
514 op_metrics_t *metrics;
515 shader_stage_info_t *stage;
518 } per_stage_metrics_t;
521 _is_shader_stage_counter (metrics_info_t *info,
522 unsigned group_index,
523 unsigned counter_index)
525 shader_stage_info_t *stage;
528 for (i = 0; i < info->num_shader_stages; i++) {
529 stage = &info->stages[i];
531 if (stage->active_group_index == group_index &&
532 stage->active_counter_index == counter_index)
537 if (stage->stall_group_index == group_index &&
538 stage->stall_counter_index == counter_index)
548 print_per_stage_metrics (metrics_t *metrics,
549 per_stage_metrics_t *per_stage,
552 metrics_info_t *info = metrics->info;
553 op_metrics_t *op_metrics = per_stage->metrics;
554 metrics_group_info_t *group;
555 const char *op_string;
556 unsigned group_index, counter;
559 /* Don't print anything for stages with no alloted time. */
560 if (per_stage->time_ns == 0.0)
563 op_string = metrics_op_string (op_metrics->op);
565 printf ("%21s", op_string);
567 if (op_metrics->op >= METRICS_OP_SHADER) {
568 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
574 if (per_stage->stage)
575 printf (" %cS:", per_stage->stage->name[0]);
579 printf ("\t%7.2f ms (%4.1f%%)",
580 per_stage->time_ns / 1e6,
581 per_stage->time_ns / total * 100);
583 if (per_stage->active)
584 printf (", %4.1f%% active", per_stage->active * 100);
588 /* I'm not seeing a lot of value printing the rest of these
589 * performance counters by default yet. Use --verbose to get
595 for (group_index = 0; group_index < info->num_groups; group_index++) {
596 group = &info->groups[group_index];
597 for (counter = 0; counter < group->num_counters; counter++) {
599 /* Don't print this counter value if it's a
600 * per-stage cycle counter, (which we have
601 * already accounted for). */
602 if (_is_shader_stage_counter (info, group_index, counter))
605 value = op_metrics->counters[group_index][counter];
608 printf ("%s: %.2f ", group->counter_names[counter],
616 time_compare(const void *in_a, const void *in_b, void *arg unused)
618 const per_stage_metrics_t *a = in_a;
619 const per_stage_metrics_t *b = in_b;
622 if (a->time_ns < b->time_ns)
624 if (a->time_ns > b->time_ns)
630 print_program_metrics (metrics_t *metrics)
632 metrics_info_t *info = metrics->info;
633 unsigned num_shader_stages = info->num_shader_stages;
634 per_stage_metrics_t *sorted, *per_stage;
635 double total_time, op_cycles;
637 unsigned group_index, counter_index;
638 unsigned i, j, num_sorted;
640 if (! info->have_perfmon &&
641 ! info->printed_missing_perfmon_warning)
644 "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
645 " Some fips features, (such as per-shader-stage timings),\n"
646 " will not be available.\n");
648 info->printed_missing_perfmon_warning = true;
651 /* Make a sorted list of the per-stage operations by time
652 * used, and figure out the total so we can print percentages.
654 if (num_shader_stages)
655 num_sorted = metrics->num_op_metrics * num_shader_stages;
657 num_sorted = metrics->num_op_metrics;
659 sorted = xmalloc (sizeof (*sorted) * num_sorted);
663 for (i = 0; i < metrics->num_op_metrics; i++) {
665 op = &metrics->op_metrics[i];
667 /* Accumulate total time across all ops. */
668 total_time += op->time_ns;
670 /* Also, find total cycles in all stages of this op. */
673 if (num_shader_stages == 0) {
674 per_stage = &sorted[i];
675 per_stage->metrics = op;
676 per_stage->stage = NULL;
677 per_stage->time_ns = op->time_ns;
678 per_stage->active = 0.0;
681 for (j = 0; j < num_shader_stages; j++) {
683 group_index = info->stages[j].active_group_index;
684 counter_index = info->stages[j].active_counter_index;
685 op_cycles += op->counters[group_index][counter_index];
688 group_index = info->stages[j].stall_group_index;
689 counter_index = info->stages[j].stall_counter_index;
690 op_cycles += op->counters[group_index][counter_index];
693 for (j = 0; j < num_shader_stages; j++) {
694 double active_cycles, stall_cycles, stage_cycles;
697 group_index = info->stages[j].active_group_index;
698 counter_index = info->stages[j].active_counter_index;
699 active_cycles = op->counters[group_index][counter_index];
702 group_index = info->stages[j].stall_group_index;
703 counter_index = info->stages[j].stall_counter_index;
704 stall_cycles = op->counters[group_index][counter_index];
706 stage_cycles = active_cycles + stall_cycles;
708 per_stage = &sorted[i * num_shader_stages + j];
709 per_stage->metrics = op;
712 per_stage->stage = &info->stages[j];
713 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
715 /* If we don't have any per-stage cycle counts
716 * for this operation, then use the first
717 * stage as a placeholder for all the time,
718 * but NULL-ify the stage info so that the
719 * report doesn't lie about this time being
720 * from any particular stage. */
721 per_stage->stage = NULL;
723 per_stage->time_ns = op->time_ns;
725 per_stage->time_ns = 0.0;
730 per_stage->active = active_cycles / stage_cycles;
732 per_stage->active = 0.0;
737 qsort_r (sorted, num_sorted, sizeof (*sorted),
738 time_compare, metrics->op_metrics);
740 for (i = 0; i < num_sorted; i++)
741 print_per_stage_metrics (metrics, &sorted[i], total_time);
747 metrics_collect_available (metrics_t *metrics)
749 /* Consume all timer queries that are ready. */
750 timer_query_t *timer = metrics->timer_head;
753 GLuint available, elapsed;
755 glGetQueryObjectuiv (timer->id,
756 GL_QUERY_RESULT_AVAILABLE, &available);
760 glGetQueryObjectuiv (timer->id,
761 GL_QUERY_RESULT, &elapsed);
763 accumulate_program_time (metrics, timer->op, elapsed);
765 metrics->timer_head = timer->next;
766 if (metrics->timer_head == NULL)
767 metrics->timer_tail = NULL;
769 glDeleteQueries (1, &timer->id);
772 timer = metrics->timer_head;
775 if (! metrics->info->have_perfmon)
778 /* And similarly for all performance monitors that are ready. */
779 monitor_t *monitor = metrics->monitor_head;
782 GLuint available, result_size, *result;
785 glGetPerfMonitorCounterDataAMD (monitor->id,
786 GL_PERFMON_RESULT_AVAILABLE_AMD,
787 sizeof (available), &available,
792 glGetPerfMonitorCounterDataAMD (monitor->id,
793 GL_PERFMON_RESULT_SIZE_AMD,
794 sizeof (result_size),
797 result = xmalloc (result_size);
799 glGetPerfMonitorCounterDataAMD (monitor->id,
800 GL_PERFMON_RESULT_AMD,
804 accumulate_program_metrics (metrics, monitor->op, result, result_size);
808 metrics->monitor_head = monitor->next;
809 if (metrics->monitor_head == NULL)
810 metrics->monitor_tail = NULL;
812 glDeletePerfMonitorsAMD (1, &monitor->id);
816 metrics->monitors_in_flight--;
818 monitor = metrics->monitor_head;
822 /* Subtract timespec values: Return (a - b) in seconds as a double. */
823 #define SUBTRACT_TIMESPEC(a, b) (double) (a.tv_sec - b.tv_sec + \
824 (a.tv_nsec - b.tv_nsec) / 1e9)
827 metrics_end_frame_pre_swap (metrics_t *metrics)
829 GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
832 /* Don't leave any counters running over the end_frame work we
833 * do here. The counters will be started again at the end of
834 * metrics_end_frame_post_swap. */
836 metrics_counter_stop (metrics);
838 /* Now that an entire frame's worth of content has gone by, we
839 * can be sure that the timer quiery for the previous frame's
842 * Note: The only case in which this query isn't immediately
843 * available would be if there was effectively nothing in this
844 * frame. In that case, triggering a block on this query
845 * result is not a concern (since an empty frame is not
846 * interesting, and all of our counters are stopped anyway).
848 * Accepting this one block is much better than adding a
849 * linked list for these results that would reaslisticly never
850 * have more than one entry anyway.
853 glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
854 GL_QUERY_RESULT, &swap_end_timestamp);
858 printf ("# frame: Frame_Number Frame_Time_milliseconds Frame_latency_milliseconds CPU_load GPU_load\n");
860 /* Subtract previous frame's times to get frame times. */
861 frame_time_ns = subtract_timestamp (swap_end_timestamp,
862 metrics->previous_swap_end_timestamp);
864 latency_ns = subtract_timestamp (swap_end_timestamp,
865 metrics->swap_begin_timestamp);
867 cpu_time = SUBTRACT_TIMESPEC (metrics->cpu_time_ts,
868 metrics->previous_cpu_time_ts);
870 /* We've waited one frame to ensure we have a timestamp
871 * result. So the time we've actually measured here is
872 * for the previous frame. */
873 printf ("frame: %d %g %g %g %g\n",
875 (double) frame_time_ns / 1e6,
876 (double) latency_ns / 1e6,
877 cpu_time / (frame_time_ns / 1e9),
881 metrics->previous_swap_end_timestamp = swap_end_timestamp;
882 metrics->previous_cpu_time_ts = metrics->cpu_time_ts;
884 /* Before the actual swap call, we get the current timestamp
885 * value. This is a synchronous get so we can use this as the
886 * baseline for a frame latency measurment. */
888 glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
892 metrics_end_frame_post_swap (metrics_t *metrics)
894 static int initialized = 0;
895 static struct timeval tv_start, tv_now;
898 /* Now that the swap command has been queued, we issue an
899 * asynchronous query of the timestamp value. Comparing this
900 * to the synchronous get we just sent in
901 * metrics_end_frame_pre_swap allows us to measure the
902 * per-frame swap latency. */
904 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
906 /* Query the total amount of CPU time spent by this process. */
907 err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
909 fprintf (stderr, "Failed to query CPU time with clock_gettime: %s\n",
915 gettimeofday (&tv_start, NULL);
916 if (getenv ("FIPS_VERBOSE"))
921 metrics_collect_available (metrics);
925 if (frames % 15 == 0) {
928 gettimeofday (&tv_now, NULL);
930 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
931 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
933 printf("FPS: %.3f\n", fps);
935 print_program_metrics (metrics);
938 /* Start the counter up again now that we're done. */
939 metrics_counter_start (metrics);