mmap.c 13.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2011-2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
 *
 * Parts came from evlist.c builtin-{top,stat,record}.c, see those files for further
 * copyright notes.
 */

#include <sys/mman.h>
10 11
#include <inttypes.h>
#include <asm/bug.h>
12
#include <linux/zalloc.h>
13 14
#include <stdlib.h>
#include <string.h>
15 16 17
#ifdef HAVE_LIBNUMA_SUPPORT
#include <numaif.h>
#endif
18
#include "cpumap.h"
19
#include "debug.h"
20 21
#include "event.h"
#include "mmap.h"
22
#include "../perf.h"
23 24
#include "util.h" /* page_size */

25
size_t perf_mmap__mmap_len(struct mmap *map)
26
{
27
	return map->core.mask + 1 + page_size;
28 29 30
}

/* When check_messup is true, 'end' must points to a good entry */
31
static union perf_event *perf_mmap__read(struct mmap *map,
32
					 u64 *startp, u64 end)
33
{
Jiri Olsa's avatar
Jiri Olsa committed
34
	unsigned char *data = map->core.base + page_size;
35
	union perf_event *event = NULL;
36
	int diff = end - *startp;
37 38 39 40

	if (diff >= (int)sizeof(event->header)) {
		size_t size;

41
		event = (union perf_event *)&data[*startp & map->core.mask];
42 43
		size = event->header.size;

44 45
		if (size < sizeof(event->header) || diff < (int)size)
			return NULL;
46 47 48 49 50

		/*
		 * Event straddles the mmap boundary -- header should always
		 * be inside due to u64 alignment of output.
		 */
51
		if ((*startp & map->core.mask) + size != ((*startp + size) & map->core.mask)) {
52
			unsigned int offset = *startp;
53 54 55 56
			unsigned int len = min(sizeof(*event), size), cpy;
			void *dst = map->event_copy;

			do {
57 58
				cpy = min(map->core.mask + 1 - (offset & map->core.mask), len);
				memcpy(dst, &data[offset & map->core.mask], cpy);
59 60 61 62 63 64 65 66
				offset += cpy;
				dst += cpy;
				len -= cpy;
			} while (len);

			event = (union perf_event *)map->event_copy;
		}

67
		*startp += size;
68 69 70 71 72
	}

	return event;
}

73 74 75 76 77 78 79 80 81 82 83 84
/*
 * Read event from ring buffer one by one.
 * Return one event for each call.
 *
 * Usage:
 * perf_mmap__read_init()
 * while(event = perf_mmap__read_event()) {
 *	//process the event
 *	perf_mmap__consume()
 * }
 * perf_mmap__read_done()
 */
85
union perf_event *perf_mmap__read_event(struct mmap *map)
86 87 88 89 90 91 92 93 94 95
{
	union perf_event *event;

	/*
	 * Check if event was unmapped due to a POLLHUP/POLLERR.
	 */
	if (!refcount_read(&map->refcnt))
		return NULL;

	/* non-overwirte doesn't pause the ringbuffer */
96 97
	if (!map->overwrite)
		map->end = perf_mmap__read_head(map);
98

99
	event = perf_mmap__read(map, &map->start, map->end);
100

101 102
	if (!map->overwrite)
		map->prev = map->start;
103 104 105 106

	return event;
}

107
static bool perf_mmap__empty(struct mmap *map)
108 109 110 111
{
	return perf_mmap__read_head(map) == map->prev && !map->auxtrace_mmap.base;
}

112
void perf_mmap__get(struct mmap *map)
113 114 115 116
{
	refcount_inc(&map->refcnt);
}

117
void perf_mmap__put(struct mmap *map)
118
{
Jiri Olsa's avatar
Jiri Olsa committed
119
	BUG_ON(map->core.base && refcount_read(&map->refcnt) == 0);
120 121 122 123 124

	if (refcount_dec_and_test(&map->refcnt))
		perf_mmap__munmap(map);
}

125
void perf_mmap__consume(struct mmap *map)
126
{
127
	if (!map->overwrite) {
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
		u64 old = map->prev;

		perf_mmap__write_tail(map, old);
	}

	if (refcount_read(&map->refcnt) == 1 && perf_mmap__empty(map))
		perf_mmap__put(map);
}

int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
			       struct auxtrace_mmap_params *mp __maybe_unused,
			       void *userpg __maybe_unused,
			       int fd __maybe_unused)
{
	return 0;
}

void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
{
}

void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused,
				       off_t auxtrace_offset __maybe_unused,
				       unsigned int auxtrace_pages __maybe_unused,
				       bool auxtrace_overwrite __maybe_unused)
{
}

