aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin KaFai Lau2017-04-14 12:30:30 -0500
committerDavid S. Miller2017-04-17 12:55:52 -0500
commit3a5795b83d578cc542a92c94399946258cf1a2af (patch)
treef5d017cbdf7cd49aaa75c17c97abc4a3346f1237 /samples
parent695ba2651a2ecbb336145f9cc033c85b9c6a5cee (diff)
downloadkernel-3a5795b83d578cc542a92c94399946258cf1a2af.tar.gz
kernel-3a5795b83d578cc542a92c94399946258cf1a2af.tar.xz
kernel-3a5795b83d578cc542a92c94399946258cf1a2af.zip
bpf: lru: Add map-in-map LRU example
This patch adds a map-in-map LRU example. If we know only a subset of cores will use the LRU, we can allocate a common LRU list per targeting core and store it into an array-of-hashs. It allows using the common LRU map with map-update performance comparable to the BPF_F_NO_COMMON_LRU map but without wasting memory on the unused cores that we know they will never access the LRU map. BPF_F_NO_COMMON_LRU: > map_perf_test 32 8 10000000 10000000 | awk '{sum += $3}END{print sum}' 9234314 (9.23M/s) map-in-map LRU: > map_perf_test 512 8 1260000 80000000 | awk '{sum += $3}END{print sum}' 9962743 (9.96M/s) Notes that the max_entries for the map-in-map LRU test is 1260000 which is the max_entries for each inner LRU map. 8 processes have been started, so 8 * 1260000 = 10080000 (~10M) which is close to what is used in the BPF_F_NO_COMMON_LRU test. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/map_perf_test_kern.c34
-rw-r--r--samples/bpf/map_perf_test_user.c62
2 files changed, 93 insertions, 3 deletions
diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test_kern.c
index 404ed53b8a53..245165817fbe 100644
--- a/samples/bpf/map_perf_test_kern.c
+++ b/samples/bpf/map_perf_test_kern.c
@@ -11,6 +11,7 @@
11#include "bpf_helpers.h" 11#include "bpf_helpers.h"
12 12
13#define MAX_ENTRIES 1000 13#define MAX_ENTRIES 1000
14#define MAX_NR_CPUS 1024
14 15
15struct bpf_map_def SEC("maps") hash_map = { 16struct bpf_map_def SEC("maps") hash_map = {
16 .type = BPF_MAP_TYPE_HASH, 17 .type = BPF_MAP_TYPE_HASH,
@@ -34,6 +35,19 @@ struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
34 .map_flags = BPF_F_NO_COMMON_LRU, 35 .map_flags = BPF_F_NO_COMMON_LRU,
35}; 36};
36 37
38struct bpf_map_def SEC("maps") inner_lru_hash_map = {
39 .type = BPF_MAP_TYPE_LRU_HASH,
40 .key_size = sizeof(u32),
41 .value_size = sizeof(long),
42 .max_entries = MAX_ENTRIES,
43};
44
45struct bpf_map_def SEC("maps") array_of_lru_hashs = {
46 .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
47 .key_size = sizeof(u32),
48 .max_entries = MAX_NR_CPUS,
49};
50
37struct bpf_map_def SEC("maps") percpu_hash_map = { 51struct bpf_map_def SEC("maps") percpu_hash_map = {
38 .type = BPF_MAP_TYPE_PERCPU_HASH, 52 .type = BPF_MAP_TYPE_PERCPU_HASH,
39 .key_size = sizeof(u32), 53 .key_size = sizeof(u32),
@@ -154,13 +168,27 @@ int stress_lru_hmap_alloc(struct pt_regs *ctx)
154 168
155 test_case = dst6[7]; 169 test_case = dst6[7];
156 170
157 if (test_case == 0) 171 if (test_case == 0) {
158 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY); 172 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
159 else if (test_case == 1) 173 } else if (test_case == 1) {
160 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val, 174 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
161 BPF_ANY); 175 BPF_ANY);
162 else 176 } else if (test_case == 2) {
177 void *nolocal_lru_map;
178 int cpu = bpf_get_smp_processor_id();
179
180 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
181 &cpu);
182 if (!nolocal_lru_map) {
183 ret = -ENOENT;
184 goto done;
185 }
186
187 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
188 BPF_ANY);
189 } else {
163 ret = -EINVAL; 190 ret = -EINVAL;
191 }
164 192
165done: 193done:
166 if (ret) 194 if (ret)
diff --git a/samples/bpf/map_perf_test_user.c b/samples/bpf/map_perf_test_user.c
index 2a12f48b5c6d..6ac778153315 100644
--- a/samples/bpf/map_perf_test_user.c
+++ b/samples/bpf/map_perf_test_user.c
@@ -25,6 +25,7 @@
25#include "bpf_load.h" 25#include "bpf_load.h"
26 26
27#define TEST_BIT(t) (1U << (t)) 27#define TEST_BIT(t) (1U << (t))
28#define MAX_NR_CPUS 1024
28 29
29static __u64 time_get_ns(void) 30static __u64 time_get_ns(void)
30{ 31{
@@ -44,6 +45,7 @@ enum test_type {
44 LPM_KMALLOC, 45 LPM_KMALLOC,
45 HASH_LOOKUP, 46 HASH_LOOKUP,
46 ARRAY_LOOKUP, 47 ARRAY_LOOKUP,
48 INNER_LRU_HASH_PREALLOC,
47 NR_TESTS, 49 NR_TESTS,
48}; 50};
49 51
@@ -57,10 +59,14 @@ const char *test_map_names[NR_TESTS] = {
57 [LPM_KMALLOC] = "lpm_trie_map_alloc", 59 [LPM_KMALLOC] = "lpm_trie_map_alloc",
58 [HASH_LOOKUP] = "hash_map", 60 [HASH_LOOKUP] = "hash_map",
59 [ARRAY_LOOKUP] = "array_map", 61 [ARRAY_LOOKUP] = "array_map",
62 [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
60}; 63};
61 64
62static int test_flags = ~0; 65static int test_flags = ~0;
63static uint32_t num_map_entries; 66static uint32_t num_map_entries;
67static uint32_t inner_lru_hash_size;
68static int inner_lru_hash_idx = -1;
69static int array_of_lru_hashs_idx = -1;
64static uint32_t max_cnt = 1000000; 70static uint32_t max_cnt = 1000000;
65 71
66static int check_test_flags(enum test_type t) 72static int check_test_flags(enum test_type t)
@@ -82,11 +88,42 @@ static void test_hash_prealloc(int cpu)
82 88
83static void do_test_lru(enum test_type test, int cpu) 89static void do_test_lru(enum test_type test, int cpu)
84{ 90{
91 static int inner_lru_map_fds[MAX_NR_CPUS];
92
85 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 }; 93 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
86 const char *test_name; 94 const char *test_name;
87 __u64 start_time; 95 __u64 start_time;
88 int i, ret; 96 int i, ret;
89 97
98 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
90 in6.sin6_addr.s6_addr16[0] = 0xdead; 127 in6.sin6_addr.s6_addr16[0] = 0xdead;
91 in6.sin6_addr.s6_addr16[1] = 0xbeef; 128 in6.sin6_addr.s6_addr16[1] = 0xbeef;
92 129
@@ -96,6 +133,9 @@ static void do_test_lru(enum test_type test, int cpu)
96 } else if (test == NOCOMMON_LRU_HASH_PREALLOC) { 133 } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
97 test_name = "nocommon_lru_hash_map_perf"; 134 test_name = "nocommon_lru_hash_map_perf";
98 in6.sin6_addr.s6_addr16[7] = 1; 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;
99 } else { 139 } else {
100 assert(0); 140 assert(0);
101 } 141 }
@@ -120,6 +160,11 @@ static void test_nocommon_lru_hash_prealloc(int cpu)
120 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu); 160 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
121} 161}
122 162
163static void test_inner_lru_hash_prealloc(int cpu)
164{
165 do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
166}
167
123static void test_percpu_hash_prealloc(int cpu) 168static void test_percpu_hash_prealloc(int cpu)
124{ 169{
125 __u64 start_time; 170 __u64 start_time;
@@ -203,6 +248,7 @@ const test_func test_funcs[] = {
203 [LPM_KMALLOC] = test_lpm_kmalloc, 248 [LPM_KMALLOC] = test_lpm_kmalloc,
204 [HASH_LOOKUP] = test_hash_lookup, 249 [HASH_LOOKUP] = test_hash_lookup,
205 [ARRAY_LOOKUP] = test_array_lookup, 250 [ARRAY_LOOKUP] = test_array_lookup,
251 [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
206}; 252};
207 253
208static void loop(int cpu) 254static void loop(int cpu)
@@ -278,9 +324,25 @@ static void fixup_map(struct bpf_map_def *map, const char *name, int idx)
278{ 324{
279 int i; 325 int i;
280 326
327 if (!strcmp("inner_lru_hash_map", name)) {
328 inner_lru_hash_idx = idx;
329 inner_lru_hash_size = map->max_entries;
330 }
331
332 if (!strcmp("array_of_lru_hashs", name)) {
333 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 }
337 map->inner_map_idx = inner_lru_hash_idx;
338 array_of_lru_hashs_idx = idx;
339 }
340
281 if (num_map_entries <= 0) 341 if (num_map_entries <= 0)
282 return; 342 return;
283 343
344 inner_lru_hash_size = num_map_entries;
345
284 /* Only change the max_entries for the enabled test(s) */ 346 /* Only change the max_entries for the enabled test(s) */
285 for (i = 0; i < NR_TESTS; i++) { 347 for (i = 0; i < NR_TESTS; i++) {
286 if (!strcmp(test_map_names[i], name) && 348 if (!strcmp(test_map_names[i], name) &&