1 // SPDX-License-Identifier: GPL-2.0-only
5 * Parts came from evlist.c builtin-{top,stat,record}.c, see those files for further
12 #include <linux/zalloc.h>
15 #include <unistd.h> // sysconf()
16 #include <perf/mmap.h>
17 #ifdef HAVE_LIBNUMA_SUPPORT
25 #include <internal/lib.h> /* page_size */
27 size_t mmap__mmap_len(struct mmap *map)
29 return perf_mmap__mmap_len(&map->core);
32 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
33 struct auxtrace_mmap_params *mp __maybe_unused,
34 void *userpg __maybe_unused,
35 int fd __maybe_unused)
40 void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
44 void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused,
45 off_t auxtrace_offset __maybe_unused,
46 unsigned int auxtrace_pages __maybe_unused,
47 bool auxtrace_overwrite __maybe_unused)
51 void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused,
52 struct evlist *evlist __maybe_unused,
53 int idx __maybe_unused,
54 bool per_cpu __maybe_unused)
58 #ifdef HAVE_AIO_SUPPORT
59 static int perf_mmap__aio_enabled(struct mmap *map)
61 return map->aio.nr_cblocks > 0;
64 #ifdef HAVE_LIBNUMA_SUPPORT
65 static int perf_mmap__aio_alloc(struct mmap *map, int idx)
67 map->aio.data[idx] = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
68 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
69 if (map->aio.data[idx] == MAP_FAILED) {
70 map->aio.data[idx] = NULL;
77 static void perf_mmap__aio_free(struct mmap *map, int idx)
79 if (map->aio.data[idx]) {
80 munmap(map->aio.data[idx], mmap__mmap_len(map));
81 map->aio.data[idx] = NULL;
85 static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)
89 unsigned long node_mask;
91 if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
92 data = map->aio.data[idx];
93 mmap_len = mmap__mmap_len(map);
94 node_mask = 1UL << cpu__get_node(cpu);
95 if (mbind(data, mmap_len, MPOL_BIND, &node_mask, 1, 0)) {
96 pr_err("Failed to bind [%p-%p] AIO buffer to node %d: error %m\n",
97 data, data + mmap_len, cpu__get_node(cpu));
104 #else /* !HAVE_LIBNUMA_SUPPORT */
105 static int perf_mmap__aio_alloc(struct mmap *map, int idx)
107 map->aio.data[idx] = malloc(mmap__mmap_len(map));
108 if (map->aio.data[idx] == NULL)
114 static void perf_mmap__aio_free(struct mmap *map, int idx)
116 zfree(&(map->aio.data[idx]));
119 static int perf_mmap__aio_bind(struct mmap *map __maybe_unused, int idx __maybe_unused,
120 int cpu __maybe_unused, int affinity __maybe_unused)
126 static int perf_mmap__aio_mmap(struct mmap *map, struct mmap_params *mp)
128 int delta_max, i, prio, ret;
130 map->aio.nr_cblocks = mp->nr_cblocks;
131 if (map->aio.nr_cblocks) {
132 map->aio.aiocb = calloc(map->aio.nr_cblocks, sizeof(struct aiocb *));
133 if (!map->aio.aiocb) {
134 pr_debug2("failed to allocate aiocb for data buffer, error %m\n");
137 map->aio.cblocks = calloc(map->aio.nr_cblocks, sizeof(struct aiocb));
138 if (!map->aio.cblocks) {
139 pr_debug2("failed to allocate cblocks for data buffer, error %m\n");
142 map->aio.data = calloc(map->aio.nr_cblocks, sizeof(void *));
143 if (!map->aio.data) {
144 pr_debug2("failed to allocate data buffer, error %m\n");
147 delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
148 for (i = 0; i < map->aio.nr_cblocks; ++i) {
149 ret = perf_mmap__aio_alloc(map, i);
151 pr_debug2("failed to allocate data buffer area, error %m");
154 ret = perf_mmap__aio_bind(map, i, map->core.cpu, mp->affinity);
158 * Use cblock.aio_fildes value different from -1
159 * to denote started aio write operation on the
160 * cblock so it requires explicit record__aio_sync()
161 * call prior the cblock may be reused again.
163 map->aio.cblocks[i].aio_fildes = -1;
165 * Allocate cblocks with priority delta to have
166 * faster aio write system calls because queued requests
167 * are kept in separate per-prio queues and adding
168 * a new request will iterate thru shorter per-prio
169 * list. Blocks with numbers higher than
170 * _SC_AIO_PRIO_DELTA_MAX go with priority 0.
172 prio = delta_max - i;
173 map->aio.cblocks[i].aio_reqprio = prio >= 0 ? prio : 0;
180 static void perf_mmap__aio_munmap(struct mmap *map)
184 for (i = 0; i < map->aio.nr_cblocks; ++i)
185 perf_mmap__aio_free(map, i);
187 zfree(&map->aio.data);
188 zfree(&map->aio.cblocks);
189 zfree(&map->aio.aiocb);
191 #else /* !HAVE_AIO_SUPPORT */
192 static int perf_mmap__aio_enabled(struct mmap *map __maybe_unused)
197 static int perf_mmap__aio_mmap(struct mmap *map __maybe_unused,
198 struct mmap_params *mp __maybe_unused)
203 static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)
208 void mmap__munmap(struct mmap *map)
210 perf_mmap__aio_munmap(map);
211 if (map->data != NULL) {
212 munmap(map->data, mmap__mmap_len(map));
215 auxtrace_mmap__munmap(&map->auxtrace_mmap);
218 static void build_node_mask(int node, cpu_set_t *mask)
221 const struct perf_cpu_map *cpu_map = NULL;
223 cpu_map = cpu_map__online();
227 nr_cpus = perf_cpu_map__nr(cpu_map);
228 for (c = 0; c < nr_cpus; c++) {
229 cpu = cpu_map->map[c]; /* map c index to online cpu index */
230 if (cpu__get_node(cpu) == node)
235 static void perf_mmap__setup_affinity_mask(struct mmap *map, struct mmap_params *mp)
237 CPU_ZERO(&map->affinity_mask);
238 if (mp->affinity == PERF_AFFINITY_NODE && cpu__max_node() > 1)
239 build_node_mask(cpu__get_node(map->core.cpu), &map->affinity_mask);
240 else if (mp->affinity == PERF_AFFINITY_CPU)
241 CPU_SET(map->core.cpu, &map->affinity_mask);
244 int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
246 if (perf_mmap__mmap(&map->core, &mp->core, fd, cpu)) {
247 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
252 perf_mmap__setup_affinity_mask(map, mp);
254 map->core.flush = mp->flush;
256 map->comp_level = mp->comp_level;
258 if (map->comp_level && !perf_mmap__aio_enabled(map)) {
259 map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
260 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
261 if (map->data == MAP_FAILED) {
262 pr_debug2("failed to mmap data buffer, error %d\n",
269 if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
270 &mp->auxtrace_mp, map->core.base, fd))
273 return perf_mmap__aio_mmap(map, mp);
276 int perf_mmap__push(struct mmap *md, void *to,
277 int push(struct mmap *map, void *to, void *buf, size_t size))
279 u64 head = perf_mmap__read_head(&md->core);
280 unsigned char *data = md->core.base + page_size;
285 rc = perf_mmap__read_init(&md->core);
287 return (rc == -EAGAIN) ? 1 : -1;
289 size = md->core.end - md->core.start;
291 if ((md->core.start & md->core.mask) + size != (md->core.end & md->core.mask)) {
292 buf = &data[md->core.start & md->core.mask];
293 size = md->core.mask + 1 - (md->core.start & md->core.mask);
294 md->core.start += size;
296 if (push(md, to, buf, size) < 0) {
302 buf = &data[md->core.start & md->core.mask];
303 size = md->core.end - md->core.start;
304 md->core.start += size;
306 if (push(md, to, buf, size) < 0) {
311 md->core.prev = head;
312 perf_mmap__consume(&md->core);