blob: f15c710446068932c6aaacc1af12b8bde891d451 [file] [log] [blame]
Marek Olšákc91cf7d2013-03-21 19:47:06 +01001/**************************************************************************
2 *
3 * Copyright 2013 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/* This file contains code for reading values from pipe queries
29 * for displaying on the HUD. To prevent stalls when reading queries, we
30 * keep a list of busy queries in a ring. We read only those queries which
31 * are idle.
32 */
33
34#include "hud/hud_private.h"
35#include "pipe/p_screen.h"
Nicolai Hähnle222a2fb2017-10-22 17:38:44 +020036#include "util/os_time.h"
Nicolai Hähnle424a6142015-11-10 17:04:32 +010037#include "util/u_math.h"
Marek Olšákc91cf7d2013-03-21 19:47:06 +010038#include "util/u_memory.h"
39#include <stdio.h>
40
Nicolai Hähnle424a6142015-11-10 17:04:32 +010041// Must be a power of two
Marek Olšákc91cf7d2013-03-21 19:47:06 +010042#define NUM_QUERIES 8
43
Nicolai Hähnle424a6142015-11-10 17:04:32 +010044struct hud_batch_query_context {
Nicolai Hähnle424a6142015-11-10 17:04:32 +010045 unsigned num_query_types;
46 unsigned allocated_query_types;
47 unsigned *query_types;
48
49 boolean failed;
50 struct pipe_query *query[NUM_QUERIES];
51 union pipe_query_result *result[NUM_QUERIES];
52 unsigned head, pending, results;
53};
54
55void
Marek Olšák3132afd2017-11-18 16:25:52 +010056hud_batch_query_update(struct hud_batch_query_context *bq,
57 struct pipe_context *pipe)
Nicolai Hähnle424a6142015-11-10 17:04:32 +010058{
Nicolai Hähnle424a6142015-11-10 17:04:32 +010059 if (!bq || bq->failed)
60 return;
61
Nicolai Hähnle424a6142015-11-10 17:04:32 +010062 if (bq->query[bq->head])
63 pipe->end_query(pipe, bq->query[bq->head]);
64
65 bq->results = 0;
66
67 while (bq->pending) {
68 unsigned idx = (bq->head - bq->pending + 1) % NUM_QUERIES;
69 struct pipe_query *query = bq->query[idx];
70
71 if (!bq->result[idx])
72 bq->result[idx] = MALLOC(sizeof(bq->result[idx]->batch[0]) *
73 bq->num_query_types);
74 if (!bq->result[idx]) {
75 fprintf(stderr, "gallium_hud: out of memory.\n");
76 bq->failed = TRUE;
77 return;
78 }
79
80 if (!pipe->get_query_result(pipe, query, FALSE, bq->result[idx]))
81 break;
82
83 ++bq->results;
84 --bq->pending;
85 }
86
87 bq->head = (bq->head + 1) % NUM_QUERIES;
88
89 if (bq->pending == NUM_QUERIES) {
90 fprintf(stderr,
91 "gallium_hud: all queries busy after %i frames, dropping data.\n",
92 NUM_QUERIES);
93
94 assert(bq->query[bq->head]);
95
Marek Olšák3132afd2017-11-18 16:25:52 +010096 pipe->destroy_query(pipe, bq->query[bq->head]);
Nicolai Hähnle424a6142015-11-10 17:04:32 +010097 bq->query[bq->head] = NULL;
98 }
99
100 ++bq->pending;
101
102 if (!bq->query[bq->head]) {
103 bq->query[bq->head] = pipe->create_batch_query(pipe,
104 bq->num_query_types,
105 bq->query_types);
106
107 if (!bq->query[bq->head]) {
108 fprintf(stderr,
109 "gallium_hud: create_batch_query failed. You may have "
110 "selected too many or incompatible queries.\n");
111 bq->failed = TRUE;
112 return;
113 }
114 }
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100115}
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100116
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100117void
Marek Olšák3132afd2017-11-18 16:25:52 +0100118hud_batch_query_begin(struct hud_batch_query_context *bq,
119 struct pipe_context *pipe)
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100120{
121 if (!bq || bq->failed || !bq->query[bq->head])
122 return;
123
Marek Olšák3132afd2017-11-18 16:25:52 +0100124 if (!pipe->begin_query(pipe, bq->query[bq->head])) {
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100125 fprintf(stderr,
126 "gallium_hud: could not begin batch query. You may have "
127 "selected too many or incompatible queries.\n");
128 bq->failed = TRUE;
129 }
130}
131
132static boolean
133batch_query_add(struct hud_batch_query_context **pbq,
Marek Olšák3132afd2017-11-18 16:25:52 +0100134 unsigned query_type, unsigned *result_index)
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100135{
136 struct hud_batch_query_context *bq = *pbq;
137 unsigned i;
138
139 if (!bq) {
140 bq = CALLOC_STRUCT(hud_batch_query_context);
141 if (!bq)
142 return false;
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100143 *pbq = bq;
144 }
145
146 for (i = 0; i < bq->num_query_types; ++i) {
147 if (bq->query_types[i] == query_type) {
148 *result_index = i;
149 return true;
150 }
151 }
152
153 if (bq->num_query_types == bq->allocated_query_types) {
154 unsigned new_alloc = MAX2(16, bq->allocated_query_types * 2);
155 unsigned *new_query_types
156 = REALLOC(bq->query_types,
157 bq->allocated_query_types * sizeof(unsigned),
158 new_alloc * sizeof(unsigned));
159 if (!new_query_types)
160 return false;
161 bq->query_types = new_query_types;
162 bq->allocated_query_types = new_alloc;
163 }
164
165 bq->query_types[bq->num_query_types] = query_type;
166 *result_index = bq->num_query_types++;
167 return true;
168}
169
170void
Marek Olšák3132afd2017-11-18 16:25:52 +0100171hud_batch_query_cleanup(struct hud_batch_query_context **pbq,
172 struct pipe_context *pipe)
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100173{
174 struct hud_batch_query_context *bq = *pbq;
175 unsigned idx;
176
177 if (!bq)
178 return;
179
180 *pbq = NULL;
181
182 if (bq->query[bq->head] && !bq->failed)
Marek Olšák3132afd2017-11-18 16:25:52 +0100183 pipe->end_query(pipe, bq->query[bq->head]);
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100184
185 for (idx = 0; idx < NUM_QUERIES; ++idx) {
186 if (bq->query[idx])
Marek Olšák3132afd2017-11-18 16:25:52 +0100187 pipe->destroy_query(pipe, bq->query[idx]);
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100188 FREE(bq->result[idx]);
189 }
190
191 FREE(bq->query_types);
192 FREE(bq);
193}
194
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100195struct query_info {
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100196 struct hud_batch_query_context *batch;
Brian Paul541f5692018-01-11 09:50:42 -0700197 enum pipe_query_type query_type;
Brian Paul92840bd2018-01-11 11:11:04 -0700198
199 /** index to choose fields in pipe_query_data_pipeline_statistics,
200 * for example.
201 */
202 unsigned result_index;
Marek Olšák97a65d92015-08-02 17:24:30 +0200203 enum pipe_driver_query_result_type result_type;
Brian Paul92840bd2018-01-11 11:11:04 -0700204 enum pipe_driver_query_type type;
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100205
206 /* Ring of queries. If a query is busy, we use another slot. */
207 struct pipe_query *query[NUM_QUERIES];
208 unsigned head, tail;
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100209
210 uint64_t last_time;
211 uint64_t results_cumulative;
212 unsigned num_results;
213};
214
215static void
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100216query_new_value_batch(struct query_info *info)
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100217{
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100218 struct hud_batch_query_context *bq = info->batch;
219 unsigned result_index = info->result_index;
220 unsigned idx = (bq->head - bq->pending) % NUM_QUERIES;
221 unsigned results = bq->results;
222
223 while (results) {
224 info->results_cumulative += bq->result[idx]->batch[result_index].u64;
225 ++info->num_results;
226
227 --results;
228 idx = (idx - 1) % NUM_QUERIES;
229 }
230}
231
232static void
Marek Olšák3132afd2017-11-18 16:25:52 +0100233query_new_value_normal(struct query_info *info, struct pipe_context *pipe)
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100234{
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100235 if (info->last_time) {
Samuel Pitoisetb4b44062015-06-24 21:11:27 +0200236 if (info->query[info->head])
237 pipe->end_query(pipe, info->query[info->head]);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100238
239 /* read query results */
240 while (1) {
241 struct pipe_query *query = info->query[info->tail];
242 union pipe_query_result result;
Christoph Bumiller3d2790c2013-03-29 13:56:35 +0100243 uint64_t *res64 = (uint64_t *)&result;
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100244
Samuel Pitoisetb4b44062015-06-24 21:11:27 +0200245 if (query && pipe->get_query_result(pipe, query, FALSE, &result)) {
Brian Paul92840bd2018-01-11 11:11:04 -0700246 if (info->type == PIPE_DRIVER_QUERY_TYPE_FLOAT) {
247 assert(info->result_index == 0);
248 info->results_cumulative += (uint64_t) (result.f * 1000.0f);
249 }
250 else {
251 info->results_cumulative += res64[info->result_index];
252 }
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100253 info->num_results++;
254
255 if (info->tail == info->head)
256 break;
257
258 info->tail = (info->tail+1) % NUM_QUERIES;
259 }
260 else {
261 /* the oldest query is busy */
262 if ((info->head+1) % NUM_QUERIES == info->tail) {
263 /* all queries are busy, throw away the last query and create
264 * a new one */
265 fprintf(stderr,
266 "gallium_hud: all queries are busy after %i frames, "
267 "can't add another query\n",
268 NUM_QUERIES);
Samuel Pitoisetb4b44062015-06-24 21:11:27 +0200269 if (info->query[info->head])
270 pipe->destroy_query(pipe, info->query[info->head]);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100271 info->query[info->head] =
Ilia Mirkin43e4b3e2014-06-26 19:33:07 -0400272 pipe->create_query(pipe, info->query_type, 0);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100273 }
274 else {
275 /* the last query is busy, we need to add a new one we can use
276 * for this frame */
277 info->head = (info->head+1) % NUM_QUERIES;
278 if (!info->query[info->head]) {
279 info->query[info->head] =
Ilia Mirkin43e4b3e2014-06-26 19:33:07 -0400280 pipe->create_query(pipe, info->query_type, 0);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100281 }
282 }
283 break;
284 }
285 }
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100286 }
287 else {
288 /* initialize */
Ilia Mirkin43e4b3e2014-06-26 19:33:07 -0400289 info->query[info->head] = pipe->create_query(pipe, info->query_type, 0);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100290 }
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100291}
Samuel Pitoisetb4b44062015-06-24 21:11:27 +0200292
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100293static void
Marek Olšák3132afd2017-11-18 16:25:52 +0100294begin_query(struct hud_graph *gr, struct pipe_context *pipe)
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100295{
296 struct query_info *info = gr->query_data;
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100297
298 assert(!info->batch);
Samuel Pitoisetb4b44062015-06-24 21:11:27 +0200299 if (info->query[info->head])
300 pipe->begin_query(pipe, info->query[info->head]);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100301}
302
303static void
Marek Olšák3132afd2017-11-18 16:25:52 +0100304query_new_value(struct hud_graph *gr, struct pipe_context *pipe)
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100305{
306 struct query_info *info = gr->query_data;
307 uint64_t now = os_time_get();
308
309 if (info->batch) {
310 query_new_value_batch(info);
311 } else {
Marek Olšák3132afd2017-11-18 16:25:52 +0100312 query_new_value_normal(info, pipe);
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100313 }
314
315 if (!info->last_time) {
316 info->last_time = now;
317 return;
318 }
319
320 if (info->num_results && info->last_time + gr->pane->period <= now) {
Brian Paul92840bd2018-01-11 11:11:04 -0700321 double value;
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100322
323 switch (info->result_type) {
324 default:
325 case PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE:
326 value = info->results_cumulative / info->num_results;
327 break;
328 case PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE:
329 value = info->results_cumulative;
330 break;
331 }
332
Brian Paul92840bd2018-01-11 11:11:04 -0700333 if (info->type == PIPE_DRIVER_QUERY_TYPE_FLOAT) {
334 value /= 1000.0;
335 }
336
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100337 hud_graph_add_value(gr, value);
338
339 info->last_time = now;
340 info->results_cumulative = 0;
341 info->num_results = 0;
342 }
343}
344
345static void
Marek Olšák3132afd2017-11-18 16:25:52 +0100346free_query_info(void *ptr, struct pipe_context *pipe)
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100347{
348 struct query_info *info = ptr;
349
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100350 if (!info->batch && info->last_time) {
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100351 int i;
352
353 pipe->end_query(pipe, info->query[info->head]);
354
Brian Pauld9025042016-04-25 15:57:05 -0600355 for (i = 0; i < ARRAY_SIZE(info->query); i++) {
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100356 if (info->query[i]) {
357 pipe->destroy_query(pipe, info->query[i]);
358 }
359 }
360 }
361 FREE(info);
362}
363
Brian Paul92840bd2018-01-11 11:11:04 -0700364
365/**
366 * \param result_index to select fields of pipe_query_data_pipeline_statistics,
367 * for example.
368 */
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100369void
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100370hud_pipe_query_install(struct hud_batch_query_context **pbq,
Marek Olšák3132afd2017-11-18 16:25:52 +0100371 struct hud_pane *pane,
Brian Paul541f5692018-01-11 09:50:42 -0700372 const char *name,
373 enum pipe_query_type query_type,
Christoph Bumiller3d2790c2013-03-29 13:56:35 +0100374 unsigned result_index,
Marek Olšák97a65d92015-08-02 17:24:30 +0200375 uint64_t max_value, enum pipe_driver_query_type type,
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100376 enum pipe_driver_query_result_type result_type,
377 unsigned flags)
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100378{
379 struct hud_graph *gr;
380 struct query_info *info;
381
382 gr = CALLOC_STRUCT(hud_graph);
383 if (!gr)
384 return;
385
Ilia Mirkin2e34faa2015-03-26 19:37:50 -0400386 strncpy(gr->name, name, sizeof(gr->name));
387 gr->name[sizeof(gr->name) - 1] = '\0';
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100388 gr->query_data = CALLOC_STRUCT(query_info);
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100389 if (!gr->query_data)
390 goto fail_gr;
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100391
392 gr->query_new_value = query_new_value;
393 gr->free_query_data = free_query_info;
394
395 info = gr->query_data;
Marek Olšák97a65d92015-08-02 17:24:30 +0200396 info->result_type = result_type;
Brian Paul92840bd2018-01-11 11:11:04 -0700397 info->type = type;
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100398
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100399 if (flags & PIPE_DRIVER_QUERY_FLAG_BATCH) {
Marek Olšák3132afd2017-11-18 16:25:52 +0100400 if (!batch_query_add(pbq, query_type, &info->result_index))
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100401 goto fail_info;
402 info->batch = *pbq;
403 } else {
Marek Olšák1fe7c8d2017-01-15 22:28:15 +0100404 gr->begin_query = begin_query;
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100405 info->query_type = query_type;
406 info->result_index = result_index;
407 }
408
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100409 hud_pane_add_graph(pane, gr);
Marek Olšák0328b202016-08-18 19:42:16 +0200410 pane->type = type; /* must be set before updating the max_value */
411
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100412 if (pane->max_value < max_value)
413 hud_pane_set_max_value(pane, max_value);
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100414 return;
415
416fail_info:
417 FREE(info);
418fail_gr:
419 FREE(gr);
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100420}
421
422boolean
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100423hud_driver_query_install(struct hud_batch_query_context **pbq,
Marek Olšák3132afd2017-11-18 16:25:52 +0100424 struct hud_pane *pane, struct pipe_screen *screen,
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100425 const char *name)
426{
Juan A. Suarez Romeroa5dec102021-08-03 13:05:23 +0200427 struct pipe_driver_query_info query = { 0 };
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100428 unsigned num_queries, i;
429 boolean found = FALSE;
430
431 if (!screen->get_driver_query_info)
432 return FALSE;
433
434 num_queries = screen->get_driver_query_info(screen, 0, NULL);
435
436 for (i = 0; i < num_queries; i++) {
437 if (screen->get_driver_query_info(screen, i, &query) &&
438 strcmp(query.name, name) == 0) {
439 found = TRUE;
440 break;
441 }
442 }
443
444 if (!found)
445 return FALSE;
446
Marek Olšák3132afd2017-11-18 16:25:52 +0100447 hud_pipe_query_install(pbq, pane, query.name, query.query_type, 0,
Nicolai Hähnle424a6142015-11-10 17:04:32 +0100448 query.max_value.u64, query.type, query.result_type,
449 query.flags);
Samuel Pitoiset546ec982014-07-07 23:49:14 +0200450
Marek Olšákc91cf7d2013-03-21 19:47:06 +0100451 return TRUE;
452}