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