]> git.cworth.org Git - fips/blob - metrics.c
cbfe52e9d1ce6d8c19bed45c9d038b11d14bfa0e
[fips] / metrics.c
1 /* Copyright © 2013, Intel Corporation
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * THE SOFTWARE.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <sys/time.h>
26
27 #include "fips-dispatch.h"
28 #include "metrics.h"
29
30 typedef struct counter
31 {
32         unsigned id;
33         unsigned program;
34         struct counter *next;
35 } counter_t;
36
37 typedef struct program_metrics
38 {
39         unsigned id;
40         double ticks;
41 } program_metrics_t;
42
43 typedef struct context
44 {
45         unsigned int program;
46
47         counter_t *counter_head;
48         counter_t *counter_tail;
49
50         unsigned num_program_metrics;
51         program_metrics_t *program_metrics;
52 } context_t;
53
54 /* FIXME: Need a map from integers to context objects and track the
55  * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
56
57 context_t current_context;
58
59 unsigned
60 metrics_add_counter (void)
61 {
62         counter_t *counter;
63
64         counter = malloc (sizeof(counter_t));
65         if (counter == NULL) {
66                 fprintf (stderr, "Out of memory\n");
67                 exit (1);
68         }
69
70         glGenQueries (1, &counter->id);
71
72         counter->program = current_context.program;
73         counter->next = NULL;
74
75         if (current_context.counter_tail) {
76                 current_context.counter_tail->next = counter;
77                 current_context.counter_tail = counter;
78         } else {
79                 current_context.counter_tail = counter;
80                 current_context.counter_head = counter;
81         }
82
83         return counter->id;
84 }
85
86 void
87 metrics_set_current_program (unsigned program)
88 {
89         current_context.program = program;
90 }
91
92 static void
93 accumulate_program_ticks (unsigned program_id, unsigned ticks)
94 {
95         context_t *ctx = &current_context;
96         unsigned i;
97
98         if (program_id >= ctx->num_program_metrics) {
99                 ctx->program_metrics = realloc (ctx->program_metrics,
100                                                 (program_id + 1) * sizeof (program_metrics_t));
101                 for (i = ctx->num_program_metrics; i < program_id + 1; i++) {
102                         ctx->program_metrics[i].id = i;
103                         ctx->program_metrics[i].ticks = 0.0;
104                 }
105
106                 ctx->num_program_metrics = program_id + 1;
107         }
108
109         ctx->program_metrics[program_id].ticks += ticks;
110 }
111
112 /* FIXME: Should sort the metrics, print out percentages, etc. */
113 static void
114 print_program_metrics (void)
115 {
116         context_t *ctx = &current_context;
117         unsigned i;
118
119         for (i = 0; i < ctx->num_program_metrics; i++) {
120                 if (ctx->program_metrics[i].ticks == 0.0)
121                         continue;
122                 printf ("Program %d:\t%7.2f mega-ticks\n",
123                         i, ctx->program_metrics[i].ticks / 1e6);
124         }
125 }
126
127 void
128 metrics_end_frame (void)
129 {
130         static int initialized = 0;
131         static int frames;
132         static struct timeval tv_start, tv_now;
133
134         if (! initialized) {
135                 frames = 0;
136                 gettimeofday (&tv_start, NULL);
137                 initialized = 1;
138         }
139
140
141         frames++;
142         gettimeofday (&tv_now, NULL);
143
144         /* Consume all counters that are ready. */
145         counter_t *counter = current_context.counter_head;
146
147         while (counter) {
148                 GLuint available, elapsed;
149
150                 glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT_AVAILABLE,
151                                      &available);
152                 if (! available)
153                         break;
154
155                 glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT, &elapsed);
156
157                 accumulate_program_ticks (counter->program, elapsed);
158
159                 current_context.counter_head = counter->next;
160                 if (current_context.counter_head == NULL)
161                         current_context.counter_tail = NULL;
162
163                 glDeleteQueries (1, &counter->id);
164
165                 free (counter);
166                 counter = current_context.counter_head;
167         }
168
169         if (frames % 60 == 0) {
170                 double fps;
171
172                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
173                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
174
175                 printf("FPS: %.3f\n", fps);
176
177                 print_program_metrics ();
178         }
179 }