1 /* Copyright © 2013, Intel Corporation
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
29 #include "fips-dispatch-gl.h"
33 typedef struct counter
40 typedef struct program_metrics
42 /* This happens to also be the index into the
43 * ctx->program_metrics array currently
49 typedef struct context
53 counter_t *counter_head;
54 counter_t *counter_tail;
56 unsigned num_program_metrics;
57 program_metrics_t *program_metrics;
60 /* FIXME: Need a map from integers to context objects and track the
61 * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
63 context_t current_context;
69 metrics_counter_new (void)
73 counter = malloc (sizeof(counter_t));
74 if (counter == NULL) {
75 fprintf (stderr, "Out of memory\n");
79 glGenQueries (1, &counter->id);
81 counter->program = current_context.program;
84 if (current_context.counter_tail) {
85 current_context.counter_tail->next = counter;
86 current_context.counter_tail = counter;
88 current_context.counter_tail = counter;
89 current_context.counter_head = counter;
96 metrics_counter_start (unsigned counter)
98 glBeginQuery (GL_TIME_ELAPSED, counter);
102 metrics_counter_stop (void)
104 glEndQuery (GL_TIME_ELAPSED);
108 metrics_set_current_program (unsigned program)
110 current_context.program = program;
114 accumulate_program_ticks (unsigned program_id, unsigned ticks)
116 context_t *ctx = ¤t_context;
119 if (program_id >= ctx->num_program_metrics) {
120 ctx->program_metrics = realloc (ctx->program_metrics,
121 (program_id + 1) * sizeof (program_metrics_t));
122 for (i = ctx->num_program_metrics; i < program_id + 1; i++) {
123 ctx->program_metrics[i].id = i;
124 ctx->program_metrics[i].ticks = 0.0;
127 ctx->num_program_metrics = program_id + 1;
130 ctx->program_metrics[program_id].ticks += ticks;
134 time_compare(const void *in_a, const void *in_b, void *arg)
136 int a = *(const int *)in_a;
137 int b = *(const int *)in_b;
138 struct program_metrics *metrics = arg;
140 if (metrics[a].ticks < metrics[b].ticks)
142 if (metrics[a].ticks > metrics[b].ticks)
148 print_program_metrics (void)
150 context_t *ctx = ¤t_context;
152 int *sorted; /* Sorted indices into the ctx->program_metrics */
155 /* Make a sorted list of the programs by time used, and figure
156 * out to total so we can print percentages.
158 sorted = calloc(ctx->num_program_metrics, sizeof(*sorted));
159 for (i = 0; i < ctx->num_program_metrics; i++) {
161 total += ctx->program_metrics[i].ticks;
163 qsort_r(sorted, ctx->num_program_metrics, sizeof(*sorted),
164 time_compare, ctx->program_metrics);
166 for (i = 0; i < ctx->num_program_metrics; i++) {
167 struct program_metrics *metric =
168 &ctx->program_metrics[sorted[i]];
170 /* Since we sparsely fill the array based on program
171 * id, many "programs" have no time.
173 if (metric->ticks == 0.0)
176 printf ("Program %d:\t%7.2f ms (% 2.1f%%)\n",
177 metric->id, metric->ticks / 1e6,
178 metric->ticks / total * 100);
182 /* Called at program exit */
187 printf ("fips: terminating\n");
192 metrics_end_frame (void)
194 static int initialized = 0;
195 static struct timeval tv_start, tv_now;
198 gettimeofday (&tv_start, NULL);
199 atexit (metrics_exit);
200 if (getenv ("FIPS_VERBOSE"))
206 printf ("fips: frame %d complete\n", frames);
209 gettimeofday (&tv_now, NULL);
211 /* Consume all counters that are ready. */
212 counter_t *counter = current_context.counter_head;
215 GLuint available, elapsed;
217 glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT_AVAILABLE,
222 glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT, &elapsed);
224 accumulate_program_ticks (counter->program, elapsed);
226 current_context.counter_head = counter->next;
227 if (current_context.counter_head == NULL)
228 current_context.counter_tail = NULL;
230 glDeleteQueries (1, &counter->id);
233 counter = current_context.counter_head;
236 if (frames % 60 == 0) {
239 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
240 (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
242 printf("FPS: %.3f\n", fps);
244 print_program_metrics ();