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