1 /* Copyright (c) 2016 Facebook
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.
10 #include <sys/types.h>
11 #include <asm/unistd.h>
17 #include <linux/bpf.h>
20 #include <sys/resource.h>
21 #include <arpa/inet.h>
27 #define TEST_BIT(t) (1U << (t))
28 #define MAX_NR_CPUS 1024
30 static __u64
time_get_ns(void)
34 clock_gettime(CLOCK_MONOTONIC
, &ts
);
35 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
44 NOCOMMON_LRU_HASH_PREALLOC
,
48 INNER_LRU_HASH_PREALLOC
,
52 const char *test_map_names
[NR_TESTS
] = {
53 [HASH_PREALLOC
] = "hash_map",
54 [PERCPU_HASH_PREALLOC
] = "percpu_hash_map",
55 [HASH_KMALLOC
] = "hash_map_alloc",
56 [PERCPU_HASH_KMALLOC
] = "percpu_hash_map_alloc",
57 [LRU_HASH_PREALLOC
] = "lru_hash_map",
58 [NOCOMMON_LRU_HASH_PREALLOC
] = "nocommon_lru_hash_map",
59 [LPM_KMALLOC
] = "lpm_trie_map_alloc",
60 [HASH_LOOKUP
] = "hash_map",
61 [ARRAY_LOOKUP
] = "array_map",
62 [INNER_LRU_HASH_PREALLOC
] = "inner_lru_hash_map",
65 static int test_flags
= ~0;
66 static uint32_t num_map_entries
;
67 static uint32_t inner_lru_hash_size
;
68 static int inner_lru_hash_idx
= -1;
69 static int array_of_lru_hashs_idx
= -1;
70 static uint32_t max_cnt
= 1000000;
72 static int check_test_flags(enum test_type t
)
74 return test_flags
& TEST_BIT(t
);
77 static void test_hash_prealloc(int cpu
)
82 start_time
= time_get_ns();
83 for (i
= 0; i
< max_cnt
; i
++)
85 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
86 cpu
, max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
89 static void do_test_lru(enum test_type test
, int cpu
)
91 static int inner_lru_map_fds
[MAX_NR_CPUS
];
93 struct sockaddr_in6 in6
= { .sin6_family
= AF_INET6
};
94 const char *test_name
;
98 if (test
== INNER_LRU_HASH_PREALLOC
) {
99 int outer_fd
= map_fd
[array_of_lru_hashs_idx
];
101 assert(cpu
< MAX_NR_CPUS
);
104 inner_lru_map_fds
[cpu
] =
105 bpf_create_map(BPF_MAP_TYPE_LRU_HASH
,
106 sizeof(uint32_t), sizeof(long),
107 inner_lru_hash_size
, 0);
108 if (inner_lru_map_fds
[cpu
] == -1) {
109 printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
110 strerror(errno
), errno
);
114 inner_lru_map_fds
[cpu
] = map_fd
[inner_lru_hash_idx
];
117 ret
= bpf_map_update_elem(outer_fd
, &cpu
,
118 &inner_lru_map_fds
[cpu
],
121 printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
122 cpu
, strerror(errno
), errno
);
127 in6
.sin6_addr
.s6_addr16
[0] = 0xdead;
128 in6
.sin6_addr
.s6_addr16
[1] = 0xbeef;
130 if (test
== LRU_HASH_PREALLOC
) {
131 test_name
= "lru_hash_map_perf";
132 in6
.sin6_addr
.s6_addr16
[7] = 0;
133 } else if (test
== NOCOMMON_LRU_HASH_PREALLOC
) {
134 test_name
= "nocommon_lru_hash_map_perf";
135 in6
.sin6_addr
.s6_addr16
[7] = 1;
136 } else if (test
== INNER_LRU_HASH_PREALLOC
) {
137 test_name
= "inner_lru_hash_map_perf";
138 in6
.sin6_addr
.s6_addr16
[7] = 2;
143 start_time
= time_get_ns();
144 for (i
= 0; i
< max_cnt
; i
++) {
145 ret
= connect(-1, (const struct sockaddr
*)&in6
, sizeof(in6
));
146 assert(ret
== -1 && errno
== EBADF
);
148 printf("%d:%s pre-alloc %lld events per sec\n",
150 max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
153 static void test_lru_hash_prealloc(int cpu
)
155 do_test_lru(LRU_HASH_PREALLOC
, cpu
);
158 static void test_nocommon_lru_hash_prealloc(int cpu
)
160 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC
, cpu
);
163 static void test_inner_lru_hash_prealloc(int cpu
)
165 do_test_lru(INNER_LRU_HASH_PREALLOC
, cpu
);
168 static void test_percpu_hash_prealloc(int cpu
)
173 start_time
= time_get_ns();
174 for (i
= 0; i
< max_cnt
; i
++)
175 syscall(__NR_geteuid
);
176 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
177 cpu
, max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
180 static void test_hash_kmalloc(int cpu
)
185 start_time
= time_get_ns();
186 for (i
= 0; i
< max_cnt
; i
++)
187 syscall(__NR_getgid
);
188 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
189 cpu
, max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
192 static void test_percpu_hash_kmalloc(int cpu
)
197 start_time
= time_get_ns();
198 for (i
= 0; i
< max_cnt
; i
++)
199 syscall(__NR_getegid
);
200 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
201 cpu
, max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
204 static void test_lpm_kmalloc(int cpu
)
209 start_time
= time_get_ns();
210 for (i
= 0; i
< max_cnt
; i
++)
211 syscall(__NR_gettid
);
212 printf("%d:lpm_perf kmalloc %lld events per sec\n",
213 cpu
, max_cnt
* 1000000000ll / (time_get_ns() - start_time
));
216 static void test_hash_lookup(int cpu
)
221 start_time
= time_get_ns();
222 for (i
= 0; i
< max_cnt
; i
++)
223 syscall(__NR_getpgid
, 0);
224 printf("%d:hash_lookup %lld lookups per sec\n",
225 cpu
, max_cnt
* 1000000000ll * 64 / (time_get_ns() - start_time
));
228 static void test_array_lookup(int cpu
)
233 start_time
= time_get_ns();
234 for (i
= 0; i
< max_cnt
; i
++)
235 syscall(__NR_getpgrp
, 0);
236 printf("%d:array_lookup %lld lookups per sec\n",
237 cpu
, max_cnt
* 1000000000ll * 64 / (time_get_ns() - start_time
));
240 typedef void (*test_func
)(int cpu
);
241 const test_func test_funcs
[] = {
242 [HASH_PREALLOC
] = test_hash_prealloc
,
243 [PERCPU_HASH_PREALLOC
] = test_percpu_hash_prealloc
,
244 [HASH_KMALLOC
] = test_hash_kmalloc
,
245 [PERCPU_HASH_KMALLOC
] = test_percpu_hash_kmalloc
,
246 [LRU_HASH_PREALLOC
] = test_lru_hash_prealloc
,
247 [NOCOMMON_LRU_HASH_PREALLOC
] = test_nocommon_lru_hash_prealloc
,
248 [LPM_KMALLOC
] = test_lpm_kmalloc
,
249 [HASH_LOOKUP
] = test_hash_lookup
,
250 [ARRAY_LOOKUP
] = test_array_lookup
,
251 [INNER_LRU_HASH_PREALLOC
] = test_inner_lru_hash_prealloc
,
254 static void loop(int cpu
)
260 CPU_SET(cpu
, &cpuset
);
261 sched_setaffinity(0, sizeof(cpuset
), &cpuset
);
263 for (i
= 0; i
< NR_TESTS
; i
++) {
264 if (check_test_flags(i
))
269 static void run_perf_test(int tasks
)
274 for (i
= 0; i
< tasks
; i
++) {
279 } else if (pid
[i
] == -1) {
280 printf("couldn't spawn #%d process\n", i
);
284 for (i
= 0; i
< tasks
; i
++) {
287 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
292 static void fill_lpm_trie(void)
294 struct bpf_lpm_trie_key
*key
;
295 unsigned long value
= 0;
299 key
= alloca(sizeof(*key
) + 4);
302 for (i
= 0; i
< 512; ++i
) {
303 key
->prefixlen
= rand() % 33;
304 key
->data
[0] = rand() & 0xff;
305 key
->data
[1] = rand() & 0xff;
306 key
->data
[2] = rand() & 0xff;
307 key
->data
[3] = rand() & 0xff;
308 r
= bpf_map_update_elem(map_fd
[6], key
, &value
, 0);
319 r
= bpf_map_update_elem(map_fd
[6], key
, &value
, 0);
323 static void fixup_map(struct bpf_map_data
*map
, int idx
)
327 if (!strcmp("inner_lru_hash_map", map
->name
)) {
328 inner_lru_hash_idx
= idx
;
329 inner_lru_hash_size
= map
->def
.max_entries
;
332 if (!strcmp("array_of_lru_hashs", map
->name
)) {
333 if (inner_lru_hash_idx
== -1) {
334 printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
337 map
->def
.inner_map_idx
= inner_lru_hash_idx
;
338 array_of_lru_hashs_idx
= idx
;
341 if (num_map_entries
<= 0)
344 inner_lru_hash_size
= num_map_entries
;
346 /* Only change the max_entries for the enabled test(s) */
347 for (i
= 0; i
< NR_TESTS
; i
++) {
348 if (!strcmp(test_map_names
[i
], map
->name
) &&
349 (check_test_flags(i
))) {
350 map
->def
.max_entries
= num_map_entries
;
355 int main(int argc
, char **argv
)
357 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
361 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
362 setrlimit(RLIMIT_MEMLOCK
, &r
);
365 test_flags
= atoi(argv
[1]) ? : test_flags
;
368 num_cpu
= atoi(argv
[2]) ? : num_cpu
;
371 num_map_entries
= atoi(argv
[3]);
374 max_cnt
= atoi(argv
[4]);
376 if (load_bpf_file_fixup_map(filename
, fixup_map
)) {
377 printf("%s", bpf_log_buf
);
383 run_perf_test(num_cpu
);