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 /* Flag to indicate first frame is being processed. */
80 /* Description of all available peformance counters, counter
81 * groups, their names and IDs, etc. */
84 /* The current operation being measured. */
87 /* GL_TIME_ELAPSED query for which glEndQuery has not yet
89 unsigned timer_begun_id;
91 /* GL_TIME_ELAPSED queries for which glEndQuery has been
92 * called, (but results have not yet been queried). */
93 timer_query_t *timer_head;
94 timer_query_t *timer_tail;
96 /* Performance monitor for which glEndPerfMonitorAMD has not
98 unsigned monitor_begun_id;
100 /* Performance monitors for which glEndPerfMonitorAMD has
101 * been called, (but results have not yet been queried). */
102 monitor_t *monitor_head;
103 monitor_t *monitor_tail;
105 int monitors_in_flight;
107 unsigned num_op_metrics;
108 op_metrics_t *op_metrics;
110 /* Per-frame time and latency measurement. */
111 GLint64 swap_begin_timestamp;
112 unsigned swap_end_timestamp_id;
113 GLint64 previous_swap_end_timestamp;
115 struct timespec previous_cpu_time_ts;
116 struct timespec cpu_time_ts;
119 /* Return (a - b) while accounting for the fact that between a and b,
120 * the counter may have wrapped around (with a maximum number of bits
121 * in the counter of GL_QUERY_COUNTER_BITS). */
123 subtract_timestamp (uint64_t a, uint64_t b)
125 GLint timestamp_bits;
127 glGetQueryiv (GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, ×tamp_bits);
129 /* Note: "<<" isn't defined for the number of bits in the
130 * type, and the compiler will punish you.
132 if (timestamp_bits == 64)
136 return (1ull << timestamp_bits) + a - b;
142 metrics_create (metrics_info_t *info)
146 metrics = xmalloc (sizeof (metrics_t));
148 metrics->info = info;
150 metrics->first_frame = true;
154 metrics->timer_begun_id = 0;
156 metrics->timer_head = NULL;
157 metrics->timer_tail = NULL;
159 metrics->monitor_begun_id = 0;
161 metrics->monitor_head = NULL;
162 metrics->monitor_tail = NULL;
164 metrics->monitors_in_flight = 0;
166 metrics->num_op_metrics = 0;
167 metrics->op_metrics = NULL;
169 glGenQueries (1, &metrics->swap_end_timestamp_id);
171 /* Get the first frame timestamp started immediately. */
172 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
174 /* As well as the initial CPU time. */
175 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
181 metrics_fini (metrics_t *metrics)
183 timer_query_t *timer, *timer_next;
184 monitor_t *monitor, *monitor_next;
186 /* Discard and cleanup any outstanding queries. */
187 if (metrics->timer_begun_id) {
188 glEndQuery (GL_TIME_ELAPSED);
189 glDeleteQueries (1, &metrics->timer_begun_id);
190 metrics->timer_begun_id = 0;
193 for (timer = metrics->timer_head;
197 glDeleteQueries (1, &timer->id);
198 timer_next = timer->next;
201 metrics->timer_head = NULL;
202 metrics->timer_tail = NULL;
204 if (metrics->info->have_perfmon) {
206 if (metrics->monitor_begun_id) {
207 glEndPerfMonitorAMD (metrics->monitor_begun_id);
208 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
209 metrics->monitor_begun_id = 0;
212 for (monitor = metrics->monitor_head;
214 monitor = monitor_next)
216 glDeletePerfMonitorsAMD (1, &monitor->id);
217 monitor_next = monitor->next;
220 metrics->monitor_head = NULL;
221 metrics->monitor_tail = NULL;
225 metrics->monitors_in_flight = 0;
229 metrics_destroy (metrics_t *metrics)
231 metrics_fini (metrics);
237 metrics_op_string (metrics_op_t op)
239 if (op >= METRICS_OP_SHADER)
240 return "Shader program";
244 case METRICS_OP_ACCUM:
245 return "glAccum*(+)";
246 case METRICS_OP_BUFFER_DATA:
247 return "glBufferData(+)";
248 case METRICS_OP_BUFFER_SUB_DATA:
249 return "glCopyBufferSubData*";
250 case METRICS_OP_BITMAP:
252 case METRICS_OP_BLIT_FRAMEBUFFER:
253 return "glBlitFramebuffer*";
254 case METRICS_OP_CLEAR:
256 case METRICS_OP_CLEAR_BUFFER_DATA:
257 return "glCearBufferData(+)";
258 case METRICS_OP_CLEAR_TEX_IMAGE:
259 return "glClearTexImage(+)";
260 case METRICS_OP_COPY_PIXELS:
261 return "glCopyPixels";
262 case METRICS_OP_COPY_TEX_IMAGE:
263 return "glCopyTexImage(+)";
264 case METRICS_OP_DRAW_PIXELS:
265 return "glDrawPixels";
266 case METRICS_OP_GET_TEX_IMAGE:
267 return "glGetTexImage(+)";
268 case METRICS_OP_READ_PIXELS:
269 return "glReadPixels*";
270 case METRICS_OP_TEX_IMAGE:
271 return "glTexImage*(+)";
273 fprintf (stderr, "fips: Internal error: "
274 "Unknown metrics op value: %d\n", op);
282 metrics_counter_start (metrics_t *metrics)
286 /* Initialize the timer_query object. */
287 glGenQueries (1, &metrics->timer_begun_id);
289 /* Most everything else in this function is
290 * performance-monitor related. If we don't have that
291 * extension, just start the timer query and be done. */
292 if (! metrics->info->have_perfmon) {
293 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
297 /* Initialize the performance-monitor object */
298 glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
300 for (i = 0; i < metrics->info->num_groups; i++)
302 metrics_group_info_t *group;
305 group = &metrics->info->groups[i];
307 num_counters = group->num_counters;
308 if (group->max_active_counters < group->num_counters)
310 fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
311 group->max_active_counters,
312 group->num_counters, i);
313 num_counters = group->max_active_counters;
317 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
323 /* Start the queries */
324 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
326 glBeginPerfMonitorAMD (metrics->monitor_begun_id);
330 metrics_counter_stop (metrics_t *metrics)
332 timer_query_t *timer;
335 /* Stop the current timer and monitor. */
336 glEndQuery (GL_TIME_ELAPSED);
338 if (metrics->info->have_perfmon)
339 glEndPerfMonitorAMD (metrics->monitor_begun_id);
341 /* Add these IDs to our lists of outstanding queries and
342 * monitors so the results can be collected later. */
343 timer = xmalloc (sizeof (timer_query_t));
345 timer->op = metrics->op;
346 timer->id = metrics->timer_begun_id;
349 if (metrics->timer_tail) {
350 metrics->timer_tail->next = timer;
351 metrics->timer_tail = timer;
353 metrics->timer_tail = timer;
354 metrics->timer_head = timer;
357 if (metrics->info->have_perfmon) {
358 /* Create a new performance-monitor query */
359 monitor = xmalloc (sizeof (monitor_t));
361 monitor->op = metrics->op;
362 monitor->id = metrics->monitor_begun_id;
363 monitor->next = NULL;
365 if (metrics->monitor_tail) {
366 metrics->monitor_tail->next = monitor;
367 metrics->monitor_tail = monitor;
369 metrics->monitor_tail = monitor;
370 metrics->monitor_head = monitor;
374 metrics->monitors_in_flight++;
376 /* Avoid being a resource hog and collect outstanding results
377 * once we have sent off a large number of
378 * queries. (Presumably, many of the outstanding queries are
381 if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
382 metrics_collect_available (metrics);
386 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
392 metrics_get_current_op (metrics_t *metrics)
398 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
403 metrics->time_ns = 0.0;
405 metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
407 for (i = 0; i < info->num_groups; i++) {
408 metrics->counters[i] = xmalloc (sizeof (double) *
409 info->groups[i].num_counters);
410 for (j = 0; j < info->groups[i].num_counters; j++)
411 metrics->counters[i][j] = 0.0;
415 static op_metrics_t *
416 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
420 if (op >= metrics->num_op_metrics)
422 metrics->op_metrics = realloc (metrics->op_metrics,
423 (op + 1) * sizeof (op_metrics_t));
424 for (i = metrics->num_op_metrics; i < op + 1; i++)
425 op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
427 metrics->num_op_metrics = op + 1;
430 return &metrics->op_metrics[op];
434 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
435 GLuint *result, GLuint size)
437 #define CONSUME(var) \
438 if (p + sizeof(var) > ((unsigned char *) result) + size) \
440 fprintf (stderr, "Unexpected end-of-buffer while " \
441 "parsing results\n"); \
445 (var) = *((typeof(var) *) p); \
448 metrics_info_t *info = metrics->info;
449 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
450 unsigned char *p = (unsigned char *) result;
452 while (p < ((unsigned char *) result) + size)
454 GLuint group_id, group_index;
455 GLuint counter_id, counter_index;
456 metrics_group_info_t *group;
461 CONSUME (counter_id);
463 for (i = 0; i < info->num_groups; i++) {
464 if (info->groups[i].id == group_id)
468 assert (group_index < info->num_groups);
469 group = &info->groups[group_index];
471 for (i = 0; i < group->num_counters; i++) {
472 if (group->counter_ids[i] == counter_id)
476 assert (counter_index < group->num_counters);
478 switch (group->counter_types[counter_index])
481 uint64_t uint64_value;
483 case GL_UNSIGNED_INT:
484 CONSUME (uint_value);
487 case GL_UNSIGNED_INT64_AMD:
488 CONSUME (uint64_value);
489 value = uint64_value;
491 case GL_PERCENTAGE_AMD:
493 CONSUME (float_value);
497 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
498 group->counter_types[counter_index]);
503 op_metrics->counters[group_index][counter_index] += value;
508 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
510 op_metrics_t *op_metrics;
512 op_metrics = _get_op_metrics (metrics, op);
514 op_metrics->time_ns += time_ns;
517 typedef struct per_stage_metrics
519 op_metrics_t *metrics;
520 shader_stage_info_t *stage;
523 } per_stage_metrics_t;
526 _is_shader_stage_counter (metrics_info_t *info,
527 unsigned group_index,
528 unsigned counter_index)
530 shader_stage_info_t *stage;
533 for (i = 0; i < info->num_shader_stages; i++) {
534 stage = &info->stages[i];
536 if (stage->active_group_index == group_index &&
537 stage->active_counter_index == counter_index)
542 if (stage->stall_group_index == group_index &&
543 stage->stall_counter_index == counter_index)
553 print_per_stage_metrics (metrics_t *metrics,
554 per_stage_metrics_t *per_stage,
557 metrics_info_t *info = metrics->info;
558 op_metrics_t *op_metrics = per_stage->metrics;
559 metrics_group_info_t *group;
560 const char *op_string;
561 unsigned group_index, counter;
564 /* Don't print anything for stages with no alloted time. */
565 if (per_stage->time_ns == 0.0)
568 op_string = metrics_op_string (op_metrics->op);
570 printf ("%21s", op_string);
572 if (op_metrics->op >= METRICS_OP_SHADER) {
573 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
579 if (per_stage->stage)
580 printf (" %cS:", per_stage->stage->name[0]);
584 printf ("\t%7.2f ms (%4.1f%%)",
585 per_stage->time_ns / 1e6,
586 per_stage->time_ns / total * 100);
588 if (per_stage->active)
589 printf (", %4.1f%% active", per_stage->active * 100);
593 /* I'm not seeing a lot of value printing the rest of these
594 * performance counters by default yet. Use --verbose to get
600 for (group_index = 0; group_index < info->num_groups; group_index++) {
601 group = &info->groups[group_index];
602 for (counter = 0; counter < group->num_counters; counter++) {
604 /* Don't print this counter value if it's a
605 * per-stage cycle counter, (which we have
606 * already accounted for). */
607 if (_is_shader_stage_counter (info, group_index, counter))
610 value = op_metrics->counters[group_index][counter];
613 printf ("%s: %.2f ", group->counter_names[counter],
621 time_compare(const void *in_a, const void *in_b, void *arg unused)
623 const per_stage_metrics_t *a = in_a;
624 const per_stage_metrics_t *b = in_b;
627 if (a->time_ns < b->time_ns)
629 if (a->time_ns > b->time_ns)
635 print_program_metrics (metrics_t *metrics)
637 metrics_info_t *info = metrics->info;
638 unsigned num_shader_stages = info->num_shader_stages;
639 per_stage_metrics_t *sorted, *per_stage;
640 double total_time, op_cycles;
642 unsigned group_index, counter_index;
643 unsigned i, j, num_sorted;
645 if (! info->have_perfmon &&
646 ! info->printed_missing_perfmon_warning)
649 "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
650 " Some fips features, (such as per-shader-stage timings),\n"
651 " will not be available.\n");
653 info->printed_missing_perfmon_warning = true;
656 /* Make a sorted list of the per-stage operations by time
657 * used, and figure out the total so we can print percentages.
659 if (num_shader_stages)
660 num_sorted = metrics->num_op_metrics * num_shader_stages;
662 num_sorted = metrics->num_op_metrics;
664 sorted = xmalloc (sizeof (*sorted) * num_sorted);
668 for (i = 0; i < metrics->num_op_metrics; i++) {
670 op = &metrics->op_metrics[i];
672 /* Accumulate total time across all ops. */
673 total_time += op->time_ns;
675 /* Also, find total cycles in all stages of this op. */
678 if (num_shader_stages == 0) {
679 per_stage = &sorted[i];
680 per_stage->metrics = op;
681 per_stage->stage = NULL;
682 per_stage->time_ns = op->time_ns;
683 per_stage->active = 0.0;
686 for (j = 0; j < num_shader_stages; j++) {
688 group_index = info->stages[j].active_group_index;
689 counter_index = info->stages[j].active_counter_index;
690 op_cycles += op->counters[group_index][counter_index];
693 group_index = info->stages[j].stall_group_index;
694 counter_index = info->stages[j].stall_counter_index;
695 op_cycles += op->counters[group_index][counter_index];
698 for (j = 0; j < num_shader_stages; j++) {
699 double active_cycles, stall_cycles, stage_cycles;
702 group_index = info->stages[j].active_group_index;
703 counter_index = info->stages[j].active_counter_index;
704 active_cycles = op->counters[group_index][counter_index];
707 group_index = info->stages[j].stall_group_index;
708 counter_index = info->stages[j].stall_counter_index;
709 stall_cycles = op->counters[group_index][counter_index];
711 stage_cycles = active_cycles + stall_cycles;
713 per_stage = &sorted[i * num_shader_stages + j];
714 per_stage->metrics = op;
717 per_stage->stage = &info->stages[j];
718 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
720 /* If we don't have any per-stage cycle counts
721 * for this operation, then use the first
722 * stage as a placeholder for all the time,
723 * but NULL-ify the stage info so that the
724 * report doesn't lie about this time being
725 * from any particular stage. */
726 per_stage->stage = NULL;
728 per_stage->time_ns = op->time_ns;
730 per_stage->time_ns = 0.0;
735 per_stage->active = active_cycles / stage_cycles;
737 per_stage->active = 0.0;
742 qsort_r (sorted, num_sorted, sizeof (*sorted),
743 time_compare, metrics->op_metrics);
745 for (i = 0; i < num_sorted; i++)
746 print_per_stage_metrics (metrics, &sorted[i], total_time);
752 metrics_collect_available (metrics_t *metrics)
754 /* Consume all timer queries that are ready. */
755 timer_query_t *timer = metrics->timer_head;
758 GLuint available, elapsed;
760 glGetQueryObjectuiv (timer->id,
761 GL_QUERY_RESULT_AVAILABLE, &available);
765 glGetQueryObjectuiv (timer->id,
766 GL_QUERY_RESULT, &elapsed);
768 accumulate_program_time (metrics, timer->op, elapsed);
770 metrics->timer_head = timer->next;
771 if (metrics->timer_head == NULL)
772 metrics->timer_tail = NULL;
774 glDeleteQueries (1, &timer->id);
777 timer = metrics->timer_head;
780 if (! metrics->info->have_perfmon)
783 /* And similarly for all performance monitors that are ready. */
784 monitor_t *monitor = metrics->monitor_head;
787 GLuint available, result_size, *result;
790 glGetPerfMonitorCounterDataAMD (monitor->id,
791 GL_PERFMON_RESULT_AVAILABLE_AMD,
792 sizeof (available), &available,
797 glGetPerfMonitorCounterDataAMD (monitor->id,
798 GL_PERFMON_RESULT_SIZE_AMD,
799 sizeof (result_size),
802 result = xmalloc (result_size);
804 glGetPerfMonitorCounterDataAMD (monitor->id,
805 GL_PERFMON_RESULT_AMD,
809 accumulate_program_metrics (metrics, monitor->op, result, result_size);
813 metrics->monitor_head = monitor->next;
814 if (metrics->monitor_head == NULL)
815 metrics->monitor_tail = NULL;
817 glDeletePerfMonitorsAMD (1, &monitor->id);
821 metrics->monitors_in_flight--;
823 monitor = metrics->monitor_head;
827 /* Subtract timespec values: Return (a - b) in seconds as a double. */
828 #define SUBTRACT_TIMESPEC(a, b) (double) (a.tv_sec - b.tv_sec + \
829 (a.tv_nsec - b.tv_nsec) / 1e9)
832 metrics_end_frame_pre_swap (metrics_t *metrics)
834 GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
837 /* Don't leave any counters running over the end_frame work we
838 * do here. The counters will be started again at the end of
839 * metrics_end_frame_post_swap. */
841 metrics_counter_stop (metrics);
843 /* Now that an entire frame's worth of content has gone by, we
844 * can be sure that the timer quiery for the previous frame's
847 * Note: The only case in which this query isn't immediately
848 * available would be if there was effectively nothing in this
849 * frame. In that case, triggering a block on this query
850 * result is not a concern (since an empty frame is not
851 * interesting, and all of our counters are stopped anyway).
853 * Accepting this one block is much better than adding a
854 * linked list for these results that would reaslisticly never
855 * have more than one entry anyway.
858 glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
859 GL_QUERY_RESULT, &swap_end_timestamp);
861 if (metrics->first_frame) {
863 printf ("# frame: Frame_Number Frame_Time_milliseconds Frame_latency_milliseconds CPU_load GPU_load\n");
865 metrics->first_frame = false;
867 /* Subtract previous frame's times to get frame times. */
868 frame_time_ns = subtract_timestamp (swap_end_timestamp,
869 metrics->previous_swap_end_timestamp);
871 latency_ns = subtract_timestamp (swap_end_timestamp,
872 metrics->swap_begin_timestamp);
874 cpu_time = SUBTRACT_TIMESPEC (metrics->cpu_time_ts,
875 metrics->previous_cpu_time_ts);
877 /* We've waited one frame to ensure we have a timestamp
878 * result. So the time we've actually measured here is
879 * for the previous frame. */
880 printf ("frame: %d %g %g %g %g\n",
882 (double) frame_time_ns / 1e6,
883 (double) latency_ns / 1e6,
884 cpu_time / (frame_time_ns / 1e9),
888 metrics->previous_swap_end_timestamp = swap_end_timestamp;
889 metrics->previous_cpu_time_ts = metrics->cpu_time_ts;
891 /* Before the actual swap call, we get the current timestamp
892 * value. This is a synchronous get so we can use this as the
893 * baseline for a frame latency measurment. */
895 glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
899 metrics_end_frame_post_swap (metrics_t *metrics)
901 static int initialized = 0;
902 static struct timeval tv_start, tv_now;
905 /* Now that the swap command has been queued, we issue an
906 * asynchronous query of the timestamp value. Comparing this
907 * to the synchronous get we just sent in
908 * metrics_end_frame_pre_swap allows us to measure the
909 * per-frame swap latency. */
911 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
913 /* Query the total amount of CPU time spent by this process. */
914 err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
916 fprintf (stderr, "Failed to query CPU time with clock_gettime: %s\n",
922 gettimeofday (&tv_start, NULL);
923 if (getenv ("FIPS_VERBOSE"))
928 metrics_collect_available (metrics);
932 if (frames % 15 == 0) {
935 gettimeofday (&tv_now, NULL);
937 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
938 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
940 printf("FPS: %.3f\n", fps);
942 print_program_metrics (metrics);
945 /* Start the counter up again now that we're done. */
946 metrics_counter_start (metrics);