1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Facebook
6 #include <linux/types.h>
23 #define min(a, b) ((a) < (b) ? (a) : (b))
25 # define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
27 #define container_of(ptr, type, member) ({ \
28 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
29 (type *)( (char *)__mptr - offsetof(type,member) );})
32 static unsigned long long *dist_keys;
33 static unsigned int dist_key_counts;
36 struct list_head *next, *prev;
39 static inline void INIT_LIST_HEAD(struct list_head *list)
45 static inline int list_empty(const struct list_head *head)
47 return head->next == head;
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
60 static inline void list_add(struct list_head *new, struct list_head *head)
62 __list_add(new, head, head->next);
65 static inline void __list_del(struct list_head *prev, struct list_head *next)
71 static inline void __list_del_entry(struct list_head *entry)
73 __list_del(entry->prev, entry->next);
76 static inline void list_move(struct list_head *list, struct list_head *head)
78 __list_del_entry(list);
82 #define list_entry(ptr, type, member) \
83 container_of(ptr, type, member)
85 #define list_last_entry(ptr, type, member) \
86 list_entry((ptr)->prev, type, member)
88 struct pfect_lru_node {
89 struct list_head list;
90 unsigned long long key;
94 struct list_head list;
95 struct pfect_lru_node *free_nodes;
96 unsigned int cur_size;
97 unsigned int lru_size;
98 unsigned int nr_unique;
99 unsigned int nr_misses;
104 static void pfect_lru_init(struct pfect_lru *lru, unsigned int lru_size,
105 unsigned int nr_possible_elems)
107 lru->map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
108 sizeof(unsigned long long),
109 sizeof(struct pfect_lru_node *),
110 nr_possible_elems, NULL);
111 assert(lru->map_fd != -1);
113 lru->free_nodes = malloc(lru_size * sizeof(struct pfect_lru_node));
114 assert(lru->free_nodes);
116 INIT_LIST_HEAD(&lru->list);
118 lru->lru_size = lru_size;
119 lru->nr_unique = lru->nr_misses = lru->total = 0;
122 static void pfect_lru_destroy(struct pfect_lru *lru)
125 free(lru->free_nodes);
128 static int pfect_lru_lookup_or_insert(struct pfect_lru *lru,
129 unsigned long long key)
131 struct pfect_lru_node *node = NULL;
135 if (!bpf_map_lookup_elem(lru->map_fd, &key, &node)) {
137 list_move(&node->list, &lru->list);
143 if (lru->cur_size < lru->lru_size) {
144 node = &lru->free_nodes[lru->cur_size++];
145 INIT_LIST_HEAD(&node->list);
147 struct pfect_lru_node *null_node = NULL;
149 node = list_last_entry(&lru->list,
150 struct pfect_lru_node,
152 bpf_map_update_elem(lru->map_fd, &node->key, &null_node, BPF_EXIST);
156 list_move(&node->list, &lru->list);
160 assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_EXIST));
163 assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_NOEXIST));
169 static unsigned int read_keys(const char *dist_file,
170 unsigned long long **keys)
173 unsigned long long *retkeys;
174 unsigned int counts = 0;
179 dist_fd = open(dist_file, 0);
180 assert(dist_fd != -1);
182 assert(fstat(dist_fd, &fst) == 0);
183 b = malloc(fst.st_size);
186 assert(read(dist_fd, b, fst.st_size) == fst.st_size);
188 for (i = 0; i < fst.st_size; i++) {
192 counts++; /* in case the last line has no \n */
194 retkeys = malloc(counts * sizeof(unsigned long long));
198 for (l = strtok(b, "\n"); l; l = strtok(NULL, "\n"))
199 retkeys[counts++] = strtoull(l, NULL, 10);
207 static int create_map(int map_type, int map_flags, unsigned int size)
209 LIBBPF_OPTS(bpf_map_create_opts, opts,
210 .map_flags = map_flags,
214 map_fd = bpf_map_create(map_type, NULL, sizeof(unsigned long long),
215 sizeof(unsigned long long), size, &opts);
218 perror("bpf_create_map");
223 static int sched_next_online(int pid, int next_to_try)
227 if (next_to_try == nr_cpus)
230 while (next_to_try < nr_cpus) {
232 CPU_SET(next_to_try++, &cpuset);
233 if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset))
240 static void run_parallel(unsigned int tasks, void (*fn)(int i, void *data),
243 int next_sched_cpu = 0;
247 for (i = 0; i < tasks; i++) {
250 next_sched_cpu = sched_next_online(0, next_sched_cpu);
253 } else if (pid[i] == -1) {
254 printf("couldn't spawn #%d process\n", i);
257 /* It is mostly redundant and just allow the parent
258 * process to update next_shced_cpu for the next child
261 next_sched_cpu = sched_next_online(pid[i], next_sched_cpu);
263 for (i = 0; i < tasks; i++) {
266 assert(waitpid(pid[i], &status, 0) == pid[i]);
271 static void do_test_lru_dist(int task, void *data)
273 unsigned int nr_misses = 0;
274 struct pfect_lru pfect_lru;
275 unsigned long long key, value = 1234;
278 unsigned int lru_map_fd = ((unsigned int *)data)[0];
279 unsigned int lru_size = ((unsigned int *)data)[1];
280 unsigned long long key_offset = task * dist_key_counts;
282 pfect_lru_init(&pfect_lru, lru_size, dist_key_counts);
284 for (i = 0; i < dist_key_counts; i++) {
285 key = dist_keys[i] + key_offset;
287 pfect_lru_lookup_or_insert(&pfect_lru, key);
289 if (!bpf_map_lookup_elem(lru_map_fd, &key, &value))
292 if (bpf_map_update_elem(lru_map_fd, &key, &value, BPF_NOEXIST)) {
293 printf("bpf_map_update_elem(lru_map_fd, %llu): errno:%d\n",
301 printf(" task:%d BPF LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
302 task, pfect_lru.nr_unique, dist_key_counts, nr_misses,
304 printf(" task:%d Perfect LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
305 task, pfect_lru.nr_unique, pfect_lru.total,
306 pfect_lru.nr_misses, pfect_lru.total);
308 pfect_lru_destroy(&pfect_lru);
312 static void test_parallel_lru_dist(int map_type, int map_flags,
313 int nr_tasks, unsigned int lru_size)
318 printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
321 if (map_flags & BPF_F_NO_COMMON_LRU)
322 lru_map_fd = create_map(map_type, map_flags,
325 lru_map_fd = create_map(map_type, map_flags,
326 nr_tasks * lru_size);
327 assert(lru_map_fd != -1);
329 child_data[0] = lru_map_fd;
330 child_data[1] = lru_size;
332 run_parallel(nr_tasks, do_test_lru_dist, child_data);
337 static void test_lru_loss0(int map_type, int map_flags)
339 unsigned long long key, value[nr_cpus];
340 unsigned int old_unused_losses = 0;
341 unsigned int new_unused_losses = 0;
342 unsigned int used_losses = 0;
345 printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
348 assert(sched_next_online(0, 0) != -1);
350 if (map_flags & BPF_F_NO_COMMON_LRU)
351 map_fd = create_map(map_type, map_flags, 900 * nr_cpus);
353 map_fd = create_map(map_type, map_flags, 900);
355 assert(map_fd != -1);
359 for (key = 1; key <= 1000; key++) {
360 int start_key, end_key;
362 assert(bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST) == 0);
365 end_key = min(key, 900);
367 while (start_key <= end_key) {
368 bpf_map_lookup_elem(map_fd, &start_key, value);
373 for (key = 1; key <= 1000; key++) {
374 if (bpf_map_lookup_elem(map_fd, &key, value)) {
386 printf("older-elem-losses:%d(/100) active-elem-losses:%d(/800) "
387 "newer-elem-losses:%d(/100)\n",
388 old_unused_losses, used_losses, new_unused_losses);
391 static void test_lru_loss1(int map_type, int map_flags)
393 unsigned long long key, value[nr_cpus];
395 unsigned int nr_losses = 0;
397 printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
400 assert(sched_next_online(0, 0) != -1);
402 if (map_flags & BPF_F_NO_COMMON_LRU)
403 map_fd = create_map(map_type, map_flags, 1000 * nr_cpus);
405 map_fd = create_map(map_type, map_flags, 1000);
407 assert(map_fd != -1);
411 for (key = 1; key <= 1000; key++)
412 assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
414 for (key = 1; key <= 1000; key++) {
415 if (bpf_map_lookup_elem(map_fd, &key, value))
421 printf("nr_losses:%d(/1000)\n", nr_losses);
424 static void do_test_parallel_lru_loss(int task, void *data)
426 const unsigned int nr_stable_elems = 1000;
427 const unsigned int nr_repeats = 100000;
429 int map_fd = *(int *)data;
430 unsigned long long stable_base;
431 unsigned long long key, value[nr_cpus];
432 unsigned long long next_ins_key;
433 unsigned int nr_losses = 0;
436 stable_base = task * nr_repeats * 2 + 1;
437 next_ins_key = stable_base;
439 for (i = 0; i < nr_stable_elems; i++) {
440 assert(bpf_map_update_elem(map_fd, &next_ins_key, value,
445 for (i = 0; i < nr_repeats; i++) {
451 key = rn % nr_stable_elems + stable_base;
452 bpf_map_lookup_elem(map_fd, &key, value);
454 bpf_map_update_elem(map_fd, &next_ins_key, value,
461 for (i = 0; i < nr_stable_elems; i++) {
462 if (bpf_map_lookup_elem(map_fd, &key, value))
467 printf(" task:%d nr_losses:%u\n", task, nr_losses);
470 static void test_parallel_lru_loss(int map_type, int map_flags, int nr_tasks)
474 printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
477 /* Give 20% more than the active working set */
478 if (map_flags & BPF_F_NO_COMMON_LRU)
479 map_fd = create_map(map_type, map_flags,
480 nr_cpus * (1000 + 200));
482 map_fd = create_map(map_type, map_flags,
483 nr_tasks * (1000 + 200));
485 assert(map_fd != -1);
487 run_parallel(nr_tasks, do_test_parallel_lru_loss, &map_fd);
492 int main(int argc, char **argv)
494 int map_flags[] = {0, BPF_F_NO_COMMON_LRU};
495 const char *dist_file;
501 printf("Usage: %s <dist-file> <lru-size> <nr-tasks>\n",
507 lru_size = atoi(argv[2]);
508 nr_tasks = atoi(argv[3]);
510 setbuf(stdout, NULL);
514 nr_cpus = bpf_num_possible_cpus();
515 assert(nr_cpus != -1);
516 printf("nr_cpus:%d\n\n", nr_cpus);
518 nr_tasks = min(nr_tasks, nr_cpus);
520 dist_key_counts = read_keys(dist_file, &dist_keys);
521 if (!dist_key_counts) {
522 printf("%s has no key\n", dist_file);
526 for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
527 test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
528 test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
529 test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
531 test_parallel_lru_dist(BPF_MAP_TYPE_LRU_HASH, map_flags[f],