void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused,
157
					  struct evlist *evlist __maybe_unused,
158 159 160 161 162
					  int idx __maybe_unused,
					  bool per_cpu __maybe_unused)
{
}

163
#ifdef HAVE_AIO_SUPPORT
164
static int perf_mmap__aio_enabled(struct mmap *map)
165 166 167
{
	return map->aio.nr_cblocks > 0;
}
168 169

#ifdef HAVE_LIBNUMA_SUPPORT
170
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
171 172 173 174 175 176 177 178 179 180 181
{
	map->aio.data[idx] = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
				  MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	if (map->aio.data[idx] == MAP_FAILED) {
		map->aio.data[idx] = NULL;
		return -1;
	}

	return 0;
}

182
static void perf_mmap__aio_free(struct mmap *map, int idx)
183 184 185 186 187 188 189
{
	if (map->aio.data[idx]) {
		munmap(map->aio.data[idx], perf_mmap__mmap_len(map));
		map->aio.data[idx] = NULL;
	}
}

190
static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
	void *data;
	size_t mmap_len;
	unsigned long node_mask;

	if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
		data = map->aio.data[idx];
		mmap_len = perf_mmap__mmap_len(map);
		node_mask = 1UL << cpu__get_node(cpu);
		if (mbind(data, mmap_len, MPOL_BIND, &node_mask, 1, 0)) {
			pr_err("Failed to bind [%p-%p] AIO buffer to node %d: error %m\n",
				data, data + mmap_len, cpu__get_node(cpu));
			return -1;
		}
	}

	return 0;
}
209
#else /* !HAVE_LIBNUMA_SUPPORT */
210
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
211 212 213 214 215 216 217 218
{
	map->aio.data[idx] = malloc(perf_mmap__mmap_len(map));
	if (map->aio.data[idx] == NULL)
		return -1;

	return 0;
}

219
static void perf_mmap__aio_free(struct mmap *map, int idx)
220 221 222 223
{
	zfree(&(map->aio.data[idx]));
}

224
static int perf_mmap__aio_bind(struct mmap *map __maybe_unused, int idx __maybe_unused,
225 226 227 228 229 230
		int cpu __maybe_unused, int affinity __maybe_unused)
{
	return 0;
}
#endif

231
static int perf_mmap__aio_mmap(struct mmap *map, struct mmap_params *mp)
232
{
233
	int delta_max, i, prio, ret;
234

235 236
	map->aio.nr_cblocks = mp->nr_cblocks;
	if (map->aio.nr_cblocks) {
237 238 239 240 241 242 243 244 245 246 247
		map->aio.aiocb = calloc(map->aio.nr_cblocks, sizeof(struct aiocb *));
		if (!map->aio.aiocb) {
			pr_debug2("failed to allocate aiocb for data buffer, error %m\n");
			return -1;
		}
		map->aio.cblocks = calloc(map->aio.nr_cblocks, sizeof(struct aiocb));
		if (!map->aio.cblocks) {
			pr_debug2("failed to allocate cblocks for data buffer, error %m\n");
			return -1;
		}
		map->aio.data = calloc(map->aio.nr_cblocks, sizeof(void *));
248 249 250 251 252
		if (!map->aio.data) {
			pr_debug2("failed to allocate data buffer, error %m\n");
			return -1;
		}
		delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
253
		for (i = 0; i < map->aio.nr_cblocks; ++i) {
254 255
			ret = perf_mmap__aio_alloc(map, i);
			if (ret == -1) {
256 257 258
				pr_debug2("failed to allocate data buffer area, error %m");
				return -1;
			}
259
			ret = perf_mmap__aio_bind(map, i, map->core.cpu, mp->affinity);
260 261
			if (ret == -1)
				return -1;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
			/*
			 * Use cblock.aio_fildes value different from -1
			 * to denote started aio write operation on the
			 * cblock so it requires explicit record__aio_sync()
			 * call prior the cblock may be reused again.
			 */
			map->aio.cblocks[i].aio_fildes = -1;
			/*
			 * Allocate cblocks with priority delta to have
			 * faster aio write system calls because queued requests
			 * are kept in separate per-prio queues and adding
			 * a new request will iterate thru shorter per-prio
			 * list. Blocks with numbers higher than
			 *  _SC_AIO_PRIO_DELTA_MAX go with priority 0.
			 */
			prio = delta_max - i;
			map->aio.cblocks[i].aio_reqprio = prio >= 0 ? prio : 0;
		}
280 281 282 283 284
	}

	return 0;
}

