Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: lru: Refactor LRU map tests in map_perf_test

One more LRU test will be added later in this patch series.
In this patch, we first move all existing LRU map tests into
a single syscall (connect) first so that the future new
LRU test can be added without hunting another syscall.

One of the map name is also changed from percpu_lru_hash_map
to nocommon_lru_hash_map to avoid the confusion with percpu_hash_map.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Martin KaFai Lau and committed by
David S. Miller
bf8db5d2 6467acbc

+69 -31
+33 -14
samples/bpf/map_perf_test_kern.c
··· 26 26 .max_entries = 10000, 27 27 }; 28 28 29 - struct bpf_map_def SEC("maps") percpu_lru_hash_map = { 29 + struct bpf_map_def SEC("maps") nocommon_lru_hash_map = { 30 30 .type = BPF_MAP_TYPE_LRU_HASH, 31 31 .key_size = sizeof(u32), 32 32 .value_size = sizeof(long), ··· 100 100 bpf_map_delete_elem(&percpu_hash_map, &key); 101 101 return 0; 102 102 } 103 + 103 104 SEC("kprobe/sys_getgid") 104 105 int stress_hmap_alloc(struct pt_regs *ctx) 105 106 { ··· 129 128 return 0; 130 129 } 131 130 132 - SEC("kprobe/sys_getpid") 131 + SEC("kprobe/sys_connect") 133 132 int stress_lru_hmap_alloc(struct pt_regs *ctx) 134 133 { 135 - u32 key = bpf_get_prandom_u32(); 134 + struct sockaddr_in6 *in6; 135 + u16 test_case, dst6[8]; 136 + int addrlen, ret; 137 + char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n"; 136 138 long val = 1; 137 - 138 - bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY); 139 - 140 - return 0; 141 - } 142 - 143 - SEC("kprobe/sys_getppid") 144 - int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx) 145 - { 146 139 u32 key = bpf_get_prandom_u32(); 147 - long val = 1; 148 140 149 - bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY); 141 + in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx); 142 + addrlen = (int)PT_REGS_PARM3(ctx); 143 + 144 + if (addrlen != sizeof(*in6)) 145 + return 0; 146 + 147 + ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr); 148 + if (ret) 149 + goto done; 150 + 151 + if (dst6[0] != 0xdead || dst6[1] != 0xbeef) 152 + return 0; 153 + 154 + test_case = dst6[7]; 155 + 156 + if (test_case == 0) 157 + ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY); 158 + else if (test_case == 1) 159 + ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val, 160 + BPF_ANY); 161 + else 162 + ret = -EINVAL; 163 + 164 + done: 165 + if (ret) 166 + bpf_trace_printk(fmt, sizeof(fmt), ret); 150 167 151 168 return 0; 152 169 }
+36 -17
samples/bpf/map_perf_test_user.c
··· 18 18 #include <string.h> 19 19 #include <time.h> 20 20 #include <sys/resource.h> 21 + #include <arpa/inet.h> 22 + #include <errno.h> 23 + 21 24 #include "libbpf.h" 22 25 #include "bpf_load.h" 23 26 ··· 39 36 #define HASH_KMALLOC (1 << 2) 40 37 #define PERCPU_HASH_KMALLOC (1 << 3) 41 38 #define LRU_HASH_PREALLOC (1 << 4) 42 - #define PERCPU_LRU_HASH_PREALLOC (1 << 5) 39 + #define NOCOMMON_LRU_HASH_PREALLOC (1 << 5) 43 40 #define LPM_KMALLOC (1 << 6) 44 41 #define HASH_LOOKUP (1 << 7) 45 42 #define ARRAY_LOOKUP (1 << 8) ··· 58 55 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 59 56 } 60 57 61 - static void test_lru_hash_prealloc(int cpu) 58 + static void do_test_lru(int lru_test_flag, int cpu) 62 59 { 60 + struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 }; 61 + const char *test_name; 63 62 __u64 start_time; 64 - int i; 63 + int i, ret; 64 + 65 + in6.sin6_addr.s6_addr16[0] = 0xdead; 66 + in6.sin6_addr.s6_addr16[1] = 0xbeef; 67 + 68 + if (lru_test_flag & LRU_HASH_PREALLOC) { 69 + test_name = "lru_hash_map_perf"; 70 + in6.sin6_addr.s6_addr16[7] = 0; 71 + } else if (lru_test_flag & NOCOMMON_LRU_HASH_PREALLOC) { 72 + test_name = "nocommon_lru_hash_map_perf"; 73 + in6.sin6_addr.s6_addr16[7] = 1; 74 + } else { 75 + assert(0); 76 + } 65 77 66 78 start_time = time_get_ns(); 67 - for (i = 0; i < MAX_CNT; i++) 68 - syscall(__NR_getpid); 69 - printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n", 70 - cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 79 + for (i = 0; i < MAX_CNT; i++) { 80 + ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6)); 81 + assert(ret == -1 && errno == EBADF); 82 + } 83 + printf("%d:%s pre-alloc %lld events per sec\n", 84 + cpu, test_name, 85 + MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 71 86 } 72 87 73 - static void test_percpu_lru_hash_prealloc(int cpu) 88 + static void test_lru_hash_prealloc(int cpu) 74 89 { 75 - __u64 start_time; 76 - int i; 90 + do_test_lru(LRU_HASH_PREALLOC, cpu); 91 + } 77 92 78 - start_time = time_get_ns(); 79 - for (i = 0; i < MAX_CNT; i++) 80 - syscall(__NR_getppid); 81 - printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n", 82 - cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 93 + static void test_nocommon_lru_hash_prealloc(int cpu) 94 + { 95 + do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu); 83 96 } 84 97 85 98 static void test_percpu_hash_prealloc(int cpu) ··· 193 174 if (test_flags & LRU_HASH_PREALLOC) 194 175 test_lru_hash_prealloc(cpu); 195 176 196 - if (test_flags & PERCPU_LRU_HASH_PREALLOC) 197 - test_percpu_lru_hash_prealloc(cpu); 177 + if (test_flags & NOCOMMON_LRU_HASH_PREALLOC) 178 + test_nocommon_lru_hash_prealloc(cpu); 198 179 199 180 if (test_flags & LPM_KMALLOC) 200 181 test_lpm_kmalloc(cpu);