trace_event_user.c 7.0 KB

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