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