blob: 61e447dac08ba5248fd8ac30509310f400117665 [file] [log] [blame]
Chris Wilsonb98bade2013-08-20 21:39:27 +01001/*
2 * Copyright © 2013 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 (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
Chris Wilsoncc45a9a2013-08-17 17:38:37 +010025#include <stdint.h>
26#include <stdbool.h>
27#include <linux/perf_event.h>
28#include <sys/types.h>
29#include <sys/mman.h>
30#include <sys/ioctl.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <fcntl.h>
36#include <errno.h>
37
38#include "gpu-perf.h"
Chris Wilson9574cb12013-08-23 15:51:21 +010039#include "debugfs.h"
Chris Wilsoncc45a9a2013-08-17 17:38:37 +010040
41#if defined(__i386__)
42#define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
43#endif
44
45#if defined(__x86_64__)
46#define rmb() asm volatile("lfence" ::: "memory")
47#endif
48
Chris Wilsoncc45a9a2013-08-17 17:38:37 +010049#define N_PAGES 32
50
51struct sample_event {
52 struct perf_event_header header;
53 uint32_t pid, tid;
54 uint64_t time;
55 uint64_t id;
56 uint32_t raw_size;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +010057 uint32_t raw_hdr0;
58 uint32_t raw_hdr1;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +010059 uint32_t raw[0];
60};
61
62static int
63perf_event_open(struct perf_event_attr *attr,
64 pid_t pid,
65 int cpu,
66 int group_fd,
67 unsigned long flags)
68{
69#ifndef __NR_perf_event_open
70#if defined(__i386__)
71#define __NR_perf_event_open 336
72#elif defined(__x86_64__)
73#define __NR_perf_event_open 298
74#else
75#define __NR_perf_event_open 0
76#endif
77#endif
78
79 attr->size = sizeof(*attr);
80 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
81}
82
83static uint64_t tracepoint_id(const char *sys, const char *name)
84{
85 char buf[1024];
86 int fd, n;
87
Chris Wilson9574cb12013-08-23 15:51:21 +010088 snprintf(buf, sizeof(buf), "%s/tracing/events/%s/%s/id", debugfs_path, sys, name);
Chris Wilsoncc45a9a2013-08-17 17:38:37 +010089 fd = open(buf, 0);
90 if (fd < 0)
91 return 0;
92 n = read(fd, buf, sizeof(buf)-1);
93 close(fd);
94 if (n < 0)
95 return 0;
96
97 buf[n] = '\0';
98 return strtoull(buf, 0, 0);
99}
100
101static int perf_tracepoint_open(struct gpu_perf *gp,
102 const char *sys, const char *name,
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100103 int (*func)(struct gpu_perf *, const void *))
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100104{
105 struct perf_event_attr attr;
106 struct gpu_perf_sample *sample;
107 int n, *fd;
108
109 memset(&attr, 0, sizeof (attr));
110
111 attr.type = PERF_TYPE_TRACEPOINT;
112 attr.config = tracepoint_id(sys, name);
113 if (attr.config == 0)
114 return ENOENT;
115
116 attr.sample_period = 1;
117 attr.sample_type = (PERF_SAMPLE_TIME | PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_TID | PERF_SAMPLE_RAW);
118 attr.read_format = PERF_FORMAT_ID;
119
120 attr.exclude_guest = 1;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100121
122 n = gp->nr_cpus * (gp->nr_events+1);
123 fd = realloc(gp->fd, n*sizeof(int));
124 sample = realloc(gp->sample, n*sizeof(*gp->sample));
125 if (fd == NULL || sample == NULL)
126 return ENOMEM;
127 gp->fd = fd;
128 gp->sample = sample;
129
130 fd += gp->nr_events * gp->nr_cpus;
131 sample += gp->nr_events * gp->nr_cpus;
132 for (n = 0; n < gp->nr_cpus; n++) {
133 uint64_t track[2];
134
135 fd[n] = perf_event_open(&attr, -1, n, -1, 0);
136 if (fd[n] == -1)
137 return errno;
138
139 /* read back the event to establish id->tracepoint */
Chris Wilsone1d8d772013-08-17 22:07:49 +0100140 if (read(fd[n], track, sizeof(track)) < 0)
141 return errno;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100142 sample[n].id = track[1];
143 sample[n].func = func;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100144 }
145
146 gp->nr_events++;
147 return 0;
148}
149
150static int perf_mmap(struct gpu_perf *gp)
151{
152 int size = (1 + N_PAGES) * gp->page_size;
Chris Wilsone1d8d772013-08-17 22:07:49 +0100153 int *fd, i, j;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100154
155 gp->map = malloc(sizeof(void *)*gp->nr_cpus);
156 if (gp->map == NULL)
157 return ENOMEM;
158
Chris Wilsone1d8d772013-08-17 22:07:49 +0100159 fd = gp->fd;
160 for (j = 0; j < gp->nr_cpus; j++) {
161 gp->map[j] = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd++, 0);
162 if (gp->map[j] == (void *)-1)
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100163 goto err;
164 }
165
Chris Wilsone1d8d772013-08-17 22:07:49 +0100166 for (i = 1; i < gp->nr_events; i++) {
167 for (j = 0; j < gp->nr_cpus; j++)
168 ioctl(*fd++, PERF_EVENT_IOC_SET_OUTPUT, gp->fd[j]);
169 }
170
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100171 return 0;
172
173err:
Chris Wilsone1d8d772013-08-17 22:07:49 +0100174 while (--j > 0)
175 munmap(gp->map[j], size);
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100176 free(gp->map);
177 gp->map = NULL;
178 return EINVAL;
179}
180
Chris Wilson67f533f2013-08-17 22:33:35 +0100181static int get_comm(pid_t pid, char *comm, int len)
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100182{
183 char filename[1024];
184 int fd;
185
186 *comm = '\0';
187 snprintf(filename, sizeof(filename), "/proc/%d/comm", pid);
188
189 fd = open(filename, 0);
190 if (fd >= 0) {
191 len = read(fd, comm, len-1);
192 if (len >= 0)
193 comm[len-1] = '\0';
194 close(fd);
Chris Wilson67f533f2013-08-17 22:33:35 +0100195 } else
196 len = -1;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100197
Chris Wilson67f533f2013-08-17 22:33:35 +0100198 return len;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100199}
200
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100201static struct gpu_perf_comm *
202lookup_comm(struct gpu_perf *gp, pid_t pid)
203{
204 struct gpu_perf_comm *comm;
205
Chris Wilson67f533f2013-08-17 22:33:35 +0100206 if (pid == 0)
207 return NULL;
208
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100209 for (comm = gp->comm; comm != NULL; comm = comm->next) {
210 if (comm->pid == pid)
211 break;
212 }
213 if (comm == NULL) {
214 comm = calloc(1, sizeof(*comm));
215 if (comm == NULL)
216 return NULL;
217
Chris Wilson67f533f2013-08-17 22:33:35 +0100218 if (get_comm(pid, comm->name, sizeof(comm->name)) < 0) {
219 free(comm);
220 return NULL;
221 }
222
223 comm->pid = pid;
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100224 comm->next = gp->comm;
225 gp->comm = comm;
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100226 }
227
228 return comm;
229}
230
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100231static int request_add(struct gpu_perf *gp, const void *event)
232{
233 const struct sample_event *sample = event;
234 struct gpu_perf_comm *comm;
235
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100236 comm = lookup_comm(gp, sample->pid);
237 if (comm == NULL)
238 return 0;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100239
240 comm->nr_requests[sample->raw[1]]++;
241 return 1;
242}
243
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100244static int flip_complete(struct gpu_perf *gp, const void *event)
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100245{
Chris Wilson98572f02013-08-18 11:15:08 +0100246 const struct sample_event *sample = event;
247
248 gp->flip_complete[sample->raw[0]]++;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100249 return 1;
250}
251
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100252static int wait_begin(struct gpu_perf *gp, const void *event)
253{
254 const struct sample_event *sample = event;
255 struct gpu_perf_comm *comm;
Chris Wilson67f533f2013-08-17 22:33:35 +0100256 struct gpu_perf_time *wait;
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100257
258 comm = lookup_comm(gp, sample->pid);
259 if (comm == NULL)
260 return 0;
261
Chris Wilson6233cac2013-08-17 22:22:21 +0100262 wait = malloc(sizeof(*wait));
263 if (wait == NULL)
264 return 0;
265
Chris Wilson67f533f2013-08-17 22:33:35 +0100266 wait->comm = comm;
Chris Wilsonf185f462013-08-17 23:07:01 +0100267 wait->seqno = sample->raw[2];
Chris Wilson6233cac2013-08-17 22:22:21 +0100268 wait->time = sample->time;
Chris Wilsonb20a6b82013-08-22 01:30:02 +0100269 wait->next = gp->wait[sample->raw[1]];
270 gp->wait[sample->raw[1]] = wait;
Chris Wilson6233cac2013-08-17 22:22:21 +0100271
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100272 return 0;
273}
274
275static int wait_end(struct gpu_perf *gp, const void *event)
276{
277 const struct sample_event *sample = event;
Chris Wilson67f533f2013-08-17 22:33:35 +0100278 struct gpu_perf_time *wait, **prev;
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100279
Chris Wilsonb20a6b82013-08-22 01:30:02 +0100280 for (prev = &gp->wait[sample->raw[1]]; (wait = *prev) != NULL; prev = &wait->next) {
Chris Wilsonf185f462013-08-17 23:07:01 +0100281 if (wait->seqno != sample->raw[2])
Chris Wilson6233cac2013-08-17 22:22:21 +0100282 continue;
283
Chris Wilson67f533f2013-08-17 22:33:35 +0100284 wait->comm->wait_time += sample->time - wait->time;
Chris Wilson6233cac2013-08-17 22:22:21 +0100285 *prev = wait->next;
286 free(wait);
287 return 1;
288 }
289
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100290 return 0;
291}
292
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100293void gpu_perf_init(struct gpu_perf *gp, unsigned flags)
294{
295 memset(gp, 0, sizeof(*gp));
296 gp->nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
297 gp->page_size = getpagesize();
298
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100299 perf_tracepoint_open(gp, "i915", "i915_gem_request_add", request_add);
Chris Wilsoneed59ea2013-08-17 21:14:10 +0100300 if (perf_tracepoint_open(gp, "i915", "i915_gem_request_wait_begin", wait_begin) == 0)
301 perf_tracepoint_open(gp, "i915", "i915_gem_request_wait_end", wait_end);
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100302 perf_tracepoint_open(gp, "i915", "i915_flip_complete", flip_complete);
303
Chris Wilson18478692013-08-22 10:59:58 +0000304 if (gp->nr_events == 0) {
305 gp->error = "i915.ko tracepoints not available";
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100306 return;
Chris Wilson18478692013-08-22 10:59:58 +0000307 }
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100308
309 if (perf_mmap(gp))
310 return;
311}
312
313static int process_sample(struct gpu_perf *gp,
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100314 const struct perf_event_header *header)
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100315{
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100316 const struct sample_event *sample = (const struct sample_event *)header;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100317 int n, update = 0;
318
319 /* hash me! */
320 for (n = 0; n < gp->nr_cpus * gp->nr_events; n++) {
321 if (gp->sample[n].id != sample->id)
322 continue;
323
324 update = 1;
325 if (gp->sample[n].func)
326 update = gp->sample[n].func(gp, sample);
327 break;
328 }
329
330 return update;
331}
332
333int gpu_perf_update(struct gpu_perf *gp)
334{
335 const int size = N_PAGES * gp->page_size;
336 const int mask = size - 1;
337 uint8_t *buffer = NULL;
338 int buffer_size = 0;
339 int n, update = 0;
340
341 if (gp->map == NULL)
342 return 0;
343
344 for (n = 0; n < gp->nr_cpus; n++) {
345 struct perf_event_mmap_page *mmap = gp->map[n];
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100346 const uint8_t *data;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100347 uint64_t head, tail;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100348 int wrap = 0;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100349
350 tail = mmap->data_tail;
351 head = mmap->data_head;
352 rmb();
353
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100354 if (head < tail) {
355 wrap = 1;
356 tail &= mask;
357 head &= mask;
Chris Wilson3e430a82013-08-17 18:09:40 +0100358 head += size;
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100359 }
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100360
361 data = (uint8_t *)mmap + gp->page_size;
362 while (head - tail >= sizeof (struct perf_event_header)) {
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100363 const struct perf_event_header *header;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100364
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100365 header = (const struct perf_event_header *)(data + (tail & mask));
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100366 if (header->size > head - tail)
367 break;
368
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100369 if ((const uint8_t *)header + header->size > data + size) {
370 int before;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100371
372 if (header->size > buffer_size) {
373 uint8_t *b = realloc(buffer, header->size);
374 if (b == NULL)
375 break;
376
377 buffer = b;
378 buffer_size = header->size;
379 }
380
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100381 before = data + size - (const uint8_t *)header;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100382
Chris Wilson8cdb5bc2013-08-17 18:24:39 +0100383 memcpy(buffer, header, before);
384 memcpy(buffer + before, data, header->size - before);
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100385
386 header = (struct perf_event_header *)buffer;
387 }
388
389 if (header->type == PERF_RECORD_SAMPLE)
390 update += process_sample(gp, header);
391 tail += header->size;
392 }
393
Chris Wilsoncbbd55a2013-08-17 20:04:11 +0100394 if (wrap)
395 tail &= mask;
396 mmap->data_tail = tail;
Chris Wilsoncc45a9a2013-08-17 17:38:37 +0100397 }
398
399 free(buffer);
400 return update;
401}