Skip to content

Commit bf8db5d

Browse files
iamkafaidavem330
authored andcommitted
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>
1 parent 6467acb commit bf8db5d

2 files changed

Lines changed: 67 additions & 29 deletions

File tree

‎samples/bpf/map_perf_test_kern.c‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct bpf_map_def SEC("maps") lru_hash_map = {
2626
.max_entries = 10000,
2727
};
2828

29-
struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
29+
struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
3030
.type = BPF_MAP_TYPE_LRU_HASH,
3131
.key_size = sizeof(u32),
3232
.value_size = sizeof(long),
@@ -100,6 +100,7 @@ int stress_percpu_hmap(struct pt_regs *ctx)
100100
bpf_map_delete_elem(&percpu_hash_map, &key);
101101
return 0;
102102
}
103+
103104
SEC("kprobe/sys_getgid")
104105
int stress_hmap_alloc(struct pt_regs *ctx)
105106
{
@@ -128,24 +129,42 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
128129
return 0;
129130
}
130131

131-
SEC("kprobe/sys_getpid")
132+
SEC("kprobe/sys_connect")
132133
int stress_lru_hmap_alloc(struct pt_regs *ctx)
133134
{
134-
u32 key = bpf_get_prandom_u32();
135+
struct sockaddr_in6 *in6;
136+
u16 test_case, dst6[8];
137+
int addrlen, ret;
138+
char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n";
135139
long val = 1;
140+
u32 key = bpf_get_prandom_u32();
136141

137-
bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
142+
in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
143+
addrlen = (int)PT_REGS_PARM3(ctx);
138144

139-
return 0;
140-
}
145+
if (addrlen != sizeof(*in6))
146+
return 0;
141147

142-
SEC("kprobe/sys_getppid")
143-
int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
144-
{
145-
u32 key = bpf_get_prandom_u32();
146-
long val = 1;
148+
ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
149+
if (ret)
150+
goto done;
151+
152+
if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
153+
return 0;
154+
155+
test_case = dst6[7];
156+
157+
if (test_case == 0)
158+
ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
159+
else if (test_case == 1)
160+
ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
161+
BPF_ANY);
162+
else
163+
ret = -EINVAL;
147164

148-
bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
165+
done:
166+
if (ret)
167+
bpf_trace_printk(fmt, sizeof(fmt), ret);
149168

150169
return 0;
151170
}

‎samples/bpf/map_perf_test_user.c‎

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <string.h>
1919
#include <time.h>
2020
#include <sys/resource.h>
21+
#include <arpa/inet.h>
22+
#include <errno.h>
23+
2124
#include "libbpf.h"
2225
#include "bpf_load.h"
2326

@@ -36,7 +39,7 @@ static __u64 time_get_ns(void)
3639
#define HASH_KMALLOC (1 << 2)
3740
#define PERCPU_HASH_KMALLOC (1 << 3)
3841
#define LRU_HASH_PREALLOC (1 << 4)
39-
#define PERCPU_LRU_HASH_PREALLOC (1 << 5)
42+
#define NOCOMMON_LRU_HASH_PREALLOC (1 << 5)
4043
#define LPM_KMALLOC (1 << 6)
4144
#define HASH_LOOKUP (1 << 7)
4245
#define ARRAY_LOOKUP (1 << 8)
@@ -55,28 +58,44 @@ static void test_hash_prealloc(int cpu)
5558
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
5659
}
5760

58-
static void test_lru_hash_prealloc(int cpu)
61+
static void do_test_lru(int lru_test_flag, int cpu)
5962
{
63+
struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
64+
const char *test_name;
6065
__u64 start_time;
61-
int i;
66+
int i, ret;
67+
68+
in6.sin6_addr.s6_addr16[0] = 0xdead;
69+
in6.sin6_addr.s6_addr16[1] = 0xbeef;
70+
71+
if (lru_test_flag & LRU_HASH_PREALLOC) {
72+
test_name = "lru_hash_map_perf";
73+
in6.sin6_addr.s6_addr16[7] = 0;
74+
} else if (lru_test_flag & NOCOMMON_LRU_HASH_PREALLOC) {
75+
test_name = "nocommon_lru_hash_map_perf";
76+
in6.sin6_addr.s6_addr16[7] = 1;
77+
} else {
78+
assert(0);
79+
}
6280

6381
start_time = time_get_ns();
64-
for (i = 0; i < MAX_CNT; i++)
65-
syscall(__NR_getpid);
66-
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
67-
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
82+
for (i = 0; i < MAX_CNT; i++) {
83+
ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
84+
assert(ret == -1 && errno == EBADF);
85+
}
86+
printf("%d:%s pre-alloc %lld events per sec\n",
87+
cpu, test_name,
88+
MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
6889
}
6990

70-
static void test_percpu_lru_hash_prealloc(int cpu)
91+
static void test_lru_hash_prealloc(int cpu)
7192
{
72-
__u64 start_time;
73-
int i;
93+
do_test_lru(LRU_HASH_PREALLOC, cpu);
94+
}
7495

75-
start_time = time_get_ns();
76-
for (i = 0; i < MAX_CNT; i++)
77-
syscall(__NR_getppid);
78-
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
79-
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
96+
static void test_nocommon_lru_hash_prealloc(int cpu)
97+
{
98+
do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
8099
}
81100

82101
static void test_percpu_hash_prealloc(int cpu)
@@ -174,8 +193,8 @@ static void loop(int cpu)
174193
if (test_flags & LRU_HASH_PREALLOC)
175194
test_lru_hash_prealloc(cpu);
176195

177-
if (test_flags & PERCPU_LRU_HASH_PREALLOC)
178-
test_percpu_lru_hash_prealloc(cpu);
196+
if (test_flags & NOCOMMON_LRU_HASH_PREALLOC)
197+
test_nocommon_lru_hash_prealloc(cpu);
179198

180199
if (test_flags & LPM_KMALLOC)
181200
test_lpm_kmalloc(cpu);

0 commit comments

Comments
 (0)