]> git.cworth.org Git - fips/blobdiff - metrics.c
Add explicit link to libpthread, to work around debugging issues
[fips] / metrics.c
index 1e2e0184e6592af840c4a69eff7247a004e49c26..9c2e080860d44ae75b1f19dd64367c586b984c7a 100644 (file)
--- a/metrics.c
+++ b/metrics.c
 #include "fips-dispatch-gl.h"
 
 #include "metrics.h"
+#include "context.h"
+#include "metrics-info.h"
 #include "xmalloc.h"
 
+int frames;
+int verbose;
+
+#define MAX_MONITORS_IN_FLIGHT 1000
+
 /* Timer query */
 typedef struct timer_query
 {
@@ -49,97 +56,133 @@ typedef struct monitor
        struct monitor *next;
 } monitor_t;
 
-
 typedef struct op_metrics
 {
        /* This happens to also be the index into the
-        * ctx->op_metrics array currently
+        * metrics->op_metrics array currently
         */
        metrics_op_t op;
        double time_ns;
 
-       double *counters;
-       unsigned num_counters;
+       double **counters;
 } op_metrics_t;
 
-typedef struct counter_group_info
-{
-       GLuint id;
-       GLint num_counters;
-       GLint max_active_counters;
-       GLuint *counters;
-} counter_group_info_t;
-
-typedef struct metrics_info
-{
-       int num_groups;
-       int max_counters_per_group;
-       counter_group_info_t *groups;
-} metrics_info_t;
-
-typedef struct context
+struct metrics
 {
-       metrics_info_t metrics_info;
+       /* Description of all available peformance counters, counter
+        * groups, their names and IDs, etc. */
+       metrics_info_t *info;
 
+       /* The current operation being measured. */
        metrics_op_t op;
 
+       /* GL_TIME_ELAPSED query for which glEndQuery has not yet
+        * been called. */
+       unsigned timer_begun_id;
+
+       /* GL_TIME_ELAPSED queries for which glEndQuery has been
+        * called, (but results have not yet been queried). */
        timer_query_t *timer_head;
        timer_query_t *timer_tail;
 
+       /* Performance monitor for which glEndPerfMonitorAMD has not
+        * yet been called. */
+       unsigned monitor_begun_id;
+
+       /* Performance monitors for which glEndPerfMonitorAMD has
+        * been called, (but results have not yet been queried). */
        monitor_t *monitor_head;
        monitor_t *monitor_tail;
 
+       int monitors_in_flight;
+
        unsigned num_op_metrics;
        op_metrics_t *op_metrics;
-} context_t;
+};
 
-/* FIXME: Need a map from integers to context objects and track the
- * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
+metrics_t *
+metrics_create (metrics_info_t *info)
+{
+       metrics_t *metrics;
 
-context_t current_context;
+       metrics = xmalloc (sizeof (metrics_t));
 
-int frames;
-int verbose;
+       metrics->info = info;
 
-void
-metrics_info_init (void)
-{
-       int i;
-       GLuint *group_ids;
-       metrics_info_t *metrics_info = &current_context.metrics_info;
+       metrics->op = 0;
 
-       glGetPerfMonitorGroupsAMD (&metrics_info->num_groups, 0, NULL);
+       metrics->timer_begun_id = 0;
 
-       group_ids = xmalloc (metrics_info->num_groups * sizeof (GLuint));
+       metrics->timer_head = NULL;
+       metrics->timer_tail = NULL;
 
-       glGetPerfMonitorGroupsAMD (NULL, metrics_info->num_groups, group_ids);
+       metrics->monitor_begun_id = 0;
 
-       metrics_info->max_counters_per_group = 0;
+       metrics->monitor_head = NULL;
+       metrics->monitor_tail = NULL;
 
-       metrics_info->groups = xmalloc (metrics_info->num_groups * sizeof (counter_group_info_t));
+       metrics->monitors_in_flight = 0;
 
-       for (i = 0; i < metrics_info->num_groups; i++)
-       {
-               counter_group_info_t *group;
+       metrics->num_op_metrics = 0;
+       metrics->op_metrics = NULL;
 
-               group = &metrics_info->groups[i];
+       return metrics;
+}
+
+void
+metrics_fini (metrics_t *metrics)
+{
+       timer_query_t *timer, *timer_next;
+       monitor_t *monitor, *monitor_next;
+
+       /* Discard and cleanup any outstanding queries. */
+       if (metrics->timer_begun_id) {
+               glEndQuery (GL_TIME_ELAPSED);
+               glDeleteQueries (1, &metrics->timer_begun_id);
+               metrics->timer_begun_id = 0;
+       }
 
