xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/iris/iris_performance_query.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <xf86drm.h>
24 
25 #include "iris_context.h"
26 #include "iris_perf.h"
27 
28 #include "main/mtypes.h"
29 
30 struct iris_perf_query {
31    struct gl_perf_query_object base;
32    struct intel_perf_query_object *query;
33    bool begin_succeeded;
34 };
35 
36 static unsigned
iris_init_perf_query_info(struct pipe_context * pipe)37 iris_init_perf_query_info(struct pipe_context *pipe)
38 {
39    struct iris_context *ice = (void *) pipe;
40    struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;
41    struct intel_perf_config *perf_cfg = NULL;
42 
43    /* make sure pipe perf counter type/data-type enums are matched with intel_perf's */
44    STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_EVENT == (enum pipe_perf_counter_type)INTEL_PERF_COUNTER_TYPE_EVENT);
45    STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_DURATION_NORM == (enum pipe_perf_counter_type)INTEL_PERF_COUNTER_TYPE_DURATION_NORM);
46    STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_DURATION_RAW == (enum pipe_perf_counter_type)INTEL_PERF_COUNTER_TYPE_DURATION_RAW);
47    STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_THROUGHPUT == (enum pipe_perf_counter_type)INTEL_PERF_COUNTER_TYPE_THROUGHPUT);
48    STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_RAW == (enum pipe_perf_counter_type)INTEL_PERF_COUNTER_TYPE_RAW);
49 
50    STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_BOOL32 == (enum pipe_perf_counter_data_type)INTEL_PERF_COUNTER_DATA_TYPE_BOOL32);
51    STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_UINT32 == (enum pipe_perf_counter_data_type)INTEL_PERF_COUNTER_DATA_TYPE_UINT32);
52    STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_UINT64 == (enum pipe_perf_counter_data_type)INTEL_PERF_COUNTER_DATA_TYPE_UINT64);
53    STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_FLOAT == (enum pipe_perf_counter_data_type)INTEL_PERF_COUNTER_DATA_TYPE_FLOAT);
54    STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_DOUBLE == (enum pipe_perf_counter_data_type)INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE);
55 
56    if (!ice->perf_ctx)
57       ice->perf_ctx = intel_perf_new_context(ice);
58 
59    if (unlikely(!ice->perf_ctx))
60       return 0;
61 
62    perf_cfg = intel_perf_config(ice->perf_ctx);
63 
64    if (perf_cfg)
65       return perf_cfg->n_queries;
66 
67    perf_cfg = intel_perf_new(ice->perf_ctx);
68 
69    iris_perf_init_vtbl(perf_cfg);
70 
71    intel_perf_init_metrics(perf_cfg, screen->devinfo, screen->fd,
72                            true /* pipeline_statistics */,
73                            true /* register snapshots */);
74 
75    intel_perf_init_context(ice->perf_ctx,
76                          perf_cfg,
77                          ice,
78                          ice,
79                          screen->bufmgr,
80                          screen->devinfo,
81                          ice->batches[IRIS_BATCH_RENDER].i915.ctx_id,
82                          screen->fd);
83 
84    return perf_cfg->n_queries;
85 }
86 
87 static struct pipe_query *
iris_new_perf_query_obj(struct pipe_context * pipe,unsigned query_index)88 iris_new_perf_query_obj(struct pipe_context *pipe, unsigned query_index)
89 {
90    struct iris_context *ice = (void *) pipe;
91    struct intel_perf_context *perf_ctx = ice->perf_ctx;
92    struct intel_perf_query_object * obj =
93       intel_perf_new_query(perf_ctx, query_index);
94    if (unlikely(!obj))
95       return NULL;
96 
97    struct iris_perf_query *q = calloc(1, sizeof(struct iris_perf_query));
98    if (unlikely(!q)) {
99       intel_perf_delete_query(perf_ctx, obj);
100       return NULL;
101    }
102 
103    q->query = obj;
104    return (struct pipe_query *)&q->base;
105 }
106 
107 static bool
iris_begin_perf_query(struct pipe_context * pipe,struct pipe_query * q)108 iris_begin_perf_query(struct pipe_context *pipe, struct pipe_query *q)
109 {
110    struct iris_context *ice = (void *) pipe;
111    struct iris_perf_query *perf_query= (struct iris_perf_query *) q;
112    struct intel_perf_query_object *obj = perf_query->query;
113    struct intel_perf_context *perf_ctx = ice->perf_ctx;
114 
115    return (perf_query->begin_succeeded = intel_perf_begin_query(perf_ctx, obj));
116 }
117 
118 static void
iris_end_perf_query(struct pipe_context * pipe,struct pipe_query * q)119 iris_end_perf_query(struct pipe_context *pipe, struct pipe_query *q)
120 {
121    struct iris_context *ice = (void *) pipe;
122    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
123    struct intel_perf_query_object *obj = perf_query->query;
124    struct intel_perf_context *perf_ctx = ice->perf_ctx;
125 
126    if (perf_query->begin_succeeded)
127       intel_perf_end_query(perf_ctx, obj);
128 }
129 
130 static void
iris_delete_perf_query(struct pipe_context * pipe,struct pipe_query * q)131 iris_delete_perf_query(struct pipe_context *pipe, struct pipe_query *q)
132 {
133    struct iris_context *ice = (void *) pipe;
134    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
135    struct intel_perf_query_object *obj = perf_query->query;
136    struct intel_perf_context *perf_ctx = ice->perf_ctx;
137 
138    intel_perf_delete_query(perf_ctx, obj);
139    free(q);
140 }
141 
142 static void
iris_get_perf_query_info(struct pipe_context * pipe,unsigned query_index,const char ** name,uint32_t * data_size,uint32_t * n_counters,uint32_t * n_active)143 iris_get_perf_query_info(struct pipe_context *pipe,
144                          unsigned query_index,
145                          const char **name,
146                          uint32_t *data_size,
147                          uint32_t *n_counters,
148                          uint32_t *n_active)
149 {
150    struct iris_context *ice = (void *) pipe;
151    struct intel_perf_context *perf_ctx = ice->perf_ctx;
152    struct intel_perf_config *perf_cfg = intel_perf_config(perf_ctx);
153    const struct intel_perf_query_info *info = &perf_cfg->queries[query_index];
154 
155    *name = info->name;
156    *data_size = info->data_size;
157    *n_counters = info->n_counters;
158    *n_active = intel_perf_active_queries(perf_ctx, info);
159 }
160 
161 static void
iris_get_perf_counter_info(struct pipe_context * pipe,unsigned query_index,unsigned counter_index,const char ** name,const char ** desc,uint32_t * offset,uint32_t * data_size,uint32_t * type_enum,uint32_t * data_type_enum,uint64_t * raw_max)162 iris_get_perf_counter_info(struct pipe_context *pipe,
163                            unsigned query_index,
164                            unsigned counter_index,
165                            const char **name,
166                            const char **desc,
167                            uint32_t *offset,
168                            uint32_t *data_size,
169                            uint32_t *type_enum,
170                            uint32_t *data_type_enum,
171                            uint64_t *raw_max)
172 {
173    struct iris_context *ice = (void *) pipe;
174    struct intel_perf_context *perf_ctx = ice->perf_ctx;
175    struct intel_perf_config *perf_cfg = intel_perf_config(perf_ctx);
176    const struct intel_perf_query_info *info = &perf_cfg->queries[query_index];
177    const struct intel_perf_query_counter *counter =
178       &info->counters[counter_index];
179    struct intel_perf_query_result results;
180 
181    intel_perf_query_result_clear(&results);
182 
183    *name = INTEL_DEBUG(DEBUG_PERF_SYMBOL_NAMES) ?
184       counter->symbol_name : counter->name;
185    *desc = counter->desc;
186    *offset = counter->offset;
187    *data_size = intel_perf_query_counter_get_size(counter);
188    *type_enum = counter->type;
189    *data_type_enum = counter->data_type;
190 
191    if (counter->oa_counter_max_uint64) {
192       if (counter->data_type == INTEL_PERF_COUNTER_DATA_TYPE_FLOAT ||
193           counter->data_type == INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE)
194          *raw_max = counter->oa_counter_max_float(perf_cfg, info, &results);
195       else
196          *raw_max = counter->oa_counter_max_uint64(perf_cfg, info, &results);
197    } else {
198       *raw_max = 0;
199    }
200 }
201 
202 static void
iris_wait_perf_query(struct pipe_context * pipe,struct pipe_query * q)203 iris_wait_perf_query(struct pipe_context *pipe, struct pipe_query *q)
204 {
205    struct iris_context *ice = (void *) pipe;
206    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
207    struct intel_perf_query_object *obj = perf_query->query;
208    struct intel_perf_context *perf_ctx = ice->perf_ctx;
209 
210    if (perf_query->begin_succeeded)
211       intel_perf_wait_query(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER]);
212 }
213 
214 static bool
iris_is_perf_query_ready(struct pipe_context * pipe,struct pipe_query * q)215 iris_is_perf_query_ready(struct pipe_context *pipe, struct pipe_query *q)
216 {
217    struct iris_context *ice = (void *) pipe;
218    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
219    struct intel_perf_query_object *obj = perf_query->query;
220    struct intel_perf_context *perf_ctx = ice->perf_ctx;
221 
222    if (perf_query->base.Ready)
223       return true;
224    if (!perf_query->begin_succeeded)
225       return true;
226 
227    return intel_perf_is_query_ready(perf_ctx, obj,
228                                     &ice->batches[IRIS_BATCH_RENDER]);
229 }
230 
231 static bool
iris_get_perf_query_data(struct pipe_context * pipe,struct pipe_query * q,size_t data_size,uint32_t * data,uint32_t * bytes_written)232 iris_get_perf_query_data(struct pipe_context *pipe,
233                          struct pipe_query *q,
234                          size_t data_size,
235                          uint32_t *data,
236                          uint32_t *bytes_written)
237 {
238    struct iris_context *ice = (void *) pipe;
239    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
240    struct intel_perf_query_object *obj = perf_query->query;
241    struct intel_perf_context *perf_ctx = ice->perf_ctx;
242 
243    if (perf_query->begin_succeeded) {
244       intel_perf_get_query_data(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER],
245             data_size, data, bytes_written);
246    }
247 
248    return perf_query->begin_succeeded;
249 }
250 
251 void
iris_init_perfquery_functions(struct pipe_context * ctx)252 iris_init_perfquery_functions(struct pipe_context *ctx)
253 {
254    ctx->init_intel_perf_query_info = iris_init_perf_query_info;
255    ctx->get_intel_perf_query_info = iris_get_perf_query_info;
256    ctx->get_intel_perf_query_counter_info = iris_get_perf_counter_info;
257    ctx->new_intel_perf_query_obj = iris_new_perf_query_obj;
258    ctx->begin_intel_perf_query = iris_begin_perf_query;
259    ctx->end_intel_perf_query = iris_end_perf_query;
260    ctx->delete_intel_perf_query = iris_delete_perf_query;
261    ctx->wait_intel_perf_query = iris_wait_perf_query;
262    ctx->is_intel_perf_query_ready = iris_is_perf_query_ready;
263    ctx->get_intel_perf_query_data = iris_get_perf_query_data;
264 }
265