trace_output_user.c 4.0 KB

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