285
static void perf_mmap__aio_munmap(struct mmap *map)
286
{
287 288 289
	int i;

	for (i = 0; i < map->aio.nr_cblocks; ++i)
290
		perf_mmap__aio_free(map, i);
291 292
	if (map->aio.data)
		zfree(&map->aio.data);
293 294
	zfree(&map->aio.cblocks);
	zfree(&map->aio.aiocb);
295
}
296
#else /* !HAVE_AIO_SUPPORT */
297
static int perf_mmap__aio_enabled(struct mmap *map __maybe_unused)
298 299 300 301
{
	return 0;
}

302
static int perf_mmap__aio_mmap(struct mmap *map __maybe_unused,
303 304 305 306 307
			       struct mmap_params *mp __maybe_unused)
{
	return 0;
}

308
static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)
309 310 311 312
{
}
#endif

313
void perf_mmap__munmap(struct mmap *map)
314
{
315
	perf_mmap__aio_munmap(map);
316 317 318 319
	if (map->data != NULL) {
		munmap(map->data, perf_mmap__mmap_len(map));
		map->data = NULL;
	}
Jiri Olsa's avatar
Jiri Olsa committed
320 321 322
	if (map->core.base != NULL) {
		munmap(map->core.base, perf_mmap__mmap_len(map));
		map->core.base = NULL;
323
		map->core.fd = -1;
324 325 326 327 328
		refcount_set(&map->refcnt, 0);
	}
	auxtrace_mmap__munmap(&map->auxtrace_mmap);
}

329 330 331
static void build_node_mask(int node, cpu_set_t *mask)
{
	int c, cpu, nr_cpus;
332
	const struct perf_cpu_map *cpu_map = NULL;
333 334 335 336 337

	cpu_map = cpu_map__online();
	if (!cpu_map)
		return;

338
	nr_cpus = perf_cpu_map__nr(cpu_map);
339 340 341 342 343 344 345
	for (c = 0; c < nr_cpus; c++) {
		cpu = cpu_map->map[c]; /* map c index to online cpu index */
		if (cpu__get_node(cpu) == node)
			CPU_SET(cpu, mask);
	}
}

346
static void perf_mmap__setup_affinity_mask(struct mmap *map, struct mmap_params *mp)
347 348 349
{
	CPU_ZERO(&map->affinity_mask);
	if (mp->affinity == PERF_AFFINITY_NODE && cpu__max_node() > 1)
350
		build_node_mask(cpu__get_node(map->core.cpu), &map->affinity_mask);
351
	else if (mp->affinity == PERF_AFFINITY_CPU)
352
		CPU_SET(map->core.cpu, &map->affinity_mask);
353 354
}

355
int perf_mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
356 357
{
	/*
358
	 * The last one will be done at perf_mmap__consume(), so that we
359 360 361 362 363 364 365 366 367 368 369 370 371
	 * make sure we don't prevent tools from consuming every last event in
	 * the ring buffer.
	 *
	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
	 * anymore, but the last events for it are still in the ring buffer,
	 * waiting to be consumed.
	 *
	 * Tools can chose to ignore this at their own discretion, but the
	 * evlist layer can't just drop it when filtering events in
	 * perf_evlist__filter_pollfd().
	 */
	refcount_set(&map->refcnt, 2);
	map->prev = 0;
372
	map->core.mask = mp->mask;
Jiri Olsa's avatar
Jiri Olsa committed
373
	map->core.base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
374
			 MAP_SHARED, fd, 0);
Jiri Olsa's avatar
Jiri Olsa committed
375
	if (map->core.base == MAP_FAILED) {
376 377
		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
			  errno);
Jiri Olsa's avatar
Jiri Olsa committed
378
		map->core.base = NULL;
379 380
		return -1;
	}
381
	map->core.fd = fd;
382
	map->core.cpu = cpu;
383

384
	perf_mmap__setup_affinity_mask(map, mp);
385

386 387
	map->flush = mp->flush;

388 389 390 391 392 393 394 395 396 397 398 399 400
	map->comp_level = mp->comp_level;

	if (map->comp_level && !perf_mmap__aio_enabled(map)) {
		map->data = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
				 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
		if (map->data == MAP_FAILED) {
			pr_debug2("failed to mmap data buffer, error %d\n",
					errno);
			map->data = NULL;
			return -1;
		}
	}

401
	if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
Jiri Olsa's avatar
Jiri Olsa committed
402
				&mp->auxtrace_mp, map->core.base, fd))
403 404
		return -1;

405
	return perf_mmap__aio_mmap(map, mp);
