Skip to content

Commit fa6809b

Browse files
author
yimingx
committed
feat: eval heap ready
1 parent 9286f2c commit fa6809b

6 files changed

Lines changed: 224 additions & 32 deletions

File tree

test/evaluation/km/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ init_ll.o: llbench_bpf.c
1212
lookup_ll.o: llbench_lookup_bpf.c
1313
clang -O2 -I/usr/include/$(shell uname -m)-linux-gnu -target bpf -g -c $< -o lookup_ll.o
1414

15+
delete_ll.o: llbench_delete_bpf.c
16+
clang -O2 -I/usr/include/$(shell uname -m)-linux-gnu -target bpf -g -c $< -o delete_ll.o
17+
1518
cleanobj:
16-
rm -f init_ll.o lookup_ll.o
19+
rm -f init_ll.o lookup_ll.o delete_ll.o
1720
sudo rm -f /sys/fs/bpf/llbench
1821

19-
autoattach: cleanobj init_ll.o lookup_ll.o
20-
sudo bpftool prog load init_ll.o /sys/fs/bpf/llbench autoattach
22+
autoattach: cleanobj init_ll.o lookup_ll.o delete_ll.o
23+
sudo bpftool prog load lookup_ll.o /sys/fs/bpf/llbench autoattach
2124

2225
clean:
2326
$(MAKE) -C $(KDIR) M=$(PWD) clean

test/evaluation/km/llbench.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <linux/random.h>
55
#include <linux/ktime.h>
66

7-
#define LIST_SIZE (10000)
7+
#define LIST_SIZE (64 * 1000)
88

99
struct ll_node {
1010
u32 key;
@@ -31,8 +31,8 @@ static struct ll_node *ll_lookup(u32 key)
3131
{
3232
struct ll_node *cur = head;
3333
while (cur) {
34-
if (cur->key == key)
35-
return cur;
34+
// if (cur->key == key)
35+
// return cur;
3636
cur = cur->next;
3737
}
3838
return NULL;
@@ -89,11 +89,11 @@ static void benchmark(void)
8989

9090
/* lookup test: 1K random accesses */
9191
t_start = now_ns();
92-
// for (i = 0; i < LIST_SIZE; i++) {
93-
ll_lookup(11111);
94-
// }
92+
for (i = 0; i < 100; i++) {
93+
ll_lookup(11111);
94+
}
9595
t_end = now_ns();
96-
latency = t_end - t_start;
96+
latency = (t_end - t_start)/100;
9797

9898
pr_info("llbench: lookup: %llu ns\n", latency);
9999

test/evaluation/km/llbench_bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <bpf/bpf_helpers.h>
33
#include <linux/bpf.h>
44

5-
#define META_SIZE 40960
5+
#define META_SIZE 65000
66
#define META_BLOCK_SIZE 32
77
#define HEAP_SIZE ((META_SIZE + 1) * META_BLOCK_SIZE)
88

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <linux/types.h>
2+
#include <bpf/bpf_helpers.h>
3+
#include <linux/bpf.h>
4+
5+
#define META_SIZE 65000
6+
#define META_BLOCK_SIZE 32
7+
#define HEAP_SIZE ((META_SIZE + 1) * META_BLOCK_SIZE)
8+
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_ARRAY);
11+
__uint(max_entries, META_SIZE + 1);
12+
__type(key, __u32);
13+
__type(value, __u32);
14+
} meta SEC(".maps");
15+
16+
struct {
17+
__uint(type, BPF_MAP_TYPE_ARRAY);
18+
__uint(max_entries, 1);
19+
__type(key, __u32);
20+
__type(value, char[HEAP_SIZE]);
21+
} data SEC(".maps");
22+
23+
static __always_inline char *init_heap(long size) {
24+
int i = 0;
25+
char *head = bpf_map_lookup_elem(&data, &i);
26+
return head;
27+
}
28+
29+
static __u64 gettop() {
30+
__u32 i = 0;
31+
__u32 *head = bpf_map_lookup_elem(&meta, &i);
32+
if (head == 0) {
33+
return 0;
34+
}
35+
return *head * META_BLOCK_SIZE;
36+
}
37+
38+
static __noinline __u64 malloc(__u32 size) {
39+
__u32 i = 0;
40+
// long rv = 0;
41+
__u32 *head = bpf_map_lookup_elem(&meta, &i);
42+
if (head == 0) {
43+
return 0;
44+
}
45+
if (size >= HEAP_SIZE || size <= 1) {
46+
return 0;
47+
}
48+
__u32 rid = *head + 1;
49+
50+
__u32 blocks = 1 + (size - 1) / META_BLOCK_SIZE;
51+
52+
if (*head + blocks >= META_SIZE) {
53+
*head = 0;
54+
// TODO find free space
55+
return 0;
56+
}
57+
(*head) += blocks;
58+
// Directly use head->pos
59+
// int newpos = head->pos+1;
60+
// We got a free space at idx head->pos and data pos head->size
61+
62+
__u32 *init_pos = bpf_map_lookup_elem(&meta, &rid);
63+
if (init_pos == 0) {
64+
return 0;
65+
}
66+
*init_pos = blocks;
67+
68+
for (int i = 1; i < META_SIZE; i++) {
69+
if (i >= blocks) {
70+
break;
71+
}
72+
__u32 check_idx = rid + i;
73+
if (check_idx >= META_SIZE) {
74+
// Out of space
75+
return 0;
76+
}
77+
__u32 *check_pos = bpf_map_lookup_elem(&meta, &check_idx);
78+
if (check_pos == 0) {
79+
return 0;
80+
}
81+
*check_pos = 1;
82+
}
83+
84+
return (rid - 1) * META_BLOCK_SIZE;
85+
}
86+
87+
static __noinline void free(__u32 idx) {
88+
__u32 block_idx = idx / META_BLOCK_SIZE + 1;
89+
__u32 *meta_pos = bpf_map_lookup_elem(&meta, &block_idx);
90+
if (meta_pos == 0) {
91+
return;
92+
}
93+
__u32 blocks = *meta_pos;
94+
if (blocks >= 1) {
95+
__u32 *cur_pos = bpf_map_lookup_elem(&meta, &block_idx);
96+
if (cur_pos == 0) {
97+
return;
98+
}
99+
*cur_pos = 0;
100+
}
101+
// for (int i = 0; i < 2; i++) {
102+
// if (i >= blocks) {
103+
// return;
104+
// }
105+
// __u32 cur_idx = block_idx + i;
106+
// __u32 *cur_pos = bpf_map_lookup_elem(&meta, &cur_idx);
107+
// if (cur_pos == 0) {
108+
// return;
109+
// }
110+
// *cur_pos = 0;
111+
// }
112+
}
113+
114+
struct test_struct {
115+
__u64 a;
116+
__u64 next;
117+
};
118+
119+
SEC("tracepoint/syscalls/sys_enter_mount")
120+
int delete_ll(void *ctx) {
121+
char *data_ptr = init_heap(HEAP_SIZE);
122+
if (data_ptr == NULL) {
123+
return 0;
124+
} // Lookup
125+
__u64 top = gettop();
126+
__u64 curr = top - META_BLOCK_SIZE;
127+
128+
long starttime = bpf_ktime_get_ns();
129+
for (int i = 0; i < 2000; ++i) {
130+
if (curr > HEAP_SIZE - 100) {
131+
return 0;
132+
}
133+
struct test_struct *ts = (struct test_struct *)(data_ptr + curr);
134+
free(curr);
135+
curr = ts->next;
136+
}
137+
long endtime = bpf_ktime_get_ns();
138+
bpf_printk("delete: %ld\n", endtime - starttime);
139+
// bpf_printk("last curr: %llu\n", curr);
140+
141+
return 0;
142+
}
143+
144+
char _license[] SEC("license") = "GPL";

