Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011-2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Parts came from evlist.c builtin-{top,stat,record}.c, see those files for further |
| 5 | * copyright notes. |
| 6 | * |
| 7 | * Released under the GPL v2. (and only v2, not any later version) |
| 8 | */ |
| 9 | |
| 10 | #include <sys/mman.h> |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 11 | #include <inttypes.h> |
| 12 | #include <asm/bug.h> |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 13 | #ifdef HAVE_LIBNUMA_SUPPORT |
| 14 | #include <numaif.h> |
| 15 | #endif |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 16 | #include "debug.h" |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 17 | #include "event.h" |
| 18 | #include "mmap.h" |
| 19 | #include "util.h" /* page_size */ |
| 20 | |
| 21 | size_t perf_mmap__mmap_len(struct perf_mmap *map) |
| 22 | { |
| 23 | return map->mask + 1 + page_size; |
| 24 | } |
| 25 | |
| 26 | /* When check_messup is true, 'end' must points to a good entry */ |
Wang Nan | 8eb7a1f | 2017-12-03 02:00:41 +0000 | [diff] [blame] | 27 | static union perf_event *perf_mmap__read(struct perf_mmap *map, |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 28 | u64 *startp, u64 end) |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 29 | { |
| 30 | unsigned char *data = map->base + page_size; |
| 31 | union perf_event *event = NULL; |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 32 | int diff = end - *startp; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 33 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 34 | if (diff >= (int)sizeof(event->header)) { |
| 35 | size_t size; |
| 36 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 37 | event = (union perf_event *)&data[*startp & map->mask]; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 38 | size = event->header.size; |
| 39 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 40 | if (size < sizeof(event->header) || diff < (int)size) |
| 41 | return NULL; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Event straddles the mmap boundary -- header should always |
| 45 | * be inside due to u64 alignment of output. |
| 46 | */ |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 47 | if ((*startp & map->mask) + size != ((*startp + size) & map->mask)) { |
| 48 | unsigned int offset = *startp; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 49 | unsigned int len = min(sizeof(*event), size), cpy; |
| 50 | void *dst = map->event_copy; |
| 51 | |
| 52 | do { |
| 53 | cpy = min(map->mask + 1 - (offset & map->mask), len); |
| 54 | memcpy(dst, &data[offset & map->mask], cpy); |
| 55 | offset += cpy; |
| 56 | dst += cpy; |
| 57 | len -= cpy; |
| 58 | } while (len); |
| 59 | |
| 60 | event = (union perf_event *)map->event_copy; |
| 61 | } |
| 62 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 63 | *startp += size; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 66 | return event; |
| 67 | } |
| 68 | |
Kan Liang | 3effc2f | 2018-01-18 13:26:25 -0800 | [diff] [blame] | 69 | /* |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 70 | * Read event from ring buffer one by one. |
| 71 | * Return one event for each call. |
| 72 | * |
| 73 | * Usage: |
| 74 | * perf_mmap__read_init() |
| 75 | * while(event = perf_mmap__read_event()) { |
| 76 | * //process the event |
| 77 | * perf_mmap__consume() |
| 78 | * } |
| 79 | * perf_mmap__read_done() |
| 80 | */ |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 81 | union perf_event *perf_mmap__read_event(struct perf_mmap *map) |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 82 | { |
| 83 | union perf_event *event; |
| 84 | |
| 85 | /* |
| 86 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 87 | */ |
| 88 | if (!refcount_read(&map->refcnt)) |
| 89 | return NULL; |
| 90 | |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 91 | /* non-overwirte doesn't pause the ringbuffer */ |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 92 | if (!map->overwrite) |
| 93 | map->end = perf_mmap__read_head(map); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 94 | |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 95 | event = perf_mmap__read(map, &map->start, map->end); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 96 | |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 97 | if (!map->overwrite) |
| 98 | map->prev = map->start; |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 99 | |
| 100 | return event; |
| 101 | } |
| 102 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 103 | static bool perf_mmap__empty(struct perf_mmap *map) |
| 104 | { |
| 105 | return perf_mmap__read_head(map) == map->prev && !map->auxtrace_mmap.base; |
| 106 | } |
| 107 | |
| 108 | void perf_mmap__get(struct perf_mmap *map) |
| 109 | { |
| 110 | refcount_inc(&map->refcnt); |
| 111 | } |
| 112 | |
| 113 | void perf_mmap__put(struct perf_mmap *map) |
| 114 | { |
| 115 | BUG_ON(map->base && refcount_read(&map->refcnt) == 0); |
| 116 | |
| 117 | if (refcount_dec_and_test(&map->refcnt)) |
| 118 | perf_mmap__munmap(map); |
| 119 | } |
| 120 | |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 121 | void perf_mmap__consume(struct perf_mmap *map) |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 122 | { |
Kan Liang | bdec8b2 | 2018-03-06 10:36:04 -0500 | [diff] [blame] | 123 | if (!map->overwrite) { |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 124 | u64 old = map->prev; |
| 125 | |
| 126 | perf_mmap__write_tail(map, old); |
| 127 | } |
| 128 | |
| 129 | if (refcount_read(&map->refcnt) == 1 && perf_mmap__empty(map)) |
| 130 | perf_mmap__put(map); |
| 131 | } |
| 132 | |
| 133 | int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, |
| 134 | struct auxtrace_mmap_params *mp __maybe_unused, |
| 135 | void *userpg __maybe_unused, |
| 136 | int fd __maybe_unused) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused, |
| 146 | off_t auxtrace_offset __maybe_unused, |
| 147 | unsigned int auxtrace_pages __maybe_unused, |
| 148 | bool auxtrace_overwrite __maybe_unused) |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused, |
| 153 | struct perf_evlist *evlist __maybe_unused, |
| 154 | int idx __maybe_unused, |
| 155 | bool per_cpu __maybe_unused) |
| 156 | { |
| 157 | } |
| 158 | |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 159 | #ifdef HAVE_AIO_SUPPORT |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 160 | |
| 161 | #ifdef HAVE_LIBNUMA_SUPPORT |
| 162 | static int perf_mmap__aio_alloc(struct perf_mmap *map, int idx) |
| 163 | { |
| 164 | map->aio.data[idx] = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE, |
| 165 | MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
| 166 | if (map->aio.data[idx] == MAP_FAILED) { |
| 167 | map->aio.data[idx] = NULL; |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void perf_mmap__aio_free(struct perf_mmap *map, int idx) |
| 175 | { |
| 176 | if (map->aio.data[idx]) { |
| 177 | munmap(map->aio.data[idx], perf_mmap__mmap_len(map)); |
| 178 | map->aio.data[idx] = NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static int perf_mmap__aio_bind(struct perf_mmap *map, int idx, int cpu, int affinity) |
| 183 | { |
| 184 | void *data; |
| 185 | size_t mmap_len; |
| 186 | unsigned long node_mask; |
| 187 | |
| 188 | if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) { |
| 189 | data = map->aio.data[idx]; |
| 190 | mmap_len = perf_mmap__mmap_len(map); |
| 191 | node_mask = 1UL << cpu__get_node(cpu); |
| 192 | if (mbind(data, mmap_len, MPOL_BIND, &node_mask, 1, 0)) { |
| 193 | pr_err("Failed to bind [%p-%p] AIO buffer to node %d: error %m\n", |
| 194 | data, data + mmap_len, cpu__get_node(cpu)); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | #else |
| 202 | static int perf_mmap__aio_alloc(struct perf_mmap *map, int idx) |
| 203 | { |
| 204 | map->aio.data[idx] = malloc(perf_mmap__mmap_len(map)); |
| 205 | if (map->aio.data[idx] == NULL) |
| 206 | return -1; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void perf_mmap__aio_free(struct perf_mmap *map, int idx) |
| 212 | { |
| 213 | zfree(&(map->aio.data[idx])); |
| 214 | } |
| 215 | |
| 216 | static int perf_mmap__aio_bind(struct perf_mmap *map __maybe_unused, int idx __maybe_unused, |
| 217 | int cpu __maybe_unused, int affinity __maybe_unused) |
| 218 | { |
| 219 | return 0; |
| 220 | } |
| 221 | #endif |
| 222 | |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 223 | static int perf_mmap__aio_mmap(struct perf_mmap *map, struct mmap_params *mp) |
| 224 | { |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 225 | int delta_max, i, prio, ret; |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 226 | |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 227 | map->aio.nr_cblocks = mp->nr_cblocks; |
| 228 | if (map->aio.nr_cblocks) { |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 229 | map->aio.aiocb = calloc(map->aio.nr_cblocks, sizeof(struct aiocb *)); |
| 230 | if (!map->aio.aiocb) { |
| 231 | pr_debug2("failed to allocate aiocb for data buffer, error %m\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | map->aio.cblocks = calloc(map->aio.nr_cblocks, sizeof(struct aiocb)); |
| 235 | if (!map->aio.cblocks) { |
| 236 | pr_debug2("failed to allocate cblocks for data buffer, error %m\n"); |
| 237 | return -1; |
| 238 | } |
| 239 | map->aio.data = calloc(map->aio.nr_cblocks, sizeof(void *)); |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 240 | if (!map->aio.data) { |
| 241 | pr_debug2("failed to allocate data buffer, error %m\n"); |
| 242 | return -1; |
| 243 | } |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 244 | delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX); |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 245 | for (i = 0; i < map->aio.nr_cblocks; ++i) { |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 246 | ret = perf_mmap__aio_alloc(map, i); |
| 247 | if (ret == -1) { |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 248 | pr_debug2("failed to allocate data buffer area, error %m"); |
| 249 | return -1; |
| 250 | } |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 251 | ret = perf_mmap__aio_bind(map, i, map->cpu, mp->affinity); |
| 252 | if (ret == -1) |
| 253 | return -1; |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 254 | /* |
| 255 | * Use cblock.aio_fildes value different from -1 |
| 256 | * to denote started aio write operation on the |
| 257 | * cblock so it requires explicit record__aio_sync() |
| 258 | * call prior the cblock may be reused again. |
| 259 | */ |
| 260 | map->aio.cblocks[i].aio_fildes = -1; |
| 261 | /* |
| 262 | * Allocate cblocks with priority delta to have |
| 263 | * faster aio write system calls because queued requests |
| 264 | * are kept in separate per-prio queues and adding |
| 265 | * a new request will iterate thru shorter per-prio |
| 266 | * list. Blocks with numbers higher than |
| 267 | * _SC_AIO_PRIO_DELTA_MAX go with priority 0. |
| 268 | */ |
| 269 | prio = delta_max - i; |
| 270 | map->aio.cblocks[i].aio_reqprio = prio >= 0 ? prio : 0; |
| 271 | } |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static void perf_mmap__aio_munmap(struct perf_mmap *map) |
| 278 | { |
Alexey Budankov | c8dd6ee | 2018-12-05 20:19:41 +0300 | [diff] [blame] | 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < map->aio.nr_cblocks; ++i) |
Alexey Budankov | c44a8b4 | 2019-01-22 20:48:54 +0300 | [diff] [blame] | 282 | perf_mmap__aio_free(map, i); |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 283 | if (map->aio.data) |
| 284 | zfree(&map->aio.data); |
Alexey Budankov | c8dd6ee | 2018-12-05 20:19:41 +0300 | [diff] [blame] | 285 | zfree(&map->aio.cblocks); |
| 286 | zfree(&map->aio.aiocb); |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 287 | } |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 288 | |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 289 | int perf_mmap__aio_push(struct perf_mmap *md, void *to, int idx, |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 290 | int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off), |
| 291 | off_t *off) |
| 292 | { |
| 293 | u64 head = perf_mmap__read_head(md); |
| 294 | unsigned char *data = md->base + page_size; |
| 295 | unsigned long size, size0 = 0; |
| 296 | void *buf; |
| 297 | int rc = 0; |
| 298 | |
| 299 | rc = perf_mmap__read_init(md); |
| 300 | if (rc < 0) |
| 301 | return (rc == -EAGAIN) ? 0 : -1; |
| 302 | |
| 303 | /* |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 304 | * md->base data is copied into md->data[idx] buffer to |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 305 | * release space in the kernel buffer as fast as possible, |
| 306 | * thru perf_mmap__consume() below. |
| 307 | * |
| 308 | * That lets the kernel to proceed with storing more |
| 309 | * profiling data into the kernel buffer earlier than other |
| 310 | * per-cpu kernel buffers are handled. |
| 311 | * |
| 312 | * Coping can be done in two steps in case the chunk of |
| 313 | * profiling data crosses the upper bound of the kernel buffer. |
| 314 | * In this case we first move part of data from md->start |
| 315 | * till the upper bound and then the reminder from the |
| 316 | * beginning of the kernel buffer till the end of |
| 317 | * the data chunk. |
| 318 | */ |
| 319 | |
| 320 | size = md->end - md->start; |
| 321 | |
| 322 | if ((md->start & md->mask) + size != (md->end & md->mask)) { |
| 323 | buf = &data[md->start & md->mask]; |
| 324 | size = md->mask + 1 - (md->start & md->mask); |
| 325 | md->start += size; |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 326 | memcpy(md->aio.data[idx], buf, size); |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 327 | size0 = size; |
| 328 | } |
| 329 | |
| 330 | buf = &data[md->start & md->mask]; |
| 331 | size = md->end - md->start; |
| 332 | md->start += size; |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 333 | memcpy(md->aio.data[idx] + size0, buf, size); |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 334 | |
| 335 | /* |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 336 | * Increment md->refcount to guard md->data[idx] buffer |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 337 | * from premature deallocation because md object can be |
| 338 | * released earlier than aio write request started |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 339 | * on mmap->data[idx] is complete. |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 340 | * |
| 341 | * perf_mmap__put() is done at record__aio_complete() |
| 342 | * after started request completion. |
| 343 | */ |
| 344 | perf_mmap__get(md); |
| 345 | |
| 346 | md->prev = head; |
| 347 | perf_mmap__consume(md); |
| 348 | |
Alexey Budankov | 93f20c0 | 2018-11-06 12:07:19 +0300 | [diff] [blame] | 349 | rc = push(to, &md->aio.cblocks[idx], md->aio.data[idx], size0 + size, *off); |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame] | 350 | if (!rc) { |
| 351 | *off += size0 + size; |
| 352 | } else { |
| 353 | /* |
| 354 | * Decrement md->refcount back if aio write |
| 355 | * operation failed to start. |
| 356 | */ |
| 357 | perf_mmap__put(md); |
| 358 | } |
| 359 | |
| 360 | return rc; |
| 361 | } |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 362 | #else |
| 363 | static int perf_mmap__aio_mmap(struct perf_mmap *map __maybe_unused, |
| 364 | struct mmap_params *mp __maybe_unused) |
| 365 | { |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static void perf_mmap__aio_munmap(struct perf_mmap *map __maybe_unused) |
| 370 | { |
| 371 | } |
| 372 | #endif |
| 373 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 374 | void perf_mmap__munmap(struct perf_mmap *map) |
| 375 | { |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 376 | perf_mmap__aio_munmap(map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 377 | if (map->base != NULL) { |
| 378 | munmap(map->base, perf_mmap__mmap_len(map)); |
| 379 | map->base = NULL; |
| 380 | map->fd = -1; |
| 381 | refcount_set(&map->refcnt, 0); |
| 382 | } |
| 383 | auxtrace_mmap__munmap(&map->auxtrace_mmap); |
| 384 | } |
| 385 | |
Alexey Budankov | f13de66 | 2019-01-22 20:50:57 +0300 | [diff] [blame] | 386 | static void build_node_mask(int node, cpu_set_t *mask) |
| 387 | { |
| 388 | int c, cpu, nr_cpus; |
| 389 | const struct cpu_map *cpu_map = NULL; |
| 390 | |
| 391 | cpu_map = cpu_map__online(); |
| 392 | if (!cpu_map) |
| 393 | return; |
| 394 | |
| 395 | nr_cpus = cpu_map__nr(cpu_map); |
| 396 | for (c = 0; c < nr_cpus; c++) { |
| 397 | cpu = cpu_map->map[c]; /* map c index to online cpu index */ |
| 398 | if (cpu__get_node(cpu) == node) |
| 399 | CPU_SET(cpu, mask); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void perf_mmap__setup_affinity_mask(struct perf_mmap *map, struct mmap_params *mp) |
| 404 | { |
| 405 | CPU_ZERO(&map->affinity_mask); |
| 406 | if (mp->affinity == PERF_AFFINITY_NODE && cpu__max_node() > 1) |
| 407 | build_node_mask(cpu__get_node(map->cpu), &map->affinity_mask); |
| 408 | else if (mp->affinity == PERF_AFFINITY_CPU) |
| 409 | CPU_SET(map->cpu, &map->affinity_mask); |
| 410 | } |
| 411 | |
Jiri Olsa | 31fb4c0 | 2018-08-17 13:45:55 +0200 | [diff] [blame] | 412 | int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu) |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 413 | { |
| 414 | /* |
Kan Liang | 6afad54 | 2018-03-01 18:09:11 -0500 | [diff] [blame] | 415 | * The last one will be done at perf_mmap__consume(), so that we |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 416 | * make sure we don't prevent tools from consuming every last event in |
| 417 | * the ring buffer. |
| 418 | * |
| 419 | * I.e. we can get the POLLHUP meaning that the fd doesn't exist |
| 420 | * anymore, but the last events for it are still in the ring buffer, |
| 421 | * waiting to be consumed. |
| 422 | * |
| 423 | * Tools can chose to ignore this at their own discretion, but the |
| 424 | * evlist layer can't just drop it when filtering events in |
| 425 | * perf_evlist__filter_pollfd(). |
| 426 | */ |
| 427 | refcount_set(&map->refcnt, 2); |
| 428 | map->prev = 0; |
| 429 | map->mask = mp->mask; |
| 430 | map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot, |
| 431 | MAP_SHARED, fd, 0); |
| 432 | if (map->base == MAP_FAILED) { |
| 433 | pr_debug2("failed to mmap perf event ring buffer, error %d\n", |
| 434 | errno); |
| 435 | map->base = NULL; |
| 436 | return -1; |
| 437 | } |
| 438 | map->fd = fd; |
Jiri Olsa | 31fb4c0 | 2018-08-17 13:45:55 +0200 | [diff] [blame] | 439 | map->cpu = cpu; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 440 | |
Alexey Budankov | f13de66 | 2019-01-22 20:50:57 +0300 | [diff] [blame] | 441 | perf_mmap__setup_affinity_mask(map, mp); |
Alexey Budankov | 9d2ed64 | 2019-01-22 20:47:43 +0300 | [diff] [blame] | 442 | |
Alexey Budankov | 470530b | 2019-03-18 20:40:26 +0300 | [diff] [blame^] | 443 | map->flush = mp->flush; |
| 444 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 445 | if (auxtrace_mmap__mmap(&map->auxtrace_mmap, |
| 446 | &mp->auxtrace_mp, map->base, fd)) |
| 447 | return -1; |
| 448 | |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 449 | return perf_mmap__aio_mmap(map, mp); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 450 | } |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 451 | |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 452 | static int overwrite_rb_find_range(void *buf, int mask, u64 *start, u64 *end) |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 453 | { |
| 454 | struct perf_event_header *pheader; |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 455 | u64 evt_head = *start; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 456 | int size = mask + 1; |
| 457 | |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 458 | pr_debug2("%s: buf=%p, start=%"PRIx64"\n", __func__, buf, *start); |
| 459 | pheader = (struct perf_event_header *)(buf + (*start & mask)); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 460 | while (true) { |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 461 | if (evt_head - *start >= (unsigned int)size) { |
Wang Nan | 0b72d69 | 2017-12-04 16:51:07 +0000 | [diff] [blame] | 462 | pr_debug("Finished reading overwrite ring buffer: rewind\n"); |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 463 | if (evt_head - *start > (unsigned int)size) |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 464 | evt_head -= pheader->size; |
| 465 | *end = evt_head; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | pheader = (struct perf_event_header *)(buf + (evt_head & mask)); |
| 470 | |
| 471 | if (pheader->size == 0) { |
Wang Nan | 0b72d69 | 2017-12-04 16:51:07 +0000 | [diff] [blame] | 472 | pr_debug("Finished reading overwrite ring buffer: get start\n"); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 473 | *end = evt_head; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | evt_head += pheader->size; |
| 478 | pr_debug3("move evt_head: %"PRIx64"\n", evt_head); |
| 479 | } |
| 480 | WARN_ONCE(1, "Shouldn't get here\n"); |
| 481 | return -1; |
| 482 | } |
| 483 | |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 484 | /* |
| 485 | * Report the start and end of the available data in ringbuffer |
| 486 | */ |
Arnaldo Carvalho de Melo | 895e3b0 | 2018-03-26 11:42:15 -0300 | [diff] [blame] | 487 | static int __perf_mmap__read_init(struct perf_mmap *md) |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 488 | { |
| 489 | u64 head = perf_mmap__read_head(md); |
| 490 | u64 old = md->prev; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 491 | unsigned char *data = md->base + page_size; |
| 492 | unsigned long size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 493 | |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 494 | md->start = md->overwrite ? head : old; |
| 495 | md->end = md->overwrite ? old : head; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 496 | |
Alexey Budankov | 470530b | 2019-03-18 20:40:26 +0300 | [diff] [blame^] | 497 | if ((md->end - md->start) < md->flush) |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 498 | return -EAGAIN; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 499 | |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 500 | size = md->end - md->start; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 501 | if (size > (unsigned long)(md->mask) + 1) { |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 502 | if (!md->overwrite) { |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 503 | WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n"); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 504 | |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 505 | md->prev = head; |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 506 | perf_mmap__consume(md); |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 507 | return -EAGAIN; |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Backward ring buffer is full. We still have a chance to read |
| 512 | * most of data from it. |
| 513 | */ |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 514 | if (overwrite_rb_find_range(data, md->mask, &md->start, &md->end)) |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 515 | return -EINVAL; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 516 | } |
| 517 | |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 518 | return 0; |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Arnaldo Carvalho de Melo | 895e3b0 | 2018-03-26 11:42:15 -0300 | [diff] [blame] | 521 | int perf_mmap__read_init(struct perf_mmap *map) |
| 522 | { |
| 523 | /* |
| 524 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 525 | */ |
| 526 | if (!refcount_read(&map->refcnt)) |
| 527 | return -ENOENT; |
| 528 | |
| 529 | return __perf_mmap__read_init(map); |
| 530 | } |
| 531 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 532 | int perf_mmap__push(struct perf_mmap *md, void *to, |
Jiri Olsa | ded2b8f | 2018-09-13 14:54:06 +0200 | [diff] [blame] | 533 | int push(struct perf_mmap *map, void *to, void *buf, size_t size)) |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 534 | { |
| 535 | u64 head = perf_mmap__read_head(md); |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 536 | unsigned char *data = md->base + page_size; |
| 537 | unsigned long size; |
| 538 | void *buf; |
| 539 | int rc = 0; |
| 540 | |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 541 | rc = perf_mmap__read_init(md); |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 542 | if (rc < 0) |
| 543 | return (rc == -EAGAIN) ? 0 : -1; |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 544 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 545 | size = md->end - md->start; |
Kan Liang | dc6c35c | 2018-01-18 13:26:17 -0800 | [diff] [blame] | 546 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 547 | if ((md->start & md->mask) + size != (md->end & md->mask)) { |
| 548 | buf = &data[md->start & md->mask]; |
| 549 | size = md->mask + 1 - (md->start & md->mask); |
| 550 | md->start += size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 551 | |
Jiri Olsa | ded2b8f | 2018-09-13 14:54:06 +0200 | [diff] [blame] | 552 | if (push(md, to, buf, size) < 0) { |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 553 | rc = -1; |
| 554 | goto out; |
| 555 | } |
| 556 | } |
| 557 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 558 | buf = &data[md->start & md->mask]; |
| 559 | size = md->end - md->start; |
| 560 | md->start += size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 561 | |
Jiri Olsa | ded2b8f | 2018-09-13 14:54:06 +0200 | [diff] [blame] | 562 | if (push(md, to, buf, size) < 0) { |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 563 | rc = -1; |
| 564 | goto out; |
| 565 | } |
| 566 | |
| 567 | md->prev = head; |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 568 | perf_mmap__consume(md); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 569 | out: |
| 570 | return rc; |
| 571 | } |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | * Mandatory for overwrite mode |
| 575 | * The direction of overwrite mode is backward. |
| 576 | * The last perf_mmap__read() will set tail to map->prev. |
| 577 | * Need to correct the map->prev to head which is the end of next read. |
| 578 | */ |
| 579 | void perf_mmap__read_done(struct perf_mmap *map) |
| 580 | { |
Kan Liang | f58385f | 2018-03-26 09:42:09 -0400 | [diff] [blame] | 581 | /* |
| 582 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 583 | */ |
| 584 | if (!refcount_read(&map->refcnt)) |
| 585 | return; |
| 586 | |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 587 | map->prev = perf_mmap__read_head(map); |
| 588 | } |