map_perf_test_user.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <unistd.h>
  13. #include <assert.h>
  14. #include <sys/wait.h>
  15. #include <stdlib.h>
  16. #include <signal.h>
  17. #include <linux/bpf.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <sys/resource.h>
  21. #include "libbpf.h"
  22. #include "bpf_load.h"
  23. #define MAX_CNT 1000000
  24. static __u64 time_get_ns(void)
  25. {
  26. struct timespec ts;
  27. clock_gettime(CLOCK_MONOTONIC, &ts);
  28. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  29. }
  30. #define HASH_PREALLOC (1 << 0)
  31. #define PERCPU_HASH_PREALLOC (1 << 1)
  32. #define HASH_KMALLOC (1 << 2)
  33. #define PERCPU_HASH_KMALLOC (1 << 3)
  34. #define LRU_HASH_PREALLOC (1 << 4)
  35. #define PERCPU_LRU_HASH_PREALLOC (1 << 5)
  36. #define LPM_KMALLOC (1 << 6)
  37. static int test_flags = ~0;
  38. static void test_hash_prealloc(int cpu)
  39. {
  40. __u64 start_time;
  41. int i;
  42. start_time = time_get_ns();
  43. for (i = 0; i < MAX_CNT; i++)
  44. syscall(__NR_getuid);
  45. printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
  46. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  47. }
  48. static void test_lru_hash_prealloc(int cpu)
  49. {
  50. __u64 start_time;
  51. int i;
  52. start_time = time_get_ns();
  53. for (i = 0; i < MAX_CNT; i++)
  54. syscall(__NR_getpid);
  55. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  56. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  57. }
  58. static void test_percpu_lru_hash_prealloc(int cpu)
  59. {
  60. __u64 start_time;
  61. int i;
  62. start_time = time_get_ns();
  63. for (i = 0; i < MAX_CNT; i++)
  64. syscall(__NR_getppid);
  65. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  66. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  67. }
  68. static void test_percpu_hash_prealloc(int cpu)
  69. {
  70. __u64 start_time;
  71. int i;
  72. start_time = time_get_ns();
  73. for (i = 0; i < MAX_CNT; i++)
  74. syscall(__NR_geteuid);
  75. printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
  76. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  77. }
  78. static void test_hash_kmalloc(int cpu)
  79. {
  80. __u64 start_time;
  81. int i;
  82. start_time = time_get_ns();
  83. for (i = 0; i < MAX_CNT; i++)
  84. syscall(__NR_getgid);
  85. printf("%d:hash_map_perf kmalloc %lld events per sec\n",
  86. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  87. }
  88. static void test_percpu_hash_kmalloc(int cpu)
  89. {
  90. __u64 start_time;
  91. int i;
  92. start_time = time_get_ns();
  93. for (i = 0; i < MAX_CNT; i++)
  94. syscall(__NR_getegid);
  95. printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
  96. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  97. }
  98. static void test_lpm_kmalloc(int cpu)
  99. {
  100. __u64 start_time;
  101. int i;
  102. start_time = time_get_ns();
  103. for (i = 0; i < MAX_CNT; i++)
  104. syscall(__NR_gettid);
  105. printf("%d:lpm_perf kmalloc %lld events per sec\n",
  106. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  107. }
  108. static void loop(int cpu)
  109. {
  110. cpu_set_t cpuset;
  111. CPU_ZERO(&cpuset);
  112. CPU_SET(cpu, &cpuset);
  113. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  114. if (test_flags & HASH_PREALLOC)
  115. test_hash_prealloc(cpu);
  116. if (test_flags & PERCPU_HASH_PREALLOC)
  117. test_percpu_hash_prealloc(cpu);
  118. if (test_flags & HASH_KMALLOC)
  119. test_hash_kmalloc(cpu);
  120. if (test_flags & PERCPU_HASH_KMALLOC)
  121. test_percpu_hash_kmalloc(cpu);
  122. if (test_flags & LRU_HASH_PREALLOC)
  123. test_lru_hash_prealloc(cpu);
  124. if (test_flags & PERCPU_LRU_HASH_PREALLOC)
  125. test_percpu_lru_hash_prealloc(cpu);
  126. if (test_flags & LPM_KMALLOC)
  127. test_lpm_kmalloc(cpu);
  128. }
  129. static void run_perf_test(int tasks)
  130. {
  131. pid_t pid[tasks];
  132. int i;
  133. for (i = 0; i < tasks; i++) {
  134. pid[i] = fork();
  135. if (pid[i] == 0) {
  136. loop(i);
  137. exit(0);
  138. } else if (pid[i] == -1) {
  139. printf("couldn't spawn #%d process\n", i);
  140. exit(1);
  141. }
  142. }
  143. for (i = 0; i < tasks; i++) {
  144. int status;
  145. assert(waitpid(pid[i], &status, 0) == pid[i]);
  146. assert(status == 0);
  147. }
  148. }
  149. static void fill_lpm_trie(void)
  150. {
  151. struct bpf_lpm_trie_key *key;
  152. unsigned long value = 0;
  153. unsigned int i;
  154. int r;
  155. key = alloca(sizeof(*key) + 4);
  156. key->prefixlen = 32;
  157. for (i = 0; i < 512; ++i) {
  158. key->prefixlen = rand() % 33;
  159. key->data[0] = rand() & 0xff;
  160. key->data[1] = rand() & 0xff;
  161. key->data[2] = rand() & 0xff;
  162. key->data[3] = rand() & 0xff;
  163. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  164. assert(!r);
  165. }
  166. key->prefixlen = 32;
  167. key->data[0] = 192;
  168. key->data[1] = 168;
  169. key->data[2] = 0;
  170. key->data[3] = 1;
  171. value = 128;
  172. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  173. assert(!r);
  174. }
  175. int main(int argc, char **argv)
  176. {
  177. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  178. char filename[256];
  179. int num_cpu = 8;
  180. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  181. setrlimit(RLIMIT_MEMLOCK, &r);
  182. if (argc > 1)
  183. test_flags = atoi(argv[1]) ? : test_flags;
  184. if (argc > 2)
  185. num_cpu = atoi(argv[2]) ? : num_cpu;
  186. if (load_bpf_file(filename)) {
  187. printf("%s", bpf_log_buf);
  188. return 1;
  189. }
  190. fill_lpm_trie();
  191. run_perf_test(num_cpu);
  192. return 0;
  193. }