aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorMartin KaFai Lau <kafai@fb.com>2017-04-14 13:30:27 -0400
committerDavid S. Miller <davem@davemloft.net>2017-04-17 13:55:52 -0400
commitbf8db5d243a103ccd3f6d82a110e2302608e248c (patch)
tree80e2505b50f17841299ae6273bcdbb4eeb04cc33 /samples
parent6467acbc70f011d5852f60f6e04825a268c8e8b0 (diff)
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>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/map_perf_test_kern.c43
-rw-r--r--samples/bpf/map_perf_test_user.c53
2 files changed, 67 insertions, 29 deletions
diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test_kern.c
index 9da2a3441b0a..404ed53b8a53 100644
--- a/samples/bpf/map_perf_test_kern.c
+++ b/samples/bpf/map_perf_test_kern.c
@@ -26,7 +26,7 @@ struct bpf_map_def SEC("maps") lru_hash_map = {
26 .max_entries = 10000, 26 .max_entries = 10000,
27}; 27};
28 28
29struct bpf_map_def SEC("maps") percpu_lru_hash_map = { 29struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
30 .type = BPF_MAP_TYPE_LRU_HASH, 30 .type = BPF_MAP_TYPE_LRU_HASH,
31 .key_size = sizeof(u32), 31 .key_size = sizeof(u32),
32 .value_size = sizeof(long), 32 .value_size = sizeof(long),
@@ -100,6 +100,7 @@ int stress_percpu_hmap(struct pt_regs *ctx)
100 bpf_map_delete_elem(&percpu_hash_map, &key); 100 bpf_map_delete_elem(&percpu_hash_map, &key);
101 return 0; 101 return 0;
102} 102}
103
103SEC("kprobe/sys_getgid") 104SEC("kprobe/sys_getgid")
104int stress_hmap_alloc(struct pt_regs *ctx) 105int stress_hmap_alloc(struct pt_regs *ctx)
105{ 106{
@@ -128,24 +129,42 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
128 return 0; 129 return 0;
129} 130}
130 131
131SEC("kprobe/sys_getpid") 132SEC("kprobe/sys_connect")
132int stress_lru_hmap_alloc(struct pt_regs *ctx) 133int stress_lru_hmap_alloc(struct pt_regs *ctx)
133{ 134{
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";
135 long val = 1; 139 long val = 1;
140 u32 key = bpf_get_prandom_u32();
136 141
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);
138 144
139 return 0; 145 if (addrlen != sizeof(*in6))
140} 146 return 0;
141 147
142SEC("kprobe/sys_getppid") 148 ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
143int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx) 149 if (ret)
144{ 150 goto done;
145 u32 key = bpf_get_prandom_u32(); 151
146 long val = 1; 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;
147 164
148 bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY); 165done:
166 if (ret)
167 bpf_trace_printk(fmt, sizeof(fmt), ret);
149 168
150 return 0; 169 return 0;
151} 170}
diff --git a/samples/bpf/map_perf_test_user.c b/samples/bpf/map_perf_test_user.c
index e29ff318a793..51cb8f238aa2 100644
--- a/samples/bpf/map_perf_test_user.c
+++ b/samples/bpf/map_perf_test_user.c
@@ -18,6 +18,9 @@
18#include <string.h> 18#include <string.h>
19#include <time.h> 19#include <time.h>
20#include <sys/resource.h> 20#include <sys/resource.h>
21#include <arpa/inet.h>
22#include <errno.h>
23
21#include "libbpf.h" 24#include "libbpf.h"
22#include "bpf_load.h" 25#include "bpf_load.h"
23 26
@@ -36,7 +39,7 @@ static __u64 time_get_ns(void)
36#define HASH_KMALLOC (1 << 2) 39#define HASH_KMALLOC (1 << 2)
37#define PERCPU_HASH_KMALLOC (1 << 3) 40#define PERCPU_HASH_KMALLOC (1 << 3)
38#define LRU_HASH_PREALLOC (1 << 4) 41#define LRU_HASH_PREALLOC (1 << 4)
39#define PERCPU_LRU_HASH_PREALLOC (1 << 5) 42#define NOCOMMON_LRU_HASH_PREALLOC (1 << 5)
40#define LPM_KMALLOC (1 << 6) 43#define LPM_KMALLOC (1 << 6)
41#define HASH_LOOKUP (1 << 7) 44#define HASH_LOOKUP (1 << 7)
42#define ARRAY_LOOKUP (1 << 8) 45#define ARRAY_LOOKUP (1 << 8)
@@ -55,28 +58,44 @@ static void test_hash_prealloc(int cpu)
55 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 58 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
56} 59}
57 60
58static void test_lru_hash_prealloc(int cpu) 61static void do_test_lru(int lru_test_flag, int cpu)
59{ 62{
63 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
64 const char *test_name;
60 __u64 start_time; 65 __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 }
62 80
63 start_time = time_get_ns(); 81 start_time = time_get_ns();
64 for (i = 0; i < MAX_CNT; i++) 82 for (i = 0; i < MAX_CNT; i++) {
65 syscall(__NR_getpid); 83 ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
66 printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n", 84 assert(ret == -1 && errno == EBADF);
67 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 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));
68} 89}
69 90
70static void test_percpu_lru_hash_prealloc(int cpu) 91static void test_lru_hash_prealloc(int cpu)
71{ 92{
72 __u64 start_time; 93 do_test_lru(LRU_HASH_PREALLOC, cpu);
73 int i; 94}
74 95
75 start_time = time_get_ns(); 96static void test_nocommon_lru_hash_prealloc(int cpu)
76 for (i = 0; i < MAX_CNT; i++) 97{
77 syscall(__NR_getppid); 98 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
78 printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
79 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
80} 99}
81 100
82static void test_percpu_hash_prealloc(int cpu) 101static void test_percpu_hash_prealloc(int cpu)
@@ -174,8 +193,8 @@ static void loop(int cpu)
174 if (test_flags & LRU_HASH_PREALLOC) 193 if (test_flags & LRU_HASH_PREALLOC)
175 test_lru_hash_prealloc(cpu); 194 test_lru_hash_prealloc(cpu);
176 195
177 if (test_flags & PERCPU_LRU_HASH_PREALLOC) 196 if (test_flags & NOCOMMON_LRU_HASH_PREALLOC)
178 test_percpu_lru_hash_prealloc(cpu); 197 test_nocommon_lru_hash_prealloc(cpu);
179 198
180 if (test_flags & LPM_KMALLOC) 199 if (test_flags & LPM_KMALLOC)
181 test_lpm_kmalloc(cpu); 200 test_lpm_kmalloc(cpu);