test_overhead_user.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <sys/resource.h>
  20. #include <bpf/bpf.h>
  21. #include "bpf_load.h"
  22. #define MAX_CNT 1000000
  23. static __u64 time_get_ns(void)
  24. {
  25. struct timespec ts;
  26. clock_gettime(CLOCK_MONOTONIC, &ts);
  27. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  28. }
  29. static void test_task_rename(int cpu)
  30. {
  31. __u64 start_time;
  32. char buf[] = "test\n";
  33. int i, fd;
  34. fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
  35. if (fd < 0) {
  36. printf("couldn't open /proc\n");
  37. exit(1);
  38. }
  39. start_time = time_get_ns();
  40. for (i = 0; i < MAX_CNT; i++) {
  41. if (write(fd, buf, sizeof(buf)) < 0) {
  42. printf("task rename failed: %s\n", strerror(errno));
  43. close(fd);
  44. return;
  45. }
  46. }
  47. printf("task_rename:%d: %lld events per sec\n",
  48. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  49. close(fd);
  50. }
  51. static void test_urandom_read(int cpu)
  52. {
  53. __u64 start_time;
  54. char buf[4];
  55. int i, fd;
  56. fd = open("/dev/urandom", O_RDONLY);
  57. if (fd < 0) {
  58. printf("couldn't open /dev/urandom\n");
  59. exit(1);
  60. }
  61. start_time = time_get_ns();
  62. for (i = 0; i < MAX_CNT; i++) {
  63. if (read(fd, buf, sizeof(buf)) < 0) {
  64. printf("failed to read from /dev/urandom: %s\n", strerror(errno));
  65. close(fd);
  66. return;
  67. }
  68. }
  69. printf("urandom_read:%d: %lld events per sec\n",
  70. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  71. close(fd);
  72. }
  73. static void loop(int cpu, int flags)
  74. {
  75. cpu_set_t cpuset;
  76. CPU_ZERO(&cpuset);
  77. CPU_SET(cpu, &cpuset);
  78. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  79. if (flags & 1)
  80. test_task_rename(cpu);
  81. if (flags & 2)
  82. test_urandom_read(cpu);
  83. }
  84. static void run_perf_test(int tasks, int flags)
  85. {
  86. pid_t pid[tasks];
  87. int i;
  88. for (i = 0; i < tasks; i++) {
  89. pid[i] = fork();
  90. if (pid[i] == 0) {
  91. loop(i, flags);
  92. exit(0);
  93. } else if (pid[i] == -1) {
  94. printf("couldn't spawn #%d process\n", i);
  95. exit(1);
  96. }
  97. }
  98. for (i = 0; i < tasks; i++) {
  99. int status;
  100. assert(waitpid(pid[i], &status, 0) == pid[i]);
  101. assert(status == 0);
  102. }
  103. }
  104. static void unload_progs(void)
  105. {
  106. close(prog_fd[0]);
  107. close(prog_fd[1]);
  108. close(event_fd[0]);
  109. close(event_fd[1]);
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  114. char filename[256];
  115. int num_cpu = 8;
  116. int test_flags = ~0;
  117. setrlimit(RLIMIT_MEMLOCK, &r);
  118. if (argc > 1)
  119. test_flags = atoi(argv[1]) ? : test_flags;
  120. if (argc > 2)
  121. num_cpu = atoi(argv[2]) ? : num_cpu;
  122. if (test_flags & 0x3) {
  123. printf("BASE\n");
  124. run_perf_test(num_cpu, test_flags);
  125. }
  126. if (test_flags & 0xC) {
  127. snprintf(filename, sizeof(filename),
  128. "%s_kprobe_kern.o", argv[0]);
  129. if (load_bpf_file(filename)) {
  130. printf("%s", bpf_log_buf);
  131. return 1;
  132. }
  133. printf("w/KPROBE\n");
  134. run_perf_test(num_cpu, test_flags >> 2);
  135. unload_progs();
  136. }
  137. if (test_flags & 0x30) {
  138. snprintf(filename, sizeof(filename),
  139. "%s_tp_kern.o", argv[0]);
  140. if (load_bpf_file(filename)) {
  141. printf("%s", bpf_log_buf);
  142. return 1;
  143. }
  144. printf("w/TRACEPOINT\n");
  145. run_perf_test(num_cpu, test_flags >> 4);
  146. unload_progs();
  147. }
  148. if (test_flags & 0xC0) {
  149. snprintf(filename, sizeof(filename),
  150. "%s_raw_tp_kern.o", argv[0]);
  151. if (load_bpf_file(filename)) {
  152. printf("%s", bpf_log_buf);
  153. return 1;
  154. }
  155. printf("w/RAW_TRACEPOINT\n");
  156. run_perf_test(num_cpu, test_flags >> 6);
  157. unload_progs();
  158. }
  159. return 0;
  160. }