test/evaluation/km/llbench_lookup_bpf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <bpf/bpf_helpers.h>
33
#include <linux/bpf.h>
44

5-
#define META_SIZE 40960
5+
#define META_SIZE 65000
66
#define META_BLOCK_SIZE 32
77
#define HEAP_SIZE ((META_SIZE + 1) * META_BLOCK_SIZE)
88

@@ -26,7 +26,7 @@ static __always_inline char *init_heap(long size) {
2626
return head;
2727
}
2828

29-
static __noinline __u32 malloc(__u32 size) {
29+
static __noinline __u64 malloc(__u32 size) {
3030
__u32 i = 0;
3131
// long rv = 0;
3232
__u32 *head = bpf_map_lookup_elem(&meta, &i);
@@ -114,10 +114,10 @@ int lookup_ll(void *ctx) {
114114
return 0;
115115
} // Lookup
116116

117-
__u64 curr = 384064;
117+
__u64 curr = 2048000;
118118
// long iter = 0;
119119
long starttime = bpf_ktime_get_ns();
120-
for (int i = 0; i < 10000; ++i) {
120+
for (int i = 0; i < 64000; ++i) {
121121
if (curr > HEAP_SIZE - 100) {
122122
break;
123123
}

test/evaluation/km/loader.c

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ void testcall() {
2222
#define DATA_PIN_PATH "/sys/fs/bpf/data"
2323

2424
/* detach programs and cleanup pinned maps */
25-
void cleanup(struct bpf_link *link1, struct bpf_link *link2) {
26-
if (link1)
27-
bpf_link__destroy(link1);
28-
if (link2)
29-
bpf_link__destroy(link2);
25+
void cleanup() {
3026
unlink(META_PIN_PATH);
3127
unlink(DATA_PIN_PATH);
3228
}
@@ -101,9 +97,10 @@ int reuse_pinned_maps(struct bpf_object *obj) {
10197
}
10298

10399
int main() {
104-
cleanup(NULL, NULL);
105-
struct bpf_link *link1 = NULL, *link2 = NULL;
100+
cleanup();
101+
struct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;
106102
struct bpf_object *obj2 = NULL;
103+
struct bpf_object *obj3 = NULL;
107104

108105
/* Step 1: Load first program and attach */
109106
if (attach_prog("init_ll.o", "init_ll", &link1) != 0)
@@ -112,9 +109,9 @@ int main() {
112109

113110
/* Optional: run program briefly */
114111
sleep(1);
115-
testcall();
116-
testcall();
117-
testcall();
112+
for(int i = 0; i < 16;++i){
113+
testcall();
114+
}
118115
sleep(1);
119116

120117
/* Step 3: Detach first program */
@@ -126,20 +123,20 @@ int main() {
126123
obj2 = bpf_object__open_file("lookup_ll.o", NULL);
127124
if (libbpf_get_error(obj2)) {
128125
fprintf(stderr, "Failed to open lookup_ll.o\n");
129-
cleanup(NULL, NULL);
126+
cleanup();
130127
return 1;
131128
}
132129
/* Step 5: Reuse pinned maps */
133130
if (reuse_pinned_maps(obj2) != 0) {
134131
fprintf(stderr, "Failed to reuse pinned maps\n");
135-
cleanup(NULL, NULL);
132+
cleanup();
136133
return 1;
137134
}
138135
printf("lookup_ll will use existing maps\n");
139136

140137
if (bpf_object__load(obj2)) {
141138
fprintf(stderr, "Failed to load lookup_ll.o\n");
142-
cleanup(NULL, NULL);
139+
cleanup();
143140
return 1;
144141
}
145142

@@ -149,17 +146,18 @@ int main() {
149146
bpf_object__find_program_by_name(obj2, "lookup_ll");
150147
if (!prog2) {
151148
fprintf(stderr, "lookup_ll program not found\n");
152-
cleanup(NULL, NULL);
149+
cleanup();
153150
return 1;
154151
}
155152

156153
link2 = bpf_program__attach_tracepoint(prog2, "syscalls", "sys_enter_mount");
157154
if (libbpf_get_error(link2)) {
158155
fprintf(stderr, "Failed to attach lookup_ll\n");
159-
cleanup(NULL, NULL);
156+
cleanup();
160157
return 1;
161158
}
162159
printf("lookup_ll attached\n");
160+
bpf_object__close(obj2);
163161

164162
/* Run for some time */
165163
sleep(1);
@@ -168,11 +166,58 @@ int main() {
168166
}
169167
sleep(1);
170168

169+
bpf_link__destroy(link2);
170+
171+
obj3 = bpf_object__open_file("delete_ll.o", NULL);
172+
if (libbpf_get_error(obj3)) {
173+
fprintf(stderr, "Failed to open delete_ll.o\n");
174+
cleanup();
175+
return 1;
176+
}
177+
/* Step 5: Reuse pinned maps */
178+
if (reuse_pinned_maps(obj3) != 0) {
179+
fprintf(stderr, "Failed to reuse pinned maps\n");
180+
cleanup();
181+
return 1;
182+
}
183+
printf("lookup_ll will use existing maps\n");
184+
185+
if (bpf_object__load(obj3)) {
186+
fprintf(stderr, "Failed to load delete_ll.o\n");
187+
cleanup();
188+
return 1;
189+
}
190+
191+
192+
/* Step 6: Attach second program */
193+
struct bpf_program *prog3 =
194+
bpf_object__find_program_by_name(obj3, "delete_ll");
195+
if (!prog3) {
196+
fprintf(stderr, "delete_ll program not found\n");
197+
cleanup();
198+
return 1;
199+
}
200+
201+
link3 = bpf_program__attach_tracepoint(prog3, "syscalls", "sys_enter_mount");
202+
if (libbpf_get_error(link3)) {
203+
fprintf(stderr, "Failed to attach delete_ll\n");
204+
cleanup();
205+
return 1;
206+
}
207+
printf("delete_ll attached\n");
208+
bpf_object__close(obj3);
209+
210+
/* Run for some time */
211+
sleep(1);
212+
for(int i = 0; i< 32;++i){
213+
testcall();
214+
}
215+
sleep(1);
216+
171217
/* Step 7: Detach second program and cleanup maps */
172-
cleanup(NULL, link2);
218+
cleanup();
173219
printf("lookup_ll detached, pinned maps removed\n");
174220

175-
bpf_object__close(obj2);
176221

177222
return 0;
178223
}

0 commit comments

Comments
 (0)