X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=metrics.c;h=74d6c567d9e92cf46cde1b5110498a54605c0ef9;hb=7c716eca3492764413e63bbd5386b7ec18d2efa7;hp=7298fa82bb093ba225371132e03abb1339df3077;hpb=6475596e655063624dbf54359bd2a45de6a17dd1;p=fips diff --git a/metrics.c b/metrics.c index 7298fa8..74d6c56 100644 --- a/metrics.c +++ b/metrics.c @@ -19,6 +19,8 @@ * THE SOFTWARE. */ +#define _GNU_SOURCE + #include #include @@ -37,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; @@ -57,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; @@ -84,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) { @@ -110,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 = ¤t_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);