-               group->id = group_ids[i];
+       for (timer = metrics->timer_head;
+            timer;
+            timer = timer_next)
+       {
+               glDeleteQueries (1, &timer->id);
+               timer_next = timer->next;
+               free (timer);
+       }
+       metrics->timer_head = NULL;
+       metrics->timer_tail = NULL;
 
-               glGetPerfMonitorCountersAMD (group->id, &group->num_counters,
-                                            &group->max_active_counters, 0, NULL);
+       if (metrics->info->have_perfmon) {
 
-               group->counters = xmalloc (group->num_counters * sizeof (GLuint));
+               if (metrics->monitor_begun_id) {
+                       glEndPerfMonitorAMD (metrics->monitor_begun_id);
+                       glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
+                       metrics->monitor_begun_id = 0;
+               }
 
-               glGetPerfMonitorCountersAMD (group->id, NULL, NULL,
-                                            group->num_counters,
-                                            group->counters);
+               for (monitor = metrics->monitor_head;
+                    monitor;
+                    monitor = monitor_next)
+               {
+                       glDeletePerfMonitorsAMD (1, &monitor->id);
+                       monitor_next = monitor->next;
+                       free (monitor);
+               }
+               metrics->monitor_head = NULL;
+               metrics->monitor_tail = NULL;
 
-               if (group->num_counters > metrics_info->max_counters_per_group)
-                       metrics_info->max_counters_per_group = group->num_counters;
        }
 
-       free (group_ids);
+       metrics->monitors_in_flight = 0;
+}
+
+void
+metrics_destroy (metrics_t *metrics)
+{
+       metrics_fini (metrics);
+
+       free (metrics);
 }
 
 static const char *
@@ -188,52 +231,30 @@ metrics_op_string (metrics_op_t op)
 }
 
 void
-metrics_counter_start (void)
+metrics_counter_start (metrics_t *metrics)
 {
-       context_t *ctx = &current_context;
-       timer_query_t *timer;
-       monitor_t *monitor;
-       int i;
-
-       /* Create new timer query, add to list */
-       timer = xmalloc (sizeof (timer_query_t));
+       unsigned i;
 
-       timer->op = ctx->op;
-       timer->next = NULL;
+       /* Initialize the timer_query object. */
+       glGenQueries (1, &metrics->timer_begun_id);
 
-       if (ctx->timer_tail) {
-               ctx->timer_tail->next = timer;
-               ctx->timer_tail = timer;
-       } else {
-               ctx->timer_tail = timer;
-               ctx->timer_head = timer;
+       /* Most everything else in this function is
+        * performance-monitor related. If we don't have that
+        * extension, just start the timer query and be done. */
+       if (! metrics->info->have_perfmon) {
+               glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
+               return;
        }
 
-       /* Create a new performance-monitor query */
-       monitor = xmalloc (sizeof (monitor_t));
-
-       monitor->op = ctx->op;
-       monitor->next = NULL;
-
-       if (ctx->monitor_tail) {
-               ctx->monitor_tail->next = monitor;
-               ctx->monitor_tail = monitor;
-       } else {
-               ctx->monitor_tail = monitor;
-               ctx->monitor_head = monitor;
-       }
+       /* Initialize the performance-monitor object */
+       glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
 
-       /* Initialize the timer_query and monitor objects */
-       glGenQueries (1, &timer->id);
-
-       glGenPerfMonitorsAMD (1, &monitor->id);
-
-       for (i = 0; i < ctx->metrics_info.num_groups; i++)
+       for (i = 0; i < metrics->info->num_groups; i++)
        {
-               counter_group_info_t *group;
+               metrics_group_info_t *group;
                int num_counters;
 
-               group = &ctx->metrics_info.groups[i];
+               group = &metrics->info->groups[i];
 
                num_counters = group->num_counters;
                if (group->max_active_counters < group->num_counters)
@@ -245,73 +266,125 @@ metrics_counter_start (void)
 
                }
 
-               glSelectPerfMonitorCountersAMD(monitor->id,
+               glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
                                               GL_TRUE, group->id,
                                               num_counters,
-                                              group->counters);
+                                              group->counter_ids);
        }
 
        /* Start the queries */
