]> git.cworth.org Git - fips/blob - metrics.c
stash: This adds the reset code, but to a bogusly hard-coded frame number
[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         metrics_collect_available ();
335
336         if (ctx->timer_begun_id) {
337                 glEndQuery (GL_TIME_ELAPSED);
338                 glDeleteQueries (1, &ctx->timer_begun_id);
339                 ctx->timer_begun_id = 0;
340         }
341
342         for (timer = ctx->timer_head;
343              timer;
344              timer = timer_next)
345         {
346                 glDeleteQueries (1, &timer->id);
347                 timer_next = timer->next;
348                 free (timer);
349         }
350         ctx->timer_head = NULL;
351         ctx->timer_tail = NULL;
352
353         if (ctx->monitor_begun_id) {
354                 glEndPerfMonitorAMD (ctx->monitor_begun_id);
355                 glDeletePerfMonitorsAMD (1, &ctx->monitor_begun_id);
356                 ctx->monitor_begun_id = 0;
357         }
358
359         for (monitor = ctx->monitor_head;
360              monitor;
361              monitor = monitor_next)
362         {
363                 glDeletePerfMonitorsAMD (1, &monitor->id);
364                 monitor_next = monitor->next;
365                 free (monitor);
366         }
367         ctx->monitor_head = NULL;
368         ctx->monitor_tail = NULL;
369
370         current_context.monitors_in_flight = 0;
371
372         for (i = 0; i < info->num_groups; i++)
373                 metrics_group_info_fini (&info->groups[i]);
374
375         free (info->groups);
376         info->groups = NULL;
377
378         for (i = 0; i < info->num_shader_stages; i++)
379                 free (info->stages[i].name);
380
381         free (info->stages);
382         info->stages = NULL;
383
384         info->initialized = 0;
385 }
386
387 static const char *
388 metrics_op_string (metrics_op_t op)
389 {
390         if (op >= METRICS_OP_SHADER)
391                 return "Shader program";
392
393         switch (op)
394         {
395         case METRICS_OP_ACCUM:
396                 return "glAccum*(+)";
397         case METRICS_OP_BUFFER_DATA:
398                 return "glBufferData(+)";
399         case METRICS_OP_BUFFER_SUB_DATA:
400                 return "glCopyBufferSubData*";
401         case METRICS_OP_BITMAP:
402                 return "glBitmap*";
403         case METRICS_OP_BLIT_FRAMEBUFFER:
404                 return "glBlitFramebuffer*";
405         case METRICS_OP_CLEAR:
406                 return "glClear(+)";
407         case METRICS_OP_CLEAR_BUFFER_DATA:
408                 return "glCearBufferData(+)";
409         case METRICS_OP_CLEAR_TEX_IMAGE:
410                 return "glClearTexImage(+)";
411         case METRICS_OP_COPY_PIXELS:
412                 return "glCopyPixels";
413         case METRICS_OP_COPY_TEX_IMAGE:
414                 return "glCopyTexImage(+)";
415         case METRICS_OP_DRAW_PIXELS:
416                 return "glDrawPixels";
417         case METRICS_OP_GET_TEX_IMAGE:
418                 return "glGetTexImage(+)";
419         case METRICS_OP_READ_PIXELS:
420                 return "glReadPixels*";
421         case METRICS_OP_TEX_IMAGE:
422                 return "glTexImage*(+)";
423         default:
424                 fprintf (stderr, "fips: Internal error: "
425                          "Unknown metrics op value: %d\n", op);
426                 exit (1);
427         }
428
429         return "";
430 }
431
432 void
433 metrics_counter_start (void)
434 {
435         context_t *ctx = &current_context;
436         unsigned i;
437
438         /* Initialize the timer_query and monitor objects */
439         glGenQueries (1, &ctx->timer_begun_id);
440
441         glGenPerfMonitorsAMD (1, &ctx->monitor_begun_id);
442
443         for (i = 0; i < ctx->metrics_info.num_groups; i++)
444         {
445                 metrics_group_info_t *group;
446                 int num_counters;
447
448                 group = &ctx->metrics_info.groups[i];
449
450                 num_counters = group->num_counters;
451                 if (group->max_active_counters < group->num_counters)
452                 {
453                         fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
454                                  group->max_active_counters,
455                                  group->num_counters, i);
456                         num_counters = group->max_active_counters;
457
458                 }
459
460                 glSelectPerfMonitorCountersAMD(ctx->monitor_begun_id,
461                                                GL_TRUE, group->id,
462                                                num_counters,
463                                                group->counter_ids);
464         }
465
466         /* Start the queries */
467         glBeginQuery (GL_TIME_ELAPSED, ctx->timer_begun_id);
468
469         glBeginPerfMonitorAMD (ctx->monitor_begun_id);
470 }
471
472 void
473 metrics_counter_stop (void)
474 {
475         context_t *ctx = &current_context;
476         timer_query_t *timer;
477         monitor_t *monitor;
478
479         /* Stop the current timer and monitor. */
480         glEndQuery (GL_TIME_ELAPSED);
481         glEndPerfMonitorAMD (ctx->monitor_begun_id);
482
483         /* Add these IDs to our lists of outstanding queries and
484          * monitors so the results can be collected later. */
485         timer = xmalloc (sizeof (timer_query_t));
486
487         timer->op = ctx->op;
488         timer->id = ctx->timer_begun_id;
489         timer->next = NULL;
490
491         if (ctx->timer_tail) {
492                 ctx->timer_tail->next = timer;
493                 ctx->timer_tail = timer;
494         } else {
495                 ctx->timer_tail = timer;
496                 ctx->timer_head = timer;
497         }
498
499         /* Create a new performance-monitor query */
500         monitor = xmalloc (sizeof (monitor_t));
501
502         monitor->op = ctx->op;
503         monitor->id = ctx->monitor_begun_id;
504         monitor->next = NULL;
505
506         if (ctx->monitor_tail) {
507                 ctx->monitor_tail->next = monitor;
508                 ctx->monitor_tail = monitor;
509         } else {
510                 ctx->monitor_tail = monitor;
511                 ctx->monitor_head = monitor;
512         }
513
514         ctx->monitors_in_flight++;
515
516         /* Avoid being a resource hog and collect outstanding results
517          * once we have sent off a large number of
518          * queries. (Presumably, many of the outstanding queries are
519          * available by now.)
520          */
521         if (ctx->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
522                 metrics_collect_available ();
523 }
524
525 void
526 metrics_set_current_op (metrics_op_t op)
527 {
528         current_context.op = op;
529 }
530
531 metrics_op_t
532 metrics_get_current_op (void)
533 {
534         return current_context.op;
535 }
536
537 static void
538 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
539 {
540         metrics_info_t *info = &ctx->metrics_info;
541         unsigned i, j;
542
543         metrics->op = op;
544         metrics->time_ns = 0.0;
545
546         metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
547
548         for (i = 0; i < info->num_groups; i++) {
549                 metrics->counters[i] = xmalloc (sizeof (double) *
550                                                 info->groups[i].num_counters);
551                 for (j = 0; j < info->groups[i].num_counters; j++)
552                         metrics->counters[i][j] = 0.0;
553         }
554 }
555
556 static op_metrics_t *
557 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
558 {
559         unsigned i;
560
561         if (op >= ctx->num_op_metrics)
562         {
563                 ctx->op_metrics = realloc (ctx->op_metrics,
564                                            (op + 1) * sizeof (op_metrics_t));
565                 for (i = ctx->num_op_metrics; i < op + 1; i++)
566                         op_metrics_init (ctx, &ctx->op_metrics[i], i);
567
568                 ctx->num_op_metrics = op + 1;
569         }
570
571         return &ctx->op_metrics[op];
572 }
573
574 static void
575 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
576 {
577 #define CONSUME(var)                                                    \
578         if (p + sizeof(var) > ((unsigned char *) result) + size)        \
579         {                                                               \
580                 fprintf (stderr, "Unexpected end-of-buffer while "      \
581                          "parsing results\n");                          \
582                 break;                                                  \
583         }                                                               \
584         (var) = *((typeof(var) *) p);                                   \
585         p += sizeof(var);
586
587         context_t *ctx = &current_context;
588         metrics_info_t *info = &ctx->metrics_info;
589         op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
590         unsigned char *p = (unsigned char *) result;
591
592         while (p < ((unsigned char *) result) + size)
593         {
594                 GLuint group_id, group_index;
595                 GLuint counter_id, counter_index;
596                 metrics_group_info_t *group;
597                 double value;
598                 unsigned i;
599
600                 CONSUME (group_id);
601                 CONSUME (counter_id);
602
603                 for (i = 0; i < info->num_groups; i++) {
604                         if (info->groups[i].id == group_id)
605                                 break;
606                 }
607                 group_index = i;
608                 assert (group_index < info->num_groups);
609                 group = &info->groups[group_index];
610
611                 for (i = 0; i < group->num_counters; i++) {
612                         if (group->counter_ids[i] == counter_id)
613                                 break;
614                 }
615                 counter_index = i;
616                 assert (counter_index < group->num_counters);
617
618                 switch (group->counter_types[counter_index])
619                 {
620                         uint uint_value;
621                         uint64_t uint64_value;
622                         float float_value;
623                 case GL_UNSIGNED_INT:
624                         CONSUME (uint_value);
625                         value = uint_value;
626                         break;
627                 case GL_UNSIGNED_INT64_AMD:
628                         CONSUME (uint64_value);
629                         value = uint64_value;
630                         break;
631                 case GL_PERCENTAGE_AMD:
632                 case GL_FLOAT:
633                         CONSUME (float_value);
634                         value = float_value;
635                         break;
636                 default:
637                         fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
638                                  group->counter_types[counter_index]);
639                         value = 0.0;
640                         break;
641                 }
642
643                 metrics->counters[group_index][counter_index] += value;
644         }
645 }
646
647 static void
648 accumulate_program_time (metrics_op_t op, unsigned time_ns)
649 {
650         op_metrics_t *metrics;
651
652         metrics = ctx_get_op_metrics (&current_context, op);
653
654         metrics->time_ns += time_ns;
655 }
656
657 typedef struct per_stage_metrics
658 {
659         op_metrics_t *metrics;
660         shader_stage_info_t *stage;
661         double time_ns;
662         double active;
663 } per_stage_metrics_t;
664
665 static int
666 _is_shader_stage_counter (metrics_info_t *info,
667                           unsigned group_index,
668                           unsigned counter_index)
669 {
670         shader_stage_info_t *stage;
671         unsigned i;
672
673         for (i = 0; i < info->num_shader_stages; i++) {
674                 stage = &info->stages[i];
675
676                 if (stage->active_group_index == group_index &&
677                     stage->active_counter_index == counter_index)
678                 {
679                         return 1;
680                 }
681
682                 if (stage->stall_group_index == group_index &&
683                     stage->stall_counter_index == counter_index)
684                 {
685                         return 1;
686                 }
687         }
688
689         return 0;
690 }
691
692 static void
693 print_per_stage_metrics (context_t *ctx,
694                          per_stage_metrics_t *per_stage,
695                          double total)
696 {
697         metrics_info_t *info = &ctx->metrics_info;
698         op_metrics_t *metric = per_stage->metrics;
699         metrics_group_info_t *group;
700         const char *op_string;
701         unsigned group_index, counter;
702         double value;
703
704         /* Don't print anything for stages with no alloted time. */
705         if (per_stage->time_ns == 0.0)
706                 return;
707
708         op_string = metrics_op_string (metric->op);
709
710         printf ("%21s", op_string);
711
712         if (metric->op >= METRICS_OP_SHADER) {
713                 printf (" %3d", metric->op - METRICS_OP_SHADER);
714         } else {
715                 printf ("    ");
716
717         }
718
719         if (per_stage->stage)
720                 printf (" %cS:", per_stage->stage->name[0]);
721         else
722                 printf ("   :");
723
724         printf ("\t%7.2f ms (%4.1f%%)",
725                 per_stage->time_ns / 1e6,
726                 per_stage->time_ns / total * 100);
727
728         if (per_stage->active)
729                 printf (", %4.1f%% active", per_stage->active * 100);
730
731         printf ("\n");
732
733         /* I'm not seeing a lot of value printing the rest of these
734          * performance counters by default yet. Use --verbose to get
735          * them for now. */
736         if (! verbose)
737                 return;
738
739         printf ("[");
740         for (group_index = 0; group_index < info->num_groups; group_index++) {
741                 group = &info->groups[group_index];
742                 for (counter = 0; counter < group->num_counters; counter++) {
743
744                         /* Don't print this counter value if it's a
745                          * per-stage cycle counter, (which we have
746                          * already accounted for). */
747                         if (_is_shader_stage_counter (info, group_index, counter))
748                                 continue;
749
750                         value = metric->counters[group_index][counter];
751                         if (value == 0.0)
752                                 continue;
753                         printf ("%s: %.2f ", group->counter_names[counter],
754                                 value / 1e6);
755                 }
756         }
757         printf ("]\n");
758 }
759
760 static int
761 time_compare(const void *in_a, const void *in_b, void *arg unused)
762 {
763         const per_stage_metrics_t *a = in_a;
764         const per_stage_metrics_t *b = in_b;
765
766
767         if (a->time_ns < b->time_ns)
768                 return -1;
769         if (a->time_ns > b->time_ns)
770                 return 1;
771         return 0;
772 }
773
774 static void
775 op_metrics_reset (op_metrics_t *op)
776 {
777         context_t *ctx = &current_context;
778         metrics_info_t *info = &ctx->metrics_info;
779         unsigned group_index, counter;
780         metrics_group_info_t *group;
781
782         op->time_ns = 0.0;
783
784         for (group_index = 0; group_index < info->num_groups; group_index++) {
785                 group = &info->groups[group_index];
786                 for (counter = 0; counter < group->num_counters; counter++)
787                 {
788                         op->counters[group_index][counter] = 0.0;
789                 }
790         }
791 }
792
793 static void
794 reset_metrics (void)
795 {
796         context_t *ctx = &current_context;
797         unsigned i;
798
799         for (i = 0; i < ctx->num_op_metrics; i++) {
800
801                 op_metrics_reset (&ctx->op_metrics[i]);
802
803         }
804 }
805
806 static void
807 print_program_metrics (void)
808 {
809         context_t *ctx = &current_context;
810         metrics_info_t *info = &ctx->metrics_info;
811         unsigned num_shader_stages = info->num_shader_stages;
812         per_stage_metrics_t *sorted, *per_stage;
813         double total_time, op_cycles;
814         op_metrics_t *op;
815         unsigned group_index, counter_index;
816         unsigned i, j, num_sorted;
817
818         /* Make a sorted list of the per-stage operations by time
819          * used, and figure out the total so we can print percentages.
820          */
821         num_sorted = ctx->num_op_metrics * num_shader_stages;
822
823         sorted = xmalloc (sizeof (*sorted) * num_sorted);
824
825         total_time = 0.0;
826
827         for (i = 0; i < ctx->num_op_metrics; i++) {
828
829                 op = &ctx->op_metrics[i];
830
831                 /* Accumulate total time across all ops. */
832                 total_time += op->time_ns;
833
834                 /* Also, find total cycles in all stages of this op. */
835                 op_cycles = 0.0;
836
837                 for (j = 0; j < num_shader_stages; j++) {
838                         /* Active cycles */
839                         group_index = info->stages[j].active_group_index;
840                         counter_index = info->stages[j].active_counter_index;
841                         op_cycles += op->counters[group_index][counter_index];
842
843                         /* Stall cycles */
844                         group_index = info->stages[j].stall_group_index;
845                         counter_index = info->stages[j].stall_counter_index;
846                         op_cycles += op->counters[group_index][counter_index];
847                 }
848
849                 for (j = 0; j < num_shader_stages; j++) {
850                         double active_cycles, stall_cycles, stage_cycles;
851
852                         /* Active cycles */
853                         group_index = info->stages[j].active_group_index;
854                         counter_index = info->stages[j].active_counter_index;
855                         active_cycles = op->counters[group_index][counter_index];
856
857                         /* Stall cycles */
858                         group_index = info->stages[j].stall_group_index;
859                         counter_index = info->stages[j].stall_counter_index;
860                         stall_cycles = op->counters[group_index][counter_index];
861
862                         stage_cycles = active_cycles + stall_cycles;
863
864                         per_stage = &sorted[i * num_shader_stages + j];
865                         per_stage->metrics = op;
866
867                         if (op_cycles) {
868                                 per_stage->stage = &info->stages[j];
869                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
870                         } else {
871                                 /* If we don't have any per-stage cycle counts
872                                  * for this operation, then use the first
873                                  * stage as a placeholder for all the time,
874                                  * but NULL-ify the stage info so that the
875                                  * report doesn't lie about this time being
876                                  * from any particular stage. */
877                                 per_stage->stage = NULL;
878                                 if (j == 0) {
879                                         per_stage->time_ns = op->time_ns;
880                                 } else {
881                                         per_stage->time_ns = 0.0;
882                                 }
883                         }
884
885                         if (stage_cycles) {
886                                 per_stage->active = active_cycles / stage_cycles;
887                         } else {
888                                 per_stage->active = 0.0;
889                         }
890                 }
891         }
892
893         qsort_r (sorted, num_sorted, sizeof (*sorted),
894                  time_compare, ctx->op_metrics);
895
896         for (i = 0; i < num_sorted; i++)
897                 print_per_stage_metrics (ctx, &sorted[i], total_time);
898
899         free (sorted);
900 }
901
902 /* Called at program exit.
903  *
904  * This is similar to metrics_info_fini, but only frees any used
905  * memory. Notably, it does not call any OpenGL functions, (since the
906  * OpenGL context no longer exists at program exit).
907  */
908 static void
909 metrics_exit (void)
910 {
911         context_t *ctx = &current_context;
912         metrics_info_t *info = &ctx->metrics_info;
913         unsigned i;
914         timer_query_t *timer, *timer_next;
915         monitor_t *monitor, *monitor_next;
916
917         if (verbose)
918                 printf ("fips: terminating\n");
919
920         if (! info->initialized)
921                 return;
922
923         for (timer = ctx->timer_head;
924              timer;
925              timer = timer_next)
926         {
927                 timer_next = timer->next;
928                 free (timer);
929         }
930
931         for (monitor = ctx->monitor_head;
932              monitor;
933              monitor = monitor_next)
934         {
935                 monitor_next = monitor->next;
936                 free (monitor);
937         }
938
939         for (i = 0; i < info->num_groups; i++)
940                 metrics_group_info_fini (&info->groups[i]);
941
942         free (info->groups);
943
944         for (i = 0; i < info->num_shader_stages; i++)
945                 free (info->stages[i].name);
946
947         free (info->stages);
948 }
949
950 void
951 metrics_collect_available (void)
952 {
953         context_t *ctx = &current_context;
954
955         /* Consume all timer queries that are ready. */
956         timer_query_t *timer = ctx->timer_head;
957
958         while (timer) {
959                 GLuint available, elapsed;
960
961                 glGetQueryObjectuiv (timer->id,
962                                      GL_QUERY_RESULT_AVAILABLE, &available);
963                 if (! available)
964                         break;
965
966                 glGetQueryObjectuiv (timer->id,
967                                      GL_QUERY_RESULT, &elapsed);
968
969                 accumulate_program_time (timer->op, elapsed);
970
971                 ctx->timer_head = timer->next;
972                 if (ctx->timer_head == NULL)
973                         ctx->timer_tail = NULL;
974
975                 glDeleteQueries (1, &timer->id);
976
977                 free (timer);
978                 timer = ctx->timer_head;
979         }
980
981         /* And similarly for all performance monitors that are ready. */
982         monitor_t *monitor = ctx->monitor_head;
983
984         while (monitor) {
985                 GLuint available, result_size, *result;
986                 GLint bytes_written;
987
988                 glGetPerfMonitorCounterDataAMD (monitor->id,
989                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
990                                                 sizeof (available), &available,
991                                                 NULL);
992                 if (! available)
993                         break;
994
995                 glGetPerfMonitorCounterDataAMD (monitor->id,
996                                                 GL_PERFMON_RESULT_SIZE_AMD,
997                                                 sizeof (result_size),
998                                                 &result_size, NULL);
999
1000                 result = xmalloc (result_size);
1001
1002                 glGetPerfMonitorCounterDataAMD (monitor->id,
1003                                                 GL_PERFMON_RESULT_AMD,
1004                                                 result_size, result,
1005                                                 &bytes_written);
1006
1007                 accumulate_program_metrics (monitor->op, result, result_size);
1008
1009                 free (result);
1010
1011                 ctx->monitor_head = monitor->next;
1012                 if (ctx->monitor_head == NULL)
1013                         ctx->monitor_tail = NULL;
1014
1015                 glDeletePerfMonitorsAMD (1, &monitor->id);
1016
1017                 free (monitor);
1018
1019                 ctx->monitors_in_flight--;
1020
1021                 monitor = ctx->monitor_head;
1022         }
1023 }
1024
1025
1026 void
1027 metrics_end_frame (void)
1028 {
1029         static int initialized = 0;
1030         static struct timeval tv_start, tv_now;
1031
1032         if (! initialized) {
1033                 gettimeofday (&tv_start, NULL);
1034                 atexit (metrics_exit);
1035                 if (getenv ("FIPS_VERBOSE"))
1036                         verbose = 1;
1037                 initialized = 1;
1038         }
1039
1040         frames++;
1041
1042         metrics_collect_available ();
1043
1044         if (frames % 15 == 0) {
1045                 double fps;
1046
1047                 gettimeofday (&tv_now, NULL);
1048
1049                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
1050                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
1051
1052                 printf("FPS: %.3f\n", fps);
1053
1054                 print_program_metrics ();
1055         }
1056
1057         if (frames == 208)
1058                 reset_metrics ();
1059 }