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