1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
8 #include <asm/unistd.h>
16 #include <arpa/inet.h>
20 #include <bpf/libbpf.h>
22 #define TEST_BIT(t) (1U << (t))
23 #define MAX_NR_CPUS 1024
25 static __u64 time_get_ns(void)
29 clock_gettime(CLOCK_MONOTONIC, &ts);
30 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
39 NOCOMMON_LRU_HASH_PREALLOC,
43 INNER_LRU_HASH_PREALLOC,
48 const char *test_map_names[NR_TESTS] = {
49 [HASH_PREALLOC] = "hash_map",
50 [PERCPU_HASH_PREALLOC] = "percpu_hash_map",
51 [HASH_KMALLOC] = "hash_map_alloc",
52 [PERCPU_HASH_KMALLOC] = "percpu_hash_map_alloc",
53 [LRU_HASH_PREALLOC] = "lru_hash_map",
54 [NOCOMMON_LRU_HASH_PREALLOC] = "nocommon_lru_hash_map",
55 [LPM_KMALLOC] = "lpm_trie_map_alloc",
56 [HASH_LOOKUP] = "hash_map",
57 [ARRAY_LOOKUP] = "array_map",
58 [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
59 [LRU_HASH_LOOKUP] = "lru_hash_lookup_map",
63 array_of_lru_hashs_idx,
69 static int map_fd[NR_IDXES];
71 static int test_flags = ~0;
72 static uint32_t num_map_entries;
73 static uint32_t inner_lru_hash_size;
74 static int lru_hash_lookup_test_entries = 32;
75 static uint32_t max_cnt = 10000;
77 static int check_test_flags(enum test_type t)
79 return test_flags & TEST_BIT(t);
82 static void test_hash_prealloc(int cpu)
87 start_time = time_get_ns();
88 for (i = 0; i < max_cnt; i++)
90 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
91 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
94 static int pre_test_lru_hash_lookup(int tasks)
96 int fd = map_fd[lru_hash_lookup_idx];
101 if (num_map_entries > lru_hash_lookup_test_entries)
102 lru_hash_lookup_test_entries = num_map_entries;
104 /* Populate the lru_hash_map for LRU_HASH_LOOKUP perf test.
106 * It is fine that the user requests for a map with
107 * num_map_entries < 32 and some of the later lru hash lookup
108 * may return not found. For LRU map, we are not interested
109 * in such small map performance.
111 for (key = 0; key < lru_hash_lookup_test_entries; key++) {
112 ret = bpf_map_update_elem(fd, &key, &val, BPF_NOEXIST);
120 static void do_test_lru(enum test_type test, int cpu)
122 static int inner_lru_map_fds[MAX_NR_CPUS];
124 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
125 const char *test_name;
129 if (test == INNER_LRU_HASH_PREALLOC && cpu) {
130 /* If CPU is not 0, create inner_lru hash map and insert the fd
131 * value into the array_of_lru_hash map. In case of CPU 0,
132 * 'inner_lru_hash_map' was statically inserted on the map init
134 int outer_fd = map_fd[array_of_lru_hashs_idx];
135 unsigned int mycpu, mynode;
136 LIBBPF_OPTS(bpf_map_create_opts, opts,
137 .map_flags = BPF_F_NUMA_NODE,
140 assert(cpu < MAX_NR_CPUS);
142 ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL);
145 opts.numa_node = mynode;
146 inner_lru_map_fds[cpu] =
147 bpf_map_create(BPF_MAP_TYPE_LRU_HASH,
148 test_map_names[INNER_LRU_HASH_PREALLOC],
151 inner_lru_hash_size, &opts);
152 if (inner_lru_map_fds[cpu] == -1) {
153 printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
154 strerror(errno), errno);
158 ret = bpf_map_update_elem(outer_fd, &cpu,
159 &inner_lru_map_fds[cpu],
162 printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
163 cpu, strerror(errno), errno);
168 in6.sin6_addr.s6_addr16[0] = 0xdead;
169 in6.sin6_addr.s6_addr16[1] = 0xbeef;
171 if (test == LRU_HASH_PREALLOC) {
172 test_name = "lru_hash_map_perf";
173 in6.sin6_addr.s6_addr16[2] = 0;
174 } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
175 test_name = "nocommon_lru_hash_map_perf";
176 in6.sin6_addr.s6_addr16[2] = 1;
177 } else if (test == INNER_LRU_HASH_PREALLOC) {
178 test_name = "inner_lru_hash_map_perf";
179 in6.sin6_addr.s6_addr16[2] = 2;
180 } else if (test == LRU_HASH_LOOKUP) {
181 test_name = "lru_hash_lookup_perf";
182 in6.sin6_addr.s6_addr16[2] = 3;
183 in6.sin6_addr.s6_addr32[3] = 0;
188 start_time = time_get_ns();
189 for (i = 0; i < max_cnt; i++) {
190 ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
191 assert(ret == -1 && errno == EBADF);
192 if (in6.sin6_addr.s6_addr32[3] <
193 lru_hash_lookup_test_entries - 32)
194 in6.sin6_addr.s6_addr32[3] += 32;
196 in6.sin6_addr.s6_addr32[3] = 0;
198 printf("%d:%s pre-alloc %lld events per sec\n",
200 max_cnt * 1000000000ll / (time_get_ns() - start_time));
203 static void test_lru_hash_prealloc(int cpu)
205 do_test_lru(LRU_HASH_PREALLOC, cpu);
208 static void test_nocommon_lru_hash_prealloc(int cpu)
210 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
213 static void test_inner_lru_hash_prealloc(int cpu)
215 do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
218 static void test_lru_hash_lookup(int cpu)
220 do_test_lru(LRU_HASH_LOOKUP, cpu);
223 static void test_percpu_hash_prealloc(int cpu)
228 start_time = time_get_ns();
229 for (i = 0; i < max_cnt; i++)
230 syscall(__NR_geteuid);
231 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
232 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
235 static void test_hash_kmalloc(int cpu)
240 start_time = time_get_ns();
241 for (i = 0; i < max_cnt; i++)
242 syscall(__NR_getgid);
243 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
244 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
247 static void test_percpu_hash_kmalloc(int cpu)
252 start_time = time_get_ns();
253 for (i = 0; i < max_cnt; i++)
254 syscall(__NR_getegid);
255 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
256 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
259 static void test_lpm_kmalloc(int cpu)
264 start_time = time_get_ns();
265 for (i = 0; i < max_cnt; i++)
266 syscall(__NR_gettid);
267 printf("%d:lpm_perf kmalloc %lld events per sec\n",
268 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
271 static void test_hash_lookup(int cpu)
276 start_time = time_get_ns();
277 for (i = 0; i < max_cnt; i++)
278 syscall(__NR_getpgid, 0);
279 printf("%d:hash_lookup %lld lookups per sec\n",
280 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
283 static void test_array_lookup(int cpu)
288 start_time = time_get_ns();
289 for (i = 0; i < max_cnt; i++)
290 syscall(__NR_getppid, 0);
291 printf("%d:array_lookup %lld lookups per sec\n",
292 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
295 typedef int (*pre_test_func)(int tasks);
296 const pre_test_func pre_test_funcs[] = {
297 [LRU_HASH_LOOKUP] = pre_test_lru_hash_lookup,
300 typedef void (*test_func)(int cpu);
301 const test_func test_funcs[] = {
302 [HASH_PREALLOC] = test_hash_prealloc,
303 [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc,
304 [HASH_KMALLOC] = test_hash_kmalloc,
305 [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc,
306 [LRU_HASH_PREALLOC] = test_lru_hash_prealloc,
307 [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc,
308 [LPM_KMALLOC] = test_lpm_kmalloc,
309 [HASH_LOOKUP] = test_hash_lookup,
310 [ARRAY_LOOKUP] = test_array_lookup,
311 [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
312 [LRU_HASH_LOOKUP] = test_lru_hash_lookup,
315 static int pre_test(int tasks)
319 for (i = 0; i < NR_TESTS; i++) {
320 if (pre_test_funcs[i] && check_test_flags(i)) {
321 int ret = pre_test_funcs[i](tasks);
331 static void loop(int cpu)
337 CPU_SET(cpu, &cpuset);
338 sched_setaffinity(0, sizeof(cpuset), &cpuset);
340 for (i = 0; i < NR_TESTS; i++) {
341 if (check_test_flags(i))
346 static void run_perf_test(int tasks)
351 assert(!pre_test(tasks));
353 for (i = 0; i < tasks; i++) {
358 } else if (pid[i] == -1) {
359 printf("couldn't spawn #%d process\n", i);
363 for (i = 0; i < tasks; i++) {
366 assert(waitpid(pid[i], &status, 0) == pid[i]);
371 static void fill_lpm_trie(void)
373 struct bpf_lpm_trie_key *key;
374 unsigned long value = 0;
378 key = alloca(sizeof(*key) + 4);
381 for (i = 0; i < 512; ++i) {
382 key->prefixlen = rand() % 33;
383 key->data[0] = rand() & 0xff;
384 key->data[1] = rand() & 0xff;
385 key->data[2] = rand() & 0xff;
386 key->data[3] = rand() & 0xff;
387 r = bpf_map_update_elem(map_fd[hash_map_alloc_idx],
399 r = bpf_map_update_elem(map_fd[hash_map_alloc_idx], key, &value, 0);
403 static void fixup_map(struct bpf_object *obj)
408 bpf_object__for_each_map(map, obj) {
409 const char *name = bpf_map__name(map);
411 /* Only change the max_entries for the enabled test(s) */
412 for (i = 0; i < NR_TESTS; i++) {
413 if (!strcmp(test_map_names[i], name) &&
414 (check_test_flags(i))) {
415 bpf_map__set_max_entries(map, num_map_entries);
421 inner_lru_hash_size = num_map_entries;
424 int main(int argc, char **argv)
426 int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
427 struct bpf_link *links[8];
428 struct bpf_program *prog;
429 struct bpf_object *obj;
435 test_flags = atoi(argv[1]) ? : test_flags;
438 nr_cpus = atoi(argv[2]) ? : nr_cpus;
441 num_map_entries = atoi(argv[3]);
444 max_cnt = atoi(argv[4]);
446 snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
447 obj = bpf_object__open_file(filename, NULL);
448 if (libbpf_get_error(obj)) {
449 fprintf(stderr, "ERROR: opening BPF object file failed\n");
453 map = bpf_object__find_map_by_name(obj, "inner_lru_hash_map");
454 if (libbpf_get_error(map)) {
455 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
459 inner_lru_hash_size = bpf_map__max_entries(map);
460 if (!inner_lru_hash_size) {
461 fprintf(stderr, "ERROR: failed to get map attribute\n");
465 /* resize BPF map prior to loading */
466 if (num_map_entries > 0)
469 /* load BPF program */
470 if (bpf_object__load(obj)) {
471 fprintf(stderr, "ERROR: loading BPF object file failed\n");
475 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "array_of_lru_hashs");
476 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "hash_map_alloc");
477 map_fd[2] = bpf_object__find_map_fd_by_name(obj, "lru_hash_lookup_map");
478 if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) {
479 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
483 bpf_object__for_each_program(prog, obj) {
484 links[i] = bpf_program__attach(prog);
485 if (libbpf_get_error(links[i])) {
486 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
495 run_perf_test(nr_cpus);
498 for (i--; i >= 0; i--)
499 bpf_link__destroy(links[i]);
501 bpf_object__close(obj);