618812841e4c4d016cdf300f48ef7f48e175c893
[mesa.git] / src / freedreno / computerator / main.c
1 /*
2 * Copyright © 2020 Google, Inc.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <getopt.h>
25 #include <inttypes.h>
26 #include <locale.h>
27 #include <xf86drm.h>
28
29 #include "util/u_math.h"
30
31 #include "perfcntrs/freedreno_perfcntr.h"
32
33 #include "main.h"
34
35
36 static void
37 dump_float(void *buf, int sz)
38 {
39 uint8_t *ptr = (uint8_t *)buf;
40 uint8_t *end = ptr + sz - 3;
41 int i = 0;
42
43 while (ptr < end) {
44 uint32_t d = 0;
45
46 printf((i % 8) ? " " : "\t");
47
48 d |= *(ptr++) << 0;
49 d |= *(ptr++) << 8;
50 d |= *(ptr++) << 16;
51 d |= *(ptr++) << 24;
52
53 printf("%8f", uif(d));
54
55 if ((i % 8) == 7) {
56 printf("\n");
57 }
58
59 i++;
60 }
61
62 if (i % 8) {
63 printf("\n");
64 }
65 }
66
67 static void
68 dump_hex(void *buf, int sz)
69 {
70 uint8_t *ptr = (uint8_t *)buf;
71 uint8_t *end = ptr + sz;
72 int i = 0;
73
74 while (ptr < end) {
75 uint32_t d = 0;
76
77 printf((i % 8) ? " " : "\t");
78
79 d |= *(ptr++) << 0;
80 d |= *(ptr++) << 8;
81 d |= *(ptr++) << 16;
82 d |= *(ptr++) << 24;
83
84 printf("%08x", d);
85
86 if ((i % 8) == 7) {
87 printf("\n");
88 }
89
90 i++;
91 }
92
93 if (i % 8) {
94 printf("\n");
95 }
96 }
97
98 static const char *shortopts = "df:g:hp:";
99
100 static const struct option longopts[] = {
101 {"disasm", no_argument, 0, 'd'},
102 {"file", required_argument, 0, 'f'},
103 {"groups", required_argument, 0, 'g'},
104 {"help", no_argument, 0, 'h'},
105 {"perfcntr", required_argument, 0, 'p'},
106 {0, 0, 0, 0}
107 };
108
109 static void
110 usage(const char *name)
111 {
112 printf("Usage: %s [-dfgh]\n"
113 "\n"
114 "options:\n"
115 " -d, --disasm print disassembled shader\n"
116 " -f, --file=FILE read shader from file (instead of stdin)\n"
117 " -g, --groups=X,Y,Z use specified group size\n"
118 " -h, --help show this message\n"
119 " -p, --perfcntr=LIST sample specified performance counters (comma\n"
120 " separated list)\n"
121 ,
122 name);
123 }
124
125 /* performance counter description: */
126 static unsigned num_groups;
127 static const struct fd_perfcntr_group *groups;
128
129 /* Track enabled counters per group: */
130 static unsigned *enabled_counters;
131
132 static void
133 setup_counter(const char *name, struct perfcntr *c)
134 {
135 for (int i = 0; i < num_groups; i++) {
136 const struct fd_perfcntr_group *group = &groups[i];
137
138 for (int j = 0; j < group->num_countables; j++) {
139 const struct fd_perfcntr_countable *countable = &group->countables[j];
140
141 if (strcmp(name, countable->name) != 0)
142 continue;
143
144 /*
145 * Allocate a counter to use to monitor the requested countable:
146 */
147 if (enabled_counters[i] >= group->num_counters) {
148 errx(-1, "Too many counters selected in group: %s", group->name);
149 }
150
151 unsigned idx = enabled_counters[i]++;
152 const struct fd_perfcntr_counter *counter = &group->counters[idx];
153
154 /*
155 * And initialize the perfcntr struct, pulling together the info
156 * about selected counter and countable, to simplify life for the
157 * backend:
158 */
159 c->name = name;
160 c->select_reg = counter->select_reg;
161 c->counter_reg_lo = counter->counter_reg_lo;
162 c->counter_reg_hi = counter->counter_reg_hi;
163 c->selector = countable->selector;
164
165 return;
166 }
167 }
168
169 errx(-1, "could not find countable: %s", name);
170 }
171
172 static struct perfcntr *
173 parse_perfcntrs(uint32_t gpu_id, const char *perfcntrstr, unsigned *num_perfcntrs)
174 {
175 struct perfcntr *counters = NULL;
176 char *cnames, *s;
177 unsigned cnt = 0;
178
179 groups = fd_perfcntrs(gpu_id, &num_groups);
180 enabled_counters = calloc(num_groups, sizeof(enabled_counters[0]));
181
182 cnames = strdup(perfcntrstr);
183 while ((s = strstr(cnames, ","))) {
184 char *name = cnames;
185 s[0] = '\0';
186 cnames = &s[1];
187
188 counters = realloc(counters, ++cnt * sizeof(counters[0]));
189 setup_counter(name, &counters[cnt-1]);
190 }
191
192 char * name = cnames;
193 counters = realloc(counters, ++cnt * sizeof(counters[0]));
194 setup_counter(name, &counters[cnt-1]);
195
196 *num_perfcntrs = cnt;
197
198 return counters;
199 }
200
201 int
202 main(int argc, char **argv)
203 {
204 FILE *in = stdin;
205 const char *perfcntrstr = NULL;
206 struct perfcntr *perfcntrs = NULL;
207 unsigned num_perfcntrs = 0;
208 bool disasm = false;
209 uint32_t grid[3] = {0};
210 int opt, ret;
211
212 setlocale(LC_NUMERIC, "en_US.UTF-8");
213
214 while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) != -1) {
215 switch (opt) {
216 case 'd':
217 disasm = true;
218 break;
219 case 'f':
220 in = fopen(optarg, "r");
221 if (!in)
222 err(1, "could not open '%s'", optarg);
223 break;
224 case 'g':
225 ret = sscanf(optarg, "%u,%u,%u", &grid[0], &grid[1], &grid[2]);
226 if (ret != 3)
227 goto usage;
228 break;
229 case 'h':
230 goto usage;
231 case 'p':
232 perfcntrstr = optarg;
233 break;
234 default:
235 printf("unrecognized arg: %c\n", opt);
236 goto usage;
237 }
238 }
239
240 int fd = drmOpen("msm", NULL);
241 if (fd < 0)
242 err(1, "could not open drm device");
243
244 struct fd_device *dev = fd_device_new(fd);
245 struct fd_pipe *pipe = fd_pipe_new(dev, FD_PIPE_3D);
246
247 uint64_t val;
248 fd_pipe_get_param(pipe, FD_GPU_ID, &val);
249 uint32_t gpu_id = val;
250
251 printf("got gpu_id: %u\n", gpu_id);
252
253 struct backend *backend;
254 switch (gpu_id) {
255 case 600 ... 699:
256 backend = a6xx_init(dev, gpu_id);
257 break;
258 default:
259 err(1, "unsupported gpu: a%u", gpu_id);
260 }
261
262 struct kernel *kernel = backend->assemble(backend, in);
263 printf("localsize: %dx%dx%d\n", kernel->local_size[0],
264 kernel->local_size[1], kernel->local_size[2]);
265 for (int i = 0; i < kernel->num_bufs; i++) {
266 printf("buf[%d]: size=%u\n", i, kernel->buf_sizes[i]);
267 kernel->bufs[i] = fd_bo_new(dev, kernel->buf_sizes[i] * 4,
268 DRM_FREEDRENO_GEM_TYPE_KMEM, "buf[%d]", i);
269 }
270
271 if (disasm)
272 backend->disassemble(kernel, stdout);
273
274 if (grid[0] == 0)
275 return 0;
276
277 struct fd_submit *submit = fd_submit_new(pipe);
278
279 if (perfcntrstr) {
280 if (!backend->set_perfcntrs) {
281 err(1, "performance counters not supported");
282 }
283 perfcntrs = parse_perfcntrs(gpu_id, perfcntrstr, &num_perfcntrs);
284 backend->set_perfcntrs(backend, perfcntrs, num_perfcntrs);
285 }
286
287 backend->emit_grid(kernel, grid, submit);
288
289 fd_submit_flush(submit, -1, NULL, NULL);
290
291 for (int i = 0; i < kernel->num_bufs; i++) {
292 fd_bo_cpu_prep(kernel->bufs[i], pipe, DRM_FREEDRENO_PREP_READ);
293 void *map = fd_bo_map(kernel->bufs[i]);
294
295 printf("buf[%d]:\n", i);
296 dump_hex(map, kernel->buf_sizes[i] * 4);
297 dump_float(map, kernel->buf_sizes[i] * 4);
298 }
299
300 if (perfcntrstr) {
301 uint64_t results[num_perfcntrs];
302 backend->read_perfcntrs(backend, results);
303
304 for (unsigned i = 0; i < num_perfcntrs; i++) {
305 printf("%s:\t%'"PRIu64"\n", perfcntrs[i].name, results[i]);
306 }
307 }
308
309 return 0;
310
311 usage:
312 usage(argv[0]);
313 return -1;
314 }