• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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].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 = counter->name;
184    *desc = counter->desc;
185    *offset = counter->offset;
186    *data_size = intel_perf_query_counter_get_size(counter);
187    *type_enum = counter->type;
188    *data_type_enum = counter->data_type;
189 
190    if (counter->oa_counter_max_uint64) {
191       if (counter->data_type == INTEL_PERF_COUNTER_DATA_TYPE_FLOAT ||
192           counter->data_type == INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE)
193          *raw_max = counter->oa_counter_max_float(perf_cfg, info, &results);
194       else
195          *raw_max = counter->oa_counter_max_uint64(perf_cfg, info, &results);
196    } else {
197       *raw_max = 0;
198    }
199 }
200 
201 static void
iris_wait_perf_query(struct pipe_context * pipe,struct pipe_query * q)202 iris_wait_perf_query(struct pipe_context *pipe, struct pipe_query *q)
203 {
204    struct iris_context *ice = (void *) pipe;
205    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
206    struct intel_perf_query_object *obj = perf_query->query;
207    struct intel_perf_context *perf_ctx = ice->perf_ctx;
208 
209    if (perf_query->begin_succeeded)
210       intel_perf_wait_query(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER]);
211 }
212 
213 static bool
iris_is_perf_query_ready(struct pipe_context * pipe,struct pipe_query * q)214 iris_is_perf_query_ready(struct pipe_context *pipe, struct pipe_query *q)
215 {
216    struct iris_context *ice = (void *) pipe;
217    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
218    struct intel_perf_query_object *obj = perf_query->query;
219    struct intel_perf_context *perf_ctx = ice->perf_ctx;
220 
221    if (perf_query->base.Ready)
222       return true;
223    if (!perf_query->begin_succeeded)
224       return true;
225 
226    return intel_perf_is_query_ready(perf_ctx, obj,
227                                     &ice->batches[IRIS_BATCH_RENDER]);
228 }
229 
230 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)231 iris_get_perf_query_data(struct pipe_context *pipe,
232                          struct pipe_query *q,
233                          size_t data_size,
234                          uint32_t *data,
235                          uint32_t *bytes_written)
236 {
237    struct iris_context *ice = (void *) pipe;
238    struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
239    struct intel_perf_query_object *obj = perf_query->query;
240    struct intel_perf_context *perf_ctx = ice->perf_ctx;
241 
242    if (perf_query->begin_succeeded) {
243       intel_perf_get_query_data(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER],
244             data_size, data, bytes_written);
245    }
246 
247    return perf_query->begin_succeeded;
248 }
249 
250 void
iris_init_perfquery_functions(struct pipe_context * ctx)251 iris_init_perfquery_functions(struct pipe_context *ctx)
252 {
253    ctx->init_intel_perf_query_info = iris_init_perf_query_info;
254    ctx->get_intel_perf_query_info = iris_get_perf_query_info;
255    ctx->get_intel_perf_query_counter_info = iris_get_perf_counter_info;
256    ctx->new_intel_perf_query_obj = iris_new_perf_query_obj;
257    ctx->begin_intel_perf_query = iris_begin_perf_query;
258    ctx->end_intel_perf_query = iris_end_perf_query;
259    ctx->delete_intel_perf_query = iris_delete_perf_query;
260    ctx->wait_intel_perf_query = iris_wait_perf_query;
261    ctx->is_intel_perf_query_ready = iris_is_perf_query_ready;
262    ctx->get_intel_perf_query_data = iris_get_perf_query_data;
263 }
264