test_overhead_user.c 3.7 KB

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