mmap.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  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.
  7. */
  8. #include <sys/mman.h>
  9. #include <errno.h>
  10. #include <inttypes.h>
  11. #include <asm/bug.h>
  12. #include <linux/zalloc.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h> // sysconf()
  16. #include <perf/mmap.h>
  17. #ifdef HAVE_LIBNUMA_SUPPORT
  18. #include <numaif.h>
  19. #endif
  20. #include "cpumap.h"
  21. #include "debug.h"
  22. #include "event.h"
  23. #include "mmap.h"
  24. #include "../perf.h"
  25. #include <internal/lib.h> /* page_size */
  26. #include <linux/bitmap.h>
  27. #define MASK_SIZE 1023
  28. void mmap_cpu_mask__scnprintf(struct mmap_cpu_mask *mask, const char *tag)
  29. {
  30. char buf[MASK_SIZE + 1];
  31. size_t len;
  32. len = bitmap_scnprintf(mask->bits, mask->nbits, buf, MASK_SIZE);
  33. buf[len] = '\0';
  34. pr_debug("%p: %s mask[%zd]: %s\n", mask, tag, mask->nbits, buf);
  35. }
  36. size_t mmap__mmap_len(struct mmap *map)
  37. {
  38. return perf_mmap__mmap_len(&map->core);
  39. }
  40. int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
  41. struct auxtrace_mmap_params *mp __maybe_unused,
  42. void *userpg __maybe_unused,
  43. int fd __maybe_unused)
  44. {
  45. return 0;
  46. }
  47. void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
  48. {
  49. }
  50. void __weak auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp __maybe_unused,
  51. off_t auxtrace_offset __maybe_unused,
  52. unsigned int auxtrace_pages __maybe_unused,
  53. bool auxtrace_overwrite __maybe_unused)
  54. {
  55. }
  56. void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __maybe_unused,
  57. struct evlist *evlist __maybe_unused,
  58. struct evsel *evsel __maybe_unused,
  59. int idx __maybe_unused)
  60. {
  61. }
  62. #ifdef HAVE_AIO_SUPPORT
  63. static int perf_mmap__aio_enabled(struct mmap *map)
  64. {
  65. return map->aio.nr_cblocks > 0;
  66. }
  67. #ifdef HAVE_LIBNUMA_SUPPORT
  68. static int perf_mmap__aio_alloc(struct mmap *map, int idx)
  69. {
  70. map->aio.data[idx] = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
  71. MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  72. if (map->aio.data[idx] == MAP_FAILED) {
  73. map->aio.data[idx] = NULL;
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. static void perf_mmap__aio_free(struct mmap *map, int idx)
  79. {
  80. if (map->aio.data[idx]) {
  81. munmap(map->aio.data[idx], mmap__mmap_len(map));
  82. map->aio.data[idx] = NULL;
  83. }
  84. }
  85. static int perf_mmap__aio_bind(struct mmap *map, int idx, struct perf_cpu cpu, int affinity)
  86. {
  87. void *data;
  88. size_t mmap_len;
  89. unsigned long *node_mask;
  90. unsigned long node_index;
  91. int err = 0;
  92. if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
  93. data = map->aio.data[idx];
  94. mmap_len = mmap__mmap_len(map);
  95. node_index = cpu__get_node(cpu);
  96. node_mask = bitmap_zalloc(node_index + 1);
  97. if (!node_mask) {
  98. pr_err("Failed to allocate node mask for mbind: error %m\n");
  99. return -1;
  100. }
  101. __set_bit(node_index, node_mask);
  102. if (mbind(data, mmap_len, MPOL_BIND, node_mask, node_index + 1 + 1, 0)) {
  103. pr_err("Failed to bind [%p-%p] AIO buffer to node %lu: error %m\n",
  104. data, data + mmap_len, node_index);
  105. err = -1;
  106. }
  107. bitmap_free(node_mask);
  108. }
  109. return err;
  110. }
  111. #else /* !HAVE_LIBNUMA_SUPPORT */
  112. static int perf_mmap__aio_alloc(struct mmap *map, int idx)
  113. {
  114. map->aio.data[idx] = malloc(mmap__mmap_len(map));
  115. if (map->aio.data[idx] == NULL)
  116. return -1;
  117. return 0;
  118. }
  119. static void perf_mmap__aio_free(struct mmap *map, int idx)
  120. {
  121. zfree(&(map->aio.data[idx]));
  122. }
  123. static int perf_mmap__aio_bind(struct mmap *map __maybe_unused, int idx __maybe_unused,
  124. struct perf_cpu cpu __maybe_unused, int affinity __maybe_unused)
  125. {
  126. return 0;
  127. }
  128. #endif
  129. static int perf_mmap__aio_mmap(struct mmap *map, struct mmap_params *mp)
  130. {
  131. int delta_max, i, prio, ret;
  132. map->aio.nr_cblocks = mp->nr_cblocks;
  133. if (map->aio.nr_cblocks) {
  134. map->aio.aiocb = calloc(map->aio.nr_cblocks, sizeof(struct aiocb *));
  135. if (!map->aio.aiocb) {
  136. pr_debug2("failed to allocate aiocb for data buffer, error %m\n");
  137. return -1;
  138. }
  139. map->aio.cblocks = calloc(map->aio.nr_cblocks, sizeof(struct aiocb));
  140. if (!map->aio.cblocks) {
  141. pr_debug2("failed to allocate cblocks for data buffer, error %m\n");
  142. return -1;
  143. }
  144. map->aio.data = calloc(map->aio.nr_cblocks, sizeof(void *));
  145. if (!map->aio.data) {
  146. pr_debug2("failed to allocate data buffer, error %m\n");
  147. return -1;
  148. }
  149. delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
  150. for (i = 0; i < map->aio.nr_cblocks; ++i) {
  151. ret = perf_mmap__aio_alloc(map, i);
  152. if (ret == -1) {
  153. pr_debug2("failed to allocate data buffer area, error %m");
  154. return -1;
  155. }
  156. ret = perf_mmap__aio_bind(map, i, map->core.cpu, mp->affinity);
  157. if (ret == -1)
  158. return -1;
  159. /*
  160. * Use cblock.aio_fildes value different from -1
  161. * to denote started aio write operation on the
  162. * cblock so it requires explicit record__aio_sync()
  163. * call prior the cblock may be reused again.
  164. */
  165. map->aio.cblocks[i].aio_fildes = -1;
  166. /*
  167. * Allocate cblocks with priority delta to have
  168. * faster aio write system calls because queued requests
  169. * are kept in separate per-prio queues and adding
  170. * a new request will iterate thru shorter per-prio
  171. * list. Blocks with numbers higher than
  172. * _SC_AIO_PRIO_DELTA_MAX go with priority 0.
  173. */
  174. prio = delta_max - i;
  175. map->aio.cblocks[i].aio_reqprio = prio >= 0 ? prio : 0;
  176. }
  177. }
  178. return 0;
  179. }
  180. static void perf_mmap__aio_munmap(struct mmap *map)
  181. {
  182. int i;
  183. for (i = 0; i < map->aio.nr_cblocks; ++i)
  184. perf_mmap__aio_free(map, i);
  185. if (map->aio.data)
  186. zfree(&map->aio.data);
  187. zfree(&map->aio.cblocks);
  188. zfree(&map->aio.aiocb);
  189. }
  190. #else /* !HAVE_AIO_SUPPORT */
  191. static int perf_mmap__aio_enabled(struct mmap *map __maybe_unused)
  192. {
  193. return 0;
  194. }
  195. static int perf_mmap__aio_mmap(struct mmap *map __maybe_unused,
  196. struct mmap_params *mp __maybe_unused)
  197. {
  198. return 0;
  199. }
  200. static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)
  201. {
  202. }
  203. #endif
  204. void mmap__munmap(struct mmap *map)
  205. {
  206. bitmap_free(map->affinity_mask.bits);
  207. zstd_fini(&map->zstd_data);
  208. perf_mmap__aio_munmap(map);
  209. if (map->data != NULL) {
  210. munmap(map->data, mmap__mmap_len(map));
  211. map->data = NULL;
  212. }
  213. auxtrace_mmap__munmap(&map->auxtrace_mmap);
  214. }
  215. static void build_node_mask(int node, struct mmap_cpu_mask *mask)
  216. {
  217. int idx, nr_cpus;
  218. struct perf_cpu cpu;
  219. struct perf_cpu_map *cpu_map = cpu_map__online();
  220. if (!cpu_map)
  221. return;
  222. nr_cpus = perf_cpu_map__nr(cpu_map);
  223. for (idx = 0; idx < nr_cpus; idx++) {
  224. cpu = perf_cpu_map__cpu(cpu_map, idx); /* map c index to online cpu index */
  225. if (cpu__get_node(cpu) == node)
  226. __set_bit(cpu.cpu, mask->bits);
  227. }
  228. perf_cpu_map__put(cpu_map);
  229. }
  230. static int perf_mmap__setup_affinity_mask(struct mmap *map, struct mmap_params *mp)
  231. {
  232. map->affinity_mask.nbits = cpu__max_cpu().cpu;
  233. map->affinity_mask.bits = bitmap_zalloc(map->affinity_mask.nbits);
  234. if (!map->affinity_mask.bits)
  235. return -1;
  236. if (mp->affinity == PERF_AFFINITY_NODE && cpu__max_node() > 1)
  237. build_node_mask(cpu__get_node(map->core.cpu), &map->affinity_mask);
  238. else if (mp->affinity == PERF_AFFINITY_CPU)
  239. __set_bit(map->core.cpu.cpu, map->affinity_mask.bits);
  240. return 0;
  241. }
  242. int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, struct perf_cpu cpu)
  243. {
  244. if (perf_mmap__mmap(&map->core, &mp->core, fd, cpu)) {
  245. pr_debug2("failed to mmap perf event ring buffer, error %d\n",
  246. errno);
  247. return -1;
  248. }
  249. if (mp->affinity != PERF_AFFINITY_SYS &&
  250. perf_mmap__setup_affinity_mask(map, mp)) {
  251. pr_debug2("failed to alloc mmap affinity mask, error %d\n",
  252. errno);
  253. return -1;
  254. }
  255. if (verbose == 2)
  256. mmap_cpu_mask__scnprintf(&map->affinity_mask, "mmap");
  257. map->core.flush = mp->flush;
  258. if (zstd_init(&map->zstd_data, mp->comp_level)) {
  259. pr_debug2("failed to init mmap compressor, error %d\n", errno);
  260. return -1;
  261. }
  262. if (mp->comp_level && !perf_mmap__aio_enabled(map)) {
  263. map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
  264. MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  265. if (map->data == MAP_FAILED) {
  266. pr_debug2("failed to mmap data buffer, error %d\n",
  267. errno);
  268. map->data = NULL;
  269. return -1;
  270. }
  271. }
  272. if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
  273. &mp->auxtrace_mp, map->core.base, fd))
  274. return -1;
  275. return perf_mmap__aio_mmap(map, mp);
  276. }
  277. int perf_mmap__push(struct mmap *md, void *to,
  278. int push(struct mmap *map, void *to, void *buf, size_t size))
  279. {
  280. u64 head = perf_mmap__read_head(&md->core);
  281. unsigned char *data = md->core.base + page_size;
  282. unsigned long size;
  283. void *buf;
  284. int rc = 0;
  285. rc = perf_mmap__read_init(&md->core);
  286. if (rc < 0)
  287. return (rc == -EAGAIN) ? 1 : -1;
  288. size = md->core.end - md->core.start;
  289. if ((md->core.start & md->core.mask) + size != (md->core.end & md->core.mask)) {
  290. buf = &data[md->core.start & md->core.mask];
  291. size = md->core.mask + 1 - (md->core.start & md->core.mask);
  292. md->core.start += size;
  293. if (push(md, to, buf, size) < 0) {
  294. rc = -1;
  295. goto out;
  296. }
  297. }
  298. buf = &data[md->core.start & md->core.mask];
  299. size = md->core.end - md->core.start;
  300. md->core.start += size;
  301. if (push(md, to, buf, size) < 0) {
  302. rc = -1;
  303. goto out;
  304. }
  305. md->core.prev = head;
  306. perf_mmap__consume(&md->core);
  307. out:
  308. return rc;
  309. }