test_overhead_user.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #define _GNU_SOURCE
  5. #include <sched.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <asm/unistd.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <assert.h>
  13. #include <sys/wait.h>
  14. #include <stdlib.h>
  15. #include <signal.h>
  16. #include <linux/bpf.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <bpf/bpf.h>
  20. #include <bpf/libbpf.h>
  21. #define MAX_CNT 1000000
  22. static struct bpf_link *links[2];
  23. static struct bpf_object *obj;
  24. static int cnt;
  25. static __u64 time_get_ns(void)
  26. {
  27. struct timespec ts;
  28. clock_gettime(CLOCK_MONOTONIC, &ts);
  29. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  30. }
  31. static void test_task_rename(int cpu)
  32. {
  33. __u64 start_time;
  34. char buf[] = "test\n";
  35. int i, fd;
  36. fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
  37. if (fd < 0) {
  38. printf("couldn't open /proc\n");
  39. exit(1);
  40. }
  41. start_time = time_get_ns();
  42. for (i = 0; i < MAX_CNT; i++) {
  43. if (write(fd, buf, sizeof(buf)) < 0) {
  44. printf("task rename failed: %s\n", strerror(errno));
  45. close(fd);
  46. return;
  47. }
  48. }
  49. printf("task_rename:%d: %lld events per sec\n",
  50. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  51. close(fd);
  52. }
  53. static void test_urandom_read(int cpu)
  54. {
  55. __u64 start_time;
  56. char buf[4];
  57. int i, fd;
  58. fd = open("/dev/urandom", O_RDONLY);
  59. if (fd < 0) {
  60. printf("couldn't open /dev/urandom\n");
  61. exit(1);
  62. }
  63. start_time = time_get_ns();
  64. for (i = 0; i < MAX_CNT; i++) {
  65. if (read(fd, buf, sizeof(buf)) < 0) {
  66. printf("failed to read from /dev/urandom: %s\n", strerror(errno));
  67. close(fd);
  68. return;
  69. }
  70. }
  71. printf("urandom_read:%d: %lld events per sec\n",
  72. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  73. close(fd);
  74. }
  75. static void loop(int cpu, int flags)
  76. {
  77. cpu_set_t cpuset;
  78. CPU_ZERO(&cpuset);
  79. CPU_SET(cpu, &cpuset);
  80. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  81. if (flags & 1)
  82. test_task_rename(cpu);
  83. if (flags & 2)
  84. test_urandom_read(cpu);
  85. }
  86. static void run_perf_test(int tasks, int flags)
  87. {
  88. pid_t pid[tasks];
  89. int i;
  90. for (i = 0; i < tasks; i++) {
  91. pid[i] = fork();
  92. if (pid[i] == 0) {
  93. loop(i, flags);
  94. exit(0);
  95. } else if (pid[i] == -1) {
  96. printf("couldn't spawn #%d process\n", i);
  97. exit(1);
  98. }
  99. }
  100. for (i = 0; i < tasks; i++) {
  101. int status;
  102. assert(waitpid(pid[i], &status, 0) == pid[i]);
  103. assert(status == 0);
  104. }
  105. }
  106. static int load_progs(char *filename)
  107. {
  108. struct bpf_program *prog;
  109. int err = 0;
  110. obj = bpf_object__open_file(filename, NULL);
  111. err = libbpf_get_error(obj);
  112. if (err < 0) {
  113. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  114. return err;
  115. }
  116. /* load BPF program */
  117. err = bpf_object__load(obj);
  118. if (err < 0) {
  119. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  120. return err;
  121. }
  122. bpf_object__for_each_program(prog, obj) {
  123. links[cnt] = bpf_program__attach(prog);
  124. err = libbpf_get_error(links[cnt]);
  125. if (err < 0) {
  126. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  127. links[cnt] = NULL;
  128. return err;
  129. }
  130. cnt++;
  131. }
  132. return err;
  133. }
  134. static void unload_progs(void)
  135. {
  136. while (cnt)
  137. bpf_link__destroy(links[--cnt]);
  138. bpf_object__close(obj);
  139. }
  140. int main(int argc, char **argv)
  141. {
  142. int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
  143. int test_flags = ~0;
  144. char filename[256];
  145. int err = 0;
  146. if (argc > 1)
  147. test_flags = atoi(argv[1]) ? : test_flags;
  148. if (argc > 2)
  149. num_cpu = atoi(argv[2]) ? : num_cpu;
  150. if (test_flags & 0x3) {
  151. printf("BASE\n");
  152. run_perf_test(num_cpu, test_flags);
  153. }
  154. if (test_flags & 0xC) {
  155. snprintf(filename, sizeof(filename),
  156. "%s_kprobe_kern.o", argv[0]);
  157. printf("w/KPROBE\n");
  158. err = load_progs(filename);
  159. if (!err)
  160. run_perf_test(num_cpu, test_flags >> 2);
  161. unload_progs();
  162. }
  163. if (test_flags & 0x30) {
  164. snprintf(filename, sizeof(filename),
  165. "%s_tp_kern.o", argv[0]);
  166. printf("w/TRACEPOINT\n");
  167. err = load_progs(filename);
  168. if (!err)
  169. run_perf_test(num_cpu, test_flags >> 4);
  170. unload_progs();
  171. }
  172. if (test_flags & 0xC0) {
  173. snprintf(filename, sizeof(filename),
  174. "%s_raw_tp_kern.o", argv[0]);
  175. printf("w/RAW_TRACEPOINT\n");
  176. err = load_progs(filename);
  177. if (!err)
  178. run_perf_test(num_cpu, test_flags >> 6);
  179. unload_progs();
  180. }
  181. return err;
  182. }