blob: 1a8894b5ac5147c36833ffae500ddfbeb00277d1 [file] [log] [blame]
Alexei Starovoitov26e90932016-03-08 15:07:54 -08001/* 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>
Naveen N. Rao77e63532016-04-04 22:31:32 +053020#include <sys/resource.h>
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070021#include <arpa/inet.h>
22#include <errno.h>
23
Alexei Starovoitov26e90932016-03-08 15:07:54 -080024#include "libbpf.h"
25#include "bpf_load.h"
26
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070027#define TEST_BIT(t) (1U << (t))
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070028#define MAX_NR_CPUS 1024
Alexei Starovoitov26e90932016-03-08 15:07:54 -080029
30static __u64 time_get_ns(void)
31{
32 struct timespec ts;
33
34 clock_gettime(CLOCK_MONOTONIC, &ts);
35 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
36}
37
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070038enum test_type {
39 HASH_PREALLOC,
40 PERCPU_HASH_PREALLOC,
41 HASH_KMALLOC,
42 PERCPU_HASH_KMALLOC,
43 LRU_HASH_PREALLOC,
44 NOCOMMON_LRU_HASH_PREALLOC,
45 LPM_KMALLOC,
46 HASH_LOOKUP,
47 ARRAY_LOOKUP,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070048 INNER_LRU_HASH_PREALLOC,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070049 NR_TESTS,
50};
51
52const 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",
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070062 [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070063};
Alexei Starovoitov26e90932016-03-08 15:07:54 -080064
65static int test_flags = ~0;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070066static uint32_t num_map_entries;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070067static uint32_t inner_lru_hash_size;
68static int inner_lru_hash_idx = -1;
69static int array_of_lru_hashs_idx = -1;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070070static uint32_t max_cnt = 1000000;
71
72static int check_test_flags(enum test_type t)
73{
74 return test_flags & TEST_BIT(t);
75}
Alexei Starovoitov26e90932016-03-08 15:07:54 -080076
77static void test_hash_prealloc(int cpu)
78{
79 __u64 start_time;
80 int i;
81
82 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070083 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -080084 syscall(__NR_getuid);
85 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070086 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -080087}
88
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070089static void do_test_lru(enum test_type test, int cpu)
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080090{
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070091 static int inner_lru_map_fds[MAX_NR_CPUS];
92
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070093 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
94 const char *test_name;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080095 __u64 start_time;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070096 int i, ret;
97
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070098 if (test == INNER_LRU_HASH_PREALLOC) {
99 int outer_fd = map_fd[array_of_lru_hashs_idx];
100
101 assert(cpu < MAX_NR_CPUS);
102
103 if (cpu) {
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);
111 exit(1);
112 }
113 } else {
114 inner_lru_map_fds[cpu] = map_fd[inner_lru_hash_idx];
115 }
116
117 ret = bpf_map_update_elem(outer_fd, &cpu,
118 &inner_lru_map_fds[cpu],
119 BPF_ANY);
120 if (ret) {
121 printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
122 cpu, strerror(errno), errno);
123 exit(1);
124 }
125 }
126
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700127 in6.sin6_addr.s6_addr16[0] = 0xdead;
128 in6.sin6_addr.s6_addr16[1] = 0xbeef;
129
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700130 if (test == LRU_HASH_PREALLOC) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700131 test_name = "lru_hash_map_perf";
132 in6.sin6_addr.s6_addr16[7] = 0;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700133 } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700134 test_name = "nocommon_lru_hash_map_perf";
135 in6.sin6_addr.s6_addr16[7] = 1;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700136 } else if (test == INNER_LRU_HASH_PREALLOC) {
137 test_name = "inner_lru_hash_map_perf";
138 in6.sin6_addr.s6_addr16[7] = 2;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700139 } else {
140 assert(0);
141 }
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800142
143 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700144 for (i = 0; i < max_cnt; i++) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700145 ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
146 assert(ret == -1 && errno == EBADF);
147 }
148 printf("%d:%s pre-alloc %lld events per sec\n",
149 cpu, test_name,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700150 max_cnt * 1000000000ll / (time_get_ns() - start_time));
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800151}
152
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700153static void test_lru_hash_prealloc(int cpu)
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800154{
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700155 do_test_lru(LRU_HASH_PREALLOC, cpu);
156}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800157
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700158static void test_nocommon_lru_hash_prealloc(int cpu)
159{
160 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800161}
162
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700163static void test_inner_lru_hash_prealloc(int cpu)
164{
165 do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
166}
167
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800168static void test_percpu_hash_prealloc(int cpu)
169{
170 __u64 start_time;
171 int i;
172
173 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700174 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800175 syscall(__NR_geteuid);
176 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700177 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800178}
179
180static void test_hash_kmalloc(int cpu)
181{
182 __u64 start_time;
183 int i;
184
185 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700186 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800187 syscall(__NR_getgid);
188 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700189 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800190}
191
192static void test_percpu_hash_kmalloc(int cpu)
193{
194 __u64 start_time;
195 int i;
196
197 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700198 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800199 syscall(__NR_getegid);
200 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700201 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800202}
203
David Herrmannb8a943e2017-01-21 17:26:13 +0100204static void test_lpm_kmalloc(int cpu)
205{
206 __u64 start_time;
207 int i;
208
209 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700210 for (i = 0; i < max_cnt; i++)
David Herrmannb8a943e2017-01-21 17:26:13 +0100211 syscall(__NR_gettid);
212 printf("%d:lpm_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700213 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
David Herrmannb8a943e2017-01-21 17:26:13 +0100214}
215
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700216static void test_hash_lookup(int cpu)
217{
218 __u64 start_time;
219 int i;
220
221 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700222 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700223 syscall(__NR_getpgid, 0);
224 printf("%d:hash_lookup %lld lookups per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700225 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700226}
227
228static void test_array_lookup(int cpu)
229{
230 __u64 start_time;
231 int i;
232
233 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700234 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700235 syscall(__NR_getpgrp, 0);
236 printf("%d:array_lookup %lld lookups per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700237 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700238}
239
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700240typedef void (*test_func)(int cpu);
241const 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,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700251 [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700252};
253
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800254static void loop(int cpu)
255{
256 cpu_set_t cpuset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700257 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800258
259 CPU_ZERO(&cpuset);
260 CPU_SET(cpu, &cpuset);
261 sched_setaffinity(0, sizeof(cpuset), &cpuset);
262
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700263 for (i = 0; i < NR_TESTS; i++) {
264 if (check_test_flags(i))
265 test_funcs[i](cpu);
266 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800267}
268
269static void run_perf_test(int tasks)
270{
271 pid_t pid[tasks];
272 int i;
273
274 for (i = 0; i < tasks; i++) {
275 pid[i] = fork();
276 if (pid[i] == 0) {
277 loop(i);
278 exit(0);
279 } else if (pid[i] == -1) {
280 printf("couldn't spawn #%d process\n", i);
281 exit(1);
282 }
283 }
284 for (i = 0; i < tasks; i++) {
285 int status;
286
287 assert(waitpid(pid[i], &status, 0) == pid[i]);
288 assert(status == 0);
289 }
290}
291
David Herrmannb8a943e2017-01-21 17:26:13 +0100292static void fill_lpm_trie(void)
293{
294 struct bpf_lpm_trie_key *key;
295 unsigned long value = 0;
296 unsigned int i;
297 int r;
298
299 key = alloca(sizeof(*key) + 4);
300 key->prefixlen = 32;
301
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);
309 assert(!r);
310 }
311
312 key->prefixlen = 32;
313 key->data[0] = 192;
314 key->data[1] = 168;
315 key->data[2] = 0;
316 key->data[3] = 1;
317 value = 128;
318
319 r = bpf_map_update_elem(map_fd[6], key, &value, 0);
320 assert(!r);
321}
322
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200323static void fixup_map(struct bpf_map_data *map, int idx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700324{
325 int i;
326
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200327 if (!strcmp("inner_lru_hash_map", map->name)) {
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700328 inner_lru_hash_idx = idx;
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200329 inner_lru_hash_size = map->def.max_entries;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700330 }
331
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200332 if (!strcmp("array_of_lru_hashs", map->name)) {
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700333 if (inner_lru_hash_idx == -1) {
334 printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
335 exit(1);
336 }
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200337 map->def.inner_map_idx = inner_lru_hash_idx;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700338 array_of_lru_hashs_idx = idx;
339 }
340
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700341 if (num_map_entries <= 0)
342 return;
343
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700344 inner_lru_hash_size = num_map_entries;
345
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700346 /* Only change the max_entries for the enabled test(s) */
347 for (i = 0; i < NR_TESTS; i++) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200348 if (!strcmp(test_map_names[i], map->name) &&
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700349 (check_test_flags(i))) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200350 map->def.max_entries = num_map_entries;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700351 }
352 }
353}
354
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800355int main(int argc, char **argv)
356{
357 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
358 char filename[256];
359 int num_cpu = 8;
360
361 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
362 setrlimit(RLIMIT_MEMLOCK, &r);
363
364 if (argc > 1)
365 test_flags = atoi(argv[1]) ? : test_flags;
366
367 if (argc > 2)
368 num_cpu = atoi(argv[2]) ? : num_cpu;
369
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700370 if (argc > 3)
371 num_map_entries = atoi(argv[3]);
372
373 if (argc > 4)
374 max_cnt = atoi(argv[4]);
375
376 if (load_bpf_file_fixup_map(filename, fixup_map)) {
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800377 printf("%s", bpf_log_buf);
378 return 1;
379 }
380
David Herrmannb8a943e2017-01-21 17:26:13 +0100381 fill_lpm_trie();
382
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800383 run_perf_test(num_cpu);
384
385 return 0;
386}