trace_event_user.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* open perf_event on all cpus */
  115. for (i = 0; i < nr_cpus; i++) {
  116. pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
  117. if (pmu_fd[i] < 0) {
  118. printf("sys_perf_event_open failed\n");
  119. error = 1;
  120. goto all_cpu_err;
  121. }
  122. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  123. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE) == 0);
  124. }
  125. system("dd if=/dev/zero of=/dev/null count=5000k status=none");
  126. print_stacks();
  127. all_cpu_err:
  128. for (i--; i >= 0; i--) {
  129. ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
  130. close(pmu_fd[i]);
  131. }
  132. free(pmu_fd);
  133. if (error)
  134. int_exit(0);
  135. }
  136. static void test_perf_event_task(struct perf_event_attr *attr)
  137. {
  138. int pmu_fd;
  139. /* open task bound event */
  140. pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
  141. if (pmu_fd < 0) {
  142. printf("sys_perf_event_open failed\n");
  143. int_exit(0);
  144. }
  145. assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  146. assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE) == 0);
  147. system("dd if=/dev/zero of=/dev/null count=5000k status=none");
  148. print_stacks();
  149. ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
  150. close(pmu_fd);
  151. }
  152. static void test_bpf_perf_event(void)
  153. {
  154. struct perf_event_attr attr_type_hw = {
  155. .sample_freq = SAMPLE_FREQ,
  156. .freq = 1,
  157. .type = PERF_TYPE_HARDWARE,
  158. .config = PERF_COUNT_HW_CPU_CYCLES,
  159. .inherit = 1,
  160. };
  161. struct perf_event_attr attr_type_sw = {
  162. .sample_freq = SAMPLE_FREQ,
  163. .freq = 1,
  164. .type = PERF_TYPE_SOFTWARE,
  165. .config = PERF_COUNT_SW_CPU_CLOCK,
  166. .inherit = 1,
  167. };
  168. struct perf_event_attr attr_hw_cache_l1d = {
  169. .sample_freq = SAMPLE_FREQ,
  170. .freq = 1,
  171. .type = PERF_TYPE_HW_CACHE,
  172. .config =
  173. PERF_COUNT_HW_CACHE_L1D |
  174. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  175. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
  176. .inherit = 1,
  177. };
  178. struct perf_event_attr attr_hw_cache_branch_miss = {
  179. .sample_freq = SAMPLE_FREQ,
  180. .freq = 1,
  181. .type = PERF_TYPE_HW_CACHE,
  182. .config =
  183. PERF_COUNT_HW_CACHE_BPU |
  184. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  185. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
  186. .inherit = 1,
  187. };
  188. struct perf_event_attr attr_type_raw = {
  189. .sample_freq = SAMPLE_FREQ,
  190. .freq = 1,
  191. .type = PERF_TYPE_RAW,
  192. /* Intel Instruction Retired */
  193. .config = 0xc0,
  194. .inherit = 1,
  195. };
  196. printf("Test HW_CPU_CYCLES\n");
  197. test_perf_event_all_cpu(&attr_type_hw);
  198. test_perf_event_task(&attr_type_hw);
  199. printf("Test SW_CPU_CLOCK\n");
  200. test_perf_event_all_cpu(&attr_type_sw);
  201. test_perf_event_task(&attr_type_sw);
  202. printf("Test HW_CACHE_L1D\n");
  203. test_perf_event_all_cpu(&attr_hw_cache_l1d);
  204. test_perf_event_task(&attr_hw_cache_l1d);
  205. printf("Test HW_CACHE_BPU\n");
  206. test_perf_event_all_cpu(&attr_hw_cache_branch_miss);
  207. test_perf_event_task(&attr_hw_cache_branch_miss);
  208. printf("Test Instruction Retired\n");
  209. test_perf_event_all_cpu(&attr_type_raw);
  210. test_perf_event_task(&attr_type_raw);
  211. printf("*** PASS ***\n");
  212. }
  213. int main(int argc, char **argv)
  214. {
  215. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  216. char filename[256];
  217. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  218. setrlimit(RLIMIT_MEMLOCK, &r);
  219. signal(SIGINT, int_exit);
  220. signal(SIGTERM, int_exit);
  221. if (load_kallsyms()) {
  222. printf("failed to process /proc/kallsyms\n");
  223. return 1;
  224. }
  225. if (load_bpf_file(filename)) {
  226. printf("%s", bpf_log_buf);
  227. return 2;
  228. }
  229. if (fork() == 0) {
  230. read_trace_pipe();
  231. return 0;
  232. }
  233. test_bpf_perf_event();
  234. int_exit(0);
  235. return 0;
  236. }