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