trace_event_user.c 6.3 KB

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