trace_event_user.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. printf("\n");
  70. if (key->kernstack == -EEXIST && !warned) {
  71. printf("stackmap collisions seen. Consider increasing size\n");
  72. warned = true;
  73. } else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
  74. printf("err stackid %d %d\n", key->kernstack, key->userstack);
  75. }
  76. }
  77. static void int_exit(int sig)
  78. {
  79. kill(0, SIGKILL);
  80. exit(0);
  81. }
  82. static void print_stacks(void)
  83. {
  84. struct key_t key = {}, next_key;
  85. __u64 value;
  86. __u32 stackid = 0, next_id;
  87. int fd = map_fd[0], stack_map = map_fd[1];
  88. sys_read_seen = sys_write_seen = false;
  89. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  90. bpf_map_lookup_elem(fd, &next_key, &value);
  91. print_stack(&next_key, value);
  92. bpf_map_delete_elem(fd, &next_key);
  93. key = next_key;
  94. }
  95. if (!sys_read_seen || !sys_write_seen) {
  96. printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
  97. int_exit(0);
  98. }
  99. /* clear stack map */
  100. while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) {
  101. bpf_map_delete_elem(stack_map, &next_id);
  102. stackid = next_id;
  103. }
  104. }
  105. static void test_perf_event_all_cpu(struct perf_event_attr *attr)
  106. {
  107. int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
  108. int *pmu_fd = malloc(nr_cpus * sizeof(int));
  109. int i;
  110. /* open perf_event on all cpus */
  111. for (i = 0; i < nr_cpus; i++) {
  112. pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
  113. if (pmu_fd[i] < 0) {
  114. printf("sys_perf_event_open failed\n");
  115. goto all_cpu_err;
  116. }
  117. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  118. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
  119. }
  120. system("dd if=/dev/zero of=/dev/null count=5000k");
  121. print_stacks();
  122. all_cpu_err:
  123. for (i--; i >= 0; i--)
  124. close(pmu_fd[i]);
  125. free(pmu_fd);
  126. }
  127. static void test_perf_event_task(struct perf_event_attr *attr)
  128. {
  129. int pmu_fd;
  130. /* open task bound event */
  131. pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
  132. if (pmu_fd < 0) {
  133. printf("sys_perf_event_open failed\n");
  134. return;
  135. }
  136. assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  137. assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0);
  138. system("dd if=/dev/zero of=/dev/null count=5000k");
  139. print_stacks();
  140. close(pmu_fd);
  141. }
  142. static void test_bpf_perf_event(void)
  143. {
  144. struct perf_event_attr attr_type_hw = {
  145. .sample_freq = SAMPLE_FREQ,
  146. .freq = 1,
  147. .type = PERF_TYPE_HARDWARE,
  148. .config = PERF_COUNT_HW_CPU_CYCLES,
  149. .inherit = 1,
  150. };
  151. struct perf_event_attr attr_type_sw = {
  152. .sample_freq = SAMPLE_FREQ,
  153. .freq = 1,
  154. .type = PERF_TYPE_SOFTWARE,
  155. .config = PERF_COUNT_SW_CPU_CLOCK,
  156. .inherit = 1,
  157. };
  158. test_perf_event_all_cpu(&attr_type_hw);
  159. test_perf_event_task(&attr_type_hw);
  160. test_perf_event_all_cpu(&attr_type_sw);
  161. test_perf_event_task(&attr_type_sw);
  162. }
  163. int main(int argc, char **argv)
  164. {
  165. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  166. char filename[256];
  167. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  168. setrlimit(RLIMIT_MEMLOCK, &r);
  169. signal(SIGINT, int_exit);
  170. if (load_kallsyms()) {
  171. printf("failed to process /proc/kallsyms\n");
  172. return 1;
  173. }
  174. if (load_bpf_file(filename)) {
  175. printf("%s", bpf_log_buf);
  176. return 2;
  177. }
  178. if (fork() == 0) {
  179. read_trace_pipe();
  180. return 0;
  181. }
  182. test_bpf_perf_event();
  183. int_exit(0);
  184. return 0;
  185. }