]> git.cworth.org Git - fips/blob - metrics.c
8f961331707569e0759bb635e3826af94bb1697a
[fips] / metrics.c
1 /* Copyright © 2013, Intel Corporation
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * THE SOFTWARE.
20  */
21
22 #define _GNU_SOURCE
23
24 #include <inttypes.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/time.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include "fips-dispatch-gl.h"
35
36 #include "metrics.h"
37 #include "context.h"
38 #include "metrics-info.h"
39 #include "xmalloc.h"
40
41 int frames;
42 int verbose;
43
44 #define MAX_MONITORS_IN_FLIGHT 1000
45
46 /* Timer query */
47 typedef struct timer_query
48 {
49         unsigned id;
50
51         metrics_op_t op;
52         struct timer_query *next;
53 } timer_query_t;
54
55 /* Performance-monitor query */
56 typedef struct monitor
57 {
58         unsigned id;
59
60         metrics_op_t op;
61         struct monitor *next;
62 } monitor_t;
63
64 typedef struct op_metrics
65 {
66         /* This happens to also be the index into the
67          * metrics->op_metrics array currently
68          */
69         metrics_op_t op;
70         double time_ns;
71
72         double **counters;
73 } op_metrics_t;
74
75 struct metrics
76 {
77         /* Flag to indicate first frame is being processed. */
78         bool first_frame;
79
80         /* Description of all available peformance counters, counter
81          * groups, their names and IDs, etc. */
82         metrics_info_t *info;
83
84         /* The current operation being measured. */
85         metrics_op_t op;
86
87         /* GL_TIME_ELAPSED query for which glEndQuery has not yet
88          * been called. */
89         unsigned timer_begun_id;
90
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;
95
96         /* Performance monitor for which glEndPerfMonitorAMD has not
97          * yet been called. */
98         unsigned monitor_begun_id;
99
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;
104
105         int monitors_in_flight;
106
107         unsigned num_op_metrics;
108         op_metrics_t *op_metrics;
109
110         /* Per-frame time and latency measurement. */
111         GLint64 swap_begin_timestamp;
112         unsigned swap_end_timestamp_id;
113         GLint64 previous_swap_end_timestamp;
114
115         struct timespec previous_cpu_time_ts;
116         struct timespec cpu_time_ts;
117 };
118
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). */
122 static uint64_t
123 subtract_timestamp (uint64_t a, uint64_t b)
124 {
125         GLint timestamp_bits;
126
127         glGetQueryiv (GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &timestamp_bits);
128
129         /* Note: "<<" isn't defined for the number of bits in the
130          * type, and the compiler will punish you.
131          */
132         if (timestamp_bits == 64)
133                 return a - b;
134
135         if (b > a)
136                 return (1ull << timestamp_bits) + a - b;
137         else
138                 return a - b;
139 }
140
141 metrics_t *
142 metrics_create (metrics_info_t *info)
143 {
144         metrics_t *metrics;
145
146         metrics = xmalloc (sizeof (metrics_t));
147
148         metrics->info = info;
149
150         metrics->first_frame = true;
151
152         metrics->op = 0;
153
154         metrics->timer_begun_id = 0;
155
156         metrics->timer_head = NULL;
157         metrics->timer_tail = NULL;
158
159         metrics->monitor_begun_id = 0;
160
161         metrics->monitor_head = NULL;
162         metrics->monitor_tail = NULL;
163
164         metrics->monitors_in_flight = 0;
165
166         metrics->num_op_metrics = 0;
167         metrics->op_metrics = NULL;
168
169         glGenQueries (1, &metrics->swap_end_timestamp_id);
170
171         /* Get the first frame timestamp started immediately. */
172         glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
173
174         /* As well as the initial CPU time. */
175         clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
176
177         return metrics;
178 }
179
180 void
181 metrics_fini (metrics_t *metrics)
182 {
183         timer_query_t *timer, *timer_next;
184         monitor_t *monitor, *monitor_next;
185
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;
191         }
192
193         for (timer = metrics->timer_head;
194              timer;
195              timer = timer_next)
196         {
197                 glDeleteQueries (1, &timer->id);
198                 timer_next = timer->next;
199                 free (timer);
200         }
201         metrics->timer_head = NULL;
202         metrics->timer_tail = NULL;
203
204         if (metrics->info->have_perfmon) {
205
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;
210                 }
211
212                 for (monitor = metrics->monitor_head;
213                      monitor;
214                      monitor = monitor_next)
215                 {
216                         glDeletePerfMonitorsAMD (1, &monitor->id);
217                         monitor_next = monitor->next;
218                         free (monitor);
219                 }
220                 metrics->monitor_head = NULL;
221                 metrics->monitor_tail = NULL;
222
223         }
224
225         metrics->monitors_in_flight = 0;
226 }
227
228 void
229 metrics_destroy (metrics_t *metrics)
230 {
231         metrics_fini (metrics);
232
233         free (metrics);
234 }
235
236 static const char *
237 metrics_op_string (metrics_op_t op)
238 {
239         if (op >= METRICS_OP_SHADER)
240                 return "Shader program";
241
242         switch (op)
243         {
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:
251                 return "glBitmap*";
252         case METRICS_OP_BLIT_FRAMEBUFFER:
253                 return "glBlitFramebuffer*";
254         case METRICS_OP_CLEAR:
255                 return "glClear(+)";
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*(+)";
272         default:
273                 fprintf (stderr, "fips: Internal error: "
274                          "Unknown metrics op value: %d\n", op);
275                 exit (1);
276         }
277
278         return "";
279 }
280
281 void
282 metrics_counter_start (metrics_t *metrics)
283 {
284         unsigned i;
285
286         /* Initialize the timer_query object. */
287         glGenQueries (1, &metrics->timer_begun_id);
288
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);
294                 return;
295         }
296
297         /* Initialize the performance-monitor object */
298         glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
299
300         for (i = 0; i < metrics->info->num_groups; i++)
301         {
302                 metrics_group_info_t *group;
303                 int num_counters;
304
305                 group = &metrics->info->groups[i];
306
307                 num_counters = group->num_counters;
308                 if (group->max_active_counters < group->num_counters)
309                 {
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;
314
315                 }
316
317                 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
318                                                GL_TRUE, group->id,
319                                                num_counters,
320                                                group->counter_ids);
321         }
322
323         /* Start the queries */
324         glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
325
326         glBeginPerfMonitorAMD (metrics->monitor_begun_id);
327 }
328
329 void
330 metrics_counter_stop (metrics_t *metrics)
331 {
332         timer_query_t *timer;
333         monitor_t *monitor;
334
335         /* Stop the current timer and monitor. */
336         glEndQuery (GL_TIME_ELAPSED);
337
338         if (metrics->info->have_perfmon)
339                 glEndPerfMonitorAMD (metrics->monitor_begun_id);
340
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));
344
345         timer->op = metrics->op;
346         timer->id = metrics->timer_begun_id;
347         timer->next = NULL;
348
349         if (metrics->timer_tail) {
350                 metrics->timer_tail->next = timer;
351                 metrics->timer_tail = timer;
352         } else {
353                 metrics->timer_tail = timer;
354                 metrics->timer_head = timer;
355         }
356
357         if (metrics->info->have_perfmon) {
358                 /* Create a new performance-monitor query */
359                 monitor = xmalloc (sizeof (monitor_t));
360
361                 monitor->op = metrics->op;
362                 monitor->id = metrics->monitor_begun_id;
363                 monitor->next = NULL;
364
365                 if (metrics->monitor_tail) {
366                         metrics->monitor_tail->next = monitor;
367                         metrics->monitor_tail = monitor;
368                 } else {
369                         metrics->monitor_tail = monitor;
370                         metrics->monitor_head = monitor;
371                 }
372         }
373
374         metrics->monitors_in_flight++;
375
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
379          * available by now.)
380          */
381         if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
382                 metrics_collect_available (metrics);
383 }
384
385 void
386 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
387 {
388         metrics->op = op;
389 }
390
391 metrics_op_t
392 metrics_get_current_op (metrics_t *metrics)
393 {
394         return metrics->op;
395 }
396
397 static void
398 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
399 {
400         unsigned i, j;
401
402         metrics->op = op;
403         metrics->time_ns = 0.0;
404
405         metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
406
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;
412         }
413 }
414
415 static op_metrics_t *
416 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
417 {
418         unsigned i;
419
420         if (op >= metrics->num_op_metrics)
421         {
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);
426
427                 metrics->num_op_metrics = op + 1;
428         }
429
430         return &metrics->op_metrics[op];
431 }
432
433 static void
434 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
435                             GLuint *result, GLuint size)
436 {
437 #define CONSUME(var)                                                    \
438         if (p + sizeof(var) > ((unsigned char *) result) + size)        \
439         {                                                               \
440                 fprintf (stderr, "Unexpected end-of-buffer while "      \
441                          "parsing results\n");                          \
442                 value = 0.0;                                            \
443                 break;                                                  \
444         }                                                               \
445         (var) = *((typeof(var) *) p);                                   \
446         p += sizeof(var);
447
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;
451
452         while (p < ((unsigned char *) result) + size)
453         {
454                 GLuint group_id, group_index;
455                 GLuint counter_id, counter_index;
456                 metrics_group_info_t *group;
457                 double value;
458                 unsigned i;
459
460                 CONSUME (group_id);
461                 CONSUME (counter_id);
462
463                 for (i = 0; i < info->num_groups; i++) {
464                         if (info->groups[i].id == group_id)
465                                 break;
466                 }
467                 group_index = i;
468                 assert (group_index < info->num_groups);
469                 group = &info->groups[group_index];
470
471                 for (i = 0; i < group->num_counters; i++) {
472                         if (group->counter_ids[i] == counter_id)
473                                 break;
474                 }
475                 counter_index = i;
476                 assert (counter_index < group->num_counters);
477
478                 switch (group->counter_types[counter_index])
479                 {
480                         uint uint_value;
481                         uint64_t uint64_value;
482                         float float_value;
483                 case GL_UNSIGNED_INT:
484                         CONSUME (uint_value);
485                         value = uint_value;
486                         break;
487                 case GL_UNSIGNED_INT64_AMD:
488                         CONSUME (uint64_value);
489                         value = uint64_value;
490                         break;
491                 case GL_PERCENTAGE_AMD:
492                 case GL_FLOAT:
493                         CONSUME (float_value);
494                         value = float_value;
495                         break;
496                 default:
497                         fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
498                                  group->counter_types[counter_index]);
499                         value = 0.0;
500                         break;
501                 }
502
503                 op_metrics->counters[group_index][counter_index] += value;
504         }
505 }
506
507 static void
508 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
509 {
510         op_metrics_t *op_metrics;
511
512         op_metrics = _get_op_metrics (metrics, op);
513
514         op_metrics->time_ns += time_ns;
515 }
516
517 typedef struct per_stage_metrics
518 {
519         op_metrics_t *metrics;
520         shader_stage_info_t *stage;
521         double time_ns;
522         double active;
523 } per_stage_metrics_t;
524
525 static int
526 _is_shader_stage_counter (metrics_info_t *info,
527                           unsigned group_index,
528                           unsigned counter_index)
529 {
530         shader_stage_info_t *stage;
531         unsigned i;
532
533         for (i = 0; i < info->num_shader_stages; i++) {
534                 stage = &info->stages[i];
535
536                 if (stage->active_group_index == group_index &&
537                     stage->active_counter_index == counter_index)
538                 {
539                         return 1;
540                 }
541
542                 if (stage->stall_group_index == group_index &&
543                     stage->stall_counter_index == counter_index)
544                 {
545                         return 1;
546                 }
547         }
548
549         return 0;
550 }
551
552 static void
553 print_per_stage_metrics (metrics_t *metrics,
554                          per_stage_metrics_t *per_stage,
555                          double total)
556 {
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;
562         double value;
563
564         /* Don't print anything for stages with no alloted time. */
565         if (per_stage->time_ns == 0.0)
566                 return;
567
568         op_string = metrics_op_string (op_metrics->op);
569
570         printf ("%21s", op_string);
571
572         if (op_metrics->op >= METRICS_OP_SHADER) {
573                 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
574         } else {
575                 printf ("    ");
576
577         }
578
579         if (per_stage->stage)
580                 printf (" %cS:", per_stage->stage->name[0]);
581         else
582                 printf ("   :");
583
584         printf ("\t%7.2f ms (%4.1f%%)",
585                 per_stage->time_ns / 1e6,
586                 per_stage->time_ns / total * 100);
587
588         if (per_stage->active)
589                 printf (", %4.1f%% active", per_stage->active * 100);
590
591         printf ("\n");
592
593         /* I'm not seeing a lot of value printing the rest of these
594          * performance counters by default yet. Use --verbose to get
595          * them for now. */
596         if (! verbose)
597                 return;
598
599         printf ("[");
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++) {
603
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))
608                                 continue;
609
610                         value = op_metrics->counters[group_index][counter];
611                         if (value == 0.0)
612                                 continue;
613                         printf ("%s: %.2f ", group->counter_names[counter],
614                                 value / 1e6);
615                 }
616         }
617         printf ("]\n");
618 }
619
620 static int
621 time_compare(const void *in_a, const void *in_b, void *arg unused)
622 {
623         const per_stage_metrics_t *a = in_a;
624         const per_stage_metrics_t *b = in_b;
625
626
627         if (a->time_ns < b->time_ns)
628                 return -1;
629         if (a->time_ns > b->time_ns)
630                 return 1;
631         return 0;
632 }
633
634 static void
635 print_program_metrics (metrics_t *metrics)
636 {
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;
641         op_metrics_t *op;
642         unsigned group_index, counter_index;
643         unsigned i, j, num_sorted;
644
645         if (! info->have_perfmon &&
646             ! info->printed_missing_perfmon_warning)
647         {
648                 fprintf (stderr,
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");
652
653                 info->printed_missing_perfmon_warning = true;
654         }
655
656         /* Make a sorted list of the per-stage operations by time
657          * used, and figure out the total so we can print percentages.
658          */
659         if (num_shader_stages)
660                 num_sorted = metrics->num_op_metrics * num_shader_stages;
661         else
662                 num_sorted = metrics->num_op_metrics;
663
664         sorted = xmalloc (sizeof (*sorted) * num_sorted);
665
666         total_time = 0.0;
667
668         for (i = 0; i < metrics->num_op_metrics; i++) {
669
670                 op = &metrics->op_metrics[i];
671
672                 /* Accumulate total time across all ops. */
673                 total_time += op->time_ns;
674
675                 /* Also, find total cycles in all stages of this op. */
676                 op_cycles = 0.0;
677
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;
684                 }
685
686                 for (j = 0; j < num_shader_stages; j++) {
687                         /* Active cycles */
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];
691
692                         /* Stall cycles */
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];
696                 }
697
698                 for (j = 0; j < num_shader_stages; j++) {
699                         double active_cycles, stall_cycles, stage_cycles;
700
701                         /* Active 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];
705
706                         /* Stall cycles */
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];
710
711                         stage_cycles = active_cycles + stall_cycles;
712
713                         per_stage = &sorted[i * num_shader_stages + j];
714                         per_stage->metrics = op;
715
716                         if (op_cycles) {
717                                 per_stage->stage = &info->stages[j];
718                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
719                         } else {
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;
727                                 if (j == 0) {
728                                         per_stage->time_ns = op->time_ns;
729                                 } else {
730                                         per_stage->time_ns = 0.0;
731                                 }
732                         }
733
734                         if (stage_cycles) {
735                                 per_stage->active = active_cycles / stage_cycles;
736                         } else {
737                                 per_stage->active = 0.0;
738                         }
739                 }
740         }
741
742         qsort_r (sorted, num_sorted, sizeof (*sorted),
743                  time_compare, metrics->op_metrics);
744
745         for (i = 0; i < num_sorted; i++)
746                 print_per_stage_metrics (metrics, &sorted[i], total_time);
747
748         free (sorted);
749 }
750
751 void
752 metrics_collect_available (metrics_t *metrics)
753 {
754         /* Consume all timer queries that are ready. */
755         timer_query_t *timer = metrics->timer_head;
756
757         while (timer) {
758                 GLuint available, elapsed;
759
760                 glGetQueryObjectuiv (timer->id,
761                                      GL_QUERY_RESULT_AVAILABLE, &available);
762                 if (! available)
763                         break;
764
765                 glGetQueryObjectuiv (timer->id,
766                                      GL_QUERY_RESULT, &elapsed);
767
768                 accumulate_program_time (metrics, timer->op, elapsed);
769
770                 metrics->timer_head = timer->next;
771                 if (metrics->timer_head == NULL)
772                         metrics->timer_tail = NULL;
773
774                 glDeleteQueries (1, &timer->id);
775
776                 free (timer);
777                 timer = metrics->timer_head;
778         }
779
780         if (! metrics->info->have_perfmon)
781                 return;
782
783         /* And similarly for all performance monitors that are ready. */
784         monitor_t *monitor = metrics->monitor_head;
785
786         while (monitor) {
787                 GLuint available, result_size, *result;
788                 GLint bytes_written;
789
790                 glGetPerfMonitorCounterDataAMD (monitor->id,
791                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
792                                                 sizeof (available), &available,
793                                                 NULL);
794                 if (! available)
795                         break;
796
797                 glGetPerfMonitorCounterDataAMD (monitor->id,
798                                                 GL_PERFMON_RESULT_SIZE_AMD,
799                                                 sizeof (result_size),
800                                                 &result_size, NULL);
801
802                 result = xmalloc (result_size);
803
804                 glGetPerfMonitorCounterDataAMD (monitor->id,
805                                                 GL_PERFMON_RESULT_AMD,
806                                                 result_size, result,
807                                                 &bytes_written);
808
809                 accumulate_program_metrics (metrics, monitor->op, result, result_size);
810
811                 free (result);
812
813                 metrics->monitor_head = monitor->next;
814                 if (metrics->monitor_head == NULL)
815                         metrics->monitor_tail = NULL;
816
817                 glDeletePerfMonitorsAMD (1, &monitor->id);
818
819                 free (monitor);
820
821                 metrics->monitors_in_flight--;
822
823                 monitor = metrics->monitor_head;
824         }
825 }
826
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)
830
831 void
832 metrics_end_frame_pre_swap (metrics_t *metrics)
833 {
834         GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
835         double cpu_time;
836
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.  */
840
841         metrics_counter_stop (metrics);
842
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
845          * swap is available.
846          *
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).
852          *
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.
856          */
857
858         glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
859                                GL_QUERY_RESULT, &swap_end_timestamp);
860  
861         if (metrics->first_frame) {
862                 /* Print header */
863                 printf ("# frame: Frame_Number Frame_Time_milliseconds Frame_latency_milliseconds CPU_load GPU_load\n");
864
865                 metrics->first_frame = false;
866         } else {
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);
870
871                 latency_ns = subtract_timestamp (swap_end_timestamp,
872                                                  metrics->swap_begin_timestamp);
873
874                 cpu_time = SUBTRACT_TIMESPEC (metrics->cpu_time_ts,
875                                               metrics->previous_cpu_time_ts);
876
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",
881                         frames - 1,
882                         (double) frame_time_ns / 1e6,
883                         (double) latency_ns / 1e6,
884                         cpu_time / (frame_time_ns / 1e9),
885                         0.0);
886         }
887
888         metrics->previous_swap_end_timestamp = swap_end_timestamp;
889         metrics->previous_cpu_time_ts = metrics->cpu_time_ts;
890
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. */
894
895         glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
896 }
897
898 void
899 metrics_end_frame_post_swap (metrics_t *metrics)
900 {
901         static int initialized = 0;
902         static struct timeval tv_start, tv_now;
903         int err;
904
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. */
910
911         glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
912
913         /* Query the total amount of CPU time spent by this process. */
914         err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &metrics->cpu_time_ts);
915         if (err == -1 ) {
916                 fprintf (stderr, "Failed to query CPU time with clock_gettime: %s\n",
917                          strerror (errno));
918                 exit (1);
919         }
920
921         if (! initialized) {
922                 gettimeofday (&tv_start, NULL);
923                 if (getenv ("FIPS_VERBOSE"))
924                         verbose = 1;
925                 initialized = 1;
926         }
927
928         metrics_collect_available (metrics);
929
930         frames++;
931
932         if (frames % 15 == 0) {
933                 double fps;
934
935                 gettimeofday (&tv_now, NULL);
936
937                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
938                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
939
940                 printf("FPS: %.3f\n", fps);
941
942                 print_program_metrics (metrics);
943         }
944
945         /* Start the counter up again now that we're done. */
946         metrics_counter_start (metrics);
947 }