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 | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 49 | LRU_HASH_LOOKUP, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 50 | NR_TESTS, |
| 51 | }; |
| 52 | |
| 53 | const char *test_map_names[NR_TESTS] = { |
| 54 | [HASH_PREALLOC] = "hash_map", |
| 55 | [PERCPU_HASH_PREALLOC] = "percpu_hash_map", |
| 56 | [HASH_KMALLOC] = "hash_map_alloc", |
| 57 | [PERCPU_HASH_KMALLOC] = "percpu_hash_map_alloc", |
| 58 | [LRU_HASH_PREALLOC] = "lru_hash_map", |
| 59 | [NOCOMMON_LRU_HASH_PREALLOC] = "nocommon_lru_hash_map", |
| 60 | [LPM_KMALLOC] = "lpm_trie_map_alloc", |
| 61 | [HASH_LOOKUP] = "hash_map", |
| 62 | [ARRAY_LOOKUP] = "array_map", |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 63 | [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map", |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 64 | [LRU_HASH_LOOKUP] = "lru_hash_lookup_map", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 65 | }; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 66 | |
| 67 | static int test_flags = ~0; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 68 | static uint32_t num_map_entries; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 69 | static uint32_t inner_lru_hash_size; |
| 70 | static int inner_lru_hash_idx = -1; |
| 71 | static int array_of_lru_hashs_idx = -1; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 72 | static int lru_hash_lookup_idx = -1; |
| 73 | static int lru_hash_lookup_test_entries = 32; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 74 | static uint32_t max_cnt = 1000000; |
| 75 | |
| 76 | static int check_test_flags(enum test_type t) |
| 77 | { |
| 78 | return test_flags & TEST_BIT(t); |
| 79 | } |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 80 | |
| 81 | static void test_hash_prealloc(int cpu) |
| 82 | { |
| 83 | __u64 start_time; |
| 84 | int i; |
| 85 | |
| 86 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 87 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 88 | syscall(__NR_getuid); |
| 89 | 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] | 90 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 93 | static int pre_test_lru_hash_lookup(int tasks) |
| 94 | { |
| 95 | int fd = map_fd[lru_hash_lookup_idx]; |
| 96 | uint32_t key; |
| 97 | long val = 1; |
| 98 | int ret; |
| 99 | |
| 100 | if (num_map_entries > lru_hash_lookup_test_entries) |
| 101 | lru_hash_lookup_test_entries = num_map_entries; |
| 102 | |
| 103 | /* Populate the lru_hash_map for LRU_HASH_LOOKUP perf test. |
| 104 | * |
| 105 | * It is fine that the user requests for a map with |
| 106 | * num_map_entries < 32 and some of the later lru hash lookup |
| 107 | * may return not found. For LRU map, we are not interested |
| 108 | * in such small map performance. |
| 109 | */ |
| 110 | for (key = 0; key < lru_hash_lookup_test_entries; key++) { |
| 111 | ret = bpf_map_update_elem(fd, &key, &val, BPF_NOEXIST); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 119 | static void do_test_lru(enum test_type test, int cpu) |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 120 | { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 121 | static int inner_lru_map_fds[MAX_NR_CPUS]; |
| 122 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 123 | struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 }; |
| 124 | const char *test_name; |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 125 | __u64 start_time; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 126 | int i, ret; |
| 127 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 128 | if (test == INNER_LRU_HASH_PREALLOC) { |
| 129 | int outer_fd = map_fd[array_of_lru_hashs_idx]; |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 130 | unsigned int mycpu, mynode; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 131 | |
| 132 | assert(cpu < MAX_NR_CPUS); |
| 133 | |
| 134 | if (cpu) { |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 135 | ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL); |
| 136 | assert(!ret); |
| 137 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 138 | inner_lru_map_fds[cpu] = |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 139 | bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH, |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame^] | 140 | test_map_names[INNER_LRU_HASH_PREALLOC], |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 141 | sizeof(uint32_t), |
| 142 | sizeof(long), |
| 143 | inner_lru_hash_size, 0, |
| 144 | mynode); |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 145 | if (inner_lru_map_fds[cpu] == -1) { |
| 146 | printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n", |
| 147 | strerror(errno), errno); |
| 148 | exit(1); |
| 149 | } |
| 150 | } else { |
| 151 | inner_lru_map_fds[cpu] = map_fd[inner_lru_hash_idx]; |
| 152 | } |
| 153 | |
| 154 | ret = bpf_map_update_elem(outer_fd, &cpu, |
| 155 | &inner_lru_map_fds[cpu], |
| 156 | BPF_ANY); |
| 157 | if (ret) { |
| 158 | printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n", |
| 159 | cpu, strerror(errno), errno); |
| 160 | exit(1); |
| 161 | } |
| 162 | } |
| 163 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 164 | in6.sin6_addr.s6_addr16[0] = 0xdead; |
| 165 | in6.sin6_addr.s6_addr16[1] = 0xbeef; |
| 166 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 167 | if (test == LRU_HASH_PREALLOC) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 168 | test_name = "lru_hash_map_perf"; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 169 | in6.sin6_addr.s6_addr16[2] = 0; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 170 | } else if (test == NOCOMMON_LRU_HASH_PREALLOC) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 171 | test_name = "nocommon_lru_hash_map_perf"; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 172 | in6.sin6_addr.s6_addr16[2] = 1; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 173 | } else if (test == INNER_LRU_HASH_PREALLOC) { |
| 174 | test_name = "inner_lru_hash_map_perf"; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 175 | in6.sin6_addr.s6_addr16[2] = 2; |
| 176 | } else if (test == LRU_HASH_LOOKUP) { |
| 177 | test_name = "lru_hash_lookup_perf"; |
| 178 | in6.sin6_addr.s6_addr16[2] = 3; |
| 179 | in6.sin6_addr.s6_addr32[3] = 0; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 180 | } else { |
| 181 | assert(0); |
| 182 | } |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 183 | |
| 184 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 185 | for (i = 0; i < max_cnt; i++) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 186 | ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6)); |
| 187 | assert(ret == -1 && errno == EBADF); |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 188 | if (in6.sin6_addr.s6_addr32[3] < |
| 189 | lru_hash_lookup_test_entries - 32) |
| 190 | in6.sin6_addr.s6_addr32[3] += 32; |
| 191 | else |
| 192 | in6.sin6_addr.s6_addr32[3] = 0; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 193 | } |
| 194 | printf("%d:%s pre-alloc %lld events per sec\n", |
| 195 | cpu, test_name, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 196 | max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 199 | static void test_lru_hash_prealloc(int cpu) |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 200 | { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 201 | do_test_lru(LRU_HASH_PREALLOC, cpu); |
| 202 | } |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 203 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 204 | static void test_nocommon_lru_hash_prealloc(int cpu) |
| 205 | { |
| 206 | do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 209 | static void test_inner_lru_hash_prealloc(int cpu) |
| 210 | { |
| 211 | do_test_lru(INNER_LRU_HASH_PREALLOC, cpu); |
| 212 | } |
| 213 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 214 | static void test_lru_hash_lookup(int cpu) |
| 215 | { |
| 216 | do_test_lru(LRU_HASH_LOOKUP, cpu); |
| 217 | } |
| 218 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 219 | static void test_percpu_hash_prealloc(int cpu) |
| 220 | { |
| 221 | __u64 start_time; |
| 222 | int i; |
| 223 | |
| 224 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 225 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 226 | syscall(__NR_geteuid); |
| 227 | 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] | 228 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void test_hash_kmalloc(int cpu) |
| 232 | { |
| 233 | __u64 start_time; |
| 234 | int i; |
| 235 | |
| 236 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 237 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 238 | syscall(__NR_getgid); |
| 239 | printf("%d:hash_map_perf kmalloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 240 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void test_percpu_hash_kmalloc(int cpu) |
| 244 | { |
| 245 | __u64 start_time; |
| 246 | int i; |
| 247 | |
| 248 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 249 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 250 | syscall(__NR_getegid); |
| 251 | 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] | 252 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 253 | } |
| 254 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 255 | static void test_lpm_kmalloc(int cpu) |
| 256 | { |
| 257 | __u64 start_time; |
| 258 | int i; |
| 259 | |
| 260 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 261 | for (i = 0; i < max_cnt; i++) |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 262 | syscall(__NR_gettid); |
| 263 | printf("%d:lpm_perf kmalloc %lld events per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 264 | cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 267 | static void test_hash_lookup(int cpu) |
| 268 | { |
| 269 | __u64 start_time; |
| 270 | int i; |
| 271 | |
| 272 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 273 | for (i = 0; i < max_cnt; i++) |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 274 | syscall(__NR_getpgid, 0); |
| 275 | printf("%d:hash_lookup %lld lookups per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 276 | cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time)); |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void test_array_lookup(int cpu) |
| 280 | { |
| 281 | __u64 start_time; |
| 282 | int i; |
| 283 | |
| 284 | start_time = time_get_ns(); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 285 | for (i = 0; i < max_cnt; i++) |
Joel Fernandes | 95ec6696 | 2017-09-20 09:11:56 -0700 | [diff] [blame] | 286 | syscall(__NR_getppid, 0); |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 287 | printf("%d:array_lookup %lld lookups per sec\n", |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 288 | cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time)); |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 291 | typedef int (*pre_test_func)(int tasks); |
| 292 | const pre_test_func pre_test_funcs[] = { |
| 293 | [LRU_HASH_LOOKUP] = pre_test_lru_hash_lookup, |
| 294 | }; |
| 295 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 296 | typedef void (*test_func)(int cpu); |
| 297 | const test_func test_funcs[] = { |
| 298 | [HASH_PREALLOC] = test_hash_prealloc, |
| 299 | [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc, |
| 300 | [HASH_KMALLOC] = test_hash_kmalloc, |
| 301 | [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc, |
| 302 | [LRU_HASH_PREALLOC] = test_lru_hash_prealloc, |
| 303 | [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc, |
| 304 | [LPM_KMALLOC] = test_lpm_kmalloc, |
| 305 | [HASH_LOOKUP] = test_hash_lookup, |
| 306 | [ARRAY_LOOKUP] = test_array_lookup, |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 307 | [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc, |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 308 | [LRU_HASH_LOOKUP] = test_lru_hash_lookup, |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 311 | static int pre_test(int tasks) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | for (i = 0; i < NR_TESTS; i++) { |
| 316 | if (pre_test_funcs[i] && check_test_flags(i)) { |
| 317 | int ret = pre_test_funcs[i](tasks); |
| 318 | |
| 319 | if (ret) |
| 320 | return ret; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 327 | static void loop(int cpu) |
| 328 | { |
| 329 | cpu_set_t cpuset; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 330 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 331 | |
| 332 | CPU_ZERO(&cpuset); |
| 333 | CPU_SET(cpu, &cpuset); |
| 334 | sched_setaffinity(0, sizeof(cpuset), &cpuset); |
| 335 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 336 | for (i = 0; i < NR_TESTS; i++) { |
| 337 | if (check_test_flags(i)) |
| 338 | test_funcs[i](cpu); |
| 339 | } |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static void run_perf_test(int tasks) |
| 343 | { |
| 344 | pid_t pid[tasks]; |
| 345 | int i; |
| 346 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 347 | assert(!pre_test(tasks)); |
| 348 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 349 | for (i = 0; i < tasks; i++) { |
| 350 | pid[i] = fork(); |
| 351 | if (pid[i] == 0) { |
| 352 | loop(i); |
| 353 | exit(0); |
| 354 | } else if (pid[i] == -1) { |
| 355 | printf("couldn't spawn #%d process\n", i); |
| 356 | exit(1); |
| 357 | } |
| 358 | } |
| 359 | for (i = 0; i < tasks; i++) { |
| 360 | int status; |
| 361 | |
| 362 | assert(waitpid(pid[i], &status, 0) == pid[i]); |
| 363 | assert(status == 0); |
| 364 | } |
| 365 | } |
| 366 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 367 | static void fill_lpm_trie(void) |
| 368 | { |
| 369 | struct bpf_lpm_trie_key *key; |
| 370 | unsigned long value = 0; |
| 371 | unsigned int i; |
| 372 | int r; |
| 373 | |
| 374 | key = alloca(sizeof(*key) + 4); |
| 375 | key->prefixlen = 32; |
| 376 | |
| 377 | for (i = 0; i < 512; ++i) { |
| 378 | key->prefixlen = rand() % 33; |
| 379 | key->data[0] = rand() & 0xff; |
| 380 | key->data[1] = rand() & 0xff; |
| 381 | key->data[2] = rand() & 0xff; |
| 382 | key->data[3] = rand() & 0xff; |
| 383 | r = bpf_map_update_elem(map_fd[6], key, &value, 0); |
| 384 | assert(!r); |
| 385 | } |
| 386 | |
| 387 | key->prefixlen = 32; |
| 388 | key->data[0] = 192; |
| 389 | key->data[1] = 168; |
| 390 | key->data[2] = 0; |
| 391 | key->data[3] = 1; |
| 392 | value = 128; |
| 393 | |
| 394 | r = bpf_map_update_elem(map_fd[6], key, &value, 0); |
| 395 | assert(!r); |
| 396 | } |
| 397 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 398 | static void fixup_map(struct bpf_map_data *map, int idx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 399 | { |
| 400 | int i; |
| 401 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 402 | if (!strcmp("inner_lru_hash_map", map->name)) { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 403 | inner_lru_hash_idx = idx; |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 404 | inner_lru_hash_size = map->def.max_entries; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 407 | if (!strcmp("array_of_lru_hashs", map->name)) { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 408 | if (inner_lru_hash_idx == -1) { |
| 409 | printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n"); |
| 410 | exit(1); |
| 411 | } |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 412 | map->def.inner_map_idx = inner_lru_hash_idx; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 413 | array_of_lru_hashs_idx = idx; |
| 414 | } |
| 415 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 416 | if (!strcmp("lru_hash_lookup_map", map->name)) |
| 417 | lru_hash_lookup_idx = idx; |
| 418 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 419 | if (num_map_entries <= 0) |
| 420 | return; |
| 421 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 422 | inner_lru_hash_size = num_map_entries; |
| 423 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 424 | /* Only change the max_entries for the enabled test(s) */ |
| 425 | for (i = 0; i < NR_TESTS; i++) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 426 | if (!strcmp(test_map_names[i], map->name) && |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 427 | (check_test_flags(i))) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 428 | map->def.max_entries = num_map_entries; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 433 | int main(int argc, char **argv) |
| 434 | { |
| 435 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
| 436 | char filename[256]; |
| 437 | int num_cpu = 8; |
| 438 | |
| 439 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 440 | setrlimit(RLIMIT_MEMLOCK, &r); |
| 441 | |
| 442 | if (argc > 1) |
| 443 | test_flags = atoi(argv[1]) ? : test_flags; |
| 444 | |
| 445 | if (argc > 2) |
| 446 | num_cpu = atoi(argv[2]) ? : num_cpu; |
| 447 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 448 | if (argc > 3) |
| 449 | num_map_entries = atoi(argv[3]); |
| 450 | |
| 451 | if (argc > 4) |
| 452 | max_cnt = atoi(argv[4]); |
| 453 | |
| 454 | if (load_bpf_file_fixup_map(filename, fixup_map)) { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 455 | printf("%s", bpf_log_buf); |
| 456 | return 1; |
| 457 | } |
| 458 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 459 | fill_lpm_trie(); |
| 460 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 461 | run_perf_test(num_cpu); |
| 462 | |
| 463 | return 0; |
| 464 | } |