gallium: remove pipe_driver_query_group_info field type
[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 static bool
69 init_perf_monitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
70 {
71 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
72 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
73 struct pipe_context *pipe = st_context(ctx)->pipe;
74 int gid, cid;
75
76 st_flush_bitmap_cache(st_context(ctx));
77
78 /* Create a query for each active counter. */
79 for (gid = 0; gid < ctx->PerfMonitor.NumGroups; gid++) {
80 const struct gl_perf_monitor_group *g = &ctx->PerfMonitor.Groups[gid];
81
82 if (m->ActiveGroups[gid] > g->MaxActiveCounters) {
83 /* Maximum number of counters reached. Cannot start the session. */
84 if (ST_DEBUG & DEBUG_MESA) {
85 debug_printf("Maximum number of counters reached. "
86 "Cannot start the session!\n");
87 }
88 return false;
89 }
90
91 for (cid = 0; cid < g->NumCounters; cid++) {
92 const struct gl_perf_monitor_counter *c = &g->Counters[cid];
93 struct st_perf_counter_object *cntr;
94 int query_type;
95
96 if (!BITSET_TEST(m->ActiveCounters[gid], cid))
97 continue;
98
99 query_type = find_query_type(screen, c->Name);
100 assert(query_type != -1);
101
102 cntr = CALLOC_STRUCT(st_perf_counter_object);
103 if (!cntr)
104 return false;
105
106 cntr->query = pipe->create_query(pipe, query_type, 0);
107 cntr->id = cid;
108 cntr->group_id = gid;
109
110 list_addtail(&cntr->list, &stm->active_counters);
111 }
112 }
113 return true;
114 }
115
116 static void
117 reset_perf_monitor(struct st_perf_monitor_object *stm,
118 struct pipe_context *pipe)
119 {
120 struct st_perf_counter_object *cntr, *tmp;
121
122 LIST_FOR_EACH_ENTRY_SAFE(cntr, tmp, &stm->active_counters, list) {
123 if (cntr->query)
124 pipe->destroy_query(pipe, cntr->query);
125 list_del(&cntr->list);
126 free(cntr);
127 }
128 }
129
130 static struct gl_perf_monitor_object *
131 st_NewPerfMonitor(struct gl_context *ctx)
132 {
133 struct st_perf_monitor_object *stq = ST_CALLOC_STRUCT(st_perf_monitor_object);
134 if (stq) {
135 list_inithead(&stq->active_counters);
136 return &stq->base;
137 }
138 return NULL;
139 }
140
141 static void
142 st_DeletePerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
143 {
144 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
145 struct pipe_context *pipe = st_context(ctx)->pipe;
146
147 reset_perf_monitor(stm, pipe);
148 FREE(stm);
149 }
150
151 static GLboolean
152 st_BeginPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
153 {
154 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
155 struct pipe_context *pipe = st_context(ctx)->pipe;
156 struct st_perf_counter_object *cntr;
157
158 if (LIST_IS_EMPTY(&stm->active_counters)) {
159 /* Create a query for each active counter before starting
160 * a new monitoring session. */
161 if (!init_perf_monitor(ctx, m))
162 goto fail;
163 }
164
165 /* Start the query for each active counter. */
166 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
167 if (!pipe->begin_query(pipe, cntr->query))
168 goto fail;
169 }
170 return true;
171
172 fail:
173 /* Failed to start the monitoring session. */
174 reset_perf_monitor(stm, pipe);
175 return false;
176 }
177
178 static void
179 st_EndPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
180 {
181 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
182 struct pipe_context *pipe = st_context(ctx)->pipe;
183 struct st_perf_counter_object *cntr;
184
185 /* Stop the query for each active counter. */
186 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list)
187 pipe->end_query(pipe, cntr->query);
188 }
189
190 static void
191 st_ResetPerfMonitor(struct gl_context *ctx, struct gl_perf_monitor_object *m)
192 {
193 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
194 struct pipe_context *pipe = st_context(ctx)->pipe;
195
196 if (!m->Ended)
197 st_EndPerfMonitor(ctx, m);
198
199 reset_perf_monitor(stm, pipe);
200
201 if (m->Active)
202 st_BeginPerfMonitor(ctx, m);
203 }
204
205 static GLboolean
206 st_IsPerfMonitorResultAvailable(struct gl_context *ctx,
207 struct gl_perf_monitor_object *m)
208 {
209 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
210 struct pipe_context *pipe = st_context(ctx)->pipe;
211 struct st_perf_counter_object *cntr;
212
213 if (LIST_IS_EMPTY(&stm->active_counters))
214 return false;
215
216 /* The result of a monitoring session is only available if the query of
217 * each active counter is idle. */
218 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
219 union pipe_query_result result;
220 if (!pipe->get_query_result(pipe, cntr->query, FALSE, &result)) {
221 /* The query is busy. */
222 return false;
223 }
224 }
225 return true;
226 }
227
228 static void
229 st_GetPerfMonitorResult(struct gl_context *ctx,
230 struct gl_perf_monitor_object *m,
231 GLsizei dataSize,
232 GLuint *data,
233 GLint *bytesWritten)
234 {
235 struct st_perf_monitor_object *stm = st_perf_monitor_object(m);
236 struct pipe_context *pipe = st_context(ctx)->pipe;
237 struct st_perf_counter_object *cntr;
238
239 /* Copy data to the supplied array (data).
240 *
241 * The output data format is: <group ID, counter ID, value> for each
242 * active counter. The API allows counters to appear in any order.
243 */
244 GLsizei offset = 0;
245
246 /* Read query results for each active counter. */
247 LIST_FOR_EACH_ENTRY(cntr, &stm->active_counters, list) {
248 union pipe_query_result result = { 0 };
249 int gid, cid;
250 GLenum type;
251
252 cid = cntr->id;
253 gid = cntr->group_id;
254 type = ctx->PerfMonitor.Groups[gid].Counters[cid].Type;
255
256 if (!pipe->get_query_result(pipe, cntr->query, TRUE, &result))
257 continue;
258
259 data[offset++] = gid;
260 data[offset++] = cid;
261 switch (type) {
262 case GL_UNSIGNED_INT64_AMD:
263 *(uint64_t *)&data[offset] = result.u64;
264 offset += sizeof(uint64_t) / sizeof(GLuint);
265 break;
266 case GL_UNSIGNED_INT:
267 *(uint32_t *)&data[offset] = result.u32;
268 offset += sizeof(uint32_t) / sizeof(GLuint);
269 break;
270 case GL_FLOAT:
271 case GL_PERCENTAGE_AMD:
272 *(GLfloat *)&data[offset] = result.f;
273 offset += sizeof(GLfloat) / sizeof(GLuint);
274 break;
275 }
276 }
277
278 if (bytesWritten)
279 *bytesWritten = offset * sizeof(GLuint);
280 }
281
282
283 bool
284 st_init_perfmon(struct st_context *st)
285 {
286 struct gl_perf_monitor_state *perfmon = &st->ctx->PerfMonitor;
287 struct pipe_screen *screen = st->pipe->screen;
288 struct gl_perf_monitor_group *groups = NULL;
289 int num_counters, num_groups;
290 int gid, cid;
291
292 if (!screen->get_driver_query_info || !screen->get_driver_query_group_info)
293 return false;
294
295 /* Get the number of available queries. */
296 num_counters = screen->get_driver_query_info(screen, 0, NULL);
297 if (!num_counters)
298 return false;
299
300 /* Get the number of available groups. */
301 num_groups = screen->get_driver_query_group_info(screen, 0, NULL);
302 if (num_groups)
303 groups = CALLOC(num_groups, sizeof(*groups));
304 if (!groups)
305 return false;
306
307 for (gid = 0; gid < num_groups; gid++) {
308 struct gl_perf_monitor_group *g = &groups[perfmon->NumGroups];
309 struct pipe_driver_query_group_info group_info;
310 struct gl_perf_monitor_counter *counters = NULL;
311
312 if (!screen->get_driver_query_group_info(screen, gid, &group_info))
313 continue;
314
315 g->Name = group_info.name;
316 g->MaxActiveCounters = group_info.max_active_queries;
317 g->NumCounters = 0;
318 g->Counters = NULL;
319
320 if (group_info.num_queries)
321 counters = CALLOC(group_info.num_queries, sizeof(*counters));
322 if (!counters)
323 goto fail;
324
325 for (cid = 0; cid < num_counters; cid++) {
326 struct gl_perf_monitor_counter *c = &counters[g->NumCounters];
327 struct pipe_driver_query_info info;
328
329 if (!screen->get_driver_query_info(screen, cid, &info))
330 continue;
331 if (info.group_id != gid)
332 continue;
333
334 c->Name = info.name;
335 switch (info.type) {
336 case PIPE_DRIVER_QUERY_TYPE_UINT64:
337 c->Minimum.u64 = 0;
338 c->Maximum.u64 = info.max_value.u64 ? info.max_value.u64 : -1;
339 c->Type = GL_UNSIGNED_INT64_AMD;
340 break;
341 case PIPE_DRIVER_QUERY_TYPE_UINT:
342 c->Minimum.u32 = 0;
343 c->Maximum.u32 = info.max_value.u32 ? info.max_value.u32 : -1;
344 c->Type = GL_UNSIGNED_INT;
345 break;
346 case PIPE_DRIVER_QUERY_TYPE_FLOAT:
347 c->Minimum.f = 0.0;
348 c->Maximum.f = info.max_value.f ? info.max_value.f : -1;
349 c->Type = GL_FLOAT;
350 break;
351 case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:
352 c->Minimum.f = 0.0f;
353 c->Maximum.f = 100.0f;
354 c->Type = GL_PERCENTAGE_AMD;
355 break;
356 default:
357 unreachable("Invalid driver query type!");
358 }
359 g->NumCounters++;
360 }
361 g->Counters = counters;
362 perfmon->NumGroups++;
363 }
364 perfmon->Groups = groups;
365
366 return true;
367
368 fail:
369 for (gid = 0; gid < num_groups; gid++)
370 FREE((void *)groups[gid].Counters);
371 FREE(groups);
372 return false;
373 }
374
375 void
376 st_destroy_perfmon(struct st_context *st)
377 {
378 struct gl_perf_monitor_state *perfmon = &st->ctx->PerfMonitor;
379 int gid;
380
381 for (gid = 0; gid < perfmon->NumGroups; gid++)
382 FREE((void *)perfmon->Groups[gid].Counters);
383 FREE((void *)perfmon->Groups);
384 }
385
386 void st_init_perfmon_functions(struct dd_function_table *functions)
387 {
388 functions->NewPerfMonitor = st_NewPerfMonitor;
389 functions->DeletePerfMonitor = st_DeletePerfMonitor;
390 functions->BeginPerfMonitor = st_BeginPerfMonitor;
391 functions->EndPerfMonitor = st_EndPerfMonitor;
392 functions->ResetPerfMonitor = st_ResetPerfMonitor;
393 functions->IsPerfMonitorResultAvailable = st_IsPerfMonitorResultAvailable;
394 functions->GetPerfMonitorResult = st_GetPerfMonitorResult;
395 }