* THE SOFTWARE.
*/
+#define _GNU_SOURCE
+
#include <stdio.h>
#include <stdlib.h>
typedef struct program_metrics
{
+ /* This happens to also be the index into the
+ * ctx->program_metrics array currently
+ */
unsigned id;
- double ticks;
+ double time_ns;
} program_metrics_t;
typedef struct context
}
static void
-accumulate_program_ticks (unsigned program_id, unsigned ticks)
+accumulate_program_time (unsigned program_id, unsigned time_ns)
{
context_t *ctx = ¤t_context;
unsigned i;
(program_id + 1) * sizeof (program_metrics_t));
for (i = ctx->num_program_metrics; i < program_id + 1; i++) {
ctx->program_metrics[i].id = i;
- ctx->program_metrics[i].ticks = 0.0;
+ ctx->program_metrics[i].time_ns = 0.0;
}
ctx->num_program_metrics = program_id + 1;
}
- ctx->program_metrics[program_id].ticks += ticks;
+ ctx->program_metrics[program_id].time_ns += time_ns;
+}
+
+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].time_ns < metrics[b].time_ns)
+ return -1;
+ if (metrics[a].time_ns > metrics[b].time_ns)
+ return 1;
+ return 0;
}
-/* FIXME: Should sort the metrics, print out percentages, etc. */
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++) {
- if (ctx->program_metrics[i].ticks == 0.0)
+ sorted[i] = i;
+ total += ctx->program_metrics[i].time_ns;
+ }
+ qsort_r(sorted, ctx->num_program_metrics, sizeof(*sorted),
+ time_compare, ctx->program_metrics);
+
+ for (i = 0; i < ctx->num_program_metrics; i++) {
+ 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->time_ns == 0.0)
continue;
- printf ("Program %d:\t%7.2f ms\n",
- i, ctx->program_metrics[i].ticks / 1e6);
+
+ printf ("Program %d:\t%7.2f ms (% 2.1f%%)\n",
+ metric->id, metric->time_ns / 1e6,
+ metric->time_ns / total * 100);
}
}
glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT, &elapsed);
- accumulate_program_ticks (counter->program, elapsed);
+ accumulate_program_time (counter->program, elapsed);
current_context.counter_head = counter->next;
if (current_context.counter_head == NULL)