Chris Wilson | b98bade | 2013-08-20 21:39:27 +0100 | [diff] [blame] | 1 | /* |
| 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 25 | #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 Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame] | 39 | #include "debugfs.h" |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 40 | |
| 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 49 | #define N_PAGES 32 |
| 50 | |
| 51 | struct 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 Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 57 | uint32_t raw_hdr0; |
| 58 | uint32_t raw_hdr1; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 59 | uint32_t raw[0]; |
| 60 | }; |
| 61 | |
| 62 | static int |
| 63 | perf_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 | |
| 83 | static uint64_t tracepoint_id(const char *sys, const char *name) |
| 84 | { |
| 85 | char buf[1024]; |
| 86 | int fd, n; |
| 87 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame] | 88 | snprintf(buf, sizeof(buf), "%s/tracing/events/%s/%s/id", debugfs_path, sys, name); |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 89 | 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 | |
| 101 | static int perf_tracepoint_open(struct gpu_perf *gp, |
| 102 | const char *sys, const char *name, |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 103 | int (*func)(struct gpu_perf *, const void *)) |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 104 | { |
| 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 121 | |
| 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 Wilson | e1d8d77 | 2013-08-17 22:07:49 +0100 | [diff] [blame] | 140 | if (read(fd[n], track, sizeof(track)) < 0) |
| 141 | return errno; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 142 | sample[n].id = track[1]; |
| 143 | sample[n].func = func; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | gp->nr_events++; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int perf_mmap(struct gpu_perf *gp) |
| 151 | { |
| 152 | int size = (1 + N_PAGES) * gp->page_size; |
Chris Wilson | e1d8d77 | 2013-08-17 22:07:49 +0100 | [diff] [blame] | 153 | int *fd, i, j; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 154 | |
| 155 | gp->map = malloc(sizeof(void *)*gp->nr_cpus); |
| 156 | if (gp->map == NULL) |
| 157 | return ENOMEM; |
| 158 | |
Chris Wilson | e1d8d77 | 2013-08-17 22:07:49 +0100 | [diff] [blame] | 159 | 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 163 | goto err; |
| 164 | } |
| 165 | |
Chris Wilson | e1d8d77 | 2013-08-17 22:07:49 +0100 | [diff] [blame] | 166 | 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | |
| 173 | err: |
Chris Wilson | e1d8d77 | 2013-08-17 22:07:49 +0100 | [diff] [blame] | 174 | while (--j > 0) |
| 175 | munmap(gp->map[j], size); |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 176 | free(gp->map); |
| 177 | gp->map = NULL; |
| 178 | return EINVAL; |
| 179 | } |
| 180 | |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 181 | static int get_comm(pid_t pid, char *comm, int len) |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 182 | { |
| 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 Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 195 | } else |
| 196 | len = -1; |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 197 | |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 198 | return len; |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 201 | static struct gpu_perf_comm * |
| 202 | lookup_comm(struct gpu_perf *gp, pid_t pid) |
| 203 | { |
| 204 | struct gpu_perf_comm *comm; |
| 205 | |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 206 | if (pid == 0) |
| 207 | return NULL; |
| 208 | |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 209 | 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 Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 218 | if (get_comm(pid, comm->name, sizeof(comm->name)) < 0) { |
| 219 | free(comm); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | comm->pid = pid; |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 224 | comm->next = gp->comm; |
| 225 | gp->comm = comm; |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return comm; |
| 229 | } |
| 230 | |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 231 | static 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 Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 236 | comm = lookup_comm(gp, sample->pid); |
| 237 | if (comm == NULL) |
| 238 | return 0; |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 239 | |
| 240 | comm->nr_requests[sample->raw[1]]++; |
| 241 | return 1; |
| 242 | } |
| 243 | |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 244 | static int flip_complete(struct gpu_perf *gp, const void *event) |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 245 | { |
Chris Wilson | 98572f0 | 2013-08-18 11:15:08 +0100 | [diff] [blame] | 246 | const struct sample_event *sample = event; |
| 247 | |
| 248 | gp->flip_complete[sample->raw[0]]++; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 249 | return 1; |
| 250 | } |
| 251 | |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 252 | static 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 Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 256 | struct gpu_perf_time *wait; |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 257 | |
| 258 | comm = lookup_comm(gp, sample->pid); |
| 259 | if (comm == NULL) |
| 260 | return 0; |
| 261 | |
Chris Wilson | 6233cac | 2013-08-17 22:22:21 +0100 | [diff] [blame] | 262 | wait = malloc(sizeof(*wait)); |
| 263 | if (wait == NULL) |
| 264 | return 0; |
| 265 | |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 266 | wait->comm = comm; |
Chris Wilson | f185f46 | 2013-08-17 23:07:01 +0100 | [diff] [blame] | 267 | wait->seqno = sample->raw[2]; |
Chris Wilson | 6233cac | 2013-08-17 22:22:21 +0100 | [diff] [blame] | 268 | wait->time = sample->time; |
Chris Wilson | b20a6b8 | 2013-08-22 01:30:02 +0100 | [diff] [blame] | 269 | wait->next = gp->wait[sample->raw[1]]; |
| 270 | gp->wait[sample->raw[1]] = wait; |
Chris Wilson | 6233cac | 2013-08-17 22:22:21 +0100 | [diff] [blame] | 271 | |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int wait_end(struct gpu_perf *gp, const void *event) |
| 276 | { |
| 277 | const struct sample_event *sample = event; |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 278 | struct gpu_perf_time *wait, **prev; |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 279 | |
Chris Wilson | b20a6b8 | 2013-08-22 01:30:02 +0100 | [diff] [blame] | 280 | for (prev = &gp->wait[sample->raw[1]]; (wait = *prev) != NULL; prev = &wait->next) { |
Chris Wilson | f185f46 | 2013-08-17 23:07:01 +0100 | [diff] [blame] | 281 | if (wait->seqno != sample->raw[2]) |
Chris Wilson | 6233cac | 2013-08-17 22:22:21 +0100 | [diff] [blame] | 282 | continue; |
| 283 | |
Chris Wilson | 67f533f | 2013-08-17 22:33:35 +0100 | [diff] [blame] | 284 | wait->comm->wait_time += sample->time - wait->time; |
Chris Wilson | 6233cac | 2013-08-17 22:22:21 +0100 | [diff] [blame] | 285 | *prev = wait->next; |
| 286 | free(wait); |
| 287 | return 1; |
| 288 | } |
| 289 | |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 293 | void 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 Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 299 | perf_tracepoint_open(gp, "i915", "i915_gem_request_add", request_add); |
Chris Wilson | eed59ea | 2013-08-17 21:14:10 +0100 | [diff] [blame] | 300 | 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 Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 302 | perf_tracepoint_open(gp, "i915", "i915_flip_complete", flip_complete); |
| 303 | |
Chris Wilson | 1847869 | 2013-08-22 10:59:58 +0000 | [diff] [blame] | 304 | if (gp->nr_events == 0) { |
| 305 | gp->error = "i915.ko tracepoints not available"; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 306 | return; |
Chris Wilson | 1847869 | 2013-08-22 10:59:58 +0000 | [diff] [blame] | 307 | } |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 308 | |
| 309 | if (perf_mmap(gp)) |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | static int process_sample(struct gpu_perf *gp, |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 314 | const struct perf_event_header *header) |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 315 | { |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 316 | const struct sample_event *sample = (const struct sample_event *)header; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 317 | 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 | |
| 333 | int 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 Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 346 | const uint8_t *data; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 347 | uint64_t head, tail; |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 348 | int wrap = 0; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 349 | |
| 350 | tail = mmap->data_tail; |
| 351 | head = mmap->data_head; |
| 352 | rmb(); |
| 353 | |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 354 | if (head < tail) { |
| 355 | wrap = 1; |
| 356 | tail &= mask; |
| 357 | head &= mask; |
Chris Wilson | 3e430a8 | 2013-08-17 18:09:40 +0100 | [diff] [blame] | 358 | head += size; |
Chris Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 359 | } |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 360 | |
| 361 | data = (uint8_t *)mmap + gp->page_size; |
| 362 | while (head - tail >= sizeof (struct perf_event_header)) { |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 363 | const struct perf_event_header *header; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 364 | |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 365 | header = (const struct perf_event_header *)(data + (tail & mask)); |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 366 | if (header->size > head - tail) |
| 367 | break; |
| 368 | |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 369 | if ((const uint8_t *)header + header->size > data + size) { |
| 370 | int before; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 371 | |
| 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 Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 381 | before = data + size - (const uint8_t *)header; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 382 | |
Chris Wilson | 8cdb5bc | 2013-08-17 18:24:39 +0100 | [diff] [blame] | 383 | memcpy(buffer, header, before); |
| 384 | memcpy(buffer + before, data, header->size - before); |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 385 | |
| 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 Wilson | cbbd55a | 2013-08-17 20:04:11 +0100 | [diff] [blame] | 394 | if (wrap) |
| 395 | tail &= mask; |
| 396 | mmap->data_tail = tail; |
Chris Wilson | cc45a9a | 2013-08-17 17:38:37 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | free(buffer); |
| 400 | return update; |
| 401 | } |