xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/crocus/crocus_monitor.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 "crocus_monitor.h"
24 
25 #include <xf86drm.h>
26 
27 #include "crocus_screen.h"
28 #include "crocus_context.h"
29 #include "crocus_perf.h"
30 
31 #include "perf/intel_perf.h"
32 #include "perf/intel_perf_query.h"
33 #include "perf/intel_perf_regs.h"
34 
35 struct crocus_monitor_object {
36    int num_active_counters;
37    int *active_counters;
38 
39    size_t result_size;
40    unsigned char *result_buffer;
41 
42    struct intel_perf_query_object *query;
43 };
44 
45 int
crocus_get_monitor_info(struct pipe_screen * pscreen,unsigned index,struct pipe_driver_query_info * info)46 crocus_get_monitor_info(struct pipe_screen *pscreen, unsigned index,
47                         struct pipe_driver_query_info *info)
48 {
49    const struct crocus_screen *screen = (struct crocus_screen *)pscreen;
50    struct intel_perf_config *perf_cfg = screen->perf_cfg;
51    assert(perf_cfg);
52    if (!perf_cfg)
53       return 0;
54 
55    if (!info) {
56       /* return the number of metrics */
57       return perf_cfg->n_counters;
58    }
59 
60    struct intel_perf_query_counter_info *counter_info = &perf_cfg->counter_infos[index];
61    struct intel_perf_query_info *query_info =
62       &perf_cfg->queries[intel_perf_query_counter_info_first_query(counter_info)];
63    struct intel_perf_query_counter *counter = counter_info->counter;
64    struct intel_perf_query_result results;
65 
66    intel_perf_query_result_clear(&results);
67 
68    info->group_id = counter_info->location.group_idx;
69    info->name = counter->name;
70    info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;
71 
72    if (counter->type == INTEL_PERF_COUNTER_TYPE_THROUGHPUT)
73       info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE;
74    else
75       info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;
76    switch (counter->data_type) {
77    case INTEL_PERF_COUNTER_DATA_TYPE_BOOL32:
78    case INTEL_PERF_COUNTER_DATA_TYPE_UINT32:
79       info->type = PIPE_DRIVER_QUERY_TYPE_UINT;
80       uint64_t val =
81          counter->oa_counter_max_uint64 ?
82          counter->oa_counter_max_uint64(perf_cfg, query_info, &results) : 0;
83       info->max_value.u32 = (uint32_t)val;
84       break;
85    case INTEL_PERF_COUNTER_DATA_TYPE_UINT64:
86       info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;
87       info->max_value.u64 =
88          counter->oa_counter_max_uint64 ?
89          counter->oa_counter_max_uint64(perf_cfg, query_info, &results) : 0;
90       break;
91    case INTEL_PERF_COUNTER_DATA_TYPE_FLOAT:
92    case INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE:
93       info->type = PIPE_DRIVER_QUERY_TYPE_FLOAT;
94       info->max_value.f =
95          counter->oa_counter_max_float ?
96          counter->oa_counter_max_float(perf_cfg, query_info, &results) : 0.0f;
97       break;
98    default:
99       assert(false);
100       break;
101    }
102 
103    /* indicates that this is an OA query, not a pipeline statistics query */
104    info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;
105    return 1;
106 }
107 
108 static bool
crocus_monitor_init_metrics(struct crocus_screen * screen)109 crocus_monitor_init_metrics(struct crocus_screen *screen)
110 {
111    struct intel_perf_config *perf_cfg = intel_perf_new(screen);
112    if (unlikely(!perf_cfg))
113       return false;
114 
115    screen->perf_cfg = perf_cfg;
116 
117    crocus_perf_init_vtbl(perf_cfg);
118 
119    intel_perf_init_metrics(perf_cfg, &screen->devinfo, screen->fd, true, true);
120 
121    return perf_cfg->n_counters > 0;
122 }
123 
124 int
crocus_get_monitor_group_info(struct pipe_screen * pscreen,unsigned group_index,struct pipe_driver_query_group_info * info)125 crocus_get_monitor_group_info(struct pipe_screen *pscreen,
126                               unsigned group_index,
127                               struct pipe_driver_query_group_info *info)
128 {
129    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
130    if (!screen->perf_cfg) {
131       if (!crocus_monitor_init_metrics(screen))
132          return 0;
133    }
134 
135    const struct intel_perf_config *perf_cfg = screen->perf_cfg;
136 
137    if (!info) {
138       /* return the count that can be queried */
139       return perf_cfg->n_queries;
140    }
141 
142    if (group_index >= perf_cfg->n_queries) {
143       /* out of range */
144       return 0;
145    }
146 
147    struct intel_perf_query_info *query = &perf_cfg->queries[group_index];
148 
149    info->name = query->name;
150    info->max_active_queries = query->n_counters;
151    info->num_queries = query->n_counters;
152 
153    return 1;
154 }
155 
156 static void
crocus_init_monitor_ctx(struct crocus_context * ice)157 crocus_init_monitor_ctx(struct crocus_context *ice)
158 {
159    struct crocus_screen *screen = (struct crocus_screen *) ice->ctx.screen;
160 
161    ice->perf_ctx = intel_perf_new_context(ice);
162    if (unlikely(!ice->perf_ctx))
163       return;
164 
165    struct intel_perf_context *perf_ctx = ice->perf_ctx;
166    struct intel_perf_config *perf_cfg = screen->perf_cfg;
167    intel_perf_init_context(perf_ctx,
168                            perf_cfg,
169                            ice,
170                            ice,
171                            screen->bufmgr,
172                            &screen->devinfo,
173                            ice->batches[CROCUS_BATCH_RENDER].hw_ctx_id,
174                            screen->fd);
175 }
176 
177 /* entry point for GenPerfMonitorsAMD */
178 struct crocus_monitor_object *
crocus_create_monitor_object(struct crocus_context * ice,unsigned num_queries,unsigned * query_types)179 crocus_create_monitor_object(struct crocus_context *ice,
180                              unsigned num_queries,
181                              unsigned *query_types)
182 {
183    struct crocus_screen *screen = (struct crocus_screen *) ice->ctx.screen;
184    struct intel_perf_config *perf_cfg = screen->perf_cfg;
185    struct intel_perf_query_object *query_obj = NULL;
186 
187    /* initialize perf context if this has not already been done.  This
188     * function is the first entry point that carries the gl context.
189     */
190    if (ice->perf_ctx == NULL) {
191       crocus_init_monitor_ctx(ice);
192    }
193    struct intel_perf_context *perf_ctx = ice->perf_ctx;
194 
195    assert(num_queries > 0);
196    int query_index = query_types[0] - PIPE_QUERY_DRIVER_SPECIFIC;
197    assert(query_index <= perf_cfg->n_counters);
198    const int group = perf_cfg->counter_infos[query_index].location.group_idx;
199 
200    struct crocus_monitor_object *monitor =
201       calloc(1, sizeof(struct crocus_monitor_object));
202    if (unlikely(!monitor))
203       goto allocation_failure;
204 
205    monitor->num_active_counters = num_queries;
206    monitor->active_counters = calloc(num_queries, sizeof(int));
207    if (unlikely(!monitor->active_counters))
208       goto allocation_failure;
209 
210    for (int i = 0; i < num_queries; ++i) {
211       unsigned current_query = query_types[i];
212       unsigned current_query_index = current_query - PIPE_QUERY_DRIVER_SPECIFIC;
213 
214       /* all queries must be in the same group */
215       assert(current_query_index <= perf_cfg->n_counters);
216       assert(perf_cfg->counter_infos[current_query_index].location.group_idx == group);
217       monitor->active_counters[i] =
218          perf_cfg->counter_infos[current_query_index].location.counter_idx;
219    }
220 
221    /* create the intel_perf_query */
222    query_obj = intel_perf_new_query(perf_ctx, group);
223    if (unlikely(!query_obj))
224       goto allocation_failure;
225 
226    monitor->query = query_obj;
227    monitor->result_size = perf_cfg->queries[group].data_size;
228    monitor->result_buffer = calloc(1, monitor->result_size);
229    if (unlikely(!monitor->result_buffer))
230       goto allocation_failure;
231 
232    return monitor;
233 
234 allocation_failure:
235    if (monitor) {
236       free(monitor->active_counters);
237       free(monitor->result_buffer);
238    }
239    free(query_obj);
240    free(monitor);
241    return NULL;
242 }
243 
244 void
crocus_destroy_monitor_object(struct pipe_context * ctx,struct crocus_monitor_object * monitor)245 crocus_destroy_monitor_object(struct pipe_context *ctx,
246                               struct crocus_monitor_object *monitor)
247 {
248    struct crocus_context *ice = (struct crocus_context *)ctx;
249 
250    intel_perf_delete_query(ice->perf_ctx, monitor->query);
251    free(monitor->result_buffer);
252    monitor->result_buffer = NULL;
253    free(monitor->active_counters);
254    monitor->active_counters = NULL;
255    free(monitor);
256 }
257 
258 bool
crocus_begin_monitor(struct pipe_context * ctx,struct crocus_monitor_object * monitor)259 crocus_begin_monitor(struct pipe_context *ctx,
260                      struct crocus_monitor_object *monitor)
261 {
262    struct crocus_context *ice = (void *) ctx;
263    struct intel_perf_context *perf_ctx = ice->perf_ctx;
264 
265    return intel_perf_begin_query(perf_ctx, monitor->query);
266 }
267 
268 bool
crocus_end_monitor(struct pipe_context * ctx,struct crocus_monitor_object * monitor)269 crocus_end_monitor(struct pipe_context *ctx,
270                    struct crocus_monitor_object *monitor)
271 {
272    struct crocus_context *ice = (void *) ctx;
273    struct intel_perf_context *perf_ctx = ice->perf_ctx;
274 
275    intel_perf_end_query(perf_ctx, monitor->query);
276    return true;
277 }
278 
279 bool
crocus_get_monitor_result(struct pipe_context * ctx,struct crocus_monitor_object * monitor,bool wait,union pipe_numeric_type_union * result)280 crocus_get_monitor_result(struct pipe_context *ctx,
281                           struct crocus_monitor_object *monitor,
282                           bool wait,
283                           union pipe_numeric_type_union *result)
284 {
285    struct crocus_context *ice = (void *) ctx;
286    struct intel_perf_context *perf_ctx = ice->perf_ctx;
287    struct crocus_batch *batch = &ice->batches[CROCUS_BATCH_RENDER];
288 
289    bool monitor_ready =
290       intel_perf_is_query_ready(perf_ctx, monitor->query, batch);
291 
292    if (!monitor_ready) {
293       if (!wait)
294          return false;
295       intel_perf_wait_query(perf_ctx, monitor->query, batch);
296    }
297 
298    assert(intel_perf_is_query_ready(perf_ctx, monitor->query, batch));
299 
300    unsigned bytes_written;
301    intel_perf_get_query_data(perf_ctx, monitor->query, batch,
302                              monitor->result_size,
303                              (unsigned*) monitor->result_buffer,
304                              &bytes_written);
305    if (bytes_written != monitor->result_size)
306       return false;
307 
308    /* copy metrics into the batch result */
309    for (int i = 0; i < monitor->num_active_counters; ++i) {
310       int current_counter = monitor->active_counters[i];
311       const struct intel_perf_query_info *info =
312          intel_perf_query_info(monitor->query);
313       const struct intel_perf_query_counter *counter =
314          &info->counters[current_counter];
315       assert(intel_perf_query_counter_get_size(counter));
316       switch (counter->data_type) {
317       case INTEL_PERF_COUNTER_DATA_TYPE_UINT64:
318          result[i].u64 = *(uint64_t*)(monitor->result_buffer + counter->offset);
319          break;
320       case INTEL_PERF_COUNTER_DATA_TYPE_FLOAT:
321          result[i].f = *(float*)(monitor->result_buffer + counter->offset);
322          break;
323       case INTEL_PERF_COUNTER_DATA_TYPE_UINT32:
324       case INTEL_PERF_COUNTER_DATA_TYPE_BOOL32:
325          result[i].u64 = *(uint32_t*)(monitor->result_buffer + counter->offset);
326          break;
327       case INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE: {
328          double v = *(double*)(monitor->result_buffer + counter->offset);
329          result[i].f = v;
330          break;
331       }
332       default:
333          unreachable("unexpected counter data type");
334       }
335    }
336    return true;
337 }
338