map_perf_test_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #define _GNU_SOURCE
  5. #include <sched.h>
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <asm/unistd.h>
  9. #include <unistd.h>
  10. #include <assert.h>
  11. #include <sys/wait.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <arpa/inet.h>
  17. #include <errno.h>
  18. #include <bpf/bpf.h>
  19. #include <bpf/libbpf.h>
  20. #define TEST_BIT(t) (1U << (t))
  21. #define MAX_NR_CPUS 1024
  22. static __u64 time_get_ns(void)
  23. {
  24. struct timespec ts;
  25. clock_gettime(CLOCK_MONOTONIC, &ts);
  26. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  27. }
  28. enum test_type {
  29. HASH_PREALLOC,
  30. PERCPU_HASH_PREALLOC,
  31. HASH_KMALLOC,
  32. PERCPU_HASH_KMALLOC,
  33. LRU_HASH_PREALLOC,
  34. NOCOMMON_LRU_HASH_PREALLOC,
  35. LPM_KMALLOC,
  36. HASH_LOOKUP,
  37. ARRAY_LOOKUP,
  38. INNER_LRU_HASH_PREALLOC,
  39. LRU_HASH_LOOKUP,
  40. NR_TESTS,
  41. };
  42. const char *test_map_names[NR_TESTS] = {
  43. [HASH_PREALLOC] = "hash_map",
  44. [PERCPU_HASH_PREALLOC] = "percpu_hash_map",
  45. [HASH_KMALLOC] = "hash_map_alloc",
  46. [PERCPU_HASH_KMALLOC] = "percpu_hash_map_alloc",
  47. [LRU_HASH_PREALLOC] = "lru_hash_map",
  48. [NOCOMMON_LRU_HASH_PREALLOC] = "nocommon_lru_hash_map",
  49. [LPM_KMALLOC] = "lpm_trie_map_alloc",
  50. [HASH_LOOKUP] = "hash_map",
  51. [ARRAY_LOOKUP] = "array_map",
  52. [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
  53. [LRU_HASH_LOOKUP] = "lru_hash_lookup_map",
  54. };
  55. enum map_idx {
  56. array_of_lru_hashs_idx,
  57. hash_map_alloc_idx,
  58. lru_hash_lookup_idx,
  59. NR_IDXES,
  60. };
  61. static int map_fd[NR_IDXES];
  62. static int test_flags = ~0;
  63. static uint32_t num_map_entries;
  64. static uint32_t inner_lru_hash_size;
  65. static int lru_hash_lookup_test_entries = 32;
  66. static uint32_t max_cnt = 10000;
  67. static int check_test_flags(enum test_type t)
  68. {
  69. return test_flags & TEST_BIT(t);
  70. }
  71. static void test_hash_prealloc(int cpu)
  72. {
  73. __u64 start_time;
  74. int i;
  75. start_time = time_get_ns();
  76. for (i = 0; i < max_cnt; i++)
  77. syscall(__NR_getuid);
  78. printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
  79. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  80. }
  81. static int pre_test_lru_hash_lookup(int tasks)
  82. {
  83. int fd = map_fd[lru_hash_lookup_idx];
  84. uint32_t key;
  85. long val = 1;
  86. int ret;
  87. if (num_map_entries > lru_hash_lookup_test_entries)
  88. lru_hash_lookup_test_entries = num_map_entries;
  89. /* Populate the lru_hash_map for LRU_HASH_LOOKUP perf test.
  90. *
  91. * It is fine that the user requests for a map with
  92. * num_map_entries < 32 and some of the later lru hash lookup
  93. * may return not found. For LRU map, we are not interested
  94. * in such small map performance.
  95. */
  96. for (key = 0; key < lru_hash_lookup_test_entries; key++) {
  97. ret = bpf_map_update_elem(fd, &key, &val, BPF_NOEXIST);
  98. if (ret)
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. static void do_test_lru(enum test_type test, int cpu)
  104. {
  105. static int inner_lru_map_fds[MAX_NR_CPUS];
  106. struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
  107. const char *test_name;
  108. __u64 start_time;
  109. int i, ret;
  110. if (test == INNER_LRU_HASH_PREALLOC && cpu) {
  111. /* If CPU is not 0, create inner_lru hash map and insert the fd
  112. * value into the array_of_lru_hash map. In case of CPU 0,
  113. * 'inner_lru_hash_map' was statically inserted on the map init
  114. */
  115. int outer_fd = map_fd[array_of_lru_hashs_idx];
  116. unsigned int mycpu, mynode;
  117. LIBBPF_OPTS(bpf_map_create_opts, opts,
  118. .map_flags = BPF_F_NUMA_NODE,
  119. );
  120. assert(cpu < MAX_NR_CPUS);
  121. ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL);
  122. assert(!ret);
  123. opts.numa_node = mynode;
  124. inner_lru_map_fds[cpu] =
  125. bpf_map_create(BPF_MAP_TYPE_LRU_HASH,
  126. test_map_names[INNER_LRU_HASH_PREALLOC],
  127. sizeof(uint32_t),
  128. sizeof(long),
  129. inner_lru_hash_size, &opts);
  130. if (inner_lru_map_fds[cpu] == -1) {
  131. printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
  132. strerror(errno), errno);
  133. exit(1);
  134. }
  135. ret = bpf_map_update_elem(outer_fd, &cpu,
  136. &inner_lru_map_fds[cpu],
  137. BPF_ANY);
  138. if (ret) {
  139. printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
  140. cpu, strerror(errno), errno);
  141. exit(1);
  142. }
  143. }
  144. in6.sin6_addr.s6_addr16[0] = 0xdead;
  145. in6.sin6_addr.s6_addr16[1] = 0xbeef;
  146. if (test == LRU_HASH_PREALLOC) {
  147. test_name = "lru_hash_map_perf";
  148. in6.sin6_addr.s6_addr16[2] = 0;
  149. } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
  150. test_name = "nocommon_lru_hash_map_perf";
  151. in6.sin6_addr.s6_addr16[2] = 1;
  152. } else if (test == INNER_LRU_HASH_PREALLOC) {
  153. test_name = "inner_lru_hash_map_perf";
  154. in6.sin6_addr.s6_addr16[2] = 2;
  155. } else if (test == LRU_HASH_LOOKUP) {
  156. test_name = "lru_hash_lookup_perf";
  157. in6.sin6_addr.s6_addr16[2] = 3;
  158. in6.sin6_addr.s6_addr32[3] = 0;
  159. } else {
  160. assert(0);
  161. }
  162. start_time = time_get_ns();
  163. for (i = 0; i < max_cnt; i++) {
  164. ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
  165. assert(ret == -1 && errno == EBADF);
  166. if (in6.sin6_addr.s6_addr32[3] <
  167. lru_hash_lookup_test_entries - 32)
  168. in6.sin6_addr.s6_addr32[3] += 32;
  169. else
  170. in6.sin6_addr.s6_addr32[3] = 0;
  171. }
  172. printf("%d:%s pre-alloc %lld events per sec\n",
  173. cpu, test_name,
  174. max_cnt * 1000000000ll / (time_get_ns() - start_time));
  175. }
  176. static void test_lru_hash_prealloc(int cpu)
  177. {
  178. do_test_lru(LRU_HASH_PREALLOC, cpu);
  179. }
  180. static void test_nocommon_lru_hash_prealloc(int cpu)
  181. {
  182. do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
  183. }
  184. static void test_inner_lru_hash_prealloc(int cpu)
  185. {
  186. do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
  187. }
  188. static void test_lru_hash_lookup(int cpu)
  189. {
  190. do_test_lru(LRU_HASH_LOOKUP, cpu);
  191. }
  192. static void test_percpu_hash_prealloc(int cpu)
  193. {
  194. __u64 start_time;
  195. int i;
  196. start_time = time_get_ns();
  197. for (i = 0; i < max_cnt; i++)
  198. syscall(__NR_geteuid);
  199. printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
  200. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  201. }
  202. static void test_hash_kmalloc(int cpu)
  203. {
  204. __u64 start_time;
  205. int i;
  206. start_time = time_get_ns();
  207. for (i = 0; i < max_cnt; i++)
  208. syscall(__NR_getgid);
  209. printf("%d:hash_map_perf kmalloc %lld events per sec\n",
  210. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  211. }
  212. static void test_percpu_hash_kmalloc(int cpu)
  213. {
  214. __u64 start_time;
  215. int i;
  216. start_time = time_get_ns();
  217. for (i = 0; i < max_cnt; i++)
  218. syscall(__NR_getegid);
  219. printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
  220. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  221. }
  222. static void test_lpm_kmalloc(int cpu)
  223. {
  224. __u64 start_time;
  225. int i;
  226. start_time = time_get_ns();
  227. for (i = 0; i < max_cnt; i++)
  228. syscall(__NR_gettid);
  229. printf("%d:lpm_perf kmalloc %lld events per sec\n",
  230. cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
  231. }
  232. static void test_hash_lookup(int cpu)
  233. {
  234. __u64 start_time;
  235. int i;
  236. start_time = time_get_ns();
  237. for (i = 0; i < max_cnt; i++)
  238. syscall(__NR_getpgid, 0);
  239. printf("%d:hash_lookup %lld lookups per sec\n",
  240. cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
  241. }
  242. static void test_array_lookup(int cpu)
  243. {
  244. __u64 start_time;
  245. int i;
  246. start_time = time_get_ns();
  247. for (i = 0; i < max_cnt; i++)
  248. syscall(__NR_getppid, 0);
  249. printf("%d:array_lookup %lld lookups per sec\n",
  250. cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
  251. }
  252. typedef int (*pre_test_func)(int tasks);
  253. const pre_test_func pre_test_funcs[] = {
  254. [LRU_HASH_LOOKUP] = pre_test_lru_hash_lookup,
  255. };
  256. typedef void (*test_func)(int cpu);
  257. const test_func test_funcs[] = {
  258. [HASH_PREALLOC] = test_hash_prealloc,
  259. [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc,
  260. [HASH_KMALLOC] = test_hash_kmalloc,
  261. [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc,
  262. [LRU_HASH_PREALLOC] = test_lru_hash_prealloc,
  263. [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc,
  264. [LPM_KMALLOC] = test_lpm_kmalloc,
  265. [HASH_LOOKUP] = test_hash_lookup,
  266. [ARRAY_LOOKUP] = test_array_lookup,
  267. [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
  268. [LRU_HASH_LOOKUP] = test_lru_hash_lookup,
  269. };
  270. static int pre_test(int tasks)
  271. {
  272. int i;
  273. for (i = 0; i < NR_TESTS; i++) {
  274. if (pre_test_funcs[i] && check_test_flags(i)) {
  275. int ret = pre_test_funcs[i](tasks);
  276. if (ret)
  277. return ret;
  278. }
  279. }
  280. return 0;
  281. }
  282. static void loop(int cpu)
  283. {
  284. cpu_set_t cpuset;
  285. int i;
  286. CPU_ZERO(&cpuset);
  287. CPU_SET(cpu, &cpuset);
  288. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  289. for (i = 0; i < NR_TESTS; i++) {
  290. if (check_test_flags(i))
  291. test_funcs[i](cpu);
  292. }
  293. }
  294. static void run_perf_test(int tasks)
  295. {
  296. pid_t pid[tasks];
  297. int i;
  298. assert(!pre_test(tasks));
  299. for (i = 0; i < tasks; i++) {
  300. pid[i] = fork();
  301. if (pid[i] == 0) {
  302. loop(i);
  303. exit(0);
  304. } else if (pid[i] == -1) {
  305. printf("couldn't spawn #%d process\n", i);
  306. exit(1);
  307. }
  308. }
  309. for (i = 0; i < tasks; i++) {
  310. int status;
  311. assert(waitpid(pid[i], &status, 0) == pid[i]);
  312. assert(status == 0);
  313. }
  314. }
  315. static void fill_lpm_trie(void)
  316. {
  317. struct bpf_lpm_trie_key *key;
  318. unsigned long value = 0;
  319. unsigned int i;
  320. int r;
  321. key = alloca(sizeof(*key) + 4);
  322. key->prefixlen = 32;
  323. for (i = 0; i < 512; ++i) {
  324. key->prefixlen = rand() % 33;
  325. key->data[0] = rand() & 0xff;
  326. key->data[1] = rand() & 0xff;
  327. key->data[2] = rand() & 0xff;
  328. key->data[3] = rand() & 0xff;
  329. r = bpf_map_update_elem(map_fd[hash_map_alloc_idx],
  330. key, &value, 0);
  331. assert(!r);
  332. }
  333. key->prefixlen = 32;
  334. key->data[0] = 192;
  335. key->data[1] = 168;
  336. key->data[2] = 0;
  337. key->data[3] = 1;
  338. value = 128;
  339. r = bpf_map_update_elem(map_fd[hash_map_alloc_idx], key, &value, 0);
  340. assert(!r);
  341. }
  342. static void fixup_map(struct bpf_object *obj)
  343. {
  344. struct bpf_map *map;
  345. int i;
  346. bpf_object__for_each_map(map, obj) {
  347. const char *name = bpf_map__name(map);
  348. /* Only change the max_entries for the enabled test(s) */
  349. for (i = 0; i < NR_TESTS; i++) {
  350. if (!strcmp(test_map_names[i], name) &&
  351. (check_test_flags(i))) {
  352. bpf_map__set_max_entries(map, num_map_entries);
  353. continue;
  354. }
  355. }
  356. }
  357. inner_lru_hash_size = num_map_entries;
  358. }
  359. int main(int argc, char **argv)
  360. {
  361. int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  362. struct bpf_link *links[8];
  363. struct bpf_program *prog;
  364. struct bpf_object *obj;
  365. struct bpf_map *map;
  366. char filename[256];
  367. int i = 0;
  368. if (argc > 1)
  369. test_flags = atoi(argv[1]) ? : test_flags;
  370. if (argc > 2)
  371. nr_cpus = atoi(argv[2]) ? : nr_cpus;
  372. if (argc > 3)
  373. num_map_entries = atoi(argv[3]);
  374. if (argc > 4)
  375. max_cnt = atoi(argv[4]);
  376. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  377. obj = bpf_object__open_file(filename, NULL);
  378. if (libbpf_get_error(obj)) {
  379. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  380. return 0;
  381. }
  382. map = bpf_object__find_map_by_name(obj, "inner_lru_hash_map");
  383. if (libbpf_get_error(map)) {
  384. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  385. goto cleanup;
  386. }
  387. inner_lru_hash_size = bpf_map__max_entries(map);
  388. if (!inner_lru_hash_size) {
  389. fprintf(stderr, "ERROR: failed to get map attribute\n");
  390. goto cleanup;
  391. }
  392. /* resize BPF map prior to loading */
  393. if (num_map_entries > 0)
  394. fixup_map(obj);
  395. /* load BPF program */
  396. if (bpf_object__load(obj)) {
  397. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  398. goto cleanup;
  399. }
  400. map_fd[0] = bpf_object__find_map_fd_by_name(obj, "array_of_lru_hashs");
  401. map_fd[1] = bpf_object__find_map_fd_by_name(obj, "hash_map_alloc");
  402. map_fd[2] = bpf_object__find_map_fd_by_name(obj, "lru_hash_lookup_map");
  403. if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) {
  404. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  405. goto cleanup;
  406. }
  407. bpf_object__for_each_program(prog, obj) {
  408. links[i] = bpf_program__attach(prog);
  409. if (libbpf_get_error(links[i])) {
  410. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  411. links[i] = NULL;
  412. goto cleanup;
  413. }
  414. i++;
  415. }
  416. fill_lpm_trie();
  417. run_perf_test(nr_cpus);
  418. cleanup:
  419. for (i--; i >= 0; i--)
  420. bpf_link__destroy(links[i]);
  421. bpf_object__close(obj);
  422. return 0;
  423. }