406
}
407

408
static int overwrite_rb_find_range(void *buf, int mask, u64 *start, u64 *end)
409 410
{
	struct perf_event_header *pheader;
411
	u64 evt_head = *start;
412 413
	int size = mask + 1;

414 415
	pr_debug2("%s: buf=%p, start=%"PRIx64"\n", __func__, buf, *start);
	pheader = (struct perf_event_header *)(buf + (*start & mask));
416
	while (true) {
417
		if (evt_head - *start >= (unsigned int)size) {
418
			pr_debug("Finished reading overwrite ring buffer: rewind\n");
419
			if (evt_head - *start > (unsigned int)size)
420 421 422 423 424 425 426 427
				evt_head -= pheader->size;
			*end = evt_head;
			return 0;
		}

		pheader = (struct perf_event_header *)(buf + (evt_head & mask));

		if (pheader->size == 0) {
428
			pr_debug("Finished reading overwrite ring buffer: get start\n");
429 430 431 432 433 434 435 436 437 438 439
			*end = evt_head;
			return 0;
		}

		evt_head += pheader->size;
		pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
	}
	WARN_ONCE(1, "Shouldn't get here\n");
	return -1;
}

440 441 442
/*
 * Report the start and end of the available data in ringbuffer
 */
443
static int __perf_mmap__read_init(struct mmap *md)
444 445 446
{
	u64 head = perf_mmap__read_head(md);
	u64 old = md->prev;
Jiri Olsa's avatar
Jiri Olsa committed
447
	unsigned char *data = md->core.base + page_size;
448 449
	unsigned long size;

450 451
	md->start = md->overwrite ? head : old;
	md->end = md->overwrite ? old : head;
452

453
	if ((md->end - md->start) < md->flush)
454
		return -EAGAIN;
455

456
	size = md->end - md->start;
457
	if (size > (unsigned long)(md->core.mask) + 1) {
458
		if (!md->overwrite) {
459
			WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
460

461
			md->prev = head;
462
			perf_mmap__consume(md);
463
			return -EAGAIN;
464 465 466 467 468 469
		}

		/*
		 * Backward ring buffer is full. We still have a chance to read
		 * most of data from it.
		 */
470
		if (overwrite_rb_find_range(data, md->core.mask, &md->start, &md->end))
471
			return -EINVAL;
472 473
	}

474
	return 0;
475 476
}

477
int perf_mmap__read_init(struct mmap *map)
478 479 480 481 482 483 484 485 486 487
{
	/*
	 * Check if event was unmapped due to a POLLHUP/POLLERR.
	 */
	if (!refcount_read(&map->refcnt))
		return -ENOENT;

	return __perf_mmap__read_init(map);
}

488 489
int perf_mmap__push(struct mmap *md, void *to,
		    int push(struct mmap *map, void *to, void *buf, size_t size))
490 491
{
	u64 head = perf_mmap__read_head(md);
Jiri Olsa's avatar
Jiri Olsa committed
492
	unsigned char *data = md->core.base + page_size;
493 494 495 496
	unsigned long size;
	void *buf;
	int rc = 0;

497
	rc = perf_mmap__read_init(md);
498
	if (rc < 0)
499
		return (rc == -EAGAIN) ? 1 : -1;
500

501
	size = md->end - md->start;
502

503 504 505
	if ((md->start & md->core.mask) + size != (md->end & md->core.mask)) {
		buf = &data[md->start & md->core.mask];
		size = md->core.mask + 1 - (md->start & md->core.mask);
506
		md->start += size;
507

508
		if (push(md, to, buf, size) < 0) {
509 510 511 512 513
			rc = -1;
			goto out;
		}
	}

514
	buf = &data[md->start & md->core.mask];
515 516
	size = md->end - md->start;
	md->start += size;
517

518
	if (push(md, to, buf, size) < 0) {
519 520 521 522 523
		rc = -1;
		goto out;
	}

	md->prev = head;
524
	perf_mmap__consume(md);
525 526 527
out:
	return rc;
}
528 529 530 531 532 533 534

/*
 * Mandatory for overwrite mode
 * The direction of overwrite mode is backward.
 * The last perf_mmap__read() will set tail to map->prev.
 * Need to correct the map->prev to head which is the end of next read.
 */
535
void perf_mmap__read_done(struct mmap *map)
536
{
537 538 539 540 541 542
	/*
	 * Check if event was unmapped due to a POLLHUP/POLLERR.
	 */
	if (!refcount_read(&map->refcnt))
		return;

543 544
	map->prev = perf_mmap__read_head(map);
}