blob: 0e8ff8d1e206f2a2bc9793d37899acae540292de [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -03002/*
3 * Copyright (C) 2011-2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 *
5 * Parts came from evlist.c builtin-{top,stat,record}.c, see those files for further
6 * copyright notes.
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -03007 */
8
9#include <sys/mman.h>
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -030010#include <inttypes.h>
11#include <asm/bug.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030012#include <linux/zalloc.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030013#include <stdlib.h>
14#include <string.h>
Arnaldo Carvalho de Melo7634d532019-09-23 18:06:52 -030015#include <unistd.h> // sysconf()
Jiri Olsa7728fa02019-10-07 14:53:17 +020016#include <perf/mmap.h>
Alexey Budankovc44a8b42019-01-22 20:48:54 +030017#ifdef HAVE_LIBNUMA_SUPPORT
18#include <numaif.h>
19#endif
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030020#include "cpumap.h"
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -030021#include "debug.h"
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030022#include "event.h"
23#include "mmap.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030024#include "../perf.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020025#include <internal/lib.h> /* page_size */
Alexey Budankov9c080c02019-12-03 14:44:18 +030026#include <linux/bitmap.h>
27
28#define MASK_SIZE 1023
29void mmap_cpu_mask__scnprintf(struct mmap_cpu_mask *mask, const char *tag)
30{
31 char buf[MASK_SIZE + 1];
32 size_t len;
33
34 len = bitmap_scnprintf(mask->bits, mask->nbits, buf, MASK_SIZE);
35 buf[len] = '\0';
36 pr_debug("%p: %s mask[%zd]: %s\n", mask, tag, mask->nbits, buf);
37}
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030038
Jiri Olsabf59b302019-10-07 14:53:11 +020039size_t mmap__mmap_len(struct mmap *map)
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030040{
Jiri Olsabf59b302019-10-07 14:53:11 +020041 return perf_mmap__mmap_len(&map->core);
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030042}
43
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030044int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
45 struct auxtrace_mmap_params *mp __maybe_unused,
46 void *userpg __maybe_unused,
47 int fd __maybe_unused)
48{
49 return 0;
50}
51
52void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
53{
54}
55
56void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused,
57 off_t auxtrace_offset __maybe_unused,
58 unsigned int auxtrace_pages __maybe_unused,
59 bool auxtrace_overwrite __maybe_unused)
60{
61}
62
63void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +020064 struct evlist *evlist __maybe_unused,
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -030065 int idx __maybe_unused,
66 bool per_cpu __maybe_unused)
67{
68}
69
Alexey Budankov0b773832018-11-06 12:03:35 +030070#ifdef HAVE_AIO_SUPPORT
Jiri Olsaa5830532019-07-27 20:30:53 +020071static int perf_mmap__aio_enabled(struct mmap *map)
Alexey Budankov51255a82019-03-18 20:42:19 +030072{
73 return map->aio.nr_cblocks > 0;
74}
Alexey Budankovc44a8b42019-01-22 20:48:54 +030075
76#ifdef HAVE_LIBNUMA_SUPPORT
Jiri Olsaa5830532019-07-27 20:30:53 +020077static int perf_mmap__aio_alloc(struct mmap *map, int idx)
Alexey Budankovc44a8b42019-01-22 20:48:54 +030078{
Jiri Olsabf59b302019-10-07 14:53:11 +020079 map->aio.data[idx] = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
Alexey Budankovc44a8b42019-01-22 20:48:54 +030080 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
81 if (map->aio.data[idx] == MAP_FAILED) {
82 map->aio.data[idx] = NULL;
83 return -1;
84 }
85
86 return 0;
87}
88
Jiri Olsaa5830532019-07-27 20:30:53 +020089static void perf_mmap__aio_free(struct mmap *map, int idx)
Alexey Budankovc44a8b42019-01-22 20:48:54 +030090{
91 if (map->aio.data[idx]) {
Jiri Olsabf59b302019-10-07 14:53:11 +020092 munmap(map->aio.data[idx], mmap__mmap_len(map));
Alexey Budankovc44a8b42019-01-22 20:48:54 +030093 map->aio.data[idx] = NULL;
94 }
95}
96
Ian Rogers6d188042022-01-04 22:13:51 -080097static int perf_mmap__aio_bind(struct mmap *map, int idx, struct perf_cpu cpu, int affinity)
Alexey Budankovc44a8b42019-01-22 20:48:54 +030098{
99 void *data;
100 size_t mmap_len;
Alexey Budankov44d462a2020-03-12 15:21:45 +0300101 unsigned long *node_mask;
102 unsigned long node_index;
103 int err = 0;
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300104
105 if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
106 data = map->aio.data[idx];
Jiri Olsabf59b302019-10-07 14:53:11 +0200107 mmap_len = mmap__mmap_len(map);
Alexey Budankov44d462a2020-03-12 15:21:45 +0300108 node_index = cpu__get_node(cpu);
Andy Shevchenko7fc5b572021-09-07 19:59:35 -0700109 node_mask = bitmap_zalloc(node_index + 1);
Alexey Budankov44d462a2020-03-12 15:21:45 +0300110 if (!node_mask) {
111 pr_err("Failed to allocate node mask for mbind: error %m\n");
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300112 return -1;
113 }
Alexey Budankov44d462a2020-03-12 15:21:45 +0300114 set_bit(node_index, node_mask);
115 if (mbind(data, mmap_len, MPOL_BIND, node_mask, node_index + 1 + 1, 0)) {
116 pr_err("Failed to bind [%p-%p] AIO buffer to node %lu: error %m\n",
117 data, data + mmap_len, node_index);
118 err = -1;
119 }
120 bitmap_free(node_mask);
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300121 }
122
Alexey Budankov44d462a2020-03-12 15:21:45 +0300123 return err;
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300124}
Alexey Budankov51255a82019-03-18 20:42:19 +0300125#else /* !HAVE_LIBNUMA_SUPPORT */
Jiri Olsaa5830532019-07-27 20:30:53 +0200126static int perf_mmap__aio_alloc(struct mmap *map, int idx)
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300127{
Jiri Olsabf59b302019-10-07 14:53:11 +0200128 map->aio.data[idx] = malloc(mmap__mmap_len(map));
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300129 if (map->aio.data[idx] == NULL)
130 return -1;
131
132 return 0;
133}
134
Jiri Olsaa5830532019-07-27 20:30:53 +0200135static void perf_mmap__aio_free(struct mmap *map, int idx)
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300136{
137 zfree(&(map->aio.data[idx]));
138}
139
Jiri Olsaa5830532019-07-27 20:30:53 +0200140static int perf_mmap__aio_bind(struct mmap *map __maybe_unused, int idx __maybe_unused,
Ian Rogers6d188042022-01-04 22:13:51 -0800141 struct perf_cpu cpu __maybe_unused, int affinity __maybe_unused)
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300142{
143 return 0;
144}
145#endif
146
Jiri Olsaa5830532019-07-27 20:30:53 +0200147static int perf_mmap__aio_mmap(struct mmap *map, struct mmap_params *mp)
Alexey Budankov0b773832018-11-06 12:03:35 +0300148{
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300149 int delta_max, i, prio, ret;
Alexey Budankov0b773832018-11-06 12:03:35 +0300150
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300151 map->aio.nr_cblocks = mp->nr_cblocks;
152 if (map->aio.nr_cblocks) {
Alexey Budankov93f20c02018-11-06 12:07:19 +0300153 map->aio.aiocb = calloc(map->aio.nr_cblocks, sizeof(struct aiocb *));
154 if (!map->aio.aiocb) {
155 pr_debug2("failed to allocate aiocb for data buffer, error %m\n");
156 return -1;
157 }
158 map->aio.cblocks = calloc(map->aio.nr_cblocks, sizeof(struct aiocb));
159 if (!map->aio.cblocks) {
160 pr_debug2("failed to allocate cblocks for data buffer, error %m\n");
161 return -1;
162 }
163 map->aio.data = calloc(map->aio.nr_cblocks, sizeof(void *));
Alexey Budankov0b773832018-11-06 12:03:35 +0300164 if (!map->aio.data) {
165 pr_debug2("failed to allocate data buffer, error %m\n");
166 return -1;
167 }
Alexey Budankov0b773832018-11-06 12:03:35 +0300168 delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
Alexey Budankov93f20c02018-11-06 12:07:19 +0300169 for (i = 0; i < map->aio.nr_cblocks; ++i) {
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300170 ret = perf_mmap__aio_alloc(map, i);
171 if (ret == -1) {
Alexey Budankov93f20c02018-11-06 12:07:19 +0300172 pr_debug2("failed to allocate data buffer area, error %m");
173 return -1;
174 }
Jiri Olsa56a94702019-07-27 22:33:20 +0200175 ret = perf_mmap__aio_bind(map, i, map->core.cpu, mp->affinity);
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300176 if (ret == -1)
177 return -1;
Alexey Budankov93f20c02018-11-06 12:07:19 +0300178 /*
179 * Use cblock.aio_fildes value different from -1
180 * to denote started aio write operation on the
181 * cblock so it requires explicit record__aio_sync()
182 * call prior the cblock may be reused again.
183 */
184 map->aio.cblocks[i].aio_fildes = -1;
185 /*
186 * Allocate cblocks with priority delta to have
187 * faster aio write system calls because queued requests
188 * are kept in separate per-prio queues and adding
189 * a new request will iterate thru shorter per-prio
190 * list. Blocks with numbers higher than
191 * _SC_AIO_PRIO_DELTA_MAX go with priority 0.
192 */
193 prio = delta_max - i;
194 map->aio.cblocks[i].aio_reqprio = prio >= 0 ? prio : 0;
195 }
Alexey Budankov0b773832018-11-06 12:03:35 +0300196 }
197
198 return 0;
199}
200
Jiri Olsaa5830532019-07-27 20:30:53 +0200201static void perf_mmap__aio_munmap(struct mmap *map)
Alexey Budankov0b773832018-11-06 12:03:35 +0300202{
Alexey Budankovc8dd6ee2018-12-05 20:19:41 +0300203 int i;
204
205 for (i = 0; i < map->aio.nr_cblocks; ++i)
Alexey Budankovc44a8b42019-01-22 20:48:54 +0300206 perf_mmap__aio_free(map, i);
Alexey Budankov0b773832018-11-06 12:03:35 +0300207 if (map->aio.data)
208 zfree(&map->aio.data);
Alexey Budankovc8dd6ee2018-12-05 20:19:41 +0300209 zfree(&map->aio.cblocks);
210 zfree(&map->aio.aiocb);
Alexey Budankov0b773832018-11-06 12:03:35 +0300211}
Alexey Budankov51255a82019-03-18 20:42:19 +0300212#else /* !HAVE_AIO_SUPPORT */
Jiri Olsaa5830532019-07-27 20:30:53 +0200213static int perf_mmap__aio_enabled(struct mmap *map __maybe_unused)
Alexey Budankov51255a82019-03-18 20:42:19 +0300214{
215 return 0;
216}
217
Jiri Olsaa5830532019-07-27 20:30:53 +0200218static int perf_mmap__aio_mmap(struct mmap *map __maybe_unused,
Alexey Budankov0b773832018-11-06 12:03:35 +0300219 struct mmap_params *mp __maybe_unused)
220{
221 return 0;
222}
223
Jiri Olsaa5830532019-07-27 20:30:53 +0200224static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)
Alexey Budankov0b773832018-11-06 12:03:35 +0300225{
226}
227#endif
228
Jiri Olsa59d7ea622019-10-07 14:53:14 +0200229void mmap__munmap(struct mmap *map)
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300230{
Alexey Budankov8384a262019-12-03 14:45:27 +0300231 bitmap_free(map->affinity_mask.bits);
232
Alexey Budankov0b773832018-11-06 12:03:35 +0300233 perf_mmap__aio_munmap(map);
Alexey Budankov51255a82019-03-18 20:42:19 +0300234 if (map->data != NULL) {
Jiri Olsabf59b302019-10-07 14:53:11 +0200235 munmap(map->data, mmap__mmap_len(map));
Alexey Budankov51255a82019-03-18 20:42:19 +0300236 map->data = NULL;
237 }
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300238 auxtrace_mmap__munmap(&map->auxtrace_mmap);
239}
240
Alexey Budankov8384a262019-12-03 14:45:27 +0300241static void build_node_mask(int node, struct mmap_cpu_mask *mask)
Alexey Budankovf13de662019-01-22 20:50:57 +0300242{
Ian Rogers6d188042022-01-04 22:13:51 -0800243 int idx, nr_cpus;
244 struct perf_cpu cpu;
Jiri Olsaf8548392019-07-21 13:23:49 +0200245 const struct perf_cpu_map *cpu_map = NULL;
Alexey Budankovf13de662019-01-22 20:50:57 +0300246
247 cpu_map = cpu_map__online();
248 if (!cpu_map)
249 return;
250
Jiri Olsa6549cd82019-08-22 13:11:38 +0200251 nr_cpus = perf_cpu_map__nr(cpu_map);
Ian Rogers6d188042022-01-04 22:13:51 -0800252 for (idx = 0; idx < nr_cpus; idx++) {
Ian Rogers44028692022-01-21 20:58:10 -0800253 cpu = perf_cpu_map__cpu(cpu_map, idx); /* map c index to online cpu index */
Alexey Budankovf13de662019-01-22 20:50:57 +0300254 if (cpu__get_node(cpu) == node)
Ian Rogers6d188042022-01-04 22:13:51 -0800255 set_bit(cpu.cpu, mask->bits);
Alexey Budankovf13de662019-01-22 20:50:57 +0300256 }
257}
258
Alexey Budankov8384a262019-12-03 14:45:27 +0300259static int perf_mmap__setup_affinity_mask(struct mmap *map, struct mmap_params *mp)
Alexey Budankovf13de662019-01-22 20:50:57 +0300260{
Ian Rogers6d188042022-01-04 22:13:51 -0800261 map->affinity_mask.nbits = cpu__max_cpu().cpu;
Andy Shevchenko7fc5b572021-09-07 19:59:35 -0700262 map->affinity_mask.bits = bitmap_zalloc(map->affinity_mask.nbits);
Alexey Budankov8384a262019-12-03 14:45:27 +0300263 if (!map->affinity_mask.bits)
264 return -1;
265
Alexey Budankovf13de662019-01-22 20:50:57 +0300266 if (mp->affinity == PERF_AFFINITY_NODE && cpu__max_node() > 1)
Jiri Olsa56a94702019-07-27 22:33:20 +0200267 build_node_mask(cpu__get_node(map->core.cpu), &map->affinity_mask);
Alexey Budankovf13de662019-01-22 20:50:57 +0300268 else if (mp->affinity == PERF_AFFINITY_CPU)
Ian Rogers6d188042022-01-04 22:13:51 -0800269 set_bit(map->core.cpu.cpu, map->affinity_mask.bits);
Alexey Budankov8384a262019-12-03 14:45:27 +0300270
271 return 0;
Alexey Budankovf13de662019-01-22 20:50:57 +0300272}
273
Ian Rogers6d188042022-01-04 22:13:51 -0800274int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, struct perf_cpu cpu)
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300275{
Jiri Olsa32c261c2019-10-07 14:53:12 +0200276 if (perf_mmap__mmap(&map->core, &mp->core, fd, cpu)) {
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300277 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
278 errno);
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300279 return -1;
280 }
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300281
Alexey Budankov8384a262019-12-03 14:45:27 +0300282 if (mp->affinity != PERF_AFFINITY_SYS &&
283 perf_mmap__setup_affinity_mask(map, mp)) {
284 pr_debug2("failed to alloc mmap affinity mask, error %d\n",
285 errno);
286 return -1;
287 }
288
289 if (verbose == 2)
290 mmap_cpu_mask__scnprintf(&map->affinity_mask, "mmap");
Alexey Budankov9d2ed642019-01-22 20:47:43 +0300291
Jiri Olsa65aa2e62019-08-27 16:05:18 +0200292 map->core.flush = mp->flush;
Alexey Budankov470530b2019-03-18 20:40:26 +0300293
Alexey Budankov51255a82019-03-18 20:42:19 +0300294 map->comp_level = mp->comp_level;
295
296 if (map->comp_level && !perf_mmap__aio_enabled(map)) {
Jiri Olsabf59b302019-10-07 14:53:11 +0200297 map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
Alexey Budankov51255a82019-03-18 20:42:19 +0300298 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
299 if (map->data == MAP_FAILED) {
300 pr_debug2("failed to mmap data buffer, error %d\n",
301 errno);
302 map->data = NULL;
303 return -1;
304 }
305 }
306
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300307 if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
Jiri Olsa547740f2019-07-27 22:07:44 +0200308 &mp->auxtrace_mp, map->core.base, fd))
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300309 return -1;
310
Alexey Budankov0b773832018-11-06 12:03:35 +0300311 return perf_mmap__aio_mmap(map, mp);
Arnaldo Carvalho de Melo16958492017-10-06 10:31:47 -0300312}
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300313
Jiri Olsaa5830532019-07-27 20:30:53 +0200314int perf_mmap__push(struct mmap *md, void *to,
315 int push(struct mmap *map, void *to, void *buf, size_t size))
Kan Liang88724812018-01-18 13:26:19 -0800316{
Jiri Olsa7728fa02019-10-07 14:53:17 +0200317 u64 head = perf_mmap__read_head(&md->core);
Jiri Olsa547740f2019-07-27 22:07:44 +0200318 unsigned char *data = md->core.base + page_size;
Kan Liang88724812018-01-18 13:26:19 -0800319 unsigned long size;
320 void *buf;
321 int rc = 0;
322
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200323 rc = perf_mmap__read_init(&md->core);
Kan Liang189f2cc2018-01-18 13:26:20 -0800324 if (rc < 0)
Alexey Budankovef781122019-03-18 20:44:12 +0300325 return (rc == -EAGAIN) ? 1 : -1;
Kan Liang88724812018-01-18 13:26:19 -0800326
Jiri Olsaebe4d722019-07-27 22:39:53 +0200327 size = md->core.end - md->core.start;
Kan Liangdc6c35c2018-01-18 13:26:17 -0800328
Jiri Olsaebe4d722019-07-27 22:39:53 +0200329 if ((md->core.start & md->core.mask) + size != (md->core.end & md->core.mask)) {
330 buf = &data[md->core.start & md->core.mask];
331 size = md->core.mask + 1 - (md->core.start & md->core.mask);
332 md->core.start += size;
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300333
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200334 if (push(md, to, buf, size) < 0) {
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300335 rc = -1;
336 goto out;
337 }
338 }
339
Jiri Olsaebe4d722019-07-27 22:39:53 +0200340 buf = &data[md->core.start & md->core.mask];
341 size = md->core.end - md->core.start;
342 md->core.start += size;
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300343
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200344 if (push(md, to, buf, size) < 0) {
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300345 rc = -1;
346 goto out;
347 }
348
Jiri Olsaebe4d722019-07-27 22:39:53 +0200349 md->core.prev = head;
Jiri Olsa7728fa02019-10-07 14:53:17 +0200350 perf_mmap__consume(&md->core);
Arnaldo Carvalho de Melo73c17d82017-10-06 10:46:01 -0300351out:
352 return rc;
353}
Riccardo Mancini6bd006c2021-08-21 11:19:10 +0200354
355int mmap_cpu_mask__duplicate(struct mmap_cpu_mask *original, struct mmap_cpu_mask *clone)
356{
357 clone->nbits = original->nbits;
358 clone->bits = bitmap_zalloc(original->nbits);
359 if (!clone->bits)
360 return -ENOMEM;
361
362 memcpy(clone->bits, original->bits, MMAP_CPU_MASK_BYTES(original));
363 return 0;
364}