trace_event_user.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <linux/perf_event.h>
  10. #include <linux/bpf.h>
  11. #include <signal.h>
  12. #include <errno.h>
  13. #include <sys/resource.h>
  14. #include <bpf/bpf.h>
  15. #include <bpf/libbpf.h>
  16. #include "perf-sys.h"
  17. #include "trace_helpers.h"
  18. #define SAMPLE_FREQ 50
  19. static int pid;
  20. /* counts, stackmap */
  21. static int map_fd[2];
  22. struct bpf_program *prog;
  23. static bool sys_read_seen, sys_write_seen;
  24. static void print_ksym(__u64 addr)
  25. {
  26. struct ksym *sym;
  27. if (!addr)
  28. return;
  29. sym = ksym_search(addr);
  30. if (!sym) {
  31. printf("ksym not found. Is kallsyms loaded?\n");
  32. return;
  33. }
  34. printf("%s;", sym->name);
  35. if (!strstr(sym->name, "sys_read"))
  36. sys_read_seen = true;
  37. else if (!strstr(sym->name, "sys_write"))
  38. sys_write_seen = true;
  39. }
  40. static void print_addr(__u64 addr)
  41. {
  42. if (!addr)
  43. return;
  44. printf("%llx;", addr);
  45. }
  46. #define TASK_COMM_LEN 16
  47. struct key_t {
  48. char comm[TASK_COMM_LEN];
  49. __u32 kernstack;
  50. __u32 userstack;
  51. };
  52. static void print_stack(struct key_t *key, __u64 count)
  53. {
  54. __u64 ip[PERF_MAX_STACK_DEPTH] = {};
  55. static bool warned;
  56. int i;
  57. printf("%3lld %s;", count, key->comm);
  58. if (bpf_map_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
  59. printf("---;");
  60. } else {
  61. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  62. print_ksym(ip[i]);
  63. }
  64. printf("-;");
  65. if (bpf_map_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
  66. printf("---;");
  67. } else {
  68. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  69. print_addr(ip[i]);
  70. }
  71. if (count < 6)
  72. printf("\r");
  73. else
  74. printf("\n");
  75. if (key->kernstack == -EEXIST && !warned) {
  76. printf("stackmap collisions seen. Consider increasing size\n");
  77. warned = true;
  78. } else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
  79. printf("err stackid %d %d\n", key->kernstack, key->userstack);
  80. }
  81. }
  82. static void err_exit(int err)
  83. {
  84. kill(pid, SIGKILL);
  85. exit(err);
  86. }
  87. static void print_stacks(void)
  88. {
  89. struct key_t key = {}, next_key;
  90. __u64 value;
  91. __u32 stackid = 0, next_id;
  92. int error = 1, fd = map_fd[0], stack_map = map_fd[1];
  93. sys_read_seen = sys_write_seen = false;
  94. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  95. bpf_map_lookup_elem(fd, &next_key, &value);
  96. print_stack(&next_key, value);
  97. bpf_map_delete_elem(fd, &next_key);
  98. key = next_key;
  99. }
  100. printf("\n");
  101. if (!sys_read_seen || !sys_write_seen) {
  102. printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
  103. err_exit(error);
  104. }
  105. /* clear stack map */
  106. while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) {
  107. bpf_map_delete_elem(stack_map, &next_id);
  108. stackid = next_id;
  109. }
  110. }
  111. static inline int generate_load(void)
  112. {
  113. if (system("dd if=/dev/zero of=/dev/null count=5000k status=none") < 0) {
  114. printf("failed to generate some load with dd: %s\n", strerror(errno));
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static void test_perf_event_all_cpu(struct perf_event_attr *attr)
  120. {
  121. int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  122. struct bpf_link **links = calloc(nr_cpus, sizeof(struct bpf_link *));
  123. int i, pmu_fd, error = 1;
  124. if (!links) {
  125. printf("malloc of links failed\n");
  126. goto err;
  127. }
  128. /* system wide perf event, no need to inherit */
  129. attr->inherit = 0;
  130. /* open perf_event on all cpus */
  131. for (i = 0; i < nr_cpus; i++) {
  132. pmu_fd = sys_perf_event_open(attr, -1, i, -1, 0);
  133. if (pmu_fd < 0) {
  134. printf("sys_perf_event_open failed\n");
  135. goto all_cpu_err;
  136. }
  137. links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
  138. if (libbpf_get_error(links[i])) {
  139. printf("bpf_program__attach_perf_event failed\n");
  140. links[i] = NULL;
  141. close(pmu_fd);
  142. goto all_cpu_err;
  143. }
  144. }
  145. if (generate_load() < 0)
  146. goto all_cpu_err;
  147. print_stacks();
  148. error = 0;
  149. all_cpu_err:
  150. for (i--; i >= 0; i--)
  151. bpf_link__destroy(links[i]);
  152. err:
  153. free(links);
  154. if (error)
  155. err_exit(error);
  156. }
  157. static void test_perf_event_task(struct perf_event_attr *attr)
  158. {
  159. struct bpf_link *link = NULL;
  160. int pmu_fd, error = 1;
  161. /* per task perf event, enable inherit so the "dd ..." command can be traced properly.
  162. * Enabling inherit will cause bpf_perf_prog_read_time helper failure.
  163. */
  164. attr->inherit = 1;
  165. /* open task bound event */
  166. pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
  167. if (pmu_fd < 0) {
  168. printf("sys_perf_event_open failed\n");
  169. goto err;
  170. }
  171. link = bpf_program__attach_perf_event(prog, pmu_fd);
  172. if (libbpf_get_error(link)) {
  173. printf("bpf_program__attach_perf_event failed\n");
  174. link = NULL;
  175. close(pmu_fd);
  176. goto err;
  177. }
  178. if (generate_load() < 0)
  179. goto err;
  180. print_stacks();
  181. error = 0;
  182. err:
  183. bpf_link__destroy(link);
  184. if (error)
  185. err_exit(error);
  186. }
  187. static void test_bpf_perf_event(void)
  188. {
  189. struct perf_event_attr attr_type_hw = {
  190. .sample_freq = SAMPLE_FREQ,
  191. .freq = 1,
  192. .type = PERF_TYPE_HARDWARE,
  193. .config = PERF_COUNT_HW_CPU_CYCLES,
  194. };
  195. struct perf_event_attr attr_type_sw = {
  196. .sample_freq = SAMPLE_FREQ,
  197. .freq = 1,
  198. .type = PERF_TYPE_SOFTWARE,
  199. .config = PERF_COUNT_SW_CPU_CLOCK,
  200. };
  201. struct perf_event_attr attr_hw_cache_l1d = {
  202. .sample_freq = SAMPLE_FREQ,
  203. .freq = 1,
  204. .type = PERF_TYPE_HW_CACHE,
  205. .config =
  206. PERF_COUNT_HW_CACHE_L1D |
  207. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  208. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
  209. };
  210. struct perf_event_attr attr_hw_cache_branch_miss = {
  211. .sample_freq = SAMPLE_FREQ,
  212. .freq = 1,
  213. .type = PERF_TYPE_HW_CACHE,
  214. .config =
  215. PERF_COUNT_HW_CACHE_BPU |
  216. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  217. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
  218. };
  219. struct perf_event_attr attr_type_raw = {
  220. .sample_freq = SAMPLE_FREQ,
  221. .freq = 1,
  222. .type = PERF_TYPE_RAW,
  223. /* Intel Instruction Retired */
  224. .config = 0xc0,
  225. };
  226. struct perf_event_attr attr_type_raw_lock_load = {
  227. .sample_freq = SAMPLE_FREQ,
  228. .freq = 1,
  229. .type = PERF_TYPE_RAW,
  230. /* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
  231. .config = 0x21d0,
  232. /* Request to record lock address from PEBS */
  233. .sample_type = PERF_SAMPLE_ADDR,
  234. /* Record address value requires precise event */
  235. .precise_ip = 2,
  236. };
  237. printf("Test HW_CPU_CYCLES\n");
  238. test_perf_event_all_cpu(&attr_type_hw);
  239. test_perf_event_task(&attr_type_hw);
  240. printf("Test SW_CPU_CLOCK\n");
  241. test_perf_event_all_cpu(&attr_type_sw);
  242. test_perf_event_task(&attr_type_sw);
  243. printf("Test HW_CACHE_L1D\n");
  244. test_perf_event_all_cpu(&attr_hw_cache_l1d);
  245. test_perf_event_task(&attr_hw_cache_l1d);
  246. printf("Test HW_CACHE_BPU\n");
  247. test_perf_event_all_cpu(&attr_hw_cache_branch_miss);
  248. test_perf_event_task(&attr_hw_cache_branch_miss);
  249. printf("Test Instruction Retired\n");
  250. test_perf_event_all_cpu(&attr_type_raw);
  251. test_perf_event_task(&attr_type_raw);
  252. printf("Test Lock Load\n");
  253. test_perf_event_all_cpu(&attr_type_raw_lock_load);
  254. test_perf_event_task(&attr_type_raw_lock_load);
  255. printf("*** PASS ***\n");
  256. }
  257. int main(int argc, char **argv)
  258. {
  259. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  260. struct bpf_object *obj = NULL;
  261. char filename[256];
  262. int error = 1;
  263. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  264. setrlimit(RLIMIT_MEMLOCK, &r);
  265. signal(SIGINT, err_exit);
  266. signal(SIGTERM, err_exit);
  267. if (load_kallsyms()) {
  268. printf("failed to process /proc/kallsyms\n");
  269. goto cleanup;
  270. }
  271. obj = bpf_object__open_file(filename, NULL);
  272. if (libbpf_get_error(obj)) {
  273. printf("opening BPF object file failed\n");
  274. obj = NULL;
  275. goto cleanup;
  276. }
  277. prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
  278. if (!prog) {
  279. printf("finding a prog in obj file failed\n");
  280. goto cleanup;
  281. }
  282. /* load BPF program */
  283. if (bpf_object__load(obj)) {
  284. printf("loading BPF object file failed\n");
  285. goto cleanup;
  286. }
  287. map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
  288. map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
  289. if (map_fd[0] < 0 || map_fd[1] < 0) {
  290. printf("finding a counts/stackmap map in obj file failed\n");
  291. goto cleanup;
  292. }
  293. pid = fork();
  294. if (pid == 0) {
  295. read_trace_pipe();
  296. return 0;
  297. } else if (pid == -1) {
  298. printf("couldn't spawn process\n");
  299. goto cleanup;
  300. }
  301. test_bpf_perf_event();
  302. error = 0;
  303. cleanup:
  304. bpf_object__close(obj);
  305. err_exit(error);
  306. }