test_overhead_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <stdio.h>
  10. #include <sys/types.h>
  11. #include <asm/unistd.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <assert.h>
  15. #include <sys/wait.h>
  16. #include <stdlib.h>
  17. #include <signal.h>
  18. #include <linux/bpf.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <sys/resource.h>
  22. #include "libbpf.h"
  23. #include "bpf_load.h"
  24. #define MAX_CNT 1000000
  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. write(fd, buf, sizeof(buf));
  44. printf("task_rename:%d: %lld events per sec\n",
  45. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  46. close(fd);
  47. }
  48. static void test_urandom_read(int cpu)
  49. {
  50. __u64 start_time;
  51. char buf[4];
  52. int i, fd;
  53. fd = open("/dev/urandom", O_RDONLY);
  54. if (fd < 0) {
  55. printf("couldn't open /dev/urandom\n");
  56. exit(1);
  57. }
  58. start_time = time_get_ns();
  59. for (i = 0; i < MAX_CNT; i++)
  60. read(fd, buf, sizeof(buf));
  61. printf("urandom_read:%d: %lld events per sec\n",
  62. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  63. close(fd);
  64. }
  65. static void loop(int cpu, int flags)
  66. {
  67. cpu_set_t cpuset;
  68. CPU_ZERO(&cpuset);
  69. CPU_SET(cpu, &cpuset);
  70. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  71. if (flags & 1)
  72. test_task_rename(cpu);
  73. if (flags & 2)
  74. test_urandom_read(cpu);
  75. }
  76. static void run_perf_test(int tasks, int flags)
  77. {
  78. pid_t pid[tasks];
  79. int i;
  80. for (i = 0; i < tasks; i++) {
  81. pid[i] = fork();
  82. if (pid[i] == 0) {
  83. loop(i, flags);
  84. exit(0);
  85. } else if (pid[i] == -1) {
  86. printf("couldn't spawn #%d process\n", i);
  87. exit(1);
  88. }
  89. }
  90. for (i = 0; i < tasks; i++) {
  91. int status;
  92. assert(waitpid(pid[i], &status, 0) == pid[i]);
  93. assert(status == 0);
  94. }
  95. }
  96. static void unload_progs(void)
  97. {
  98. close(prog_fd[0]);
  99. close(prog_fd[1]);
  100. close(event_fd[0]);
  101. close(event_fd[1]);
  102. }
  103. int main(int argc, char **argv)
  104. {
  105. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  106. char filename[256];
  107. int num_cpu = 8;
  108. int test_flags = ~0;
  109. setrlimit(RLIMIT_MEMLOCK, &r);
  110. if (argc > 1)
  111. test_flags = atoi(argv[1]) ? : test_flags;
  112. if (argc > 2)
  113. num_cpu = atoi(argv[2]) ? : num_cpu;
  114. if (test_flags & 0x3) {
  115. printf("BASE\n");
  116. run_perf_test(num_cpu, test_flags);
  117. }
  118. if (test_flags & 0xC) {
  119. snprintf(filename, sizeof(filename),
  120. "%s_kprobe_kern.o", argv[0]);
  121. if (load_bpf_file(filename)) {
  122. printf("%s", bpf_log_buf);
  123. return 1;
  124. }
  125. printf("w/KPROBE\n");
  126. run_perf_test(num_cpu, test_flags >> 2);
  127. unload_progs();
  128. }
  129. if (test_flags & 0x30) {
  130. snprintf(filename, sizeof(filename),
  131. "%s_tp_kern.o", argv[0]);
  132. if (load_bpf_file(filename)) {
  133. printf("%s", bpf_log_buf);
  134. return 1;
  135. }
  136. printf("w/TRACEPOINT\n");
  137. run_perf_test(num_cpu, test_flags >> 4);
  138. unload_progs();
  139. }
  140. return 0;
  141. }