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> |
| 13 | #include "debug.h" |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 14 | #include "event.h" |
| 15 | #include "mmap.h" |
| 16 | #include "util.h" /* page_size */ |
| 17 | |
| 18 | size_t perf_mmap__mmap_len(struct perf_mmap *map) |
| 19 | { |
| 20 | return map->mask + 1 + page_size; |
| 21 | } |
| 22 | |
| 23 | /* When check_messup is true, 'end' must points to a good entry */ |
Wang Nan | 8eb7a1f | 2017-12-03 02:00:41 +0000 | [diff] [blame] | 24 | static union perf_event *perf_mmap__read(struct perf_mmap *map, |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 25 | u64 *startp, u64 end) |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 26 | { |
| 27 | unsigned char *data = map->base + page_size; |
| 28 | union perf_event *event = NULL; |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 29 | int diff = end - *startp; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 30 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 31 | if (diff >= (int)sizeof(event->header)) { |
| 32 | size_t size; |
| 33 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 34 | event = (union perf_event *)&data[*startp & map->mask]; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 35 | size = event->header.size; |
| 36 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 37 | if (size < sizeof(event->header) || diff < (int)size) |
| 38 | return NULL; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Event straddles the mmap boundary -- header should always |
| 42 | * be inside due to u64 alignment of output. |
| 43 | */ |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 44 | if ((*startp & map->mask) + size != ((*startp + size) & map->mask)) { |
| 45 | unsigned int offset = *startp; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 46 | unsigned int len = min(sizeof(*event), size), cpy; |
| 47 | void *dst = map->event_copy; |
| 48 | |
| 49 | do { |
| 50 | cpy = min(map->mask + 1 - (offset & map->mask), len); |
| 51 | memcpy(dst, &data[offset & map->mask], cpy); |
| 52 | offset += cpy; |
| 53 | dst += cpy; |
| 54 | len -= cpy; |
| 55 | } while (len); |
| 56 | |
| 57 | event = (union perf_event *)map->event_copy; |
| 58 | } |
| 59 | |
Kan Liang | b4b036b | 2018-01-18 13:26:21 -0800 | [diff] [blame] | 60 | *startp += size; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 63 | return event; |
| 64 | } |
| 65 | |
Kan Liang | 3effc2f | 2018-01-18 13:26:25 -0800 | [diff] [blame] | 66 | /* |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 67 | * Read event from ring buffer one by one. |
| 68 | * Return one event for each call. |
| 69 | * |
| 70 | * Usage: |
| 71 | * perf_mmap__read_init() |
| 72 | * while(event = perf_mmap__read_event()) { |
| 73 | * //process the event |
| 74 | * perf_mmap__consume() |
| 75 | * } |
| 76 | * perf_mmap__read_done() |
| 77 | */ |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 78 | union perf_event *perf_mmap__read_event(struct perf_mmap *map) |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 79 | { |
| 80 | union perf_event *event; |
| 81 | |
| 82 | /* |
| 83 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 84 | */ |
| 85 | if (!refcount_read(&map->refcnt)) |
| 86 | return NULL; |
| 87 | |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 88 | /* non-overwirte doesn't pause the ringbuffer */ |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 89 | if (!map->overwrite) |
| 90 | map->end = perf_mmap__read_head(map); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 91 | |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 92 | event = perf_mmap__read(map, &map->start, map->end); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 93 | |
Kan Liang | b9de0f6 | 2018-03-06 10:36:03 -0500 | [diff] [blame] | 94 | if (!map->overwrite) |
| 95 | map->prev = map->start; |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 96 | |
| 97 | return event; |
| 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 100 | static bool perf_mmap__empty(struct perf_mmap *map) |
| 101 | { |
| 102 | return perf_mmap__read_head(map) == map->prev && !map->auxtrace_mmap.base; |
| 103 | } |
| 104 | |
| 105 | void perf_mmap__get(struct perf_mmap *map) |
| 106 | { |
| 107 | refcount_inc(&map->refcnt); |
| 108 | } |
| 109 | |
| 110 | void perf_mmap__put(struct perf_mmap *map) |
| 111 | { |
| 112 | BUG_ON(map->base && refcount_read(&map->refcnt) == 0); |
| 113 | |
| 114 | if (refcount_dec_and_test(&map->refcnt)) |
| 115 | perf_mmap__munmap(map); |
| 116 | } |
| 117 | |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 118 | void perf_mmap__consume(struct perf_mmap *map) |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 119 | { |
Kan Liang | bdec8b2 | 2018-03-06 10:36:04 -0500 | [diff] [blame] | 120 | if (!map->overwrite) { |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 121 | u64 old = map->prev; |
| 122 | |
| 123 | perf_mmap__write_tail(map, old); |
| 124 | } |
| 125 | |
| 126 | if (refcount_read(&map->refcnt) == 1 && perf_mmap__empty(map)) |
| 127 | perf_mmap__put(map); |
| 128 | } |
| 129 | |
| 130 | int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, |
| 131 | struct auxtrace_mmap_params *mp __maybe_unused, |
| 132 | void *userpg __maybe_unused, |
| 133 | int fd __maybe_unused) |
| 134 | { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused, |
| 143 | off_t auxtrace_offset __maybe_unused, |
| 144 | unsigned int auxtrace_pages __maybe_unused, |
| 145 | bool auxtrace_overwrite __maybe_unused) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused, |
| 150 | struct perf_evlist *evlist __maybe_unused, |
| 151 | int idx __maybe_unused, |
| 152 | bool per_cpu __maybe_unused) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | void perf_mmap__munmap(struct perf_mmap *map) |
| 157 | { |
| 158 | if (map->base != NULL) { |
| 159 | munmap(map->base, perf_mmap__mmap_len(map)); |
| 160 | map->base = NULL; |
| 161 | map->fd = -1; |
| 162 | refcount_set(&map->refcnt, 0); |
| 163 | } |
| 164 | auxtrace_mmap__munmap(&map->auxtrace_mmap); |
| 165 | } |
| 166 | |
| 167 | int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd) |
| 168 | { |
| 169 | /* |
Kan Liang | 6afad54 | 2018-03-01 18:09:11 -0500 | [diff] [blame] | 170 | * 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] | 171 | * make sure we don't prevent tools from consuming every last event in |
| 172 | * the ring buffer. |
| 173 | * |
| 174 | * I.e. we can get the POLLHUP meaning that the fd doesn't exist |
| 175 | * anymore, but the last events for it are still in the ring buffer, |
| 176 | * waiting to be consumed. |
| 177 | * |
| 178 | * Tools can chose to ignore this at their own discretion, but the |
| 179 | * evlist layer can't just drop it when filtering events in |
| 180 | * perf_evlist__filter_pollfd(). |
| 181 | */ |
| 182 | refcount_set(&map->refcnt, 2); |
| 183 | map->prev = 0; |
| 184 | map->mask = mp->mask; |
| 185 | map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot, |
| 186 | MAP_SHARED, fd, 0); |
| 187 | if (map->base == MAP_FAILED) { |
| 188 | pr_debug2("failed to mmap perf event ring buffer, error %d\n", |
| 189 | errno); |
| 190 | map->base = NULL; |
| 191 | return -1; |
| 192 | } |
| 193 | map->fd = fd; |
| 194 | |
| 195 | if (auxtrace_mmap__mmap(&map->auxtrace_mmap, |
| 196 | &mp->auxtrace_mp, map->base, fd)) |
| 197 | return -1; |
| 198 | |
| 199 | return 0; |
| 200 | } |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 201 | |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 202 | 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] | 203 | { |
| 204 | struct perf_event_header *pheader; |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 205 | u64 evt_head = *start; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 206 | int size = mask + 1; |
| 207 | |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 208 | pr_debug2("%s: buf=%p, start=%"PRIx64"\n", __func__, buf, *start); |
| 209 | pheader = (struct perf_event_header *)(buf + (*start & mask)); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 210 | while (true) { |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 211 | if (evt_head - *start >= (unsigned int)size) { |
Wang Nan | 0b72d69 | 2017-12-04 16:51:07 +0000 | [diff] [blame] | 212 | pr_debug("Finished reading overwrite ring buffer: rewind\n"); |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 213 | if (evt_head - *start > (unsigned int)size) |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 214 | evt_head -= pheader->size; |
| 215 | *end = evt_head; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | pheader = (struct perf_event_header *)(buf + (evt_head & mask)); |
| 220 | |
| 221 | if (pheader->size == 0) { |
Wang Nan | 0b72d69 | 2017-12-04 16:51:07 +0000 | [diff] [blame] | 222 | pr_debug("Finished reading overwrite ring buffer: get start\n"); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 223 | *end = evt_head; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | evt_head += pheader->size; |
| 228 | pr_debug3("move evt_head: %"PRIx64"\n", evt_head); |
| 229 | } |
| 230 | WARN_ONCE(1, "Shouldn't get here\n"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 234 | /* |
| 235 | * Report the start and end of the available data in ringbuffer |
| 236 | */ |
Arnaldo Carvalho de Melo | 895e3b0 | 2018-03-26 11:42:15 -0300 | [diff] [blame^] | 237 | static int __perf_mmap__read_init(struct perf_mmap *md) |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 238 | { |
| 239 | u64 head = perf_mmap__read_head(md); |
| 240 | u64 old = md->prev; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 241 | unsigned char *data = md->base + page_size; |
| 242 | unsigned long size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 243 | |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 244 | md->start = md->overwrite ? head : old; |
| 245 | md->end = md->overwrite ? old : head; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 246 | |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 247 | if (md->start == md->end) |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 248 | return -EAGAIN; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 249 | |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 250 | size = md->end - md->start; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 251 | if (size > (unsigned long)(md->mask) + 1) { |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 252 | if (!md->overwrite) { |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 253 | 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] | 254 | |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 255 | md->prev = head; |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 256 | perf_mmap__consume(md); |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 257 | return -EAGAIN; |
Wang Nan | 7fb4b40 | 2017-12-04 16:51:06 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Backward ring buffer is full. We still have a chance to read |
| 262 | * most of data from it. |
| 263 | */ |
Yisheng Xie | 699db11 | 2018-03-13 20:31:13 +0800 | [diff] [blame] | 264 | if (overwrite_rb_find_range(data, md->mask, &md->start, &md->end)) |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 265 | return -EINVAL; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 266 | } |
| 267 | |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 268 | return 0; |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | 895e3b0 | 2018-03-26 11:42:15 -0300 | [diff] [blame^] | 271 | int perf_mmap__read_init(struct perf_mmap *map) |
| 272 | { |
| 273 | /* |
| 274 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 275 | */ |
| 276 | if (!refcount_read(&map->refcnt)) |
| 277 | return -ENOENT; |
| 278 | |
| 279 | return __perf_mmap__read_init(map); |
| 280 | } |
| 281 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 282 | int perf_mmap__push(struct perf_mmap *md, void *to, |
| 283 | int push(void *to, void *buf, size_t size)) |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 284 | { |
| 285 | u64 head = perf_mmap__read_head(md); |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 286 | unsigned char *data = md->base + page_size; |
| 287 | unsigned long size; |
| 288 | void *buf; |
| 289 | int rc = 0; |
| 290 | |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 291 | rc = perf_mmap__read_init(md); |
Kan Liang | 189f2cc | 2018-01-18 13:26:20 -0800 | [diff] [blame] | 292 | if (rc < 0) |
| 293 | return (rc == -EAGAIN) ? 0 : -1; |
Kan Liang | 8872481 | 2018-01-18 13:26:19 -0800 | [diff] [blame] | 294 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 295 | size = md->end - md->start; |
Kan Liang | dc6c35c | 2018-01-18 13:26:17 -0800 | [diff] [blame] | 296 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 297 | if ((md->start & md->mask) + size != (md->end & md->mask)) { |
| 298 | buf = &data[md->start & md->mask]; |
| 299 | size = md->mask + 1 - (md->start & md->mask); |
| 300 | md->start += size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 301 | |
| 302 | if (push(to, buf, size) < 0) { |
| 303 | rc = -1; |
| 304 | goto out; |
| 305 | } |
| 306 | } |
| 307 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 308 | buf = &data[md->start & md->mask]; |
| 309 | size = md->end - md->start; |
| 310 | md->start += size; |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 311 | |
| 312 | if (push(to, buf, size) < 0) { |
| 313 | rc = -1; |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | md->prev = head; |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 318 | perf_mmap__consume(md); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 319 | out: |
| 320 | return rc; |
| 321 | } |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Mandatory for overwrite mode |
| 325 | * The direction of overwrite mode is backward. |
| 326 | * The last perf_mmap__read() will set tail to map->prev. |
| 327 | * Need to correct the map->prev to head which is the end of next read. |
| 328 | */ |
| 329 | void perf_mmap__read_done(struct perf_mmap *map) |
| 330 | { |
Kan Liang | f58385f | 2018-03-26 09:42:09 -0400 | [diff] [blame] | 331 | /* |
| 332 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 333 | */ |
| 334 | if (!refcount_read(&map->refcnt)) |
| 335 | return; |
| 336 | |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 337 | map->prev = perf_mmap__read_head(map); |
| 338 | } |