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