]> git.cworth.org Git - fips/blob - metrics.c
Cleanup outstanding counters at context change.
[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         unsigned char *p = (unsigned char *) result;
536
537         while (p < ((unsigned char *) result) + size)
538         {
539                 GLuint group_id, group_index;
540                 GLuint counter_id, counter_index;
541                 metrics_group_info_t *group;
542                 double value;
543                 unsigned i;
544
545                 CONSUME (group_id);
546                 CONSUME (counter_id);
547
548                 for (i = 0; i < info->num_groups; i++) {
549                         if (info->groups[i].id == group_id)
550                                 break;
551                 }
552                 group_index = i;
553                 assert (group_index < info->num_groups);
554                 group = &info->groups[group_index];
555
556                 for (i = 0; i < group->num_counters; i++) {
557                         if (group->counter_ids[i] == counter_id)
558                                 break;
559                 }
560                 counter_index = i;
561                 assert (counter_index < group->num_counters);
562
563                 switch (group->counter_types[counter_index])
564                 {
565                         uint uint_value;
566                         uint64_t uint64_value;
567                         float float_value;
568                 case GL_UNSIGNED_INT:
569                         CONSUME (uint_value);
570                         value = uint_value;
571                         break;
572                 case GL_UNSIGNED_INT64_AMD:
573                         CONSUME (uint64_value);
574                         value = uint64_value;
575                         break;
576                 case GL_PERCENTAGE_AMD:
577                 case GL_FLOAT:
578                         CONSUME (float_value);
579                         value = float_value;
580                         break;
581                 default:
582                         fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
583                                  group->counter_types[counter_index]);
584                         value = 0.0;
585                         break;
586                 }
587
588                 ctx->op_metrics[op].counters[group_index][counter_index] += value;
589         }
590 }
591
592 static void
593 accumulate_program_time (metrics_op_t op, unsigned time_ns)
594 {
595         op_metrics_t *metrics;
596
597         metrics = ctx_get_op_metrics (&current_context, op);
598
599         metrics->time_ns += time_ns;
600 }
601
602 typedef struct per_stage_metrics
603 {
604         op_metrics_t *metrics;
605         shader_stage_info_t *stage;
606         double time_ns;
607         double active;
608 } per_stage_metrics_t;
609
610 static int
611 _is_shader_stage_counter (metrics_info_t *info,
612                           unsigned group_index,
613                           unsigned counter_index)
614 {
615         shader_stage_info_t *stage;
616         unsigned i;
617
618         for (i = 0; i < info->num_shader_stages; i++) {
619                 stage = &info->stages[i];
620
621                 if (stage->active_group_index == group_index &&
622                     stage->active_counter_index == counter_index)
623                 {
624                         return 1;
625                 }
626
627                 if (stage->stall_group_index == group_index &&
628                     stage->stall_counter_index == counter_index)
629                 {
630                         return 1;
631                 }
632         }
633
634         return 0;
635 }
636
637 static void
638 print_per_stage_metrics (context_t *ctx,
639                          per_stage_metrics_t *per_stage,
640                          double total)
641 {
642         metrics_info_t *info = &ctx->metrics_info;
643         op_metrics_t *metric = per_stage->metrics;
644         metrics_group_info_t *group;
645         const char *op_string;
646         unsigned group_index, counter;
647         double value;
648
649         /* Don't print anything for stages with no alloted time. */
650         if (per_stage->time_ns == 0.0)
651                 return;
652
653         op_string = metrics_op_string (metric->op);
654
655         printf ("%21s", op_string);
656
657         if (metric->op >= METRICS_OP_SHADER) {
658                 printf (" %3d", metric->op - METRICS_OP_SHADER);
659         } else {
660                 printf ("    ");
661
662         }
663         printf (" %cS:", per_stage->stage->name[0]);
664
665         printf ("\t%7.2f ms (%4.1f%%)",
666                 per_stage->time_ns / 1e6,
667                 per_stage->time_ns / total * 100);
668
669         if (per_stage->active)
670                 printf (", %4.1f%% active", per_stage->active * 100);
671
672         printf ("\n");
673
674         /* I'm not seeing a lot of value printing the rest of these
675          * performance counters by default yet. Use --verbose to get
676          * them for now. */
677         if (! verbose)
678                 return;
679
680         printf ("[");
681         for (group_index = 0; group_index < info->num_groups; group_index++) {
682                 group = &info->groups[group_index];
683                 for (counter = 0; counter < group->num_counters; counter++) {
684
685                         /* Don't print this counter value if it's a
686                          * per-stage cycle counter, (which we have
687                          * already accounted for). */
688                         if (_is_shader_stage_counter (info, group_index, counter))
689                                 continue;
690
691                         value = metric->counters[group_index][counter];
692                         if (value == 0.0)
693                                 continue;
694                         printf ("%s: %.2f ", group->counter_names[counter],
695                                 value / 1e6);
696                 }
697         }
698         printf ("]\n");
699 }
700
701 static int
702 time_compare(const void *in_a, const void *in_b, void *arg unused)
703 {
704         const per_stage_metrics_t *a = in_a;
705         const per_stage_metrics_t *b = in_b;
706
707
708         if (a->time_ns < b->time_ns)
709                 return -1;
710         if (a->time_ns > b->time_ns)
711                 return 1;
712         return 0;
713 }
714
715 static void
716 print_program_metrics (void)
717 {
718         context_t *ctx = &current_context;
719         metrics_info_t *info = &ctx->metrics_info;
720         unsigned num_shader_stages = info->num_shader_stages;
721         per_stage_metrics_t *sorted, *per_stage;
722         double total_time, op_cycles;
723         op_metrics_t *op;
724         unsigned group_index, counter_index;
725         unsigned i, j, num_sorted;
726
727         /* Make a sorted list of the per-stage operations by time
728          * used, and figure out the total so we can print percentages.
729          */
730         num_sorted = ctx->num_op_metrics * num_shader_stages;
731
732         sorted = xmalloc (sizeof (*sorted) * num_sorted);
733
734         total_time = 0.0;
735
736         for (i = 0; i < ctx->num_op_metrics; i++) {
737
738                 op = &ctx->op_metrics[i];
739
740                 /* Accumulate total time across all ops. */
741                 total_time += op->time_ns;
742
743                 /* Also, find total cycles in all stages of this op. */
744                 op_cycles = 0.0;
745
746                 for (j = 0; j < num_shader_stages; j++) {
747                         /* Active cycles */
748                         group_index = info->stages[j].active_group_index;
749                         counter_index = info->stages[j].active_counter_index;
750                         op_cycles += op->counters[group_index][counter_index];
751
752                         /* Stall cycles */
753                         group_index = info->stages[j].stall_group_index;
754                         counter_index = info->stages[j].stall_counter_index;
755                         op_cycles += op->counters[group_index][counter_index];
756                 }
757
758                 for (j = 0; j < num_shader_stages; j++) {
759                         double active_cycles, stall_cycles, stage_cycles;
760
761                         /* Active cycles */
762                         group_index = info->stages[j].active_group_index;
763                         counter_index = info->stages[j].active_counter_index;
764                         active_cycles = op->counters[group_index][counter_index];
765
766                         /* Stall cycles */
767                         group_index = info->stages[j].stall_group_index;
768                         counter_index = info->stages[j].stall_counter_index;
769                         stall_cycles = op->counters[group_index][counter_index];
770
771                         stage_cycles = active_cycles + stall_cycles;
772
773                         per_stage = &sorted[i * num_shader_stages + j];
774                         per_stage->metrics = op;
775                         per_stage->stage = &info->stages[j];
776                         if (op_cycles)
777                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
778                         else
779                                 per_stage->time_ns = 0.0;
780                         if (stage_cycles)
781                                 per_stage->active = active_cycles / stage_cycles;
782                         else
783                                 per_stage->active = 0.0;
784                 }
785         }
786
787         qsort_r (sorted, num_sorted, sizeof (*sorted),
788                  time_compare, ctx->op_metrics);
789
790         for (i = 0; i < num_sorted; i++)
791                 print_per_stage_metrics (ctx, &sorted[i], total_time);
792
793         free (sorted);
794 }
795
796 /* Called at program exit */
797 static void
798 metrics_exit (void)
799 {
800         if (verbose)
801                 printf ("fips: terminating\n");
802
803         metrics_info_fini ();
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         gettimeofday (&tv_now, NULL);
823
824         /* Consume all timer queries that are ready. */
825         timer_query_t *timer = current_context.timer_head;
826
827         while (timer) {
828                 GLuint available, elapsed;
829
830                 glGetQueryObjectuiv (timer->id,
831                                      GL_QUERY_RESULT_AVAILABLE, &available);
832                 if (! available)
833                         break;
834
835                 glGetQueryObjectuiv (timer->id,
836                                      GL_QUERY_RESULT, &elapsed);
837
838                 accumulate_program_time (timer->op, elapsed);
839
840                 current_context.timer_head = timer->next;
841                 if (current_context.timer_head == NULL)
842                         current_context.timer_tail = NULL;
843
844                 glDeleteQueries (1, &timer->id);
845
846                 free (timer);
847                 timer = current_context.timer_head;
848         }
849
850         /* And similarly for all performance monitors that are ready. */
851         monitor_t *monitor = current_context.monitor_head;
852
853         while (monitor) {
854                 GLuint available, result_size, *result;
855                 GLint bytes_written;
856
857                 glGetPerfMonitorCounterDataAMD (monitor->id,
858                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
859                                                 sizeof (available), &available,
860                                                 NULL);
861                 if (! available)
862                         break;
863
864                 glGetPerfMonitorCounterDataAMD (monitor->id,
865                                                 GL_PERFMON_RESULT_SIZE_AMD,
866                                                 sizeof (result_size),
867                                                 &result_size, NULL);
868
869                 result = xmalloc (result_size);
870
871                 glGetPerfMonitorCounterDataAMD (monitor->id,
872                                                 GL_PERFMON_RESULT_AMD,
873                                                 result_size, result,
874                                                 &bytes_written);
875
876                 accumulate_program_metrics (monitor->op, result, result_size);
877
878                 free (result);
879
880                 current_context.monitor_head = monitor->next;
881                 if (current_context.monitor_head == NULL)
882                         current_context.monitor_tail = NULL;
883
884                 glDeletePerfMonitorsAMD (1, &monitor->id);
885
886                 free (monitor);
887                 monitor = current_context.monitor_head;
888         }
889
890         if (frames % 60 == 0) {
891                 double fps;
892
893                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
894                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
895
896                 printf("FPS: %.3f\n", fps);
897
898                 print_program_metrics ();
899         }
900 }