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"); \
414 (var) = *((typeof(var) *) p); \
417 metrics_info_t *info = metrics->info;
418 op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
419 unsigned char *p = (unsigned char *) result;
421 while (p < ((unsigned char *) result) + size)
423 GLuint group_id, group_index;
424 GLuint counter_id, counter_index;
425 metrics_group_info_t *group;
430 CONSUME (counter_id);
432 for (i = 0; i < info->num_groups; i++) {
433 if (info->groups[i].id == group_id)
437 assert (group_index < info->num_groups);
438 group = &info->groups[group_index];
440 for (i = 0; i < group->num_counters; i++) {
441 if (group->counter_ids[i] == counter_id)
445 assert (counter_index < group->num_counters);
447 switch (group->counter_types[counter_index])
450 uint64_t uint64_value;
452 case GL_UNSIGNED_INT:
453 CONSUME (uint_value);
456 case GL_UNSIGNED_INT64_AMD:
457 CONSUME (uint64_value);
458 value = uint64_value;
460 case GL_PERCENTAGE_AMD:
462 CONSUME (float_value);
466 fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
467 group->counter_types[counter_index]);
472 op_metrics->counters[group_index][counter_index] += value;
477 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
479 op_metrics_t *op_metrics;
481 op_metrics = _get_op_metrics (metrics, op);
483 op_metrics->time_ns += time_ns;
486 typedef struct per_stage_metrics
488 op_metrics_t *metrics;
489 shader_stage_info_t *stage;
492 } per_stage_metrics_t;
495 _is_shader_stage_counter (metrics_info_t *info,
496 unsigned group_index,
497 unsigned counter_index)
499 shader_stage_info_t *stage;
502 for (i = 0; i < info->num_shader_stages; i++) {
503 stage = &info->stages[i];
505 if (stage->active_group_index == group_index &&
506 stage->active_counter_index == counter_index)
511 if (stage->stall_group_index == group_index &&
512 stage->stall_counter_index == counter_index)
522 print_per_stage_metrics (metrics_t *metrics,
523 per_stage_metrics_t *per_stage,
526 metrics_info_t *info = metrics->info;
527 op_metrics_t *op_metrics = per_stage->metrics;
528 metrics_group_info_t *group;
529 const char *op_string;
530 unsigned group_index, counter;
533 /* Don't print anything for stages with no alloted time. */
534 if (per_stage->time_ns == 0.0)
537 op_string = metrics_op_string (op_metrics->op);
539 printf ("%21s", op_string);
541 if (op_metrics->op >= METRICS_OP_SHADER) {
542 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
548 if (per_stage->stage)
549 printf (" %cS:", per_stage->stage->name[0]);
553 printf ("\t%7.2f ms (%4.1f%%)",
554 per_stage->time_ns / 1e6,
555 per_stage->time_ns / total * 100);
557 if (per_stage->active)
558 printf (", %4.1f%% active", per_stage->active * 100);
562 /* I'm not seeing a lot of value printing the rest of these
563 * performance counters by default yet. Use --verbose to get
569 for (group_index = 0; group_index < info->num_groups; group_index++) {
570 group = &info->groups[group_index];
571 for (counter = 0; counter < group->num_counters; counter++) {
573 /* Don't print this counter value if it's a
574 * per-stage cycle counter, (which we have
575 * already accounted for). */
576 if (_is_shader_stage_counter (info, group_index, counter))
579 value = op_metrics->counters[group_index][counter];
582 printf ("%s: %.2f ", group->counter_names[counter],
590 time_compare(const void *in_a, const void *in_b, void *arg unused)
592 const per_stage_metrics_t *a = in_a;
593 const per_stage_metrics_t *b = in_b;
596 if (a->time_ns < b->time_ns)
598 if (a->time_ns > b->time_ns)
604 print_program_metrics (metrics_t *metrics)
606 metrics_info_t *info = metrics->info;
607 unsigned num_shader_stages = info->num_shader_stages;
608 per_stage_metrics_t *sorted, *per_stage;
609 double total_time, op_cycles;
611 unsigned group_index, counter_index;
612 unsigned i, j, num_sorted;
614 if (! info->have_perfmon &&
615 ! info->printed_missing_perfmon_warning)
618 "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
619 " Some fips features, (such as per-shader-stage timings),\n"
620 " will not be available.\n");
622 info->printed_missing_perfmon_warning = true;
625 /* Make a sorted list of the per-stage operations by time
626 * used, and figure out the total so we can print percentages.
628 if (num_shader_stages)
629 num_sorted = metrics->num_op_metrics * num_shader_stages;
631 num_sorted = metrics->num_op_metrics;
633 sorted = xmalloc (sizeof (*sorted) * num_sorted);
637 for (i = 0; i < metrics->num_op_metrics; i++) {
639 op = &metrics->op_metrics[i];
641 /* Accumulate total time across all ops. */
642 total_time += op->time_ns;
644 /* Also, find total cycles in all stages of this op. */
647 if (num_shader_stages == 0) {
648 per_stage = &sorted[i];
649 per_stage->metrics = op;
650 per_stage->stage = NULL;
651 per_stage->time_ns = op->time_ns;
652 per_stage->active = 0.0;
655 for (j = 0; j < num_shader_stages; j++) {
657 group_index = info->stages[j].active_group_index;
658 counter_index = info->stages[j].active_counter_index;
659 op_cycles += op->counters[group_index][counter_index];
662 group_index = info->stages[j].stall_group_index;
663 counter_index = info->stages[j].stall_counter_index;
664 op_cycles += op->counters[group_index][counter_index];
667 for (j = 0; j < num_shader_stages; j++) {
668 double active_cycles, stall_cycles, stage_cycles;
671 group_index = info->stages[j].active_group_index;
672 counter_index = info->stages[j].active_counter_index;
673 active_cycles = op->counters[group_index][counter_index];
676 group_index = info->stages[j].stall_group_index;
677 counter_index = info->stages[j].stall_counter_index;
678 stall_cycles = op->counters[group_index][counter_index];
680 stage_cycles = active_cycles + stall_cycles;
682 per_stage = &sorted[i * num_shader_stages + j];
683 per_stage->metrics = op;
686 per_stage->stage = &info->stages[j];
687 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
689 /* If we don't have any per-stage cycle counts
690 * for this operation, then use the first
691 * stage as a placeholder for all the time,
692 * but NULL-ify the stage info so that the
693 * report doesn't lie about this time being
694 * from any particular stage. */
695 per_stage->stage = NULL;
697 per_stage->time_ns = op->time_ns;
699 per_stage->time_ns = 0.0;
704 per_stage->active = active_cycles / stage_cycles;
706 per_stage->active = 0.0;
711 qsort_r (sorted, num_sorted, sizeof (*sorted),
712 time_compare, metrics->op_metrics);
714 for (i = 0; i < num_sorted; i++)
715 print_per_stage_metrics (metrics, &sorted[i], total_time);
721 metrics_collect_available (metrics_t *metrics)
723 /* Consume all timer queries that are ready. */
724 timer_query_t *timer = metrics->timer_head;
727 GLuint available, elapsed;
729 glGetQueryObjectuiv (timer->id,
730 GL_QUERY_RESULT_AVAILABLE, &available);
734 glGetQueryObjectuiv (timer->id,
735 GL_QUERY_RESULT, &elapsed);
737 accumulate_program_time (metrics, timer->op, elapsed);
739 metrics->timer_head = timer->next;
740 if (metrics->timer_head == NULL)
741 metrics->timer_tail = NULL;
743 glDeleteQueries (1, &timer->id);
746 timer = metrics->timer_head;
749 if (! metrics->info->have_perfmon)
752 /* And similarly for all performance monitors that are ready. */
753 monitor_t *monitor = metrics->monitor_head;
756 GLuint available, result_size, *result;
759 glGetPerfMonitorCounterDataAMD (monitor->id,
760 GL_PERFMON_RESULT_AVAILABLE_AMD,
761 sizeof (available), &available,
766 glGetPerfMonitorCounterDataAMD (monitor->id,
767 GL_PERFMON_RESULT_SIZE_AMD,
768 sizeof (result_size),
771 result = xmalloc (result_size);
773 glGetPerfMonitorCounterDataAMD (monitor->id,
774 GL_PERFMON_RESULT_AMD,
778 accumulate_program_metrics (metrics, monitor->op, result, result_size);
782 metrics->monitor_head = monitor->next;
783 if (metrics->monitor_head == NULL)
784 metrics->monitor_tail = NULL;
786 glDeletePerfMonitorsAMD (1, &monitor->id);
790 metrics->monitors_in_flight--;
792 monitor = metrics->monitor_head;
796 /* Return the difference from ts0 to ts1 as floating-point seconds. */
797 #define TIMESPEC_DIFF(ts0, ts1) (double) (ts1.tv_sec - ts0.tv_sec + \
798 (ts1.tv_nsec - ts0.tv_nsec) / 1e9)
801 metrics_end_frame_pre_swap (metrics_t *metrics)
803 GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
806 /* Don't leave any counters running over the end_frame work we
807 * do here. The counters will be started again at the end of
808 * metrics_end_frame_post_swap. */
810 metrics_counter_stop (metrics);
812 /* Now that an entire frame's worth of content has gone by, we
813 * can be sure that the timer quiery for the previous frame's
816 * Note: The only case in which this query isn't immediately
817 * available would be if there was effectively nothing in this
818 * frame. In that case, triggering a block on this query
819 * result is not a concern (since an empty frame is not
820 * interesting, and all of our counters are stopped anyway).
822 * Accepting this one block is much better than adding a
823 * linked list for these results that would reaslisticly never
824 * have more than one entry anyway.
827 glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
828 GL_QUERY_RESULT, &swap_end_timestamp);
832 printf ("# frame: Frame_Number Frame_Time_milliseconds Frame_latency_milliseconds CPU_load GPU_load\n");
834 /* Subtract previous frame's times to get frame times. */
835 frame_time_ns = swap_end_timestamp - metrics->previous_swap_end_timestamp;
837 latency_ns = swap_end_timestamp - metrics->swap_begin_timestamp;
839 cpu_time = TIMESPEC_DIFF (metrics->previous_cpu_time_ts, metrics->cpu_time_ts);
841 /* We've waited one frame to ensure we have a timestamp
842 * result. So the time we've actually measured here is
843 * for the previous frame. */
844 printf ("frame: %d %g %g %g %g\n",
846 (double) frame_time_ns / 1e6,
847 (double) latency_ns / 1e6,
848 cpu_time / (frame_time_ns / 1e9),
852 metrics->previous_swap_end_timestamp = swap_end_timestamp;
853 metrics->previous_cpu_time_ts = metrics->cpu_time_ts;
855 /* Before the actual swap call, we get the current timestamp
856 * value. This is a synchronous get so we can use this as the
857 * baseline for a frame latency measurment. */
859 glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
863 metrics_end_frame_post_swap (metrics_t *metrics)
865 static int initialized = 0;
866 static struct timeval tv_start, tv_now;
869 /* Now that the swap command has been queued, we issue an
870 * asynchronous query of the timestamp value. Comparing this
871 * to the synchronous get we just sent in
872 * metrics_end_frame_pre_swap allows us to measure the
873 * per-frame swap latency. */
875 glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
877 /* Query the total amount of CPU time spent by this process. */
878 err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
880 fprintf (stderr, "Failed to query CPU time with clock_gettime: %s\n",
886 gettimeofday (&tv_start, NULL);
887 if (getenv ("FIPS_VERBOSE"))
892 metrics_collect_available (metrics);
896 if (frames % 15 == 0) {
899 gettimeofday (&tv_now, NULL);
901 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
902 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
904 printf("FPS: %.3f\n", fps);
906 print_program_metrics (metrics);
909 /* Start the counter up again now that we're done. */
910 metrics_counter_start (metrics);