trace_output_user.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* This program is free software; you can redistribute it and/or
  2. * modify it under the terms of version 2 of the GNU General Public
  3. * License as published by the Free Software Foundation.
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <poll.h>
  12. #include <sys/ioctl.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/bpf.h>
  15. #include <errno.h>
  16. #include <assert.h>
  17. #include <sys/syscall.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/mman.h>
  20. #include <time.h>
  21. #include <signal.h>
  22. #include "libbpf.h"
  23. #include "bpf_load.h"
  24. #include "perf-sys.h"
  25. static int pmu_fd;
  26. int page_size;
  27. int page_cnt = 8;
  28. volatile struct perf_event_mmap_page *header;
  29. typedef void (*print_fn)(void *data, int size);
  30. static int perf_event_mmap(int fd)
  31. {
  32. void *base;
  33. int mmap_size;
  34. page_size = getpagesize();
  35. mmap_size = page_size * (page_cnt + 1);
  36. base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  37. if (base == MAP_FAILED) {
  38. printf("mmap err\n");
  39. return -1;
  40. }
  41. header = base;
  42. return 0;
  43. }
  44. static int perf_event_poll(int fd)
  45. {
  46. struct pollfd pfd = { .fd = fd, .events = POLLIN };
  47. return poll(&pfd, 1, 1000);
  48. }
  49. struct perf_event_sample {
  50. struct perf_event_header header;
  51. __u32 size;
  52. char data[];
  53. };
  54. static void perf_event_read(print_fn fn)
  55. {
  56. __u64 data_tail = header->data_tail;
  57. __u64 data_head = header->data_head;
  58. __u64 buffer_size = page_cnt * page_size;
  59. void *base, *begin, *end;
  60. char buf[256];
  61. asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
  62. if (data_head == data_tail)
  63. return;
  64. base = ((char *)header) + page_size;
  65. begin = base + data_tail % buffer_size;
  66. end = base + data_head % buffer_size;
  67. while (begin != end) {
  68. struct perf_event_sample *e;
  69. e = begin;
  70. if (begin + e->header.size > base + buffer_size) {
  71. long len = base + buffer_size - begin;
  72. assert(len < e->header.size);
  73. memcpy(buf, begin, len);
  74. memcpy(buf + len, base, e->header.size - len);
  75. e = (void *) buf;
  76. begin = base + e->header.size - len;
  77. } else if (begin + e->header.size == base + buffer_size) {
  78. begin = base;
  79. } else {
  80. begin += e->header.size;
  81. }
  82. if (e->header.type == PERF_RECORD_SAMPLE) {
  83. fn(e->data, e->size);
  84. } else if (e->header.type == PERF_RECORD_LOST) {
  85. struct {
  86. struct perf_event_header header;
  87. __u64 id;
  88. __u64 lost;
  89. } *lost = (void *) e;
  90. printf("lost %lld events\n", lost->lost);
  91. } else {
  92. printf("unknown event type=%d size=%d\n",
  93. e->header.type, e->header.size);
  94. }
  95. }
  96. __sync_synchronize(); /* smp_mb() */
  97. header->data_tail = data_head;
  98. }
  99. static __u64 time_get_ns(void)
  100. {
  101. struct timespec ts;
  102. clock_gettime(CLOCK_MONOTONIC, &ts);
  103. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  104. }
  105. static __u64 start_time;
  106. #define MAX_CNT 100000ll
  107. static void print_bpf_output(void *data, int size)
  108. {
  109. static __u64 cnt;
  110. struct {
  111. __u64 pid;
  112. __u64 cookie;
  113. } *e = data;
  114. if (e->cookie != 0x12345678) {
  115. printf("BUG pid %llx cookie %llx sized %d\n",
  116. e->pid, e->cookie, size);
  117. kill(0, SIGINT);
  118. }
  119. cnt++;
  120. if (cnt == MAX_CNT) {
  121. printf("recv %lld events per sec\n",
  122. MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  123. kill(0, SIGINT);
  124. }
  125. }
  126. static void test_bpf_perf_event(void)
  127. {
  128. struct perf_event_attr attr = {
  129. .sample_type = PERF_SAMPLE_RAW,
  130. .type = PERF_TYPE_SOFTWARE,
  131. .config = PERF_COUNT_SW_BPF_OUTPUT,
  132. };
  133. int key = 0;
  134. pmu_fd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
  135. assert(pmu_fd >= 0);
  136. assert(bpf_map_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) == 0);
  137. ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. char filename[256];
  142. FILE *f;
  143. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  144. if (load_bpf_file(filename)) {
  145. printf("%s", bpf_log_buf);
  146. return 1;
  147. }
  148. test_bpf_perf_event();
  149. if (perf_event_mmap(pmu_fd) < 0)
  150. return 1;
  151. f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
  152. (void) f;
  153. start_time = time_get_ns();
  154. for (;;) {
  155. perf_event_poll(pmu_fd);
  156. perf_event_read(print_bpf_output);
  157. }
  158. return 0;
  159. }