map_perf_test_user.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <arpa/inet.h>
  22. #include <errno.h>
  23. #include "libbpf.h"
  24. #include "bpf_load.h"
  25. #define TEST_BIT(t) (1U << (t))
  26. #define MAX_NR_CPUS 1024
  27. static __u64 time_get_ns(void)
  28. {
  29. struct timespec ts;
  30. clock_gettime(CLOCK_MONOTONIC, &ts);
  31. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  32. }
  33. enum test_type {
  34. HASH_PREALLOC,
  35. PERCPU_HASH_PREALLOC,
  36. HASH_KMALLOC,
  37. PERCPU_HASH_KMALLOC,
  38. LRU_HASH_PREALLOC,
  39. NOCOMMON_LRU_HASH_PREALLOC,
  40. LPM_KMALLOC,
  41. HASH_LOOKUP,
  42. ARRAY_LOOKUP,
  43. INNER_LRU_HASH_PREALLOC,
  44. NR_TESTS,
  45. };
  46. const char *test_map_names[NR_TESTS] = {
  47. [HASH_PREALLOC] = "hash_map",
  48. [PERCPU_HASH_PREALLOC] = "percpu_hash_map",
  49. [HASH_KMALLOC] = "hash_map_alloc",
  50. [PERCPU_HASH_KMALLOC] = "percpu_hash_map_alloc",
  51. [LRU_HASH_PREALLOC] = "lru_hash_map",
  52. [NOCOMMON_LRU_HASH_PREALLOC] = "nocommon_lru_hash_map",
  53. [LPM_KMALLOC] = "lpm_trie_map_alloc",
  54. [HASH_LOOKUP] = "hash_map",
  55. [ARRAY_LOOKUP] = "array_map",
  56. [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
  57. };
  58. static int test_flags = ~0;
  59. static uint32_t num_map_entries;
  60. static uint32_t inner_lru_hash_size;
  61. static int inner_lru_hash_idx = -1;
  62. static int array_of_lru_hashs_idx = -1;
  63. static uint32_t max_cnt = 1000000;
  64. static int check_test_flags(enum test_type t)
  65. {
  66. return test_flags & TEST_BIT(t);
  67. }
  68. static void test_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_getuid);
  75. printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
  76. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  77. }
  78. static void do_test_lru(enum test_type test, int cpu)
  79. {
  80. static int inner_lru_map_fds[MAX_NR_CPUS];
  81. struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
  82. const char *test_name;
  83. __u64 start_time;
  84. int i, ret;
  85. if (test == INNER_LRU_HASH_PREALLOC) {
  86. int outer_fd = map_fd[array_of_lru_hashs_idx];
  87. assert(cpu < MAX_NR_CPUS);
  88. if (cpu) {
  89. inner_lru_map_fds[cpu] =
  90. bpf_create_map(BPF_MAP_TYPE_LRU_HASH,
  91. sizeof(uint32_t), sizeof(long),
  92. inner_lru_hash_size, 0);
  93. if (inner_lru_map_fds[cpu] == -1) {
  94. printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
  95. strerror(errno), errno);
  96. exit(1);
  97. }
  98. } else {
  99. inner_lru_map_fds[cpu] = map_fd[inner_lru_hash_idx];
  100. }
  101. ret = bpf_map_update_elem(outer_fd, &cpu,
  102. &inner_lru_map_fds[cpu],
  103. BPF_ANY);
  104. if (ret) {
  105. printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
  106. cpu, strerror(errno), errno);
  107. exit(1);
  108. }
  109. }
  110. in6.sin6_addr.s6_addr16[0] = 0xdead;
  111. in6.sin6_addr.s6_addr16[1] = 0xbeef;
  112. if (test == LRU_HASH_PREALLOC) {
  113. test_name = "lru_hash_map_perf";
  114. in6.sin6_addr.s6_addr16[7] = 0;
  115. } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
  116. test_name = "nocommon_lru_hash_map_perf";
  117. in6.sin6_addr.s6_addr16[7] = 1;
  118. } else if (test == INNER_LRU_HASH_PREALLOC) {
  119. test_name = "inner_lru_hash_map_perf";
  120. in6.sin6_addr.s6_addr16[7] = 2;
  121. } else {
  122. assert(0);
  123. }
  124. start_time = time_get_ns();
  125. for (i = 0; i < max_cnt; i++) {
  126. ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
  127. assert(ret == -1 && errno == EBADF);
  128. }
  129. printf("%d:%s pre-alloc %lld events per sec\n",
  130. cpu, test_name,
  131. max_cnt * 1000000000ll / (time_get_ns() - start_time));
  132. }
  133. static void test_lru_hash_prealloc(int cpu)
  134. {
  135. do_test_lru(LRU_HASH_PREALLOC, cpu);
  136. }
  137. static void test_nocommon_lru_hash_prealloc(int cpu)
  138. {
  139. do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
  140. }
  141. static void test_inner_lru_hash_prealloc(int cpu)
  142. {
  143. do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
  144. }
  145. static void test_percpu_hash_prealloc(int cpu)
  146. {
  147. __u64 start_time;
  148. int i;
  149. start_time = time_get_ns();
  150. for (i = 0; i < max_cnt; i++)
  151. syscall(__NR_geteuid);
  152. printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
  153. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  154. }
  155. static void test_hash_kmalloc(int cpu)
  156. {
  157. __u64 start_time;
  158. int i;
  159. start_time = time_get_ns();
  160. for (i = 0; i < max_cnt; i++)
  161. syscall(__NR_getgid);
  162. printf("%d:hash_map_perf kmalloc %lld events per sec\n",
  163. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  164. }
  165. static void test_percpu_hash_kmalloc(int cpu)
  166. {
  167. __u64 start_time;
  168. int i;
  169. start_time = time_get_ns();
  170. for (i = 0; i < max_cnt; i++)
  171. syscall(__NR_getegid);
  172. printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
  173. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  174. }
  175. static void test_lpm_kmalloc(int cpu)
  176. {
  177. __u64 start_time;
  178. int i;
  179. start_time = time_get_ns();
  180. for (i = 0; i < max_cnt; i++)
  181. syscall(__NR_gettid);
  182. printf("%d:lpm_perf kmalloc %lld events per sec\n",
  183. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  184. }
  185. static void test_hash_lookup(int cpu)
  186. {
  187. __u64 start_time;
  188. int i;
  189. start_time = time_get_ns();
  190. for (i = 0; i < max_cnt; i++)
  191. syscall(__NR_getpgid, 0);
  192. printf("%d:hash_lookup %lld lookups per sec\n",
  193. cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
  194. }
  195. static void test_array_lookup(int cpu)
  196. {
  197. __u64 start_time;
  198. int i;
  199. start_time = time_get_ns();
  200. for (i = 0; i < max_cnt; i++)
  201. syscall(__NR_getpgrp, 0);
  202. printf("%d:array_lookup %lld lookups per sec\n",
  203. cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
  204. }
  205. typedef void (*test_func)(int cpu);
  206. const test_func test_funcs[] = {
  207. [HASH_PREALLOC] = test_hash_prealloc,
  208. [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc,
  209. [HASH_KMALLOC] = test_hash_kmalloc,
  210. [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc,
  211. [LRU_HASH_PREALLOC] = test_lru_hash_prealloc,
  212. [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc,
  213. [LPM_KMALLOC] = test_lpm_kmalloc,
  214. [HASH_LOOKUP] = test_hash_lookup,
  215. [ARRAY_LOOKUP] = test_array_lookup,
  216. [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
  217. };
  218. static void loop(int cpu)
  219. {
  220. cpu_set_t cpuset;
  221. int i;
  222. CPU_ZERO(&cpuset);
  223. CPU_SET(cpu, &cpuset);
  224. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  225. for (i = 0; i < NR_TESTS; i++) {
  226. if (check_test_flags(i))
  227. test_funcs[i](cpu);
  228. }
  229. }
  230. static void run_perf_test(int tasks)
  231. {
  232. pid_t pid[tasks];
  233. int i;
  234. for (i = 0; i < tasks; i++) {
  235. pid[i] = fork();
  236. if (pid[i] == 0) {
  237. loop(i);
  238. exit(0);
  239. } else if (pid[i] == -1) {
  240. printf("couldn't spawn #%d process\n", i);
  241. exit(1);
  242. }
  243. }
  244. for (i = 0; i < tasks; i++) {
  245. int status;
  246. assert(waitpid(pid[i], &status, 0) == pid[i]);
  247. assert(status == 0);
  248. }
  249. }
  250. static void fill_lpm_trie(void)
  251. {
  252. struct bpf_lpm_trie_key *key;
  253. unsigned long value = 0;
  254. unsigned int i;
  255. int r;
  256. key = alloca(sizeof(*key) + 4);
  257. key->prefixlen = 32;
  258. for (i = 0; i < 512; ++i) {
  259. key->prefixlen = rand() % 33;
  260. key->data[0] = rand() & 0xff;
  261. key->data[1] = rand() & 0xff;
  262. key->data[2] = rand() & 0xff;
  263. key->data[3] = rand() & 0xff;
  264. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  265. assert(!r);
  266. }
  267. key->prefixlen = 32;
  268. key->data[0] = 192;
  269. key->data[1] = 168;
  270. key->data[2] = 0;
  271. key->data[3] = 1;
  272. value = 128;
  273. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  274. assert(!r);
  275. }
  276. static void fixup_map(struct bpf_map_data *map, int idx)
  277. {
  278. int i;
  279. if (!strcmp("inner_lru_hash_map", map->name)) {
  280. inner_lru_hash_idx = idx;
  281. inner_lru_hash_size = map->def.max_entries;
  282. }
  283. if (!strcmp("array_of_lru_hashs", map->name)) {
  284. if (inner_lru_hash_idx == -1) {
  285. printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
  286. exit(1);
  287. }
  288. map->def.inner_map_idx = inner_lru_hash_idx;
  289. array_of_lru_hashs_idx = idx;
  290. }
  291. if (num_map_entries <= 0)
  292. return;
  293. inner_lru_hash_size = num_map_entries;
  294. /* Only change the max_entries for the enabled test(s) */
  295. for (i = 0; i < NR_TESTS; i++) {
  296. if (!strcmp(test_map_names[i], map->name) &&
  297. (check_test_flags(i))) {
  298. map->def.max_entries = num_map_entries;
  299. }
  300. }
  301. }
  302. int main(int argc, char **argv)
  303. {
  304. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  305. char filename[256];
  306. int num_cpu = 8;
  307. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  308. setrlimit(RLIMIT_MEMLOCK, &r);
  309. if (argc > 1)
  310. test_flags = atoi(argv[1]) ? : test_flags;
  311. if (argc > 2)
  312. num_cpu = atoi(argv[2]) ? : num_cpu;
  313. if (argc > 3)
  314. num_map_entries = atoi(argv[3]);
  315. if (argc > 4)
  316. max_cnt = atoi(argv[4]);
  317. if (load_bpf_file_fixup_map(filename, fixup_map)) {
  318. printf("%s", bpf_log_buf);
  319. return 1;
  320. }
  321. fill_lpm_trie();
  322. run_perf_test(num_cpu);
  323. return 0;
  324. }