]> git.cworth.org Git - fips/blob - metrics.c
Perform reporting on a per-shader-stage basis
[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 <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <sys/time.h>
28
29 #include "fips-dispatch-gl.h"
30
31 #include "metrics.h"
32 #include "xmalloc.h"
33
34 /* Timer query */
35 typedef struct timer_query
36 {
37         unsigned id;
38
39         metrics_op_t op;
40         struct timer_query *next;
41 } timer_query_t;
42
43 /* Performance-monitor query */
44 typedef struct monitor
45 {
46         unsigned id;
47
48         metrics_op_t op;
49         struct monitor *next;
50 } monitor_t;
51
52
53 typedef struct op_metrics
54 {
55         /* This happens to also be the index into the
56          * ctx->op_metrics array currently
57          */
58         metrics_op_t op;
59         double time_ns;
60
61         double **counters;
62 } op_metrics_t;
63
64 typedef struct metrics_group_info
65 {
66         GLuint id;
67         char *name;
68
69         GLuint num_counters;
70         GLuint max_active_counters;
71
72         GLuint *counter_ids;
73         char **counter_names;
74         GLuint *counter_types;
75
76 } metrics_group_info_t;
77
78 typedef struct shader_stage_info
79 {
80         char *name;
81
82         GLuint active_group_index;
83         GLuint active_counter_index;
84
85         GLuint stall_group_index;
86         GLuint stall_counter_index;
87
88 } shader_stage_info_t;
89
90 typedef struct metrics_info
91 {
92         int initialized;
93
94         unsigned num_groups;
95         metrics_group_info_t *groups;
96
97         unsigned num_shader_stages;
98         shader_stage_info_t *stages;
99
100 } metrics_info_t;
101
102 typedef struct context
103 {
104         metrics_info_t metrics_info;
105
106         metrics_op_t op;
107
108         timer_query_t *timer_head;
109         timer_query_t *timer_tail;
110
111         monitor_t *monitor_head;
112         monitor_t *monitor_tail;
113
114         unsigned num_op_metrics;
115         op_metrics_t *op_metrics;
116 } context_t;
117
118 /* FIXME: Need a map from integers to context objects and track the
119  * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
120
121 context_t current_context;
122
123 int frames;
124 int verbose;
125
126 static void
127 metrics_group_info_init (metrics_group_info_t *group, GLuint id)
128 {
129         GLsizei length;
130         unsigned i;
131
132         group->id = id;
133
134         /* Get name */
135         glGetPerfMonitorGroupStringAMD (id, 0, &length, NULL);
136
137         group->name = xmalloc (length + 1);
138
139         glGetPerfMonitorGroupStringAMD (id, length + 1, NULL, group->name);
140
141         /* Get number of counters */
142         group->num_counters = 0;
143         group->max_active_counters = 0;
144         glGetPerfMonitorCountersAMD (group->id,
145                                      (int *) &group->num_counters,
146                                      (int *) &group->max_active_counters,
147                                      0, NULL);
148
149         /* Get counter numbers */
150         group->counter_ids = xmalloc (group->num_counters * sizeof (GLuint));
151
152         glGetPerfMonitorCountersAMD (group->id, NULL, NULL,
153                                      group->num_counters,
154                                      group->counter_ids);
155
156         /* Get counter names */
157         group->counter_names = xmalloc (group->num_counters * sizeof (char *));
158         group->counter_types = xmalloc (group->num_counters * sizeof (GLuint));
159
160         for (i = 0; i < group->num_counters; i++) {
161                 glGetPerfMonitorCounterInfoAMD (group->id,
162                                                 group->counter_ids[i],
163                                                 GL_COUNTER_TYPE_AMD,
164                                                 &group->counter_types[i]);
165
166                 /* We assume that all peformance counters are made
167                  * available as uint32 values. The code calling
168                  * CONSUME in accumulate_program_metrics will need to
169                  * be extended to accomodate other counter values. */
170                 if (group->counter_types[i] != GL_UNSIGNED_INT) {
171                         fprintf (stderr, "fips: Internal error: No support for non-uint counter values\n");
172                         exit (1);
173                 }
174
175                 glGetPerfMonitorCounterStringAMD (group->id,
176                                                   group->counter_ids[i],
177                                                   0, &length, NULL);
178
179                 group->counter_names[i] = xmalloc (length + 1);
180
181                 glGetPerfMonitorCounterStringAMD (group->id,
182                                                   group->counter_ids[i],
183                                                   length + 1, NULL,
184                                                   group->counter_names[i]);
185         }
186 }
187
188 static void
189 metrics_group_info_fini (metrics_group_info_t *group)
190 {
191         unsigned i;
192
193         for (i = 0; i < group->num_counters; i++)
194                 free (group->counter_names[i]);
195
196         free (group->counter_types);
197         free (group->counter_names);
198         free (group->counter_ids);
199
200         free (group->name);
201 }
202
203 static void
204 metrics_info_fini (metrics_info_t *info);
205
206 /* A helper function, part of metrics_info_init below. */
207
208 typedef enum {
209         SHADER_ACTIVE,
210         SHADER_STALL
211 } shader_phase_t;
212
213 static void
214 _add_shader_stage (metrics_info_t *info, const char *name,
215                    GLuint group_index, GLuint counter_index,
216                    shader_phase_t phase)
217 {
218         shader_stage_info_t *stage;
219         char *stage_name, *space;
220         unsigned i;
221
222         stage_name = xstrdup (name);
223
224         /* Terminate the stage name at the first space.
225          *
226          * This is valid for counter names such as:
227          *
228          *      "Vertex Shader Active Time"
229          * or
230          *      "Vertex Shader Stall Time - Core Stall"
231          */
232         space = strchr (stage_name, ' ');
233         if (space)
234                 *space = '\0';
235
236         /* Look for an existing stage of the given name. */
237         stage = NULL;
238
239         for (i = 0; i < info->num_shader_stages; i++) {
240                 if (strcmp (info->stages[i].name, stage_name) == 0) {
241                         stage = &info->stages[i];
242                         break;
243                 }
244         }
245
246         if (stage == NULL) {
247                 info->num_shader_stages++;
248                 info->stages = xrealloc (info->stages,
249                                          info->num_shader_stages *
250                                          sizeof (shader_stage_info_t));
251                 stage = &info->stages[info->num_shader_stages - 1];
252                 stage->name = xstrdup (stage_name);
253                 stage->active_group_index = 0;
254                 stage->active_counter_index = 0;
255                 stage->stall_group_index = 0;
256                 stage->stall_counter_index = 0;
257         }
258
259         if (phase == SHADER_ACTIVE) {
260                 stage->active_group_index = group_index;
261                 stage->active_counter_index = counter_index;
262         } else {
263                 stage->stall_group_index = group_index;
264                 stage->stall_counter_index = counter_index;
265         }
266
267         free (stage_name);
268 }
269
270 void
271 metrics_info_init (void)
272 {
273         unsigned i, j;
274         GLuint *group_ids;
275         metrics_info_t *info = &current_context.metrics_info;
276
277         if (info->initialized)
278                 metrics_info_fini (info);
279
280         glGetPerfMonitorGroupsAMD ((int *) &info->num_groups, 0, NULL);
281
282         group_ids = xmalloc (info->num_groups * sizeof (GLuint));
283
284         glGetPerfMonitorGroupsAMD (NULL, info->num_groups, group_ids);
285
286         info->groups = xmalloc (info->num_groups * sizeof (metrics_group_info_t));
287
288         for (i = 0; i < info->num_groups; i++)
289                 metrics_group_info_init (&info->groups[i], group_ids[i]);
290
291         free (group_ids);
292
293         /* Identify each shader stage (by looking at
294          * performance-counter names for specific patterns) and
295          * initialize structures referring to the corresponding
296          * counter numbers for each stage. */
297         info->num_shader_stages = 0;
298         info->stages = NULL;
299
300         for (i = 0; i < info->num_groups; i++) {
301                 metrics_group_info_t *group = &info->groups[i];
302                 for (j = 0; j < group->num_counters; j++) {
303                         char *name = group->counter_names[j];
304                         if (strstr (name, "Shader Active Time")) {
305                                 _add_shader_stage (info, name, i, j,
306                                                    SHADER_ACTIVE);
307                         }
308                         if (strstr (name, "Shader Stall Time")) {
309                                 _add_shader_stage (info, name, i, j,
310                                                    SHADER_STALL);
311                         }
312                 }
313         }
314
315         info->initialized = 1;
316 }
317
318 static void
319 metrics_info_fini (metrics_info_t *info)
320 {
321         unsigned i;
322
323         for (i = 0; i < info->num_groups; i++)
324                 metrics_group_info_fini (&info->groups[i]);
325
326         free (info->groups);
327
328         for (i = 0; i < info->num_shader_stages; i++)
329                 free (info->stages[i].name);
330
331         free (info->stages);
332 }
333
334 static const char *
335 metrics_op_string (metrics_op_t op)
336 {
337         if (op >= METRICS_OP_SHADER)
338                 return "Shader program";
339
340         switch (op)
341         {
342         case METRICS_OP_ACCUM:
343                 return "glAccum*(+)";
344         case METRICS_OP_BUFFER_DATA:
345                 return "glBufferData(+)";
346         case METRICS_OP_BUFFER_SUB_DATA:
347                 return "glCopyBufferSubData*";
348         case METRICS_OP_BITMAP:
349                 return "glBitmap*";
350         case METRICS_OP_BLIT_FRAMEBUFFER:
351                 return "glBlitFramebuffer*";
352         case METRICS_OP_CLEAR:
353                 return "glClear(+)";
354         case METRICS_OP_CLEAR_BUFFER_DATA:
355                 return "glCearBufferData(+)";
356         case METRICS_OP_CLEAR_TEX_IMAGE:
357                 return "glClearTexImage(+)";
358         case METRICS_OP_COPY_PIXELS:
359                 return "glCopyPixels";
360         case METRICS_OP_COPY_TEX_IMAGE:
361                 return "glCopyTexImage(+)";
362         case METRICS_OP_DRAW_PIXELS:
363                 return "glDrawPixels";
364         case METRICS_OP_GET_TEX_IMAGE:
365                 return "glGetTexImage(+)";
366         case METRICS_OP_READ_PIXELS:
367                 return "glReadPixels*";
368         case METRICS_OP_TEX_IMAGE:
369                 return "glTexImage*(+)";
370         default:
371                 fprintf (stderr, "fips: Internal error: "
372                          "Unknown metrics op value: %d\n", op);
373                 exit (1);
374         }
375
376         return "";
377 }
378
379 void
380 metrics_counter_start (void)
381 {
382         context_t *ctx = &current_context;
383         timer_query_t *timer;
384         monitor_t *monitor;
385         unsigned i;
386
387         /* Create new timer query, add to list */
388         timer = xmalloc (sizeof (timer_query_t));
389
390         timer->op = ctx->op;
391         timer->next = NULL;
392
393         if (ctx->timer_tail) {
394                 ctx->timer_tail->next = timer;
395                 ctx->timer_tail = timer;
396         } else {
397                 ctx->timer_tail = timer;
398                 ctx->timer_head = timer;
399         }
400
401         /* Create a new performance-monitor query */
402         monitor = xmalloc (sizeof (monitor_t));
403
404         monitor->op = ctx->op;
405         monitor->next = NULL;
406
407         if (ctx->monitor_tail) {
408                 ctx->monitor_tail->next = monitor;
409                 ctx->monitor_tail = monitor;
410         } else {
411                 ctx->monitor_tail = monitor;
412                 ctx->monitor_head = monitor;
413         }
414
415         /* Initialize the timer_query and monitor objects */
416         glGenQueries (1, &timer->id);
417
418         glGenPerfMonitorsAMD (1, &monitor->id);
419
420         for (i = 0; i < ctx->metrics_info.num_groups; i++)
421         {
422                 metrics_group_info_t *group;
423                 int num_counters;
424
425                 group = &ctx->metrics_info.groups[i];
426
427                 num_counters = group->num_counters;
428                 if (group->max_active_counters < group->num_counters)
429                 {
430                         fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
431                                  group->max_active_counters,
432                                  group->num_counters, i);
433                         num_counters = group->max_active_counters;
434
435                 }
436
437                 glSelectPerfMonitorCountersAMD(monitor->id,
438                                                GL_TRUE, group->id,
439                                                num_counters,
440                                                group->counter_ids);
441         }
442
443         /* Start the queries */
444         glBeginQuery (GL_TIME_ELAPSED, timer->id);
445
446         glBeginPerfMonitorAMD (monitor->id);
447 }
448
449 void
450 metrics_counter_stop (void)
451 {
452         glEndQuery (GL_TIME_ELAPSED);
453         glEndPerfMonitorAMD (current_context.monitor_tail->id);
454 }
455
456 void
457 metrics_set_current_op (metrics_op_t op)
458 {
459         current_context.op = op;
460 }
461
462 metrics_op_t
463 metrics_get_current_op (void)
464 {
465         return current_context.op;
466 }
467
468 static void
469 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
470 {
471         metrics_info_t *info = &ctx->metrics_info;
472         unsigned i, j;
473
474         metrics->op = op;
475         metrics->time_ns = 0.0;
476
477         metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
478
479         for (i = 0; i < info->num_groups; i++) {
480                 metrics->counters[i] = xmalloc (sizeof (double) *
481                                                 info->groups[i].num_counters);
482                 for (j = 0; j < info->groups[i].num_counters; j++)
483                         metrics->counters[i][j] = 0.0;
484         }
485 }
486
487 static op_metrics_t *
488 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
489 {
490         unsigned i;
491
492         if (op >= ctx->num_op_metrics)
493         {
494                 ctx->op_metrics = realloc (ctx->op_metrics,
495                                            (op + 1) * sizeof (op_metrics_t));
496                 for (i = ctx->num_op_metrics; i < op + 1; i++)
497                         op_metrics_init (ctx, &ctx->op_metrics[i], i);
498
499                 ctx->num_op_metrics = op + 1;
500         }
501
502         return &ctx->op_metrics[op];
503 }
504
505 static void
506 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
507 {
508 #define CONSUME(var)                                                    \
509         if (p + sizeof(var) > ((unsigned char *) result) + size)        \
510         {                                                               \
511                 fprintf (stderr, "Unexpected end-of-buffer while "      \
512                          "parsing results\n");                          \
513                 break;                                                  \
514         }                                                               \
515         (var) = *((typeof(var) *) p);                                   \
516         p += sizeof(var);
517
518         context_t *ctx = &current_context;
519         metrics_info_t *info = &ctx->metrics_info;
520         unsigned char *p = (unsigned char *) result;
521
522         while (p < ((unsigned char *) result) + size)
523         {
524                 GLuint group_id, group_index;
525                 GLuint counter_id, counter_index;
526                 metrics_group_info_t *group;
527                 uint32_t value;
528                 unsigned i;
529
530                 CONSUME (group_id);
531                 CONSUME (counter_id);
532                 CONSUME (value);
533
534                 for (i = 0; i < info->num_groups; i++) {
535                         if (info->groups[i].id == i)
536                                 break;
537                 }
538                 group_index = i;
539                 assert (group_index < info->num_groups);
540                 group = &info->groups[group_index];
541
542                 for (i = 0; i < group->num_counters; i++) {
543                         if (group->counter_ids[i] == counter_id)
544                                 break;
545                 }
546                 counter_index = i;
547                 assert (counter_index < group->num_counters);
548
549                 ctx->op_metrics[op].counters[group_index][counter_index] += value;
550         }
551 }
552
553 static void
554 accumulate_program_time (metrics_op_t op, unsigned time_ns)
555 {
556         op_metrics_t *metrics;
557
558         metrics = ctx_get_op_metrics (&current_context, op);
559
560         metrics->time_ns += time_ns;
561 }
562
563 typedef struct per_stage_metrics
564 {
565         op_metrics_t *metrics;
566         shader_stage_info_t *stage;
567         double time_ns;
568         double active;
569 } per_stage_metrics_t;
570
571 static int
572 _is_shader_stage_counter (metrics_info_t *info,
573                           unsigned group_index,
574                           unsigned counter_index)
575 {
576         shader_stage_info_t *stage;
577         unsigned i;
578
579         for (i = 0; i < info->num_shader_stages; i++) {
580                 stage = &info->stages[i];
581
582                 if (stage->active_group_index == group_index &&
583                     stage->active_counter_index == counter_index)
584                 {
585                         return 1;
586                 }
587
588                 if (stage->stall_group_index == group_index &&
589                     stage->stall_counter_index == counter_index)
590                 {
591                         return 1;
592                 }
593         }
594
595         return 0;
596 }
597
598 static void
599 print_per_stage_metrics (context_t *ctx,
600                          per_stage_metrics_t *per_stage,
601                          double total)
602 {
603         metrics_info_t *info = &ctx->metrics_info;
604         op_metrics_t *metric = per_stage->metrics;
605         metrics_group_info_t *group;
606         const char *op_string;
607         unsigned group_index, counter;
608         double value;
609
610         /* Don't print anything for stages with no alloted time. */
611         if (per_stage->time_ns == 0.0)
612                 return;
613
614         op_string = metrics_op_string (metric->op);
615
616         printf ("%21s", op_string);
617
618         if (metric->op >= METRICS_OP_SHADER) {
619                 printf (" %3d", metric->op - METRICS_OP_SHADER);
620         } else {
621                 printf ("    ");
622
623         }
624         printf (" %cS:", per_stage->stage->name[0]);
625
626         printf ("\t%7.2f ms (%4.1f%%)",
627                 per_stage->time_ns / 1e6,
628                 per_stage->time_ns / total * 100);
629
630         if (per_stage->active)
631                 printf (", %4.1f%% active", per_stage->active * 100);
632
633         printf ("\n");
634
635         /* I'm not seeing a lot of value printing the rest of these
636          * performance counters by default yet. Use --verbose to get
637          * them for now. */
638         if (! verbose)
639                 return;
640
641         printf ("[");
642         for (group_index = 0; group_index < info->num_groups; group_index++) {
643                 group = &info->groups[group_index];
644                 for (counter = 0; counter < group->num_counters; counter++) {
645
646                         /* Don't print this counter value if it's a
647                          * per-stage cycle counter, (which we have
648                          * already accounted for). */
649                         if (_is_shader_stage_counter (info, group_index, counter))
650                                 continue;
651
652                         value = metric->counters[group_index][counter];
653                         if (value == 0.0)
654                                 continue;
655                         printf ("%s: %.2f ", group->counter_names[counter],
656                                 value / 1e6);
657                 }
658         }
659         printf ("]\n");
660 }
661
662 static int
663 time_compare(const void *in_a, const void *in_b, void *arg unused)
664 {
665         const per_stage_metrics_t *a = in_a;
666         const per_stage_metrics_t *b = in_b;
667
668
669         if (a->time_ns < b->time_ns)
670                 return -1;
671         if (a->time_ns > b->time_ns)
672                 return 1;
673         return 0;
674 }
675
676 static void
677 print_program_metrics (void)
678 {
679         context_t *ctx = &current_context;
680         metrics_info_t *info = &ctx->metrics_info;
681         unsigned num_shader_stages = info->num_shader_stages;
682         per_stage_metrics_t *sorted, *per_stage;
683         double total_time, op_cycles;
684         op_metrics_t *op;
685         unsigned group_index, counter_index;
686         unsigned i, j, num_sorted;
687
688         /* Make a sorted list of the per-stage operations by time
689          * used, and figure out the total so we can print percentages.
690          */
691         num_sorted = ctx->num_op_metrics * num_shader_stages;
692
693         sorted = xmalloc (sizeof (*sorted) * num_sorted);
694
695         total_time = 0.0;
696
697         for (i = 0; i < ctx->num_op_metrics; i++) {
698
699                 op = &ctx->op_metrics[i];
700
701                 /* Accumulate total time across all ops. */
702                 total_time += op->time_ns;
703
704                 /* Also, find total cycles in all stages of this op. */
705                 op_cycles = 0.0;
706
707                 for (j = 0; j < num_shader_stages; j++) {
708                         /* Active cycles */
709                         group_index = info->stages[j].active_group_index;
710                         counter_index = info->stages[j].active_counter_index;
711                         op_cycles += op->counters[group_index][counter_index];
712
713                         /* Stall cycles */
714                         group_index = info->stages[j].stall_group_index;
715                         counter_index = info->stages[j].stall_counter_index;
716                         op_cycles += op->counters[group_index][counter_index];
717                 }
718
719                 for (j = 0; j < num_shader_stages; j++) {
720                         double active_cycles, stall_cycles, stage_cycles;
721
722                         /* Active cycles */
723                         group_index = info->stages[j].active_group_index;
724                         counter_index = info->stages[j].active_counter_index;
725                         active_cycles = op->counters[group_index][counter_index];
726
727                         /* Stall cycles */
728                         group_index = info->stages[j].stall_group_index;
729                         counter_index = info->stages[j].stall_counter_index;
730                         stall_cycles = op->counters[group_index][counter_index];
731
732                         stage_cycles = active_cycles + stall_cycles;
733
734                         per_stage = &sorted[i * num_shader_stages + j];
735                         per_stage->metrics = op;
736                         per_stage->stage = &info->stages[j];
737                         if (op_cycles)
738                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
739                         else
740                                 per_stage->time_ns = 0.0;
741                         if (stage_cycles)
742                                 per_stage->active = active_cycles / stage_cycles;
743                         else
744                                 per_stage->active = 0.0;
745                 }
746         }
747
748         qsort_r (sorted, num_sorted, sizeof (*sorted),
749                  time_compare, ctx->op_metrics);
750
751         for (i = 0; i < num_sorted; i++)
752                 print_per_stage_metrics (ctx, &sorted[i], total_time);
753
754         free (sorted);
755 }
756
757 /* Called at program exit */
758 static void
759 metrics_exit (void)
760 {
761         if (verbose)
762                 printf ("fips: terminating\n");
763
764         metrics_info_fini (&current_context.metrics_info);
765 }
766
767
768 void
769 metrics_end_frame (void)
770 {
771         static int initialized = 0;
772         static struct timeval tv_start, tv_now;
773
774         if (! initialized) {
775                 gettimeofday (&tv_start, NULL);
776                 atexit (metrics_exit);
777                 if (getenv ("FIPS_VERBOSE"))
778                         verbose = 1;
779                 initialized = 1;
780         }
781
782         frames++;
783         gettimeofday (&tv_now, NULL);
784
785         /* Consume all timer queries that are ready. */
786         timer_query_t *timer = current_context.timer_head;
787
788         while (timer) {
789                 GLuint available, elapsed;
790
791                 glGetQueryObjectuiv (timer->id,
792                                      GL_QUERY_RESULT_AVAILABLE, &available);
793                 if (! available)
794                         break;
795
796                 glGetQueryObjectuiv (timer->id,
797                                      GL_QUERY_RESULT, &elapsed);
798
799                 accumulate_program_time (timer->op, elapsed);
800
801                 current_context.timer_head = timer->next;
802                 if (current_context.timer_head == NULL)
803                         current_context.timer_tail = NULL;
804
805                 glDeleteQueries (1, &timer->id);
806
807                 free (timer);
808                 timer = current_context.timer_head;
809         }
810
811         /* And similarly for all performance monitors that are ready. */
812         monitor_t *monitor = current_context.monitor_head;
813
814         while (monitor) {
815                 GLuint available, result_size, *result;
816                 GLint bytes_written;
817
818                 glGetPerfMonitorCounterDataAMD (monitor->id,
819                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
820                                                 sizeof (available), &available,
821                                                 NULL);
822                 if (! available)
823                         break;
824
825                 glGetPerfMonitorCounterDataAMD (monitor->id,
826                                                 GL_PERFMON_RESULT_SIZE_AMD,
827                                                 sizeof (result_size),
828                                                 &result_size, NULL);
829
830                 result = xmalloc (result_size);
831
832                 glGetPerfMonitorCounterDataAMD (monitor->id,
833                                                 GL_PERFMON_RESULT_AMD,
834                                                 result_size, result,
835                                                 &bytes_written);
836
837                 accumulate_program_metrics (monitor->op, result, result_size);
838
839                 free (result);
840
841                 current_context.monitor_head = monitor->next;
842                 if (current_context.monitor_head == NULL)
843                         current_context.monitor_tail = NULL;
844
845                 glDeletePerfMonitorsAMD (1, &monitor->id);
846
847                 free (monitor);
848                 monitor = current_context.monitor_head;
849         }
850
851         if (frames % 60 == 0) {
852                 double fps;
853
854                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
855                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
856
857                 printf("FPS: %.3f\n", fps);
858
859                 print_program_metrics ();
860         }
861 }