-       glBeginQuery (GL_TIME_ELAPSED, timer->id);
+       glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
 
-       glBeginPerfMonitorAMD (monitor->id);
+       glBeginPerfMonitorAMD (metrics->monitor_begun_id);
 }
 
 void
-metrics_counter_stop (void)
+metrics_counter_stop (metrics_t *metrics)
 {
+       timer_query_t *timer;
+       monitor_t *monitor;
+
+       /* Stop the current timer and monitor. */
        glEndQuery (GL_TIME_ELAPSED);
-       glEndPerfMonitorAMD (current_context.monitor_tail->id);
+
+       if (metrics->info->have_perfmon)
+               glEndPerfMonitorAMD (metrics->monitor_begun_id);
+
+       /* Add these IDs to our lists of outstanding queries and
+        * monitors so the results can be collected later. */
+       timer = xmalloc (sizeof (timer_query_t));
+
+       timer->op = metrics->op;
+       timer->id = metrics->timer_begun_id;
+       timer->next = NULL;
+
+       if (metrics->timer_tail) {
+               metrics->timer_tail->next = timer;
+               metrics->timer_tail = timer;
+       } else {
+               metrics->timer_tail = timer;
+               metrics->timer_head = timer;
+       }
+
+       if (metrics->info->have_perfmon) {
+               /* Create a new performance-monitor query */
+               monitor = xmalloc (sizeof (monitor_t));
+
+               monitor->op = metrics->op;
+               monitor->id = metrics->monitor_begun_id;
+               monitor->next = NULL;
+
+               if (metrics->monitor_tail) {
+                       metrics->monitor_tail->next = monitor;
+                       metrics->monitor_tail = monitor;
+               } else {
+                       metrics->monitor_tail = monitor;
+                       metrics->monitor_head = monitor;
+               }
+       }
+
+       metrics->monitors_in_flight++;
+
+       /* Avoid being a resource hog and collect outstanding results
+        * once we have sent off a large number of
+        * queries. (Presumably, many of the outstanding queries are
+        * available by now.)
+        */
+       if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
+               metrics_collect_available (metrics);
 }
 
 void
-metrics_set_current_op (metrics_op_t op)
+metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
 {
-       current_context.op = op;
+       metrics->op = op;
 }
 
 metrics_op_t
-metrics_get_current_op (void)
+metrics_get_current_op (metrics_t *metrics)
 {
-       return current_context.op;
+       return metrics->op;
 }
 
 static void
-op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
+op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
 {
-       metrics_info_t *info = &ctx->metrics_info;
-       unsigned i;
+       unsigned i, j;
 
        metrics->op = op;
        metrics->time_ns = 0.0;
 
-       metrics->num_counters = info->num_groups * info->max_counters_per_group;
-       metrics->counters = xmalloc (sizeof(double) * metrics->num_counters);
+       metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
 
-       for (i = 0; i < metrics->num_counters; i++)
-               metrics->counters[i] = 0.0;
+       for (i = 0; i < info->num_groups; i++) {
+               metrics->counters[i] = xmalloc (sizeof (double) *
+                                               info->groups[i].num_counters);
+               for (j = 0; j < info->groups[i].num_counters; j++)
+                       metrics->counters[i][j] = 0.0;
+       }
 }
 
 static op_metrics_t *
-ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
+_get_op_metrics (metrics_t *metrics, metrics_op_t op)
 {
        unsigned i;
 
-       if (op >= ctx->num_op_metrics)
+       if (op >= metrics->num_op_metrics)
        {
-               ctx->op_metrics = realloc (ctx->op_metrics,
-                                          (op + 1) * sizeof (op_metrics_t));
-               for (i = ctx->num_op_metrics; i < op + 1; i++)
-                       op_metrics_init (ctx, &ctx->op_metrics[i], i);
+               metrics->op_metrics = realloc (metrics->op_metrics,
+                                              (op + 1) * sizeof (op_metrics_t));
+               for (i = metrics->num_op_metrics; i < op + 1; i++)
+                       op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
 
-               ctx->num_op_metrics = op + 1;
+               metrics->num_op_metrics = op + 1;
        }
 
-       return &ctx->op_metrics[op];
+       return &metrics->op_metrics[op];
 }
 
 static void
-accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
+accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
+                           GLuint *result, GLuint size)
 {
 #define CONSUME(var)                                                   \
        if (p + sizeof(var) > ((unsigned char *) result) + size)        \
@@ -323,157 +396,303 @@ accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
        (var) = *((typeof(var) *) p);                                   \
        p += sizeof(var);
 
-       context_t *ctx = &current_context;
+       metrics_info_t *info = metrics->info;
+       op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
        unsigned char *p = (unsigned char *) result;
 
        while (p < ((unsigned char *) result) + size)
        {
-               GLuint group_id, counter_id, counter_type;
-               uint32_t value;
+               GLuint group_id, group_index;
+               GLuint counter_id, counter_index;
+               metrics_group_info_t *group;
+               double value = 0.0;
                unsigned i;
 
                CONSUME (group_id);
                CONSUME (counter_id);
 
-               glGetPerfMonitorCounterInfoAMD (group_id, counter_id,
-                                               GL_COUNTER_TYPE_AMD,
-                                               &counter_type);
-
-               /* We assume that all peformance counters are made
-                * available as uint32 values. This code can easily be
-                * extended as needed. */
-               if (counter_type != GL_UNSIGNED_INT) {
-                       fprintf (stderr, "Warning: Non-uint counter value. Ignoring remainder of results\n");
-                       break;
+               for (i = 0; i < info->num_groups; i++) {
+                       if (info->groups[i].id == group_id)
+                               break;
                }
+               group_index = i;
+               assert (group_index < info->num_groups);
+               group = &info->groups[group_index];
 
-               CONSUME (value);
-
-               i = (group_id * ctx->metrics_info.max_counters_per_group +
-                    counter_id);
+               for (i = 0; i < group->num_counters; i++) {
+                       if (group->counter_ids[i] == counter_id)
+                               break;
+               }
+               counter_index = i;
+               assert (counter_index < group->num_counters);
 
-               assert (i < ctx->op_metrics[op].num_counters);
+               switch (group->counter_types[counter_index])
+               {
+                       uint uint_value;
+                       uint64_t uint64_value;
+                       float float_value;
+               case GL_UNSIGNED_INT:
+                       CONSUME (uint_value);
+                       value = uint_value;
+                       break;
+               case GL_UNSIGNED_INT64_AMD:
+                       CONSUME (uint64_value);
+                       value = uint64_value;
+                       break;
+               case GL_PERCENTAGE_AMD:
+               case GL_FLOAT:
+                       CONSUME (float_value);
+                       value = float_value;
+                       break;
+               default:
+                       fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
+                                group->counter_types[counter_index]);
+                       value = 0.0;
+                       break;
+               }
 
-               /* FIXME: While I'm still occasionally getting bogus
-                * numbers from the performance counters, I'm simply
-                * going to discard anything larger than half the
-                * range, (something that looks like a negative signed
-                * quantity).
-                */
-               if (((int32_t) value) < 0)
-                       fprintf (stderr, ".");
-               else
-                       ctx->op_metrics[op].counters[i] += value;
+               op_metrics->counters[group_index][counter_index] += value;
        }
 }
 
 static void
-accumulate_program_time (metrics_op_t op, unsigned time_ns)
+accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
 {
-       op_metrics_t *metrics;
+       op_metrics_t *op_metrics;
 
-       metrics = ctx_get_op_metrics (&current_context, op);
+       op_metrics = _get_op_metrics (metrics, op);
 
-       metrics->time_ns += time_ns;
+       op_metrics->time_ns += time_ns;
 }
 
+typedef struct per_stage_metrics
+{
+       op_metrics_t *metrics;
+       shader_stage_info_t *stage;
+       double time_ns;
+       double active;
+} per_stage_metrics_t;
+
 static int
-time_compare(const void *in_a, const void *in_b, void *arg)
+_is_shader_stage_counter (metrics_info_t *info,
+                         unsigned group_index,
+                         unsigned counter_index)
 {
-       int a = *(const int *)in_a;
-       int b = *(const int *)in_b;
-       struct op_metrics *metrics = arg;
+       shader_stage_info_t *stage;
+       unsigned i;
+
+       for (i = 0; i < info->num_shader_stages; i++) {
+               stage = &info->stages[i];
+
+               if (stage->active_group_index == group_index &&
+                   stage->active_counter_index == counter_index)
+               {
+                       return 1;
+               }
+
+               if (stage->stall_group_index == group_index &&
+                   stage->stall_counter_index == counter_index)
+               {
+                       return 1;
+               }
+       }
 
-       if (metrics[a].time_ns < metrics[b].time_ns)
-               return -1;
-       if (metrics[a].time_ns > metrics[b].time_ns)
-               return 1;
        return 0;
 }
 
 static void
-print_program_metrics (void)
+print_per_stage_metrics (metrics_t *metrics,
+                        per_stage_metrics_t *per_stage,
+                        double total)
 {
-       context_t *ctx = &current_context;
-       int *sorted; /* Sorted indices into the ctx->op_metrics */
-       double total = 0;
-       unsigned i, j;
+       metrics_info_t *info = metrics->info;
+       op_metrics_t *op_metrics = per_stage->metrics;
+       metrics_group_info_t *group;
+       const char *op_string;
+       unsigned group_index, counter;
+       double value;
+
+       /* Don't print anything for stages with no alloted time. */
+       if (per_stage->time_ns == 0.0)
+               return;
+
+       op_string = metrics_op_string (op_metrics->op);
+
+       printf ("%21s", op_string);
+
+       if (op_metrics->op >= METRICS_OP_SHADER) {
+               printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
+       } else {
+               printf ("    ");
 
-       /* Make a sorted list of the operations by time used, and figure
-        * out the total so we can print percentages.
-        */
-       sorted = calloc(ctx->num_op_metrics, sizeof(*sorted));
-       for (i = 0; i < ctx->num_op_metrics; i++) {
-               sorted[i] = i;
-               total += ctx->op_metrics[i].time_ns;
        }
-       qsort_r(sorted, ctx->num_op_metrics, sizeof(*sorted),
-               time_compare, ctx->op_metrics);
 
-       for (i = 0; i < ctx->num_op_metrics; i++) {
-               const char *op_string;
-               op_metrics_t *metric =&ctx->op_metrics[sorted[i]];
+       if (per_stage->stage)
+               printf (" %cS:", per_stage->stage->name[0]);
+       else
+               printf ("   :");
 
-               /* Since we sparsely fill the array based on program
-                * id, many "programs" have no time.
-                */
-               if (metric->time_ns == 0.0)
-                       continue;
+       printf ("\t%7.2f ms (%4.1f%%)",
+               per_stage->time_ns / 1e6,
+               per_stage->time_ns / total * 100);
 
-               op_string = metrics_op_string (metric->op);
+       if (per_stage->active)
+               printf (", %4.1f%% active", per_stage->active * 100);
 
-               printf ("%s", op_string);
-               if (metric->op >= METRICS_OP_SHADER) {
-                       printf (" %d:", metric->op - METRICS_OP_SHADER);
-               } else {
-                       printf (":");
-                       for (j = strlen (op_string); j < 20; j++)
-                               printf (" ");
-               }
-               printf ("\t%7.2f ms (% 2.1f%%)",
-                       metric->time_ns / 1e6,
-                       metric->time_ns / total * 100);
-               printf ("[");
-               for (j = 0; j < metric->num_counters; j++) {
-                       if (metric->counters[j] == 0.0)
+       printf ("\n");
+
+       /* I'm not seeing a lot of value printing the rest of these
+        * performance counters by default yet. Use --verbose to get
+        * them for now. */
+       if (! verbose)
+               return;
+
+       printf ("[");
+       for (group_index = 0; group_index < info->num_groups; group_index++) {
+               group = &info->groups[group_index];
+               for (counter = 0; counter < group->num_counters; counter++) {
+
+                       /* Don't print this counter value if it's a
+                        * per-stage cycle counter, (which we have
+                        * already accounted for). */
+                       if (_is_shader_stage_counter (info, group_index, counter))
                                continue;
-                       printf ("%d: %.2f ms ", j, metric->counters[j] / 1e6);
+
+                       value = op_metrics->counters[group_index][counter];
+                       if (value == 0.0)
+                               continue;
+                       printf ("%s: %.2f ", group->counter_names[counter],
+                               value / 1e6);
                }
-               printf ("]\n");
        }
+       printf ("]\n");
 }
 
-/* Called at program exit */
-static void
-metrics_exit (void)
+static int
+time_compare(const void *in_a, const void *in_b, void *arg unused)
 {
-       if (verbose)
-               printf ("fips: terminating\n");
-}
+       const per_stage_metrics_t *a = in_a;
+       const per_stage_metrics_t *b = in_b;
 
 
-void
-metrics_end_frame (void)
+       if (a->time_ns < b->time_ns)
+               return -1;
+       if (a->time_ns > b->time_ns)
+               return 1;
+       return 0;
+}
+
+static void
+print_program_metrics (metrics_t *metrics)
 {
-       static int initialized = 0;
-       static struct timeval tv_start, tv_now;
+       metrics_info_t *info = metrics->info;
+       unsigned num_shader_stages = info->num_shader_stages;
+       per_stage_metrics_t *sorted, *per_stage;
+       double total_time, op_cycles;
+       op_metrics_t *op;
+       unsigned group_index, counter_index;
+       unsigned i, j, num_sorted;
+
+       /* Make a sorted list of the per-stage operations by time
+        * used, and figure out the total so we can print percentages.
+        */
+       if (num_shader_stages)
+               num_sorted = metrics->num_op_metrics * num_shader_stages;
+       else
+               num_sorted = metrics->num_op_metrics;
 
-       if (! initialized) {
-               gettimeofday (&tv_start, NULL);
-               atexit (metrics_exit);
-               if (getenv ("FIPS_VERBOSE"))
-                       verbose = 1;
-               initialized = 1;
+       sorted = xmalloc (sizeof (*sorted) * num_sorted);
+
+       total_time = 0.0;
+
+       for (i = 0; i < metrics->num_op_metrics; i++) {
+
+               op = &metrics->op_metrics[i];
+
+               /* Accumulate total time across all ops. */
+               total_time += op->time_ns;
+
+               /* Also, find total cycles in all stages of this op. */
+               op_cycles = 0.0;
+
+               if (num_shader_stages == 0) {
+                       per_stage = &sorted[i];
+                       per_stage->metrics = op;
+                       per_stage->stage = NULL;
+                       per_stage->time_ns = op->time_ns;
+                       per_stage->active = 0.0;
+               }
+
+               for (j = 0; j < num_shader_stages; j++) {
+                       /* Active cycles */
+                       group_index = info->stages[j].active_group_index;
+                       counter_index = info->stages[j].active_counter_index;
+                       op_cycles += op->counters[group_index][counter_index];
+
+                       /* Stall cycles */
+                       group_index = info->stages[j].stall_group_index;
+                       counter_index = info->stages[j].stall_counter_index;
+                       op_cycles += op->counters[group_index][counter_index];
+               }
+
+               for (j = 0; j < num_shader_stages; j++) {
+                       double active_cycles, stall_cycles, stage_cycles;
+
+                       /* Active cycles */
+                       group_index = info->stages[j].active_group_index;
+                       counter_index = info->stages[j].active_counter_index;
+                       active_cycles = op->counters[group_index][counter_index];
+
+                       /* Stall cycles */
+                       group_index = info->stages[j].stall_group_index;
+                       counter_index = info->stages[j].stall_counter_index;
+                       stall_cycles = op->counters[group_index][counter_index];
+
+                       stage_cycles = active_cycles + stall_cycles;
+
+                       per_stage = &sorted[i * num_shader_stages + j];
+                       per_stage->metrics = op;
+
+                       if (op_cycles) {
+                               per_stage->stage = &info->stages[j];
+                               per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
+                       } else {
+                               /* If we don't have any per-stage cycle counts
+                                * for this operation, then use the first
+                                * stage as a placeholder for all the time,
+                                * but NULL-ify the stage info so that the
+                                * report doesn't lie about this time being
+                                * from any particular stage. */
+                               per_stage->stage = NULL;
+                               if (j == 0) {
+                                       per_stage->time_ns = op->time_ns;
+                               } else {
+                                       per_stage->time_ns = 0.0;
+                               }
+                       }
+
+                       if (stage_cycles) {
+                               per_stage->active = active_cycles / stage_cycles;
+                       } else {
+                               per_stage->active = 0.0;
+                       }
+               }
        }
 
-       if (verbose)
-               printf ("fips: frame %d complete\n", frames);
+       qsort_r (sorted, num_sorted, sizeof (*sorted),
+                time_compare, metrics->op_metrics);
 
-       frames++;
-       gettimeofday (&tv_now, NULL);
+       for (i = 0; i < num_sorted; i++)
+               print_per_stage_metrics (metrics, &sorted[i], total_time);
 
+       free (sorted);
+}
+
+void
+metrics_collect_available (metrics_t *metrics)
+{
        /* Consume all timer queries that are ready. */
-       timer_query_t *timer = current_context.timer_head;
+       timer_query_t *timer = metrics->timer_head;
 
        while (timer) {
                GLuint available, elapsed;
@@ -486,20 +705,23 @@ metrics_end_frame (void)
                glGetQueryObjectuiv (timer->id,
                                     GL_QUERY_RESULT, &elapsed);
 
-               accumulate_program_time (timer->op, elapsed);
+               accumulate_program_time (metrics, timer->op, elapsed);
 
-               current_context.timer_head = timer->next;
-               if (current_context.timer_head == NULL)
-                       current_context.timer_tail = NULL;
+               metrics->timer_head = timer->next;
+               if (metrics->timer_head == NULL)
+                       metrics->timer_tail = NULL;
 
                glDeleteQueries (1, &timer->id);
 
                free (timer);
-               timer = current_context.timer_head;
+               timer = metrics->timer_head;
        }
 
+       if (! metrics->info->have_perfmon)
+               return;
+
        /* And similarly for all performance monitors that are ready. */
-       monitor_t *monitor = current_context.monitor_head;
+       monitor_t *monitor = metrics->monitor_head;
 
        while (monitor) {
                GLuint available, result_size, *result;
@@ -524,26 +746,59 @@ metrics_end_frame (void)
                                                result_size, result,
                                                &bytes_written);
 
-               accumulate_program_metrics (monitor->op, result, result_size);
+               accumulate_program_metrics (metrics, monitor->op, result, result_size);
+
+               free (result);
 
-               current_context.monitor_head = monitor->next;
-               if (current_context.monitor_head == NULL)
-                       current_context.monitor_tail = NULL;
+               metrics->monitor_head = monitor->next;
+               if (metrics->monitor_head == NULL)
+                       metrics->monitor_tail = NULL;
 
                glDeletePerfMonitorsAMD (1, &monitor->id);
 
                free (monitor);
-               monitor = current_context.monitor_head;
+
+               metrics->monitors_in_flight--;
+
+               monitor = metrics->monitor_head;
        }
+}
 
-       if (frames % 60 == 0) {
+static void
+metrics_exit (void)
+{
+       if (verbose)
+               printf ("fips: terminating\n");
+}
+
+void
+metrics_end_frame (metrics_t *metrics)
+{
+       static int initialized = 0;
+       static struct timeval tv_start, tv_now;
+
+       if (! initialized) {
+               gettimeofday (&tv_start, NULL);
+               atexit (metrics_exit);
+               if (getenv ("FIPS_VERBOSE"))
+                       verbose = 1;
+               initialized = 1;
+       }
+
+       frames++;
+
+       metrics_collect_available (metrics);
+
+       if (frames % 15 == 0) {
                double fps;
 
+               gettimeofday (&tv_now, NULL);
+
                fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
                                         (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
 
                printf("FPS: %.3f\n", fps);
 
-               print_program_metrics ();
+               print_program_metrics (metrics);
        }
 }