1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Facebook
6 #include <linux/types.h>
16 #include <sys/resource.h>
24 #define min(a, b) ((a) < (b) ? (a) : (b))
26 # define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
28 #define container_of(ptr, type, member) ({ \
29 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
30 (type *)( (char *)__mptr - offsetof(type,member) );})
33 static unsigned long long *dist_keys
;
34 static unsigned int dist_key_counts
;
37 struct list_head
*next
, *prev
;
40 static inline void INIT_LIST_HEAD(struct list_head
*list
)
46 static inline int list_empty(const struct list_head
*head
)
48 return head
->next
== head
;
51 static inline void __list_add(struct list_head
*new,
52 struct list_head
*prev
,
53 struct list_head
*next
)
61 static inline void list_add(struct list_head
*new, struct list_head
*head
)
63 __list_add(new, head
, head
->next
);
66 static inline void __list_del(struct list_head
*prev
, struct list_head
*next
)
72 static inline void __list_del_entry(struct list_head
*entry
)
74 __list_del(entry
->prev
, entry
->next
);
77 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
79 __list_del_entry(list
);
83 #define list_entry(ptr, type, member) \
84 container_of(ptr, type, member)
86 #define list_last_entry(ptr, type, member) \
87 list_entry((ptr)->prev, type, member)
89 struct pfect_lru_node
{
90 struct list_head list
;
91 unsigned long long key
;
95 struct list_head list
;
96 struct pfect_lru_node
*free_nodes
;
97 unsigned int cur_size
;
98 unsigned int lru_size
;
99 unsigned int nr_unique
;
100 unsigned int nr_misses
;
105 static void pfect_lru_init(struct pfect_lru
*lru
, unsigned int lru_size
,
106 unsigned int nr_possible_elems
)
108 lru
->map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
,
109 sizeof(unsigned long long),
110 sizeof(struct pfect_lru_node
*),
111 nr_possible_elems
, 0);
112 assert(lru
->map_fd
!= -1);
114 lru
->free_nodes
= malloc(lru_size
* sizeof(struct pfect_lru_node
));
115 assert(lru
->free_nodes
);
117 INIT_LIST_HEAD(&lru
->list
);
119 lru
->lru_size
= lru_size
;
120 lru
->nr_unique
= lru
->nr_misses
= lru
->total
= 0;
123 static void pfect_lru_destroy(struct pfect_lru
*lru
)
126 free(lru
->free_nodes
);
129 static int pfect_lru_lookup_or_insert(struct pfect_lru
*lru
,
130 unsigned long long key
)
132 struct pfect_lru_node
*node
= NULL
;
136 if (!bpf_map_lookup_elem(lru
->map_fd
, &key
, &node
)) {
138 list_move(&node
->list
, &lru
->list
);
144 if (lru
->cur_size
< lru
->lru_size
) {
145 node
= &lru
->free_nodes
[lru
->cur_size
++];
146 INIT_LIST_HEAD(&node
->list
);
148 struct pfect_lru_node
*null_node
= NULL
;
150 node
= list_last_entry(&lru
->list
,
151 struct pfect_lru_node
,
153 bpf_map_update_elem(lru
->map_fd
, &node
->key
, &null_node
, BPF_EXIST
);
157 list_move(&node
->list
, &lru
->list
);
161 assert(!bpf_map_update_elem(lru
->map_fd
, &key
, &node
, BPF_EXIST
));
164 assert(!bpf_map_update_elem(lru
->map_fd
, &key
, &node
, BPF_NOEXIST
));
170 static unsigned int read_keys(const char *dist_file
,
171 unsigned long long **keys
)
174 unsigned long long *retkeys
;
175 unsigned int counts
= 0;
180 dist_fd
= open(dist_file
, 0);
181 assert(dist_fd
!= -1);
183 assert(fstat(dist_fd
, &fst
) == 0);
184 b
= malloc(fst
.st_size
);
187 assert(read(dist_fd
, b
, fst
.st_size
) == fst
.st_size
);
189 for (i
= 0; i
< fst
.st_size
; i
++) {
193 counts
++; /* in case the last line has no \n */
195 retkeys
= malloc(counts
* sizeof(unsigned long long));
199 for (l
= strtok(b
, "\n"); l
; l
= strtok(NULL
, "\n"))
200 retkeys
[counts
++] = strtoull(l
, NULL
, 10);
208 static int create_map(int map_type
, int map_flags
, unsigned int size
)
212 map_fd
= bpf_create_map(map_type
, sizeof(unsigned long long),
213 sizeof(unsigned long long), size
, map_flags
);
216 perror("bpf_create_map");
221 static int sched_next_online(int pid
, int next_to_try
)
225 if (next_to_try
== nr_cpus
)
228 while (next_to_try
< nr_cpus
) {
230 CPU_SET(next_to_try
++, &cpuset
);
231 if (!sched_setaffinity(pid
, sizeof(cpuset
), &cpuset
))
238 static void run_parallel(unsigned int tasks
, void (*fn
)(int i
, void *data
),
241 int next_sched_cpu
= 0;
245 for (i
= 0; i
< tasks
; i
++) {
248 next_sched_cpu
= sched_next_online(0, next_sched_cpu
);
251 } else if (pid
[i
] == -1) {
252 printf("couldn't spawn #%d process\n", i
);
255 /* It is mostly redundant and just allow the parent
256 * process to update next_shced_cpu for the next child
259 next_sched_cpu
= sched_next_online(pid
[i
], next_sched_cpu
);
261 for (i
= 0; i
< tasks
; i
++) {
264 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
269 static void do_test_lru_dist(int task
, void *data
)
271 unsigned int nr_misses
= 0;
272 struct pfect_lru pfect_lru
;
273 unsigned long long key
, value
= 1234;
276 unsigned int lru_map_fd
= ((unsigned int *)data
)[0];
277 unsigned int lru_size
= ((unsigned int *)data
)[1];
278 unsigned long long key_offset
= task
* dist_key_counts
;
280 pfect_lru_init(&pfect_lru
, lru_size
, dist_key_counts
);
282 for (i
= 0; i
< dist_key_counts
; i
++) {
283 key
= dist_keys
[i
] + key_offset
;
285 pfect_lru_lookup_or_insert(&pfect_lru
, key
);
287 if (!bpf_map_lookup_elem(lru_map_fd
, &key
, &value
))
290 if (bpf_map_update_elem(lru_map_fd
, &key
, &value
, BPF_NOEXIST
)) {
291 printf("bpf_map_update_elem(lru_map_fd, %llu): errno:%d\n",
299 printf(" task:%d BPF LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
300 task
, pfect_lru
.nr_unique
, dist_key_counts
, nr_misses
,
302 printf(" task:%d Perfect LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
303 task
, pfect_lru
.nr_unique
, pfect_lru
.total
,
304 pfect_lru
.nr_misses
, pfect_lru
.total
);
306 pfect_lru_destroy(&pfect_lru
);
310 static void test_parallel_lru_dist(int map_type
, int map_flags
,
311 int nr_tasks
, unsigned int lru_size
)
316 printf("%s (map_type:%d map_flags:0x%X):\n", __func__
, map_type
,
319 if (map_flags
& BPF_F_NO_COMMON_LRU
)
320 lru_map_fd
= create_map(map_type
, map_flags
,
323 lru_map_fd
= create_map(map_type
, map_flags
,
324 nr_tasks
* lru_size
);
325 assert(lru_map_fd
!= -1);
327 child_data
[0] = lru_map_fd
;
328 child_data
[1] = lru_size
;
330 run_parallel(nr_tasks
, do_test_lru_dist
, child_data
);
335 static void test_lru_loss0(int map_type
, int map_flags
)
337 unsigned long long key
, value
[nr_cpus
];
338 unsigned int old_unused_losses
= 0;
339 unsigned int new_unused_losses
= 0;
340 unsigned int used_losses
= 0;
343 printf("%s (map_type:%d map_flags:0x%X): ", __func__
, map_type
,
346 assert(sched_next_online(0, 0) != -1);
348 if (map_flags
& BPF_F_NO_COMMON_LRU
)
349 map_fd
= create_map(map_type
, map_flags
, 900 * nr_cpus
);
351 map_fd
= create_map(map_type
, map_flags
, 900);
353 assert(map_fd
!= -1);
357 for (key
= 1; key
<= 1000; key
++) {
358 int start_key
, end_key
;
360 assert(bpf_map_update_elem(map_fd
, &key
, value
, BPF_NOEXIST
) == 0);
363 end_key
= min(key
, 900);
365 while (start_key
<= end_key
) {
366 bpf_map_lookup_elem(map_fd
, &start_key
, value
);
371 for (key
= 1; key
<= 1000; key
++) {
372 if (bpf_map_lookup_elem(map_fd
, &key
, value
)) {
384 printf("older-elem-losses:%d(/100) active-elem-losses:%d(/800) "
385 "newer-elem-losses:%d(/100)\n",
386 old_unused_losses
, used_losses
, new_unused_losses
);
389 static void test_lru_loss1(int map_type
, int map_flags
)
391 unsigned long long key
, value
[nr_cpus
];
393 unsigned int nr_losses
= 0;
395 printf("%s (map_type:%d map_flags:0x%X): ", __func__
, map_type
,
398 assert(sched_next_online(0, 0) != -1);
400 if (map_flags
& BPF_F_NO_COMMON_LRU
)
401 map_fd
= create_map(map_type
, map_flags
, 1000 * nr_cpus
);
403 map_fd
= create_map(map_type
, map_flags
, 1000);
405 assert(map_fd
!= -1);
409 for (key
= 1; key
<= 1000; key
++)
410 assert(!bpf_map_update_elem(map_fd
, &key
, value
, BPF_NOEXIST
));
412 for (key
= 1; key
<= 1000; key
++) {
413 if (bpf_map_lookup_elem(map_fd
, &key
, value
))
419 printf("nr_losses:%d(/1000)\n", nr_losses
);
422 static void do_test_parallel_lru_loss(int task
, void *data
)
424 const unsigned int nr_stable_elems
= 1000;
425 const unsigned int nr_repeats
= 100000;
427 int map_fd
= *(int *)data
;
428 unsigned long long stable_base
;
429 unsigned long long key
, value
[nr_cpus
];
430 unsigned long long next_ins_key
;
431 unsigned int nr_losses
= 0;
434 stable_base
= task
* nr_repeats
* 2 + 1;
435 next_ins_key
= stable_base
;
437 for (i
= 0; i
< nr_stable_elems
; i
++) {
438 assert(bpf_map_update_elem(map_fd
, &next_ins_key
, value
,
443 for (i
= 0; i
< nr_repeats
; i
++) {
449 key
= rn
% nr_stable_elems
+ stable_base
;
450 bpf_map_lookup_elem(map_fd
, &key
, value
);
452 bpf_map_update_elem(map_fd
, &next_ins_key
, value
,
459 for (i
= 0; i
< nr_stable_elems
; i
++) {
460 if (bpf_map_lookup_elem(map_fd
, &key
, value
))
465 printf(" task:%d nr_losses:%u\n", task
, nr_losses
);
468 static void test_parallel_lru_loss(int map_type
, int map_flags
, int nr_tasks
)
472 printf("%s (map_type:%d map_flags:0x%X):\n", __func__
, map_type
,
475 /* Give 20% more than the active working set */
476 if (map_flags
& BPF_F_NO_COMMON_LRU
)
477 map_fd
= create_map(map_type
, map_flags
,
478 nr_cpus
* (1000 + 200));
480 map_fd
= create_map(map_type
, map_flags
,
481 nr_tasks
* (1000 + 200));
483 assert(map_fd
!= -1);
485 run_parallel(nr_tasks
, do_test_parallel_lru_loss
, &map_fd
);
490 int main(int argc
, char **argv
)
492 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
493 int map_flags
[] = {0, BPF_F_NO_COMMON_LRU
};
494 const char *dist_file
;
500 printf("Usage: %s <dist-file> <lru-size> <nr-tasks>\n",
506 lru_size
= atoi(argv
[2]);
507 nr_tasks
= atoi(argv
[3]);
509 setbuf(stdout
, NULL
);
511 assert(!setrlimit(RLIMIT_MEMLOCK
, &r
));
515 nr_cpus
= bpf_num_possible_cpus();
516 assert(nr_cpus
!= -1);
517 printf("nr_cpus:%d\n\n", nr_cpus
);
519 nr_tasks
= min(nr_tasks
, nr_cpus
);
521 dist_key_counts
= read_keys(dist_file
, &dist_keys
);
522 if (!dist_key_counts
) {
523 printf("%s has no key\n", dist_file
);
527 for (f
= 0; f
< sizeof(map_flags
) / sizeof(*map_flags
); f
++) {
528 test_lru_loss0(BPF_MAP_TYPE_LRU_HASH
, map_flags
[f
]);
529 test_lru_loss1(BPF_MAP_TYPE_LRU_HASH
, map_flags
[f
]);
530 test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH
, map_flags
[f
],
532 test_parallel_lru_dist(BPF_MAP_TYPE_LRU_HASH
, map_flags
[f
],