Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 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> |
Naveen N. Rao | 77e6353 | 2016-04-04 22:31:32 +0530 | [diff] [blame] | 20 | #include <sys/resource.h> |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 21 | #include <arpa/inet.h> |
| 22 | #include <errno.h> |
| 23 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 24 | #include "libbpf.h" |
| 25 | #include "bpf_load.h" |
| 26 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 27 | #define TEST_BIT(t) (1U << (t)) |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 28 | #define MAX_NR_CPUS 1024 |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 29 | |
| 30 | static __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 Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 38 | enum 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 Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 48 | INNER_LRU_HASH_PREALLOC, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 49 | NR_TESTS, |
| 50 | }; |
| 51 | |
| 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", |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 62 | [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 63 | }; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 64 | |
| 65 | static int test_flags = ~0; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 66 | static uint32_t num_map_entries; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 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; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 70 | static uint32_t max_cnt = 1000000; |
| 71 | |
| 72 | static int check_test_flags(enum test_type t) |
| 73 | { |
| 74 | return test_flags & TEST_BIT(t); |
| 75 | } |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 76 | |
| 77 | static void test_hash_prealloc(int cpu) |
| 78 | { |
| 79 | __u64 start_time; |
| 80 | int i; |
| 81 | |
| 82 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 83 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 84 | syscall(__NR_getuid); |
| 85 | printf("%d:hash_map_perf pre-alloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 86 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 89 | static void do_test_lru(enum test_type test, int cpu) |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 90 | { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 91 | static int inner_lru_map_fds[MAX_NR_CPUS]; |
| 92 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 93 | struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 }; |
| 94 | const char *test_name; |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 95 | __u64 start_time; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 96 | int i, ret; |
| 97 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 98 | if (test == INNER_LRU_HASH_PREALLOC) { |
| 99 | int outer_fd = map_fd[array_of_lru_hashs_idx]; |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 100 | unsigned int mycpu, mynode; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 101 | |
| 102 | assert(cpu < MAX_NR_CPUS); |
| 103 | |
| 104 | if (cpu) { |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 105 | ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL); |
| 106 | assert(!ret); |
| 107 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 108 | inner_lru_map_fds[cpu] = |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 109 | bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH, |
| 110 | sizeof(uint32_t), |
| 111 | sizeof(long), |
| 112 | inner_lru_hash_size, 0, |
| 113 | mynode); |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 114 | if (inner_lru_map_fds[cpu] == -1) { |
| 115 | printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n", |
| 116 | strerror(errno), errno); |
| 117 | exit(1); |
| 118 | } |
| 119 | } else { |
| 120 | inner_lru_map_fds[cpu] = map_fd[inner_lru_hash_idx]; |
| 121 | } |
| 122 | |
| 123 | ret = bpf_map_update_elem(outer_fd, &cpu, |
| 124 | &inner_lru_map_fds[cpu], |
| 125 | BPF_ANY); |
| 126 | if (ret) { |
| 127 | printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n", |
| 128 | cpu, strerror(errno), errno); |
| 129 | exit(1); |
| 130 | } |
| 131 | } |
| 132 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 133 | in6.sin6_addr.s6_addr16[0] = 0xdead; |
| 134 | in6.sin6_addr.s6_addr16[1] = 0xbeef; |
| 135 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 136 | if (test == LRU_HASH_PREALLOC) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 137 | test_name = "lru_hash_map_perf"; |
| 138 | in6.sin6_addr.s6_addr16[7] = 0; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 139 | } else if (test == NOCOMMON_LRU_HASH_PREALLOC) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 140 | test_name = "nocommon_lru_hash_map_perf"; |
| 141 | in6.sin6_addr.s6_addr16[7] = 1; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 142 | } else if (test == INNER_LRU_HASH_PREALLOC) { |
| 143 | test_name = "inner_lru_hash_map_perf"; |
| 144 | in6.sin6_addr.s6_addr16[7] = 2; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 145 | } else { |
| 146 | assert(0); |
| 147 | } |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 148 | |
| 149 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 150 | for (i = 0; i < max_cnt; i++) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 151 | ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6)); |
| 152 | assert(ret == -1 && errno == EBADF); |
| 153 | } |
| 154 | printf("%d:%s pre-alloc %lld events per sec\n", |
| 155 | cpu, test_name, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 156 | max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 159 | static void test_lru_hash_prealloc(int cpu) |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 160 | { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 161 | do_test_lru(LRU_HASH_PREALLOC, cpu); |
| 162 | } |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 163 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 164 | static void test_nocommon_lru_hash_prealloc(int cpu) |
| 165 | { |
| 166 | do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 169 | static void test_inner_lru_hash_prealloc(int cpu) |
| 170 | { |
| 171 | do_test_lru(INNER_LRU_HASH_PREALLOC, cpu); |
| 172 | } |
| 173 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 174 | static void test_percpu_hash_prealloc(int cpu) |
| 175 | { |
| 176 | __u64 start_time; |
| 177 | int i; |
| 178 | |
| 179 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 180 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 181 | syscall(__NR_geteuid); |
| 182 | printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 183 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void test_hash_kmalloc(int cpu) |
| 187 | { |
| 188 | __u64 start_time; |
| 189 | int i; |
| 190 | |
| 191 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 192 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 193 | syscall(__NR_getgid); |
| 194 | printf("%d:hash_map_perf kmalloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 195 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void test_percpu_hash_kmalloc(int cpu) |
| 199 | { |
| 200 | __u64 start_time; |
| 201 | int i; |
| 202 | |
| 203 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 204 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 205 | syscall(__NR_getegid); |
| 206 | printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 207 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 208 | } |
| 209 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 210 | static void test_lpm_kmalloc(int cpu) |
| 211 | { |
| 212 | __u64 start_time; |
| 213 | int i; |
| 214 | |
| 215 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 216 | for (i = 0; i < max_cnt; i++) |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 217 | syscall(__NR_gettid); |
| 218 | printf("%d:lpm_perf kmalloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 219 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 222 | static void test_hash_lookup(int cpu) |
| 223 | { |
| 224 | __u64 start_time; |
| 225 | int i; |
| 226 | |
| 227 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 228 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 229 | syscall(__NR_getpgid, 0); |
| 230 | printf("%d:hash_lookup %lld lookups per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 231 | cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time)); |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void test_array_lookup(int cpu) |
| 235 | { |
| 236 | __u64 start_time; |
| 237 | int i; |
| 238 | |
| 239 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 240 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 241 | syscall(__NR_getpgrp, 0); |
| 242 | printf("%d:array_lookup %lld lookups per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 243 | cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time)); |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 246 | typedef void (*test_func)(int cpu); |
| 247 | const test_func test_funcs[] = { |
| 248 | [HASH_PREALLOC] = test_hash_prealloc, |
| 249 | [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc, |
| 250 | [HASH_KMALLOC] = test_hash_kmalloc, |
| 251 | [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc, |
| 252 | [LRU_HASH_PREALLOC] = test_lru_hash_prealloc, |
| 253 | [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc, |
| 254 | [LPM_KMALLOC] = test_lpm_kmalloc, |
| 255 | [HASH_LOOKUP] = test_hash_lookup, |
| 256 | [ARRAY_LOOKUP] = test_array_lookup, |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 257 | [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 260 | static void loop(int cpu) |
| 261 | { |
| 262 | cpu_set_t cpuset; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 263 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 264 | |
| 265 | CPU_ZERO(&cpuset); |
| 266 | CPU_SET(cpu, &cpuset); |
| 267 | sched_setaffinity(0, sizeof(cpuset), &cpuset); |
| 268 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 269 | for (i = 0; i < NR_TESTS; i++) { |
| 270 | if (check_test_flags(i)) |
| 271 | test_funcs[i](cpu); |
| 272 | } |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void run_perf_test(int tasks) |
| 276 | { |
| 277 | pid_t pid[tasks]; |
| 278 | int i; |
| 279 | |
| 280 | for (i = 0; i < tasks; i++) { |
| 281 | pid[i] = fork(); |
| 282 | if (pid[i] == 0) { |
| 283 | loop(i); |
| 284 | exit(0); |
| 285 | } else if (pid[i] == -1) { |
| 286 | printf("couldn't spawn #%d process\n", i); |
| 287 | exit(1); |
| 288 | } |
| 289 | } |
| 290 | for (i = 0; i < tasks; i++) { |
| 291 | int status; |
| 292 | |
| 293 | assert(waitpid(pid[i], &status, 0) == pid[i]); |
| 294 | assert(status == 0); |
| 295 | } |
| 296 | } |
| 297 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 298 | static void fill_lpm_trie(void) |
| 299 | { |
| 300 | struct bpf_lpm_trie_key *key; |
| 301 | unsigned long value = 0; |
| 302 | unsigned int i; |
| 303 | int r; |
| 304 | |
| 305 | key = alloca(sizeof(*key) + 4); |
| 306 | key->prefixlen = 32; |
| 307 | |
| 308 | for (i = 0; i < 512; ++i) { |
| 309 | key->prefixlen = rand() % 33; |
| 310 | key->data[0] = rand() & 0xff; |
| 311 | key->data[1] = rand() & 0xff; |
| 312 | key->data[2] = rand() & 0xff; |
| 313 | key->data[3] = rand() & 0xff; |
| 314 | r = bpf_map_update_elem(map_fd[6], key, &value, 0); |
| 315 | assert(!r); |
| 316 | } |
| 317 | |
| 318 | key->prefixlen = 32; |
| 319 | key->data[0] = 192; |
| 320 | key->data[1] = 168; |
| 321 | key->data[2] = 0; |
| 322 | key->data[3] = 1; |
| 323 | value = 128; |
| 324 | |
| 325 | r = bpf_map_update_elem(map_fd[6], key, &value, 0); |
| 326 | assert(!r); |
| 327 | } |
| 328 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 329 | static void fixup_map(struct bpf_map_data *map, int idx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 330 | { |
| 331 | int i; |
| 332 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 333 | if (!strcmp("inner_lru_hash_map", map->name)) { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 334 | inner_lru_hash_idx = idx; |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 335 | inner_lru_hash_size = map->def.max_entries; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 338 | if (!strcmp("array_of_lru_hashs", map->name)) { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 339 | if (inner_lru_hash_idx == -1) { |
| 340 | printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n"); |
| 341 | exit(1); |
| 342 | } |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 343 | map->def.inner_map_idx = inner_lru_hash_idx; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 344 | array_of_lru_hashs_idx = idx; |
| 345 | } |
| 346 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 347 | if (num_map_entries <= 0) |
| 348 | return; |
| 349 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 350 | inner_lru_hash_size = num_map_entries; |
| 351 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 352 | /* Only change the max_entries for the enabled test(s) */ |
| 353 | for (i = 0; i < NR_TESTS; i++) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 354 | if (!strcmp(test_map_names[i], map->name) && |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 355 | (check_test_flags(i))) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 356 | map->def.max_entries = num_map_entries; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 361 | int main(int argc, char **argv) |
| 362 | { |
| 363 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
| 364 | char filename[256]; |
| 365 | int num_cpu = 8; |
| 366 | |
| 367 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 368 | setrlimit(RLIMIT_MEMLOCK, &r); |
| 369 | |
| 370 | if (argc > 1) |
| 371 | test_flags = atoi(argv[1]) ? : test_flags; |
| 372 | |
| 373 | if (argc > 2) |
| 374 | num_cpu = atoi(argv[2]) ? : num_cpu; |
| 375 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 376 | if (argc > 3) |
| 377 | num_map_entries = atoi(argv[3]); |
| 378 | |
| 379 | if (argc > 4) |
| 380 | max_cnt = atoi(argv[4]); |
| 381 | |
| 382 | if (load_bpf_file_fixup_map(filename, fixup_map)) { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 383 | printf("%s", bpf_log_buf); |
| 384 | return 1; |
| 385 | } |
| 386 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 387 | fill_lpm_trie(); |
| 388 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 389 | run_perf_test(num_cpu); |
| 390 | |
| 391 | return 0; |
| 392 | } |