map_perf_ring.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  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 <bpf/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/bpf.h>
  24. #include "main.h"
  25. #define MMAP_PAGE_CNT 16
  26. static volatile bool stop;
  27. struct perf_event_sample {
  28. struct perf_event_header header;
  29. __u64 time;
  30. __u32 size;
  31. unsigned char data[];
  32. };
  33. struct perf_event_lost {
  34. struct perf_event_header header;
  35. __u64 id;
  36. __u64 lost;
  37. };
  38. static void int_exit(int signo)
  39. {
  40. fprintf(stderr, "Stopping...\n");
  41. stop = true;
  42. }
  43. struct event_pipe_ctx {
  44. bool all_cpus;
  45. int cpu;
  46. int idx;
  47. };
  48. static enum bpf_perf_event_ret
  49. print_bpf_output(void *private_data, int cpu, struct perf_event_header *event)
  50. {
  51. struct perf_event_sample *e = container_of(event,
  52. struct perf_event_sample,
  53. header);
  54. struct perf_event_lost *lost = container_of(event,
  55. struct perf_event_lost,
  56. header);
  57. struct event_pipe_ctx *ctx = private_data;
  58. int idx = ctx->all_cpus ? cpu : ctx->idx;
  59. if (json_output) {
  60. jsonw_start_object(json_wtr);
  61. jsonw_name(json_wtr, "type");
  62. jsonw_uint(json_wtr, e->header.type);
  63. jsonw_name(json_wtr, "cpu");
  64. jsonw_uint(json_wtr, cpu);
  65. jsonw_name(json_wtr, "index");
  66. jsonw_uint(json_wtr, idx);
  67. if (e->header.type == PERF_RECORD_SAMPLE) {
  68. jsonw_name(json_wtr, "timestamp");
  69. jsonw_uint(json_wtr, e->time);
  70. jsonw_name(json_wtr, "data");
  71. print_data_json(e->data, e->size);
  72. } else if (e->header.type == PERF_RECORD_LOST) {
  73. jsonw_name(json_wtr, "lost");
  74. jsonw_start_object(json_wtr);
  75. jsonw_name(json_wtr, "id");
  76. jsonw_uint(json_wtr, lost->id);
  77. jsonw_name(json_wtr, "count");
  78. jsonw_uint(json_wtr, lost->lost);
  79. jsonw_end_object(json_wtr);
  80. }
  81. jsonw_end_object(json_wtr);
  82. } else {
  83. if (e->header.type == PERF_RECORD_SAMPLE) {
  84. printf("== @%lld.%09lld CPU: %d index: %d =====\n",
  85. e->time / 1000000000ULL, e->time % 1000000000ULL,
  86. cpu, idx);
  87. fprint_hex(stdout, e->data, e->size, " ");
  88. printf("\n");
  89. } else if (e->header.type == PERF_RECORD_LOST) {
  90. printf("lost %lld events\n", lost->lost);
  91. } else {
  92. printf("unknown event type=%d size=%d\n",
  93. e->header.type, e->header.size);
  94. }
  95. }
  96. return LIBBPF_PERF_EVENT_CONT;
  97. }
  98. int do_event_pipe(int argc, char **argv)
  99. {
  100. struct perf_event_attr perf_attr = {
  101. .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME,
  102. .type = PERF_TYPE_SOFTWARE,
  103. .config = PERF_COUNT_SW_BPF_OUTPUT,
  104. .sample_period = 1,
  105. .wakeup_events = 1,
  106. };
  107. struct bpf_map_info map_info = {};
  108. LIBBPF_OPTS(perf_buffer_raw_opts, opts);
  109. struct event_pipe_ctx ctx = {
  110. .all_cpus = true,
  111. .cpu = -1,
  112. .idx = -1,
  113. };
  114. struct perf_buffer *pb;
  115. __u32 map_info_len;
  116. int err, map_fd;
  117. map_info_len = sizeof(map_info);
  118. map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len);
  119. if (map_fd < 0)
  120. return -1;
  121. if (map_info.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
  122. p_err("map is not a perf event array");
  123. goto err_close_map;
  124. }
  125. while (argc) {
  126. if (argc < 2) {
  127. BAD_ARG();
  128. goto err_close_map;
  129. }
  130. if (is_prefix(*argv, "cpu")) {
  131. char *endptr;
  132. NEXT_ARG();
  133. ctx.cpu = strtoul(*argv, &endptr, 0);
  134. if (*endptr) {
  135. p_err("can't parse %s as CPU ID", *argv);
  136. goto err_close_map;
  137. }
  138. NEXT_ARG();
  139. } else if (is_prefix(*argv, "index")) {
  140. char *endptr;
  141. NEXT_ARG();
  142. ctx.idx = strtoul(*argv, &endptr, 0);
  143. if (*endptr) {
  144. p_err("can't parse %s as index", *argv);
  145. goto err_close_map;
  146. }
  147. NEXT_ARG();
  148. } else {
  149. BAD_ARG();
  150. goto err_close_map;
  151. }
  152. ctx.all_cpus = false;
  153. }
  154. if (!ctx.all_cpus) {
  155. if (ctx.idx == -1 || ctx.cpu == -1) {
  156. p_err("cpu and index must be specified together");
  157. goto err_close_map;
  158. }
  159. } else {
  160. ctx.cpu = 0;
  161. ctx.idx = 0;
  162. }
  163. opts.cpu_cnt = ctx.all_cpus ? 0 : 1;
  164. opts.cpus = &ctx.cpu;
  165. opts.map_keys = &ctx.idx;
  166. pb = perf_buffer__new_raw(map_fd, MMAP_PAGE_CNT, &perf_attr,
  167. print_bpf_output, &ctx, &opts);
  168. if (!pb) {
  169. p_err("failed to create perf buffer: %s (%d)",
  170. strerror(errno), errno);
  171. goto err_close_map;
  172. }
  173. signal(SIGINT, int_exit);
  174. signal(SIGHUP, int_exit);
  175. signal(SIGTERM, int_exit);
  176. if (json_output)
  177. jsonw_start_array(json_wtr);
  178. while (!stop) {
  179. err = perf_buffer__poll(pb, 200);
  180. if (err < 0 && err != -EINTR) {
  181. p_err("perf buffer polling failed: %s (%d)",
  182. strerror(errno), errno);
  183. goto err_close_pb;
  184. }
  185. }
  186. if (json_output)
  187. jsonw_end_array(json_wtr);
  188. perf_buffer__free(pb);
  189. close(map_fd);
  190. return 0;
  191. err_close_pb:
  192. perf_buffer__free(pb);
  193. err_close_map:
  194. close(map_fd);
  195. return -1;
  196. }