st/mesa: fix pipe_query_result result initializer
[mesa.git] / src / mesa / state_tracker / st_cb_perfmon.c
1 /*
2 * Copyright (C) 2013 Christoph Bumiller
3 * Copyright (C) 2015 Samuel Pitoiset
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * Performance monitoring counters interface to gallium.
26 */
27
28 #include "st_debug.h"
29 #include "st_context.h"
30 #include "st_cb_bitmap.h"
31 #include "st_cb_perfmon.h"
32
33 #include "util/bitset.h"
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_memory.h"
38
39 /**
40 * Return a PIPE_QUERY_x type >= PIPE_QUERY_DRIVER_SPECIFIC, or -1 if
41 * the driver-specific query doesn't exist.
42 */
43 static int
44 find_query_type(struct pipe_screen *screen, const char *name)
45 {
46 int num_queries;
47 int type = -1;
48 int i;
49
50 num_queries = screen->get_driver_query_info(screen, 0, NULL);
51 if (!num_queries)
52 return type;
53
54 for (i = 0; i < num_queries; i++) {
55 struct pipe_driver_query_info info;
56
57 if (!screen->get_driver_query_info(screen, i, &info))
58 continue;
59
60 if (!strncmp(info.name, name, strlen(name))) {
61 type = info.query_type;
62 break;
63 }
64 }
65 return type;
66 }
67
68 /**
69 * Return TRUE if the underlying driver expose GPU counters.
70 */
71 static bool
72 has_gpu_counters(struct pipe_screen *screen)
73 {
74 int num_groups, gid;
75
76 num_groups = screen->get_driver_query_group_info(screen, 0, NULL);
77 for (gid = 0; gid < num_groups; gid++) {
78 struct pipe_driver_query_group_info group_info;
79
80 if (!screen->get_driver_query_group_info(screen, gid, &group_info))
81 continue;
82
83 if (group_info.type == PIPE_DRIVER_QUERY_GROUP_TYPE_GPU)
84 return true;
85 }
86 return false;
87 }
88
89 static bool
90 init_perf_monitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
91 {
92 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
93 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
94 struct pipe_context *pipe = st_context(ctx)->pipe;
95 int gid, cid;
96
97 st_flush_bitmap_cache(st_context(ctx));
98
99 /* Create a query for each active counter. */
100 for (gid = 0; gid < ctx->PerfMonitor.NumGroups; gid++) {
101 const struct gl_perf_monitor_group *g = &ctx->PerfMonitor.Groups[gid];
102
103 if (m->ActiveGroups[gid] > g->MaxActiveCounters) {
104 /* Maximum number of counters reached. Cannot start the session. */
105 if (ST_DEBUG & DEBUG_MESA) {
106 debug_printf("Maximum number of counters reached. "
107 "Cannot start the session!\n");
108 }
109 return false;
110 }
111
112 for (cid = 0; cid < g->NumCounters; cid++) {
113 const struct gl_perf_monitor_counter *c = &g->Counters[cid];
114 struct st_perf_counter_object *cntr;
115 int query_type;
116
117 if (!BITSET_TEST(m->ActiveCounters[gid], cid))
118 continue;
119
120 query_type = find_query_type(screen, c->Name);
121 assert(query_type != -1);
122
123 cntr = CALLOC_STRUCT(st_perf_counter_object);
124 if (!cntr)
125 return false;
126
127 cntr->query = pipe->create_query(pipe, query_type, 0);
128 cntr->id = cid;
129 cntr->group_id = gid;
130
131 list_addtail(&cntr->list, &stm->active_counters);
132 }
133 }
134 return true;
135 }
136
137 static void
138 reset_perf_monitor(struct st_perf_monitor_object *stm,
139 struct pipe_context *pipe)
140 {
141 struct st_perf_counter_object *cntr, *tmp;
142
143 LIST_FOR_EACH_ENTRY_SAFE(cntr, tmp, &stm->active_counters, list) {
144 if (cntr->query)
145 pipe->destroy_query(pipe, cntr->query);
146 list_del(&cntr->list);
147 free(cntr);
148 }
149 }
150
151 static struct gl_perf_monitor_object *
152 st_NewPerfMonitor(struct gl_context *ctx)
153 {
154 struct st_perf_monitor_object *stq = ST_CALLOC_STRUCT(st_perf_monitor_object);
155 if (stq) {
156 list_inithead(&stq->active_counters);
157 return &stq->base;
158 }
159 return NULL;
160 }
161
162 static void
163 st_DeletePerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
164 {
165 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
166 struct pipe_context *pipe = st_context(ctx)->pipe;
167
168 reset_perf_monitor(stm, pipe);
169 FREE(stm);
170 }
171
172 static GLboolean
173 st_BeginPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
174 {
175 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
176 struct pipe_context *pipe = st_context(ctx)->pipe;
177 struct st_perf_counter_object *cntr;
178
179 if (LIST_IS_EMPTY(&stm->active_counters)) {
180 /* Create a query for each active counter before starting
181 * a new monitoring session. */
182 if (!init_perf_monitor(ctx, m))
183 goto fail;
184 }
185
186 /* Start the query for each active counter. */
187 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
188 if (!pipe->begin_query(pipe, cntr->query))
189 goto fail;
190 }
191 return true;
192
193 fail:
194 /* Failed to start the monitoring session. */
195 reset_perf_monitor(stm, pipe);
196 return false;
197 }
198
199 static void
200 st_EndPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
201 {
202 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
203 struct pipe_context *pipe = st_context(ctx)->pipe;
204 struct st_perf_counter_object *cntr;
205
206 /* Stop the query for each active counter. */
207 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list)
208 pipe->end_query(pipe, cntr->query);
209 }
210
211 static void
212 st_ResetPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
213 {
214 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
215 struct pipe_context *pipe = st_context(ctx)->pipe;
216
217 if (!m->Ended)
218 st_EndPerfMonitor(ctx, m);
219
220 reset_perf_monitor(stm, pipe);
221
222 if (m->Active)
223 st_BeginPerfMonitor(ctx, m);
224 }
225
226 static GLboolean
227 st_IsPerfMonitorResultAvailable(struct gl_context *ctx,
228 struct gl_perf_monitor_object *m)
229 {
230 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
231 struct pipe_context *pipe = st_context(ctx)->pipe;
232 struct st_perf_counter_object *cntr;
233
234 if (LIST_IS_EMPTY(&stm->active_counters))
235 return false;
236
237 /* The result of a monitoring session is only available if the query of
238 * each active counter is idle. */
239 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
240 union pipe_query_result result;
241 if (!pipe->get_query_result(pipe, cntr->query, FALSE, &result)) {
242 /* The query is busy. */
243 return false;
244 }
245 }
246 return true;
247 }
248
249 static void
250 st_GetPerfMonitorResult(struct gl_context *ctx,
251 struct gl_perf_monitor_object *m,
252 GLsizei dataSize,
253 GLuint *data,
254 GLint *bytesWritten)
255 {
256 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
257 struct pipe_context *pipe = st_context(ctx)->pipe;
258 struct st_perf_counter_object *cntr;
259
260 /* Copy data to the supplied array (data).
261 *
262 * The output data format is: <group ID, counter ID, value> for each
263 * active counter. The API allows counters to appear in any order.
264 */
265 GLsizei offset = 0;
266
267 /* Read query results for each active counter. */
268 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
269 union pipe_query_result result = { 0 };
270 int gid, cid;
271 GLenum type;
272
273 cid = cntr->id;
274 gid = cntr->group_id;
275 type = ctx->PerfMonitor.Groups[gid].Counters[cid].Type;
276
277 if (!pipe->get_query_result(pipe, cntr->query, TRUE, &result))
278 continue;
279
280 data[offset++] = gid;
281 data[offset++] = cid;
282 switch (type) {
283 case GL_UNSIGNED_INT64_AMD:
284 *(uint64_t *)&data[offset] = result.u64;
285 offset += sizeof(uint64_t) / sizeof(GLuint);
286 break;
287 case GL_UNSIGNED_INT:
288 *(uint32_t *)&data[offset] = result.u32;
289 offset += sizeof(uint32_t) / sizeof(GLuint);
290 break;
291 case GL_FLOAT:
292 case GL_PERCENTAGE_AMD:
293 *(GLfloat *)&data[offset] = result.f;
294 offset += sizeof(GLfloat) / sizeof(GLuint);
295 break;
296 }
297 }
298
299 if (bytesWritten)
300 *bytesWritten = offset * sizeof(GLuint);
301 }
302
303
304 bool
305 st_init_perfmon(struct st_context *st)
306 {
307 struct gl_perf_monitor_state *perfmon = &st->ctx->PerfMonitor;
308 struct pipe_screen *screen = st->pipe->screen;
309 struct gl_perf_monitor_group *groups = NULL;
310 int num_counters, num_groups;
311 int gid, cid;
312
313 if (!screen->get_driver_query_info || !screen->get_driver_query_group_info)
314 return false;
315
316 if (!has_gpu_counters(screen)) {
317 /* According to the spec, GL_AMD_performance_monitor must only
318 * expose GPU counters. */
319 return false;
320 }
321
322 /* Get the number of available queries. */
323 num_counters = screen->get_driver_query_info(screen, 0, NULL);
324 if (!num_counters)
325 return false;
326
327 /* Get the number of available groups. */
328 num_groups = screen->get_driver_query_group_info(screen, 0, NULL);
329 if (num_groups)
330 groups = CALLOC(num_groups, sizeof(*groups));
331 if (!groups)
332 return false;
333
334 for (gid = 0; gid < num_groups; gid++) {
335 struct gl_perf_monitor_group *g = &groups[perfmon->NumGroups];
336 struct pipe_driver_query_group_info group_info;
337 struct gl_perf_monitor_counter *counters = NULL;
338
339 if (!screen->get_driver_query_group_info(screen, gid, &group_info))
340 continue;
341
342 if (group_info.type != PIPE_DRIVER_QUERY_GROUP_TYPE_GPU)
343 continue;
344
345 g->Name = group_info.name;
346 g->MaxActiveCounters = group_info.max_active_queries;
347 g->NumCounters = 0;
348 g->Counters = NULL;
349
350 if (group_info.num_queries)
351 counters = CALLOC(group_info.num_queries, sizeof(*counters));
352 if (!counters)
353 goto fail;
354
355 for (cid = 0; cid < num_counters; cid++) {
356 struct gl_perf_monitor_counter *c = &counters[g->NumCounters];
357 struct pipe_driver_query_info info;
358
359 if (!screen->get_driver_query_info(screen, cid, &info))
360 continue;
361 if (info.group_id != gid)
362 continue;
363
364 c->Name = info.name;
365 switch (info.type) {
366 case PIPE_DRIVER_QUERY_TYPE_UINT64:
367 c->Minimum.u64 = 0;
368 c->Maximum.u64 = info.max_value.u64 ? info.max_value.u64 : -1;
369 c->Type = GL_UNSIGNED_INT64_AMD;
370 break;
371 case PIPE_DRIVER_QUERY_TYPE_UINT:
372 c->Minimum.u32 = 0;
373 c->Maximum.u32 = info.max_value.u32 ? info.max_value.u32 : -1;
374 c->Type = GL_UNSIGNED_INT;
375 break;
376 case PIPE_DRIVER_QUERY_TYPE_FLOAT:
377 c->Minimum.f = 0.0;
378 c->Maximum.f = info.max_value.f ? info.max_value.f : -1;
379 c->Type = GL_FLOAT;
380 break;
381 case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:
382 c->Minimum.f = 0.0f;
383 c->Maximum.f = 100.0f;
384 c->Type = GL_PERCENTAGE_AMD;
385 break;
386 default:
387 unreachable("Invalid driver query type!");
388 }
389 g->NumCounters++;
390 }
391 g->Counters = counters;
392 perfmon->NumGroups++;
393 }
394 perfmon->Groups = groups;
395
396 return true;
397
398 fail:
399 for (gid = 0; gid < num_groups; gid++)
400 FREE((void *)groups[gid].Counters);
401 FREE(groups);
402 return false;
403 }
404
405 void
406 st_destroy_perfmon(struct st_context *st)
407 {
408 struct gl_perf_monitor_state *perfmon = &st->ctx->PerfMonitor;
409 int gid;
410
411 for (gid = 0; gid < perfmon->NumGroups; gid++)
412 FREE((void *)perfmon->Groups[gid].Counters);
413 FREE((void *)perfmon->Groups);
414 }
415
416 void st_init_perfmon_functions(struct dd_function_table *functions)
417 {
418 functions->NewPerfMonitor = st_NewPerfMonitor;
419 functions->DeletePerfMonitor = st_DeletePerfMonitor;
420 functions->BeginPerfMonitor = st_BeginPerfMonitor;
421 functions->EndPerfMonitor = st_EndPerfMonitor;
422 functions->ResetPerfMonitor = st_ResetPerfMonitor;
423 functions->IsPerfMonitorResultAvailable = st_IsPerfMonitorResultAvailable;
424 functions->GetPerfMonitorResult = st_GetPerfMonitorResult;
425 }