trace_event_user.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 (!strcmp(sym->name, "sys_read"))
  35. sys_read_seen = true;
  36. else if (!strcmp(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 void test_perf_event_all_cpu(struct perf_event_attr *attr)
  111. {
  112. int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
  113. int *pmu_fd = malloc(nr_cpus * sizeof(int));
  114. int i, error = 0;
  115. /* system wide perf event, no need to inherit */
  116. attr->inherit = 0;
  117. /* open perf_event on all cpus */
  118. for (i = 0; i < nr_cpus; i++) {
  119. pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
  120. if (pmu_fd[i] < 0) {
  121. printf("sys_perf_event_open failed\n");
  122. error = 1;
  123. goto all_cpu_err;
  124. }
  125. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  126. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE) == 0);
  127. }
  128. system("dd if=/dev/zero of=/dev/null count=5000k status=none");
  129. print_stacks();
  130. all_cpu_err:
  131. for (i--; i >= 0; i--) {
  132. ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
  133. close(pmu_fd[i]);
  134. }
  135. free(pmu_fd);
  136. if (error)
  137. int_exit(0);
  138. }
  139. static void test_perf_event_task(struct perf_event_attr *attr)
  140. {
  141. int pmu_fd;
  142. /* per task perf event, enable inherit so the "dd ..." command can be traced properly.
  143. * Enabling inherit will cause bpf_perf_prog_read_time helper failure.
  144. */
  145. attr->inherit = 1;
  146. /* open task bound event */
  147. pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
  148. if (pmu_fd < 0) {
  149. printf("sys_perf_event_open failed\n");
  150. int_exit(0);
  151. }
  152. assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  153. assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE) == 0);
  154. system("dd if=/dev/zero of=/dev/null count=5000k status=none");
  155. print_stacks();
  156. ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
  157. close(pmu_fd);
  158. }
  159. static void test_bpf_perf_event(void)
  160. {
  161. struct perf_event_attr attr_type_hw = {
  162. .sample_freq = SAMPLE_FREQ,
  163. .freq = 1,
  164. .type = PERF_TYPE_HARDWARE,
  165. .config = PERF_COUNT_HW_CPU_CYCLES,
  166. };
  167. struct perf_event_attr attr_type_sw = {
  168. .sample_freq = SAMPLE_FREQ,
  169. .freq = 1,
  170. .type = PERF_TYPE_SOFTWARE,
  171. .config = PERF_COUNT_SW_CPU_CLOCK,
  172. };
  173. struct perf_event_attr attr_hw_cache_l1d = {
  174. .sample_freq = SAMPLE_FREQ,
  175. .freq = 1,
  176. .type = PERF_TYPE_HW_CACHE,
  177. .config =
  178. PERF_COUNT_HW_CACHE_L1D |
  179. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  180. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
  181. };
  182. struct perf_event_attr attr_hw_cache_branch_miss = {
  183. .sample_freq = SAMPLE_FREQ,
  184. .freq = 1,
  185. .type = PERF_TYPE_HW_CACHE,
  186. .config =
  187. PERF_COUNT_HW_CACHE_BPU |
  188. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  189. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
  190. };
  191. struct perf_event_attr attr_type_raw = {
  192. .sample_freq = SAMPLE_FREQ,
  193. .freq = 1,
  194. .type = PERF_TYPE_RAW,
  195. /* Intel Instruction Retired */
  196. .config = 0xc0,
  197. };
  198. struct perf_event_attr attr_type_raw_lock_load = {
  199. .sample_freq = SAMPLE_FREQ,
  200. .freq = 1,
  201. .type = PERF_TYPE_RAW,
  202. /* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
  203. .config = 0x21d0,
  204. /* Request to record lock address from PEBS */
  205. .sample_type = PERF_SAMPLE_ADDR,
  206. /* Record address value requires precise event */
  207. .precise_ip = 2,
  208. };
  209. printf("Test HW_CPU_CYCLES\n");
  210. test_perf_event_all_cpu(&attr_type_hw);
  211. test_perf_event_task(&attr_type_hw);
  212. printf("Test SW_CPU_CLOCK\n");
  213. test_perf_event_all_cpu(&attr_type_sw);
  214. test_perf_event_task(&attr_type_sw);
  215. printf("Test HW_CACHE_L1D\n");
  216. test_perf_event_all_cpu(&attr_hw_cache_l1d);
  217. test_perf_event_task(&attr_hw_cache_l1d);
  218. printf("Test HW_CACHE_BPU\n");
  219. test_perf_event_all_cpu(&attr_hw_cache_branch_miss);
  220. test_perf_event_task(&attr_hw_cache_branch_miss);
  221. printf("Test Instruction Retired\n");
  222. test_perf_event_all_cpu(&attr_type_raw);
  223. test_perf_event_task(&attr_type_raw);
  224. printf("Test Lock Load\n");
  225. test_perf_event_all_cpu(&attr_type_raw_lock_load);
  226. test_perf_event_task(&attr_type_raw_lock_load);
  227. printf("*** PASS ***\n");
  228. }
  229. int main(int argc, char **argv)
  230. {
  231. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  232. char filename[256];
  233. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  234. setrlimit(RLIMIT_MEMLOCK, &r);
  235. signal(SIGINT, int_exit);
  236. signal(SIGTERM, int_exit);
  237. if (load_kallsyms()) {
  238. printf("failed to process /proc/kallsyms\n");
  239. return 1;
  240. }
  241. if (load_bpf_file(filename)) {
  242. printf("%s", bpf_log_buf);
  243. return 2;
  244. }
  245. if (fork() == 0) {
  246. read_trace_pipe();
  247. return 0;
  248. }
  249. test_bpf_perf_event();
  250. int_exit(0);
  251. return 0;
  252. }