xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/freedreno_query.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2013 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "pipe/p_state.h"
10 #include "util/u_memory.h"
11 
12 #include "freedreno_context.h"
13 #include "freedreno_query.h"
14 #include "freedreno_query_hw.h"
15 #include "freedreno_query_sw.h"
16 #include "freedreno_resource.h"
17 #include "freedreno_util.h"
18 
19 /*
20  * Pipe Query interface:
21  */
22 
23 static struct pipe_query *
fd_create_query(struct pipe_context * pctx,unsigned query_type,unsigned index)24 fd_create_query(struct pipe_context *pctx, unsigned query_type, unsigned index)
25 {
26    struct fd_context *ctx = fd_context(pctx);
27    struct fd_query *q = NULL;
28 
29    if (ctx->create_query)
30       q = ctx->create_query(ctx, query_type, index);
31    if (!q)
32       q = fd_sw_create_query(ctx, query_type, index);
33 
34    return (struct pipe_query *)q;
35 }
36 
37 static void
fd_destroy_query(struct pipe_context * pctx,struct pipe_query * pq)38 fd_destroy_query(struct pipe_context *pctx, struct pipe_query *pq) in_dt
39 {
40    struct fd_query *q = fd_query(pq);
41    q->funcs->destroy_query(fd_context(pctx), q);
42 }
43 
44 static bool
fd_begin_query(struct pipe_context * pctx,struct pipe_query * pq)45 fd_begin_query(struct pipe_context *pctx, struct pipe_query *pq) in_dt
46 {
47    struct fd_query *q = fd_query(pq);
48 
49    q->funcs->begin_query(fd_context(pctx), q);
50 
51    return true;
52 }
53 
54 static bool
fd_end_query(struct pipe_context * pctx,struct pipe_query * pq)55 fd_end_query(struct pipe_context *pctx, struct pipe_query *pq) in_dt
56 {
57    struct fd_query *q = fd_query(pq);
58 
59    /* there are a couple special cases, which don't have
60     * a matching ->begin_query():
61     */
62    if (skip_begin_query(q->type))
63       fd_begin_query(pctx, pq);
64 
65    q->funcs->end_query(fd_context(pctx), q);
66 
67    return true;
68 }
69 
70 static bool
fd_get_query_result(struct pipe_context * pctx,struct pipe_query * pq,bool wait,union pipe_query_result * result)71 fd_get_query_result(struct pipe_context *pctx, struct pipe_query *pq, bool wait,
72                     union pipe_query_result *result)
73 {
74    struct fd_query *q = fd_query(pq);
75 
76    util_query_clear_result(result, q->type);
77 
78    return q->funcs->get_query_result(fd_context(pctx), q, wait, result);
79 }
80 
81 static void
fd_get_query_result_resource(struct pipe_context * pctx,struct pipe_query * pq,enum pipe_query_flags flags,enum pipe_query_value_type result_type,int index,struct pipe_resource * prsc,unsigned offset)82 fd_get_query_result_resource(struct pipe_context *pctx, struct pipe_query *pq,
83                              enum pipe_query_flags flags,
84                              enum pipe_query_value_type result_type,
85                              int index, struct pipe_resource *prsc,
86                              unsigned offset)
87    in_dt
88 {
89    struct fd_query *q = fd_query(pq);
90 
91    q->funcs->get_query_result_resource(fd_context(pctx), q, flags, result_type,
92                                        index, fd_resource(prsc), offset);
93 }
94 
95 static void
fd_render_condition(struct pipe_context * pctx,struct pipe_query * pq,bool condition,enum pipe_render_cond_flag mode)96 fd_render_condition(struct pipe_context *pctx, struct pipe_query *pq,
97                     bool condition, enum pipe_render_cond_flag mode) in_dt
98 {
99    struct fd_context *ctx = fd_context(pctx);
100    ctx->cond_query = pq;
101    ctx->cond_cond = condition;
102    ctx->cond_mode = mode;
103 }
104 
105 #define _Q(_name, _query_type, _type, _result_type) {                          \
106       .name = _name, .query_type = _query_type,                                \
107       .type = PIPE_DRIVER_QUERY_TYPE_##_type,                                  \
108       .result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_##_result_type,             \
109       .group_id = ~(unsigned)0,                                                \
110    }
111 
112 #define FQ(_name, _query_type, _type, _result_type)                            \
113    _Q(_name, FD_QUERY_##_query_type, _type, _result_type)
114 
115 #define PQ(_name, _query_type, _type, _result_type)                            \
116    _Q(_name, PIPE_QUERY_##_query_type, _type, _result_type)
117 
118 static const struct pipe_driver_query_info sw_query_list[] = {
119    FQ("draw-calls", DRAW_CALLS, UINT64, AVERAGE),
120    FQ("batches", BATCH_TOTAL, UINT64, AVERAGE),
121    FQ("batches-sysmem", BATCH_SYSMEM, UINT64, AVERAGE),
122    FQ("batches-gmem", BATCH_GMEM, UINT64, AVERAGE),
123    FQ("batches-nondraw", BATCH_NONDRAW, UINT64, AVERAGE),
124    FQ("restores", BATCH_RESTORE, UINT64, AVERAGE),
125    PQ("prims-emitted", PRIMITIVES_EMITTED, UINT64, AVERAGE),
126    FQ("staging", STAGING_UPLOADS, UINT64, AVERAGE),
127    FQ("shadow", SHADOW_UPLOADS, UINT64, AVERAGE),
128    FQ("vsregs", VS_REGS, FLOAT, AVERAGE),
129    FQ("fsregs", FS_REGS, FLOAT, AVERAGE),
130 };
131 
132 static int
fd_get_driver_query_info(struct pipe_screen * pscreen,unsigned index,struct pipe_driver_query_info * info)133 fd_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,
134                          struct pipe_driver_query_info *info)
135 {
136    struct fd_screen *screen = fd_screen(pscreen);
137 
138    if (!info)
139       return ARRAY_SIZE(sw_query_list) + screen->num_perfcntr_queries;
140 
141    if (index >= ARRAY_SIZE(sw_query_list)) {
142       index -= ARRAY_SIZE(sw_query_list);
143       if (index >= screen->num_perfcntr_queries)
144          return 0;
145       *info = screen->perfcntr_queries[index];
146       return 1;
147    }
148 
149    *info = sw_query_list[index];
150    return 1;
151 }
152 
153 static int
fd_get_driver_query_group_info(struct pipe_screen * pscreen,unsigned index,struct pipe_driver_query_group_info * info)154 fd_get_driver_query_group_info(struct pipe_screen *pscreen, unsigned index,
155                                struct pipe_driver_query_group_info *info)
156 {
157    struct fd_screen *screen = fd_screen(pscreen);
158 
159    if (!info)
160       return screen->num_perfcntr_groups;
161 
162    if (index >= screen->num_perfcntr_groups)
163       return 0;
164 
165    const struct fd_perfcntr_group *g = &screen->perfcntr_groups[index];
166 
167    info->name = g->name;
168    info->max_active_queries = g->num_counters;
169    info->num_queries = g->num_countables;
170 
171    return 1;
172 }
173 
174 static void
fd_set_active_query_state(struct pipe_context * pctx,bool enable)175 fd_set_active_query_state(struct pipe_context *pctx, bool enable) assert_dt
176 {
177    struct fd_context *ctx = fd_context(pctx);
178    ctx->active_queries = enable;
179    fd_context_dirty(ctx, FD_DIRTY_QUERY);
180 }
181 
182 static enum pipe_driver_query_type
query_type(enum fd_perfcntr_type type)183 query_type(enum fd_perfcntr_type type)
184 {
185 #define ENUM(t)                                                                \
186    case FD_PERFCNTR_##t:                                                       \
187       return PIPE_DRIVER_QUERY_##t
188    switch (type) {
189       ENUM(TYPE_UINT64);
190       ENUM(TYPE_UINT);
191       ENUM(TYPE_FLOAT);
192       ENUM(TYPE_PERCENTAGE);
193       ENUM(TYPE_BYTES);
194       ENUM(TYPE_MICROSECONDS);
195       ENUM(TYPE_HZ);
196       ENUM(TYPE_DBM);
197       ENUM(TYPE_TEMPERATURE);
198       ENUM(TYPE_VOLTS);
199       ENUM(TYPE_AMPS);
200       ENUM(TYPE_WATTS);
201    default:
202       unreachable("bad type");
203       return 0;
204    }
205 }
206 
207 static enum pipe_driver_query_result_type
query_result_type(enum fd_perfcntr_result_type type)208 query_result_type(enum fd_perfcntr_result_type type)
209 {
210    switch (type) {
211       ENUM(RESULT_TYPE_AVERAGE);
212       ENUM(RESULT_TYPE_CUMULATIVE);
213    default:
214       unreachable("bad type");
215       return 0;
216    }
217 }
218 
219 static void
setup_perfcntr_query_info(struct fd_screen * screen)220 setup_perfcntr_query_info(struct fd_screen *screen)
221 {
222    unsigned num_queries = 0;
223 
224    for (unsigned i = 0; i < screen->num_perfcntr_groups; i++)
225       num_queries += screen->perfcntr_groups[i].num_countables;
226 
227    screen->perfcntr_queries =
228       calloc(num_queries, sizeof(screen->perfcntr_queries[0]));
229    screen->num_perfcntr_queries = num_queries;
230 
231    unsigned idx = 0;
232    for (unsigned i = 0; i < screen->num_perfcntr_groups; i++) {
233       const struct fd_perfcntr_group *g = &screen->perfcntr_groups[i];
234       for (unsigned j = 0; j < g->num_countables; j++) {
235          struct pipe_driver_query_info *info = &screen->perfcntr_queries[idx];
236          const struct fd_perfcntr_countable *c = &g->countables[j];
237 
238          info->name = c->name;
239          info->query_type = FD_QUERY_FIRST_PERFCNTR + idx;
240          info->type = query_type(c->query_type);
241          info->result_type = query_result_type(c->result_type);
242          info->group_id = i;
243          info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;
244 
245          idx++;
246       }
247    }
248 }
249 
250 void
fd_query_screen_init(struct pipe_screen * pscreen)251 fd_query_screen_init(struct pipe_screen *pscreen)
252 {
253    pscreen->get_driver_query_info = fd_get_driver_query_info;
254    pscreen->get_driver_query_group_info = fd_get_driver_query_group_info;
255    setup_perfcntr_query_info(fd_screen(pscreen));
256 }
257 
258 void
fd_query_context_init(struct pipe_context * pctx)259 fd_query_context_init(struct pipe_context *pctx)
260 {
261    pctx->create_query = fd_create_query;
262    pctx->destroy_query = fd_destroy_query;
263    pctx->begin_query = fd_begin_query;
264    pctx->end_query = fd_end_query;
265    pctx->get_query_result = fd_get_query_result;
266    pctx->get_query_result_resource  = fd_get_query_result_resource;
267    pctx->set_active_query_state = fd_set_active_query_state;
268    pctx->render_condition = fd_render_condition;
269 }
270