Commit 5db58faf authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by David S. Miller

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 */
[root@kerneltest003.31.prn1 ~]# 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 */
[root@kerneltest003.31.prn1 ~]# 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: default avatarMartin KaFai Lau <kafai@fb.com>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8f844938
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
obj- := dummy.o obj- := dummy.o
# List of programs to build # List of programs to build
hostprogs-y := test_lru_dist
hostprogs-y += sock_example hostprogs-y += sock_example
hostprogs-y += fds_example hostprogs-y += fds_example
hostprogs-y += sockex1 hostprogs-y += sockex1
...@@ -28,6 +29,7 @@ hostprogs-y += trace_event ...@@ -28,6 +29,7 @@ hostprogs-y += trace_event
hostprogs-y += sampleip hostprogs-y += sampleip
hostprogs-y += tc_l2_redirect hostprogs-y += tc_l2_redirect
test_lru_dist-objs := test_lru_dist.o libbpf.o
sock_example-objs := sock_example.o libbpf.o sock_example-objs := sock_example.o libbpf.o
fds_example-objs := bpf_load.o libbpf.o fds_example.o fds_example-objs := bpf_load.o libbpf.o fds_example.o
sockex1-objs := bpf_load.o libbpf.o sockex1_user.o sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
......
...@@ -19,6 +19,21 @@ struct bpf_map_def SEC("maps") hash_map = { ...@@ -19,6 +19,21 @@ struct bpf_map_def SEC("maps") hash_map = {
.max_entries = MAX_ENTRIES, .max_entries = MAX_ENTRIES,
}; };
struct bpf_map_def SEC("maps") lru_hash_map = {
.type = BPF_MAP_TYPE_LRU_HASH,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = 10000,
};
struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
.type = BPF_MAP_TYPE_LRU_HASH,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = 10000,
.map_flags = BPF_F_NO_COMMON_LRU,
};
struct bpf_map_def SEC("maps") percpu_hash_map = { struct bpf_map_def SEC("maps") percpu_hash_map = {
.type = BPF_MAP_TYPE_PERCPU_HASH, .type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(u32), .key_size = sizeof(u32),
...@@ -53,6 +68,7 @@ int stress_hmap(struct pt_regs *ctx) ...@@ -53,6 +68,7 @@ int stress_hmap(struct pt_regs *ctx)
value = bpf_map_lookup_elem(&hash_map, &key); value = bpf_map_lookup_elem(&hash_map, &key);
if (value) if (value)
bpf_map_delete_elem(&hash_map, &key); bpf_map_delete_elem(&hash_map, &key);
return 0; return 0;
} }
...@@ -96,5 +112,28 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx) ...@@ -96,5 +112,28 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
bpf_map_delete_elem(&percpu_hash_map_alloc, &key); bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
return 0; return 0;
} }
SEC("kprobe/sys_getpid")
int stress_lru_hmap_alloc(struct pt_regs *ctx)
{
u32 key = bpf_get_prandom_u32();
long val = 1;
bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
return 0;
}
SEC("kprobe/sys_getppid")
int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
{
u32 key = bpf_get_prandom_u32();
long val = 1;
bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
return 0;
}
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
u32 _version SEC("version") = LINUX_VERSION_CODE; u32 _version SEC("version") = LINUX_VERSION_CODE;
...@@ -35,6 +35,8 @@ static __u64 time_get_ns(void) ...@@ -35,6 +35,8 @@ static __u64 time_get_ns(void)
#define PERCPU_HASH_PREALLOC (1 << 1) #define PERCPU_HASH_PREALLOC (1 << 1)
#define HASH_KMALLOC (1 << 2) #define HASH_KMALLOC (1 << 2)
#define PERCPU_HASH_KMALLOC (1 << 3) #define PERCPU_HASH_KMALLOC (1 << 3)
#define LRU_HASH_PREALLOC (1 << 4)
#define PERCPU_LRU_HASH_PREALLOC (1 << 5)
static int test_flags = ~0; static int test_flags = ~0;
...@@ -50,6 +52,30 @@ static void test_hash_prealloc(int cpu) ...@@ -50,6 +52,30 @@ static void test_hash_prealloc(int cpu)
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
} }
static void test_lru_hash_prealloc(int cpu)
{
__u64 start_time;
int i;
start_time = time_get_ns();
for (i = 0; i < MAX_CNT; i++)
syscall(__NR_getpid);
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
}
static void test_percpu_lru_hash_prealloc(int cpu)
{
__u64 start_time;
int i;
start_time = time_get_ns();
for (i = 0; i < MAX_CNT; i++)
syscall(__NR_getppid);
printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
}
static void test_percpu_hash_prealloc(int cpu) static void test_percpu_hash_prealloc(int cpu)
{ {
__u64 start_time; __u64 start_time;
...@@ -105,6 +131,12 @@ static void loop(int cpu) ...@@ -105,6 +131,12 @@ static void loop(int cpu)
if (test_flags & PERCPU_HASH_KMALLOC) if (test_flags & PERCPU_HASH_KMALLOC)
test_percpu_hash_kmalloc(cpu); test_percpu_hash_kmalloc(cpu);
if (test_flags & LRU_HASH_PREALLOC)
test_lru_hash_prealloc(cpu);
if (test_flags & PERCPU_LRU_HASH_PREALLOC)
test_percpu_lru_hash_prealloc(cpu);
} }
static void run_perf_test(int tasks) static void run_perf_test(int tasks)
......
This diff is collapsed.
CFLAGS += -Wall -O2 CFLAGS += -Wall -O2 -I../../../../usr/include
test_objs = test_verifier test_maps test_objs = test_verifier test_maps test_lru_map
TEST_PROGS := test_verifier test_maps test_kmod.sh TEST_PROGS := test_verifier test_maps test_lru_map test_kmod.sh
TEST_FILES := $(test_objs) TEST_FILES := $(test_objs)
all: $(test_objs) all: $(test_objs)
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment