]> git.cworth.org Git - fips/blobdiff - metrics.c
Sort the output and print the percentage.
[fips] / metrics.c
index 8d2d6444beae601b9de93eea5d503a91729321c0..74d6c567d9e92cf46cde1b5110498a54605c0ef9 100644 (file)
--- a/metrics.c
+++ b/metrics.c
  * THE SOFTWARE.
  */
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <sys/time.h>
 
-#define GL_GLEXT_PROTOTYPES
-#include <GL/gl.h>
+#include "fips-dispatch-gl.h"
 
 #include "metrics.h"
 
@@ -38,6 +39,9 @@ typedef struct counter
 
 typedef struct program_metrics
 {
+       /* This happens to also be the index into the
+        * ctx->program_metrics array currently
+        */
        unsigned id;
        double ticks;
 } program_metrics_t;
@@ -58,8 +62,11 @@ typedef struct context
 
 context_t current_context;
 
+int frames;
+int verbose;
+
 unsigned
-metrics_add_counter (void)
+metrics_counter_new (void)
 {
        counter_t *counter;
 
@@ -85,6 +92,18 @@ metrics_add_counter (void)
        return counter->id;
 }
 
+void
+metrics_counter_start (unsigned counter)
+{
+       glBeginQuery (GL_TIME_ELAPSED, counter);
+}
+
+void
+metrics_counter_stop (void)
+{
+       glEndQuery (GL_TIME_ELAPSED);
+}
+
 void
 metrics_set_current_program (unsigned program)
 {
@@ -111,34 +130,80 @@ accumulate_program_ticks (unsigned program_id, unsigned ticks)
        ctx->program_metrics[program_id].ticks += ticks;
 }
 
-/* FIXME: Should sort the metrics, print out percentages, etc. */
+static int
+time_compare(const void *in_a, const void *in_b, void *arg)
+{
+       int a = *(const int *)in_a;
+       int b = *(const int *)in_b;
+       struct program_metrics *metrics = arg;
+
+       if (metrics[a].ticks < metrics[b].ticks)
+               return -1;
+       if (metrics[a].ticks > metrics[b].ticks)
+               return 1;
+       return 0;
+}
+
 static void
 print_program_metrics (void)
 {
        context_t *ctx = &current_context;
        unsigned i;
+       int *sorted; /* Sorted indices into the ctx->program_metrics */
+       double total = 0;
+
+       /* Make a sorted list of the programs by time used, and figure
+        * out to total so we can print percentages.
+        */
+       sorted = calloc(ctx->num_program_metrics, sizeof(*sorted));
+       for (i = 0; i < ctx->num_program_metrics; i++) {
+               sorted[i] = i;
+               total += ctx->program_metrics[i].ticks;
+       }
+       qsort_r(sorted, ctx->num_program_metrics, sizeof(*sorted),
+               time_compare, ctx->program_metrics);
 
        for (i = 0; i < ctx->num_program_metrics; i++) {
-               if (ctx->program_metrics[i].ticks == 0.0)
+               struct program_metrics *metric =
+                       &ctx->program_metrics[sorted[i]];
+
+               /* Since we sparsely fill the array based on program
+                * id, many "programs" have no time.
+                */
+               if (metric->ticks == 0.0)
                        continue;
-               printf ("Program %d:\t%7.2f mega-ticks\n",
-                       i, ctx->program_metrics[i].ticks / 1e6);
+
+               printf ("Program %d:\t%7.2f ms (% 2.1f%%)\n",
+                       metric->id, metric->ticks / 1e6,
+                       metric->ticks / total * 100);
        }
 }
 
+/* Called at program exit */
+static void
+metrics_exit (void)
+{
+       if (verbose)
+               printf ("fips: terminating\n");
+}
+
+
 void
 metrics_end_frame (void)
 {
        static int initialized = 0;
-       static int frames;
        static struct timeval tv_start, tv_now;
 
        if (! initialized) {
-               frames = 0;
                gettimeofday (&tv_start, NULL);
+               atexit (metrics_exit);
+               if (getenv ("FIPS_VERBOSE"))
+                       verbose = 1;
                initialized = 1;
        }
 
+       if (verbose)
+               printf ("fips: frame %d complete\n", frames);
 
        frames++;
        gettimeofday (&tv_now, NULL);
@@ -147,11 +212,10 @@ metrics_end_frame (void)
        counter_t *counter = current_context.counter_head;
 
        while (counter) {
-               GLint available;
-               GLuint elapsed;
+               GLuint available, elapsed;
 
-               glGetQueryObjectiv (counter->id, GL_QUERY_RESULT_AVAILABLE,
-                                   &available);
+               glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT_AVAILABLE,
+                                    &available);
                if (! available)
                        break;