map_perf_ring.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2018 Netronome Systems, Inc. */
  3. /* This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <libbpf.h>
  10. #include <poll.h>
  11. #include <signal.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. #include <linux/bpf.h>
  19. #include <linux/perf_event.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/mman.h>
  22. #include <sys/syscall.h>
  23. #include <bpf.h>
  24. #include <perf-sys.h>
  25. #include "main.h"
  26. #define MMAP_PAGE_CNT 16
  27. static bool stop;
  28. struct event_ring_info {
  29. int fd;
  30. int key;
  31. unsigned int cpu;
  32. void *mem;
  33. };
  34. struct perf_event_sample {
  35. struct perf_event_header header;
  36. u64 time;
  37. __u32 size;
  38. unsigned char data[];
  39. };
  40. static void int_exit(int signo)
  41. {
  42. fprintf(stderr, "Stopping...\n");
  43. stop = true;
  44. }
  45. static enum bpf_perf_event_ret print_bpf_output(void *event, void *priv)
  46. {
  47. struct event_ring_info *ring = priv;
  48. struct perf_event_sample *e = event;
  49. struct {
  50. struct perf_event_header header;
  51. __u64 id;
  52. __u64 lost;
  53. } *lost = event;
  54. if (json_output) {
  55. jsonw_start_object(json_wtr);
  56. jsonw_name(json_wtr, "type");
  57. jsonw_uint(json_wtr, e->header.type);
  58. jsonw_name(json_wtr, "cpu");
  59. jsonw_uint(json_wtr, ring->cpu);
  60. jsonw_name(json_wtr, "index");
  61. jsonw_uint(json_wtr, ring->key);
  62. if (e->header.type == PERF_RECORD_SAMPLE) {
  63. jsonw_name(json_wtr, "timestamp");
  64. jsonw_uint(json_wtr, e->time);
  65. jsonw_name(json_wtr, "data");
  66. print_data_json(e->data, e->size);
  67. } else if (e->header.type == PERF_RECORD_LOST) {
  68. jsonw_name(json_wtr, "lost");
  69. jsonw_start_object(json_wtr);
  70. jsonw_name(json_wtr, "id");
  71. jsonw_uint(json_wtr, lost->id);
  72. jsonw_name(json_wtr, "count");
  73. jsonw_uint(json_wtr, lost->lost);
  74. jsonw_end_object(json_wtr);
  75. }
  76. jsonw_end_object(json_wtr);
  77. } else {
  78. if (e->header.type == PERF_RECORD_SAMPLE) {
  79. printf("== @%lld.%09lld CPU: %d index: %d =====\n",
  80. e->time / 1000000000ULL, e->time % 1000000000ULL,
  81. ring->cpu, ring->key);
  82. fprint_hex(stdout, e->data, e->size, " ");
  83. printf("\n");
  84. } else if (e->header.type == PERF_RECORD_LOST) {
  85. printf("lost %lld events\n", lost->lost);
  86. } else {
  87. printf("unknown event type=%d size=%d\n",
  88. e->header.type, e->header.size);
  89. }
  90. }
  91. return LIBBPF_PERF_EVENT_CONT;
  92. }
  93. static void
  94. perf_event_read(struct event_ring_info *ring, void **buf, size_t *buf_len)
  95. {
  96. enum bpf_perf_event_ret ret;
  97. ret = bpf_perf_event_read_simple(ring->mem,
  98. MMAP_PAGE_CNT * get_page_size(),
  99. get_page_size(), buf, buf_len,
  100. print_bpf_output, ring);
  101. if (ret != LIBBPF_PERF_EVENT_CONT) {
  102. fprintf(stderr, "perf read loop failed with %d\n", ret);
  103. stop = true;
  104. }
  105. }
  106. static int perf_mmap_size(void)
  107. {
  108. return get_page_size() * (MMAP_PAGE_CNT + 1);
  109. }
  110. static void *perf_event_mmap(int fd)
  111. {
  112. int mmap_size = perf_mmap_size();
  113. void *base;
  114. base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  115. if (base == MAP_FAILED) {
  116. p_err("event mmap failed: %s\n", strerror(errno));
  117. return NULL;
  118. }
  119. return base;
  120. }
  121. static void perf_event_unmap(void *mem)
  122. {
  123. if (munmap(mem, perf_mmap_size()))
  124. fprintf(stderr, "Can't unmap ring memory!\n");
  125. }
  126. static int bpf_perf_event_open(int map_fd, int key, int cpu)
  127. {
  128. struct perf_event_attr attr = {
  129. .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME,
  130. .type = PERF_TYPE_SOFTWARE,
  131. .config = PERF_COUNT_SW_BPF_OUTPUT,
  132. };
  133. int pmu_fd;
  134. pmu_fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
  135. if (pmu_fd < 0) {
  136. p_err("failed to open perf event %d for CPU %d", key, cpu);
  137. return -1;
  138. }
  139. if (bpf_map_update_elem(map_fd, &key, &pmu_fd, BPF_ANY)) {
  140. p_err("failed to update map for event %d for CPU %d", key, cpu);
  141. goto err_close;
  142. }
  143. if (ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
  144. p_err("failed to enable event %d for CPU %d", key, cpu);
  145. goto err_close;
  146. }
  147. return pmu_fd;
  148. err_close:
  149. close(pmu_fd);
  150. return -1;
  151. }
  152. int do_event_pipe(int argc, char **argv)
  153. {
  154. int i, nfds, map_fd, index = -1, cpu = -1;
  155. struct bpf_map_info map_info = {};
  156. struct event_ring_info *rings;
  157. size_t tmp_buf_sz = 0;
  158. void *tmp_buf = NULL;
  159. struct pollfd *pfds;
  160. __u32 map_info_len;
  161. bool do_all = true;
  162. map_info_len = sizeof(map_info);
  163. map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len);
  164. if (map_fd < 0)
  165. return -1;
  166. if (map_info.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
  167. p_err("map is not a perf event array");
  168. goto err_close_map;
  169. }
  170. while (argc) {
  171. if (argc < 2) {
  172. BAD_ARG();
  173. goto err_close_map;
  174. }
  175. if (is_prefix(*argv, "cpu")) {
  176. char *endptr;
  177. NEXT_ARG();
  178. cpu = strtoul(*argv, &endptr, 0);
  179. if (*endptr) {
  180. p_err("can't parse %s as CPU ID", *argv);
  181. goto err_close_map;
  182. }
  183. NEXT_ARG();
  184. } else if (is_prefix(*argv, "index")) {
  185. char *endptr;
  186. NEXT_ARG();
  187. index = strtoul(*argv, &endptr, 0);
  188. if (*endptr) {
  189. p_err("can't parse %s as index", *argv);
  190. goto err_close_map;
  191. }
  192. NEXT_ARG();
  193. } else {
  194. BAD_ARG();
  195. goto err_close_map;
  196. }
  197. do_all = false;
  198. }
  199. if (!do_all) {
  200. if (index == -1 || cpu == -1) {
  201. p_err("cpu and index must be specified together");
  202. goto err_close_map;
  203. }
  204. nfds = 1;
  205. } else {
  206. nfds = min(get_possible_cpus(), map_info.max_entries);
  207. cpu = 0;
  208. index = 0;
  209. }
  210. rings = calloc(nfds, sizeof(rings[0]));
  211. if (!rings)
  212. goto err_close_map;
  213. pfds = calloc(nfds, sizeof(pfds[0]));
  214. if (!pfds)
  215. goto err_free_rings;
  216. for (i = 0; i < nfds; i++) {
  217. rings[i].cpu = cpu + i;
  218. rings[i].key = index + i;
  219. rings[i].fd = bpf_perf_event_open(map_fd, rings[i].key,
  220. rings[i].cpu);
  221. if (rings[i].fd < 0)
  222. goto err_close_fds_prev;
  223. rings[i].mem = perf_event_mmap(rings[i].fd);
  224. if (!rings[i].mem)
  225. goto err_close_fds_current;
  226. pfds[i].fd = rings[i].fd;
  227. pfds[i].events = POLLIN;
  228. }
  229. signal(SIGINT, int_exit);
  230. signal(SIGHUP, int_exit);
  231. signal(SIGTERM, int_exit);
  232. if (json_output)
  233. jsonw_start_array(json_wtr);
  234. while (!stop) {
  235. poll(pfds, nfds, 200);
  236. for (i = 0; i < nfds; i++)
  237. perf_event_read(&rings[i], &tmp_buf, &tmp_buf_sz);
  238. }
  239. free(tmp_buf);
  240. if (json_output)
  241. jsonw_end_array(json_wtr);
  242. for (i = 0; i < nfds; i++) {
  243. perf_event_unmap(rings[i].mem);
  244. close(rings[i].fd);
  245. }
  246. free(pfds);
  247. free(rings);
  248. close(map_fd);
  249. return 0;
  250. err_close_fds_prev:
  251. while (i--) {
  252. perf_event_unmap(rings[i].mem);
  253. err_close_fds_current:
  254. close(rings[i].fd);
  255. }
  256. free(pfds);
  257. err_free_rings:
  258. free(rings);
  259. err_close_map:
  260. close(map_fd);
  261. return -1;
  262. }