Skip to content

Commit 5db58fa

Browse files
iamkafaidavem330
authored andcommitted
bpf: Add tests for the LRU bpf_htab
This patch has some unit tests and a test_lru_dist. The test_lru_dist reads in the numeric keys from a file. The files used here are generated by a modified fio-genzipf tool originated from the fio test suit. The sample data file can be found here: https://github.com/iamkafai/bpf-lru The zipf.* data files have 100k numeric keys and the key is also ranged from 1 to 100k. The test_lru_dist outputs the number of unique keys (nr_unique). F.e. The following means, 61239 of them is unique out of 100k keys. nr_misses means it cannot be found in the LRU map, so nr_misses must be >= nr_unique. test_lru_dist also simulates a perfect LRU map as a comparison: [root@arch-fb-vm1 ~]# ~/devshare/fb-kernel/linux/samples/bpf/test_lru_dist \ /root/zipf.100k.a1_01.out 4000 1 ... test_parallel_lru_dist (map_type:9 map_flags:0x0): task:0 BPF LRU: nr_unique:23093(/100000) nr_misses:31603(/100000) task:0 Perfect LRU: nr_unique:23093(/100000 nr_misses:34328(/100000) .... test_parallel_lru_dist (map_type:9 map_flags:0x2): task:0 BPF LRU: nr_unique:23093(/100000) nr_misses:31710(/100000) task:0 Perfect LRU: nr_unique:23093(/100000 nr_misses:34328(/100000) [root@arch-fb-vm1 ~]# ~/devshare/fb-kernel/linux/samples/bpf/test_lru_dist \ /root/zipf.100k.a0_01.out 40000 1 ... test_parallel_lru_dist (map_type:9 map_flags:0x0): task:0 BPF LRU: nr_unique:61239(/100000) nr_misses:67054(/100000) task:0 Perfect LRU: nr_unique:61239(/100000 nr_misses:66993(/100000) ... test_parallel_lru_dist (map_type:9 map_flags:0x2): task:0 BPF LRU: nr_unique:61239(/100000) nr_misses:67068(/100000) task:0 Perfect LRU: nr_unique:61239(/100000 nr_misses:66993(/100000) LRU map has also been added to map_perf_test: /* Global LRU */ [[email protected] ~]# for i in 1 4 8; do echo -n "$i cpus: "; \ ./map_perf_test 16 $i | awk '{r += $3}END{print r " updates"}'; done 1 cpus: 2934082 updates 4 cpus: 7391434 updates 8 cpus: 6500576 updates /* Percpu LRU */ [[email protected] ~]# for i in 1 4 8; do echo -n "$i cpus: "; \ ./map_perf_test 32 $i | awk '{r += $3}END{print r " updates"}'; done 1 cpus: 2896553 updates 4 cpus: 9766395 updates 8 cpus: 17460553 updates Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8f84493 commit 5db58fa

File tree

6 files changed

+1197
-3
lines changed

6 files changed

+1197
-3
lines changed

samples/bpf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
obj- := dummy.o
33

44
# List of programs to build
5+
hostprogs-y := test_lru_dist
56
hostprogs-y += sock_example
67
hostprogs-y += fds_example
78
hostprogs-y += sockex1
@@ -28,6 +29,7 @@ hostprogs-y += trace_event
2829
hostprogs-y += sampleip
2930
hostprogs-y += tc_l2_redirect
3031

32+
test_lru_dist-objs := test_lru_dist.o libbpf.o
3133
sock_example-objs := sock_example.o libbpf.o
3234
fds_example-objs := bpf_load.o libbpf.o fds_example.o
3335
sockex1-objs := bpf_load.o libbpf.o sockex1_user.o

samples/bpf/map_perf_test_kern.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ struct bpf_map_def SEC("maps") hash_map = {
1919
.max_entries = MAX_ENTRIES,
2020
};
2121

22+
struct bpf_map_def SEC("maps") lru_hash_map = {
23+
.type = BPF_MAP_TYPE_LRU_HASH,
24+
.key_size = sizeof(u32),
25+
.value_size = sizeof(long),
26+
.max_entries = 10000,
27+
};
28+
29+
struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
30+
.type = BPF_MAP_TYPE_LRU_HASH,
31+
.key_size = sizeof(u32),
32+
.value_size = sizeof(long),
33+
.max_entries = 10000,
34+
.map_flags = BPF_F_NO_COMMON_LRU,
35+
};
36+
2237
struct bpf_map_def SEC("maps") percpu_hash_map = {
2338
.type = BPF_MAP_TYPE_PERCPU_HASH,
2439
.key_size = sizeof(u32),
@@ -53,6 +68,7 @@ int stress_hmap(struct pt_regs *ctx)
5368
value = bpf_map_lookup_elem(&hash_map, &key);
5469
if (value)
5570
bpf_map_delete_elem(&hash_map, &key);
71+
5672
return 0;
5773
}
5874

@@ -96,5 +112,28 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
96112
bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
97113
return 0;
98114
}
115+
116+
SEC("kprobe/sys_getpid")
117+
int stress_lru_hmap_alloc(struct pt_regs *ctx)
118+
{
119+
u32 key = bpf_get_prandom_u32();
120+
long val = 1;
121+
122+
bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
123+
124+
return 0;
125+
}
126+
127+
SEC("kprobe/sys_getppid")
128+
int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
129+
{
130+
u32 key = bpf_get_prandom_u32();
131+
long val = 1;
132+
133+
bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
134+
135+
return 0;
136+
}
137+
99138
char _license[] SEC("license") = "GPL";
100139
u32 _version SEC("version") = LINUX_VERSION_CODE;

samples/bpf/map_perf_test_user.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static __u64 time_get_ns(void)
3535
#define PERCPU_HASH_PREALLOC (1 << 1)
3636
#define HASH_KMALLOC (1 << 2)
3737
#define PERCPU_HASH_KMALLOC (1 << 3)
38+
#define LRU_HASH_PREALLOC (1 << 4)
39+
#define PERCPU_LRU_HASH_PREALLOC (1 << 5)
3840

3941
static int test_flags = ~0;
4042

@@ -50,6 +52,30 @@ static void test_hash_prealloc(int cpu)
5052
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
5153
}
5254

55+
static void test_lru_hash_prealloc(int cpu)
56+
{
57+
__u64 start_time;
58+
int i;
59+
60+
start_time = time_get_ns();
61+
for (i = 0; i < MAX_CNT; i++)
62+
syscall(__NR_getpid);
63+
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
64+
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
65+
}
66+
67+
static void test_percpu_lru_hash_prealloc(int cpu)
68+
{
69+
__u64 start_time;
70+
int i;
71+
72+
start_time = time_get_ns();
73+
for (i = 0; i < MAX_CNT; i++)
74+
syscall(__NR_getppid);
75+
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
76+
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
77+
}
78+
5379
static void test_percpu_hash_prealloc(int cpu)
5480
{
5581
__u64 start_time;
@@ -105,6 +131,12 @@ static void loop(int cpu)
105131

106132
if (test_flags & PERCPU_HASH_KMALLOC)
107133
test_percpu_hash_kmalloc(cpu);
134+
135+
if (test_flags & LRU_HASH_PREALLOC)
136+
test_lru_hash_prealloc(cpu);
137+
138+
if (test_flags & PERCPU_LRU_HASH_PREALLOC)
139+
test_percpu_lru_hash_prealloc(cpu);
108140
}
109141

110142
static void run_perf_test(int tasks)

0 commit comments

Comments
 (0)