iris: remove iris_monitor_config
[mesa.git] / src / gallium / drivers / iris / iris_monitor.c
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 "iris_monitor.h"
24
25 #include <xf86drm.h>
26
27 #include "iris_screen.h"
28 #include "iris_context.h"
29 #include "iris_perf.h"
30
31 struct iris_monitor_object {
32 int num_active_counters;
33 int *active_counters;
34
35 size_t result_size;
36 unsigned char *result_buffer;
37
38 struct gen_perf_query_object *query;
39 };
40
41 int
42 iris_get_monitor_info(struct pipe_screen *pscreen, unsigned index,
43 struct pipe_driver_query_info *info)
44 {
45 const struct iris_screen *screen = (struct iris_screen *)pscreen;
46 const struct gen_perf_config *perf_cfg = screen->perf_cfg;
47 assert(perf_cfg);
48 if (!perf_cfg)
49 return 0;
50
51 if (!info) {
52 /* return the number of metrics */
53 return perf_cfg->n_counters;
54 }
55
56 struct gen_perf_query_counter *counter = perf_cfg->counters[index];
57
58 info->group_id = counter->location.group_idx;
59 info->name = counter->name;
60 info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;
61
62 if (counter->type == GEN_PERF_COUNTER_TYPE_THROUGHPUT)
63 info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE;
64 else
65 info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;
66 switch (counter->data_type) {
67 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32:
68 case GEN_PERF_COUNTER_DATA_TYPE_UINT32:
69 info->type = PIPE_DRIVER_QUERY_TYPE_UINT;
70 assert(counter->raw_max <= UINT32_MAX);
71 info->max_value.u32 = (uint32_t)counter->raw_max;
72 break;
73 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
74 info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;
75 info->max_value.u64 = counter->raw_max;
76 break;
77 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
78 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE:
79 info->type = PIPE_DRIVER_QUERY_TYPE_FLOAT;
80 info->max_value.f = counter->raw_max;
81 break;
82 default:
83 assert(false);
84 break;
85 }
86
87 /* indicates that this is an OA query, not a pipeline statistics query */
88 info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;
89 return 1;
90 }
91
92 static bool
93 iris_monitor_init_metrics(struct iris_screen *screen)
94 {
95 struct gen_perf_config *perf_cfg = gen_perf_new(screen);
96 if (unlikely(!perf_cfg))
97 return false;
98
99 screen->perf_cfg = perf_cfg;
100
101 iris_perf_init_vtbl(perf_cfg);
102
103 gen_perf_init_metrics(perf_cfg, &screen->devinfo, screen->fd,
104 true /* pipeline stats*/);
105
106 return perf_cfg->n_counters > 0;
107 }
108
109 int
110 iris_get_monitor_group_info(struct pipe_screen *pscreen,
111 unsigned group_index,
112 struct pipe_driver_query_group_info *info)
113 {
114 struct iris_screen *screen = (struct iris_screen *)pscreen;
115 if (!screen->perf_cfg) {
116 if (!iris_monitor_init_metrics(screen))
117 return 0;
118 }
119
120 const struct gen_perf_config *perf_cfg = screen->perf_cfg;
121
122 if (!info) {
123 /* return the count that can be queried */
124 return perf_cfg->n_queries;
125 }
126
127 if (group_index >= perf_cfg->n_queries) {
128 /* out of range */
129 return 0;
130 }
131
132 struct gen_perf_query_info *query = &perf_cfg->queries[group_index];
133
134 info->name = query->name;
135 info->max_active_queries = query->n_counters;
136 info->num_queries = query->n_counters;
137
138 return 1;
139 }
140
141 static void
142 iris_init_monitor_ctx(struct iris_context *ice)
143 {
144 struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;
145
146 ice->perf_ctx = gen_perf_new_context(ice);
147 if (unlikely(!ice->perf_ctx))
148 return;
149
150 struct gen_perf_context *perf_ctx = ice->perf_ctx;
151 struct gen_perf_config *perf_cfg = screen->perf_cfg;
152 gen_perf_init_context(perf_ctx,
153 perf_cfg,
154 ice,
155 screen->bufmgr,
156 &screen->devinfo,
157 ice->batches[IRIS_BATCH_RENDER].hw_ctx_id,
158 screen->fd);
159 }
160
161 /* entry point for GenPerfMonitorsAMD */
162 struct iris_monitor_object *
163 iris_create_monitor_object(struct iris_context *ice,
164 unsigned num_queries,
165 unsigned *query_types)
166 {
167 struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;
168 struct gen_perf_config *perf_cfg = screen->perf_cfg;
169 struct gen_perf_query_object *query_obj = NULL;
170
171 /* initialize perf context if this has not already been done. This
172 * function is the first entry point that carries the gl context.
173 */
174 if (ice->perf_ctx == NULL) {
175 iris_init_monitor_ctx(ice);
176 }
177 struct gen_perf_context *perf_ctx = ice->perf_ctx;
178
179 assert(num_queries > 0);
180 int query_index = query_types[0] - PIPE_QUERY_DRIVER_SPECIFIC;
181 assert(query_index <= perf_cfg->n_counters);
182 const int group = perf_cfg->counters[query_index]->location.group_idx;
183
184 struct iris_monitor_object *monitor =
185 calloc(1, sizeof(struct iris_monitor_object));
186 if (unlikely(!monitor))
187 goto allocation_failure;
188
189 monitor->num_active_counters = num_queries;
190 monitor->active_counters = calloc(num_queries, sizeof(int));
191 if (unlikely(!monitor->active_counters))
192 goto allocation_failure;
193
194 for (int i = 0; i < num_queries; ++i) {
195 unsigned current_query = query_types[i];
196 unsigned current_query_index = current_query - PIPE_QUERY_DRIVER_SPECIFIC;
197
198 /* all queries must be in the same group */
199 assert(current_query_index <= perf_cfg->n_counters);
200 assert(perf_cfg->counters[current_query_index]->location.group_idx == group);
201 monitor->active_counters[i] =
202 perf_cfg->counters[current_query_index]->location.counter_idx;
203 }
204
205 /* create the gen_perf_query */
206 query_obj = gen_perf_new_query(perf_ctx, group);
207 if (unlikely(!query_obj))
208 goto allocation_failure;
209
210 monitor->query = query_obj;
211 monitor->result_size = perf_cfg->queries[group].data_size;
212 monitor->result_buffer = calloc(1, monitor->result_size);
213 if (unlikely(!monitor->result_buffer))
214 goto allocation_failure;
215
216 return monitor;
217
218 allocation_failure:
219 if (monitor) {
220 free(monitor->active_counters);
221 free(monitor->result_buffer);
222 }
223 free(query_obj);
224 free(monitor);
225 return NULL;
226 }
227
228 void
229 iris_destroy_monitor_object(struct pipe_context *ctx,
230 struct iris_monitor_object *monitor)
231 {
232 struct iris_context *ice = (struct iris_context *)ctx;
233
234 gen_perf_delete_query(ice->perf_ctx, monitor->query);
235 free(monitor->result_buffer);
236 monitor->result_buffer = NULL;
237 free(monitor->active_counters);
238 monitor->active_counters = NULL;
239 free(monitor);
240 }
241
242 bool
243 iris_begin_monitor(struct pipe_context *ctx,
244 struct iris_monitor_object *monitor)
245 {
246 struct iris_context *ice = (void *) ctx;
247 struct gen_perf_context *perf_ctx = ice->perf_ctx;
248
249 return gen_perf_begin_query(perf_ctx, monitor->query);
250 }
251
252 bool
253 iris_end_monitor(struct pipe_context *ctx,
254 struct iris_monitor_object *monitor)
255 {
256 struct iris_context *ice = (void *) ctx;
257 struct gen_perf_context *perf_ctx = ice->perf_ctx;
258
259 gen_perf_end_query(perf_ctx, monitor->query);
260 return true;
261 }
262
263 bool
264 iris_get_monitor_result(struct pipe_context *ctx,
265 struct iris_monitor_object *monitor,
266 bool wait,
267 union pipe_numeric_type_union *result)
268 {
269 struct iris_context *ice = (void *) ctx;
270 struct gen_perf_context *perf_ctx = ice->perf_ctx;
271 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
272
273 bool monitor_ready =
274 gen_perf_is_query_ready(perf_ctx, monitor->query, batch);
275
276 if (!monitor_ready) {
277 if (!wait)
278 return false;
279 gen_perf_wait_query(perf_ctx, monitor->query, batch);
280 }
281
282 assert(gen_perf_is_query_ready(perf_ctx, monitor->query, batch));
283
284 unsigned bytes_written;
285 gen_perf_get_query_data(perf_ctx, monitor->query,
286 monitor->result_size,
287 (unsigned*) monitor->result_buffer,
288 &bytes_written);
289 if (bytes_written != monitor->result_size)
290 return false;
291
292 /* copy metrics into the batch result */
293 for (int i = 0; i < monitor->num_active_counters; ++i) {
294 int current_counter = monitor->active_counters[i];
295 const struct gen_perf_query_info *info =
296 gen_perf_query_info(monitor->query);
297 const struct gen_perf_query_counter *counter =
298 &info->counters[current_counter];
299 assert(gen_perf_query_counter_get_size(counter));
300 switch (counter->data_type) {
301 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
302 result[i].u64 = *(uint64_t*)(monitor->result_buffer + counter->offset);
303 break;
304 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
305 result[i].f = *(float*)(monitor->result_buffer + counter->offset);
306 break;
307 case GEN_PERF_COUNTER_DATA_TYPE_UINT32:
308 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32:
309 result[i].u64 = *(uint32_t*)(monitor->result_buffer + counter->offset);
310 break;
311 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE: {
312 double v = *(double*)(monitor->result_buffer + counter->offset);
313 result[i].f = v;
314 break;
315 }
316 default:
317 unreachable("unexpected counter data type");
318 }
319 }
320 return true;
321 }