trace_event_user.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #define SAMPLE_FREQ 50
  24. static bool sys_read_seen, sys_write_seen;
  25. static void print_ksym(__u64 addr)
  26. {
  27. struct ksym *sym;
  28. if (!addr)
  29. return;
  30. sym = ksym_search(addr);
  31. printf("%s;", sym->name);
  32. if (!strcmp(sym->name, "sys_read"))
  33. sys_read_seen = true;
  34. else if (!strcmp(sym->name, "sys_write"))
  35. sys_write_seen = true;
  36. }
  37. static void print_addr(__u64 addr)
  38. {
  39. if (!addr)
  40. return;
  41. printf("%llx;", addr);
  42. }
  43. #define TASK_COMM_LEN 16
  44. struct key_t {
  45. char comm[TASK_COMM_LEN];
  46. __u32 kernstack;
  47. __u32 userstack;
  48. };
  49. static void print_stack(struct key_t *key, __u64 count)
  50. {
  51. __u64 ip[PERF_MAX_STACK_DEPTH] = {};
  52. static bool warned;
  53. int i;
  54. printf("%3lld %s;", count, key->comm);
  55. if (bpf_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
  56. printf("---;");
  57. } else {
  58. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  59. print_ksym(ip[i]);
  60. }
  61. printf("-;");
  62. if (bpf_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
  63. printf("---;");
  64. } else {
  65. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  66. print_addr(ip[i]);
  67. }
  68. printf("\n");
  69. if (key->kernstack == -EEXIST && !warned) {
  70. printf("stackmap collisions seen. Consider increasing size\n");
  71. warned = true;
  72. } else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
  73. printf("err stackid %d %d\n", key->kernstack, key->userstack);
  74. }
  75. }
  76. static void int_exit(int sig)
  77. {
  78. kill(0, SIGKILL);
  79. exit(0);
  80. }
  81. static void print_stacks(void)
  82. {
  83. struct key_t key = {}, next_key;
  84. __u64 value;
  85. __u32 stackid = 0, next_id;
  86. int fd = map_fd[0], stack_map = map_fd[1];
  87. sys_read_seen = sys_write_seen = false;
  88. while (bpf_get_next_key(fd, &key, &next_key) == 0) {
  89. bpf_lookup_elem(fd, &next_key, &value);
  90. print_stack(&next_key, value);
  91. bpf_delete_elem(fd, &next_key);
  92. key = next_key;
  93. }
  94. if (!sys_read_seen || !sys_write_seen) {
  95. printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
  96. int_exit(0);
  97. }
  98. /* clear stack map */
  99. while (bpf_get_next_key(stack_map, &stackid, &next_id) == 0) {
  100. bpf_delete_elem(stack_map, &next_id);
  101. stackid = next_id;
  102. }
  103. }
  104. static void test_perf_event_all_cpu(struct perf_event_attr *attr)
  105. {
  106. int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
  107. int *pmu_fd = malloc(nr_cpus * sizeof(int));
  108. int i;
  109. /* open perf_event on all cpus */
  110. for (i = 0; i < nr_cpus; i++) {
  111. pmu_fd[i] = perf_event_open(attr, -1, i, -1, 0);
  112. if (pmu_fd[i] < 0) {
  113. printf("perf_event_open failed\n");
  114. goto all_cpu_err;
  115. }
  116. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  117. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
  118. }
  119. system("dd if=/dev/zero of=/dev/null count=5000k");
  120. print_stacks();
  121. all_cpu_err:
  122. for (i--; i >= 0; i--)
  123. close(pmu_fd[i]);
  124. free(pmu_fd);
  125. }
  126. static void test_perf_event_task(struct perf_event_attr *attr)
  127. {
  128. int pmu_fd;
  129. /* open task bound event */
  130. pmu_fd = perf_event_open(attr, 0, -1, -1, 0);
  131. if (pmu_fd < 0) {
  132. printf("perf_event_open failed\n");
  133. return;
  134. }
  135. assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
  136. assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0);
  137. system("dd if=/dev/zero of=/dev/null count=5000k");
  138. print_stacks();
  139. close(pmu_fd);
  140. }
  141. static void test_bpf_perf_event(void)
  142. {
  143. struct perf_event_attr attr_type_hw = {
  144. .sample_freq = SAMPLE_FREQ,
  145. .freq = 1,
  146. .type = PERF_TYPE_HARDWARE,
  147. .config = PERF_COUNT_HW_CPU_CYCLES,
  148. .inherit = 1,
  149. };
  150. struct perf_event_attr attr_type_sw = {
  151. .sample_freq = SAMPLE_FREQ,
  152. .freq = 1,
  153. .type = PERF_TYPE_SOFTWARE,
  154. .config = PERF_COUNT_SW_CPU_CLOCK,
  155. .inherit = 1,
  156. };
  157. test_perf_event_all_cpu(&attr_type_hw);
  158. test_perf_event_task(&attr_type_hw);
  159. test_perf_event_all_cpu(&attr_type_sw);
  160. test_perf_event_task(&attr_type_sw);
  161. }
  162. int main(int argc, char **argv)
  163. {
  164. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  165. char filename[256];
  166. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  167. setrlimit(RLIMIT_MEMLOCK, &r);
  168. signal(SIGINT, int_exit);
  169. if (load_kallsyms()) {
  170. printf("failed to process /proc/kallsyms\n");
  171. return 1;
  172. }
  173. if (load_bpf_file(filename)) {
  174. printf("%s", bpf_log_buf);
  175. return 2;
  176. }
  177. if (fork() == 0) {
  178. read_trace_pipe();
  179. return 0;
  180. }
  181. test_bpf_perf_event();
  182. int_exit(0);
  183. return 0;
  184. }