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