• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2021 Raspberry Pi Ltd
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * Gallium query object support for performance counters
26  *
27  * This contains the performance V3D counters queries.
28  */
29 
30 #include "v3d_query.h"
31 
32 #include "common/v3d_performance_counters.h"
33 
34 struct v3d_query_perfcnt
35 {
36         struct v3d_query base;
37 
38         unsigned num_queries;
39         struct v3d_perfmon_state *perfmon;
40 };
41 
42 static void
kperfmon_destroy(struct v3d_context * v3d,struct v3d_perfmon_state * perfmon)43 kperfmon_destroy(struct v3d_context *v3d, struct v3d_perfmon_state *perfmon)
44 {
45         struct drm_v3d_perfmon_destroy destroyreq;
46 
47         destroyreq.id = perfmon->kperfmon_id;
48         int ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_DESTROY, &destroyreq);
49         if (ret != 0)
50                 fprintf(stderr, "failed to destroy perfmon %d: %s\n",
51                         perfmon->kperfmon_id, strerror(errno));
52 }
53 
54 int
v3dX(get_driver_query_group_info_perfcnt)55 v3dX(get_driver_query_group_info_perfcnt)(struct v3d_screen *screen, unsigned index,
56                                           struct pipe_driver_query_group_info *info)
57 {
58         if (!screen->has_perfmon)
59                 return 0;
60 
61         if (!info)
62                 return 1;
63 
64         if (index > 0)
65                 return 0;
66 
67         info->name = "V3D counters";
68         info->max_active_queries = DRM_V3D_MAX_PERF_COUNTERS;
69         info->num_queries = ARRAY_SIZE(v3d_performance_counters);
70 
71         return 1;
72 }
73 
74 int
v3dX(get_driver_query_info_perfcnt)75 v3dX(get_driver_query_info_perfcnt)(struct v3d_screen *screen, unsigned index,
76                                     struct pipe_driver_query_info *info)
77 {
78         if (!screen->has_perfmon)
79                 return 0;
80 
81         if (!info)
82                 return ARRAY_SIZE(v3d_performance_counters);
83 
84         if (index >= ARRAY_SIZE(v3d_performance_counters))
85                 return 0;
86 
87         info->group_id = 0;
88         info->name = v3d_performance_counters[index][V3D_PERFCNT_NAME];
89         info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;
90         info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;
91         info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;
92         info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;
93 
94         return 1;
95 }
96 
97 static void
v3d_destroy_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)98 v3d_destroy_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
99 {
100         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
101 
102         assert(pquery->perfmon);
103 
104         if (v3d->active_perfmon == pquery->perfmon) {
105                 fprintf(stderr, "Query is active; end query before destroying\n");
106                 return;
107         }
108         if (pquery->perfmon->kperfmon_id)
109                 kperfmon_destroy(v3d, pquery->perfmon);
110 
111         v3d_fence_unreference(&pquery->perfmon->last_job_fence);
112         free(pquery->perfmon);
113         free(query);
114 }
115 
116 static bool
v3d_begin_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)117 v3d_begin_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
118 {
119         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
120         struct drm_v3d_perfmon_create createreq = { 0 };
121         int i, ret;
122 
123         /* Only one perfmon can be activated per context */
124         if (v3d->active_perfmon) {
125                 fprintf(stderr,
126                         "Another query is already active; "
127                         "finish it before starting a new one\n");
128                 return false;
129         }
130 
131         assert(pquery->perfmon);
132 
133         /* Reset the counters by destroying the previously allocated perfmon */
134         if (pquery->perfmon->kperfmon_id)
135                 kperfmon_destroy(v3d, pquery->perfmon);
136 
137         for (i = 0; i < pquery->num_queries; i++)
138                 createreq.counters[i] = pquery->perfmon->counters[i];
139 
140         createreq.ncounters = pquery->num_queries;
141         ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_CREATE, &createreq);
142         if (ret != 0)
143                 return false;
144 
145         pquery->perfmon->kperfmon_id = createreq.id;
146         pquery->perfmon->job_submitted = false;
147         v3d_fence_unreference(&pquery->perfmon->last_job_fence);
148 
149         /* Ensure all pending jobs are flushed before activating the
150          * perfmon
151          */
152         v3d_flush((struct pipe_context *)v3d);
153         v3d->active_perfmon = pquery->perfmon;
154 
155         return true;
156 }
157 
158 static bool
v3d_end_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)159 v3d_end_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
160 {
161         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
162 
163         assert(pquery->perfmon);
164 
165         if (v3d->active_perfmon != pquery->perfmon) {
166                 fprintf(stderr, "This query is not active\n");
167                 return false;
168         }
169 
170         /* Ensure all pending jobs are flushed before deactivating the
171          * perfmon
172          */
173         v3d_flush((struct pipe_context *)v3d);
174 
175         /* Get a copy of latest submitted job's fence to wait for its
176          * completion
177          */
178         if (v3d->active_perfmon->job_submitted) {
179                 int fd = -1;
180                 drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &fd);
181                 if (fd == -1) {
182                         fprintf(stderr, "export failed\n");
183                         v3d->active_perfmon->last_job_fence = NULL;
184                 } else {
185                         v3d->active_perfmon->last_job_fence =
186                                 v3d_fence_create(v3d, fd);
187                 }
188         }
189 
190         v3d->active_perfmon = NULL;
191 
192         return true;
193 }
194 
195 static bool
v3d_get_query_result_perfcnt(struct v3d_context * v3d,struct v3d_query * query,bool wait,union pipe_query_result * vresult)196 v3d_get_query_result_perfcnt(struct v3d_context *v3d, struct v3d_query *query,
197                              bool wait, union pipe_query_result *vresult)
198 {
199         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
200         struct drm_v3d_perfmon_get_values req = { 0 };
201         int i, ret;
202 
203         assert(pquery->perfmon);
204 
205         if (pquery->perfmon->job_submitted) {
206                 if (!v3d_fence_wait(v3d->screen,
207                                     pquery->perfmon->last_job_fence,
208                                     wait ? OS_TIMEOUT_INFINITE : 0))
209                         return false;
210 
211                 req.id = pquery->perfmon->kperfmon_id;
212                 req.values_ptr = (uintptr_t)pquery->perfmon->values;
213                 ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_GET_VALUES, &req);
214                 if (ret != 0) {
215                         fprintf(stderr, "Can't request perfmon counters values\n");
216                         return false;
217                 }
218         }
219 
220         for (i = 0; i < pquery->num_queries; i++)
221                 vresult->batch[i].u64 = pquery->perfmon->values[i];
222 
223         return true;
224 }
225 
226 static const struct v3d_query_funcs perfcnt_query_funcs = {
227         .destroy_query = v3d_destroy_query_perfcnt,
228         .begin_query = v3d_begin_query_perfcnt,
229         .end_query = v3d_end_query_perfcnt,
230         .get_query_result = v3d_get_query_result_perfcnt,
231 };
232 
233 struct pipe_query *
v3dX(create_batch_query_perfcnt)234 v3dX(create_batch_query_perfcnt)(struct v3d_context *v3d, unsigned num_queries,
235                                  unsigned *query_types)
236 {
237         struct v3d_query_perfcnt *pquery = NULL;
238         struct v3d_query *query;
239         struct v3d_perfmon_state *perfmon = NULL;
240         int i;
241 
242         /* Validate queries */
243         for (i = 0; i < num_queries; i++) {
244                 if (query_types[i] < PIPE_QUERY_DRIVER_SPECIFIC ||
245                     query_types[i] >= PIPE_QUERY_DRIVER_SPECIFIC +
246                     ARRAY_SIZE(v3d_performance_counters)) {
247                         fprintf(stderr, "Invalid query type\n");
248                         return NULL;
249                 }
250         }
251 
252         pquery = calloc(1, sizeof(*pquery));
253         if (!pquery)
254                 return NULL;
255 
256         perfmon = calloc(1, sizeof(*perfmon));
257         if (!perfmon) {
258                 free(pquery);
259                 return NULL;
260         }
261 
262         for (i = 0; i < num_queries; i++)
263                 perfmon->counters[i] = query_types[i] - PIPE_QUERY_DRIVER_SPECIFIC;
264 
265         pquery->perfmon = perfmon;
266         pquery->num_queries = num_queries;
267 
268         query = &pquery->base;
269         query->funcs = &perfcnt_query_funcs;
270 
271         /* Note that struct pipe_query isn't actually defined anywhere. */
272          return (struct pipe_query *)query;
273 }
274