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 void __list_add(struct list_head *new,
46 struct list_head *prev,
47 struct list_head *next)
55 static inline void list_add(struct list_head *new, struct list_head *head)
57 __list_add(new, head, head->next);
60 static inline void __list_del(struct list_head *prev, struct list_head *next)
66 static inline void __list_del_entry(struct list_head *entry)
68 __list_del(entry->prev, entry->next);
71 static inline void list_move(struct list_head *list, struct list_head *head)
73 __list_del_entry(list);
77 #define list_entry(ptr, type, member) \
78 container_of(ptr, type, member)
80 #define list_last_entry(ptr, type, member) \
81 list_entry((ptr)->prev, type, member)
83 struct pfect_lru_node {
84 struct list_head list;
85 unsigned long long key;
89 struct list_head list;
90 struct pfect_lru_node *free_nodes;
91 unsigned int cur_size;
92 unsigned int lru_size;
93 unsigned int nr_unique;
94 unsigned int nr_misses;
99 static void pfect_lru_init(struct pfect_lru *lru, unsigned int lru_size,
100 unsigned int nr_possible_elems)
102 lru->map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
103 sizeof(unsigned long long),
104 sizeof(struct pfect_lru_node *),
105 nr_possible_elems, NULL);
106 assert(lru->map_fd != -1);
108 lru->free_nodes = malloc(lru_size * sizeof(struct pfect_lru_node));
109 assert(lru->free_nodes);
111 INIT_LIST_HEAD(&lru->list);
113 lru->lru_size = lru_size;
114 lru->nr_unique = lru->nr_misses = lru->total = 0;
117 static void pfect_lru_destroy(struct pfect_lru *lru)
120 free(lru->free_nodes);
123 static int pfect_lru_lookup_or_insert(struct pfect_lru *lru,
124 unsigned long long key)
126 struct pfect_lru_node *node = NULL;
130 if (!bpf_map_lookup_elem(lru->map_fd, &key, &node)) {
132 list_move(&node->list, &lru->list);
138 if (lru->cur_size < lru->lru_size) {
139 node = &lru->free_nodes[lru->cur_size++];
140 INIT_LIST_HEAD(&node->list);
142 struct pfect_lru_node *null_node = NULL;
144 node = list_last_entry(&lru->list,
145 struct pfect_lru_node,
147 bpf_map_update_elem(lru->map_fd, &node->key, &null_node, BPF_EXIST);
151 list_move(&node->list, &lru->list);
155 assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_EXIST));
158 assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_NOEXIST));
164 static unsigned int read_keys(const char *dist_file,
165 unsigned long long **keys)
168 unsigned long long *retkeys;
169 unsigned int counts = 0;
174 dist_fd = open(dist_file, 0);
175 assert(dist_fd != -1);
177 assert(fstat(dist_fd, &fst) == 0);
178 b = malloc(fst.st_size);
181 assert(read(dist_fd, b, fst.st_size) == fst.st_size);
183 for (i = 0; i < fst.st_size; i++) {
187 counts++; /* in case the last line has no \n */
189 retkeys = malloc(counts * sizeof(unsigned long long));
193 for (l = strtok(b, "\n"); l; l = strtok(NULL, "\n"))
194 retkeys[counts++] = strtoull(l, NULL, 10);
202 static int create_map(int map_type, int map_flags, unsigned int size)
204 LIBBPF_OPTS(bpf_map_create_opts, opts,
205 .map_flags = map_flags,
209 map_fd = bpf_map_create(map_type, NULL, sizeof(unsigned long long),
210 sizeof(unsigned long long), size, &opts);
213 perror("bpf_create_map");
218 static int sched_next_online(int pid, int next_to_try)
222 if (next_to_try == nr_cpus)
225 while (next_to_try < nr_cpus) {
227 CPU_SET(next_to_try++, &cpuset);
228 if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset))
235 static void run_parallel(unsigned int tasks, void (*fn)(int i, void *data),
238 int next_sched_cpu = 0;
242 for (i = 0; i < tasks; i++) {
245 next_sched_cpu = sched_next_online(0, next_sched_cpu);
248 } else if (pid[i] == -1) {
249 printf("couldn't spawn #%d process\n", i);
252 /* It is mostly redundant and just allow the parent
253 * process to update next_shced_cpu for the next child
256 next_sched_cpu = sched_next_online(pid[i], next_sched_cpu);
258 for (i = 0; i < tasks; i++) {
261 assert(waitpid(pid[i], &status, 0) == pid[i]);
266 static void do_test_lru_dist(int task, void *data)
268 unsigned int nr_misses = 0;
269 struct pfect_lru pfect_lru;
270 unsigned long long key, value = 1234;
273 unsigned int lru_map_fd = ((unsigned int *)data)[0];
274 unsigned int lru_size = ((unsigned int *)data)[1];
275 unsigned long long key_offset = task * dist_key_counts;
277 pfect_lru_init(&pfect_lru, lru_size, dist_key_counts);
279 for (i = 0; i < dist_key_counts; i++) {
280 key = dist_keys[i] + key_offset;
282 pfect_lru_lookup_or_insert(&pfect_lru, key);
284 if (!bpf_map_lookup_elem(lru_map_fd, &key, &value))
287 if (bpf_map_update_elem(lru_map_fd, &key, &value, BPF_NOEXIST)) {
288 printf("bpf_map_update_elem(lru_map_fd, %llu): errno:%d\n",
296 printf(" task:%d BPF LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
297 task, pfect_lru.nr_unique, dist_key_counts, nr_misses,
299 printf(" task:%d Perfect LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
300 task, pfect_lru.nr_unique, pfect_lru.total,
301 pfect_lru.nr_misses, pfect_lru.total);
303 pfect_lru_destroy(&pfect_lru);
307 static void test_parallel_lru_dist(int map_type, int map_flags,
308 int nr_tasks, unsigned int lru_size)
313 printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
316 if (map_flags & BPF_F_NO_COMMON_LRU)
317 lru_map_fd = create_map(map_type, map_flags,
320 lru_map_fd = create_map(map_type, map_flags,
321 nr_tasks * lru_size);
322 assert(lru_map_fd != -1);
324 child_data[0] = lru_map_fd;
325 child_data[1] = lru_size;
327 run_parallel(nr_tasks, do_test_lru_dist, child_data);
332 static void test_lru_loss0(int map_type, int map_flags)
334 unsigned long long key, value[nr_cpus];
335 unsigned int old_unused_losses = 0;
336 unsigned int new_unused_losses = 0;
337 unsigned int used_losses = 0;
340 printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
343 assert(sched_next_online(0, 0) != -1);
345 if (map_flags & BPF_F_NO_COMMON_LRU)
346 map_fd = create_map(map_type, map_flags, 900 * nr_cpus);
348 map_fd = create_map(map_type, map_flags, 900);
350 assert(map_fd != -1);
354 for (key = 1; key <= 1000; key++) {
355 int start_key, end_key;
357 assert(bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST) == 0);
360 end_key = min(key, 900);
362 while (start_key <= end_key) {
363 bpf_map_lookup_elem(map_fd, &start_key, value);
368 for (key = 1; key <= 1000; key++) {
369 if (bpf_map_lookup_elem(map_fd, &key, value)) {
381 printf("older-elem-losses:%d(/100) active-elem-losses:%d(/800) "
382 "newer-elem-losses:%d(/100)\n",
383 old_unused_losses, used_losses, new_unused_losses);
386 static void test_lru_loss1(int map_type, int map_flags)
388 unsigned long long key, value[nr_cpus];
390 unsigned int nr_losses = 0;
392 printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
395 assert(sched_next_online(0, 0) != -1);
397 if (map_flags & BPF_F_NO_COMMON_LRU)
398 map_fd = create_map(map_type, map_flags, 1000 * nr_cpus);
400 map_fd = create_map(map_type, map_flags, 1000);
402 assert(map_fd != -1);
406 for (key = 1; key <= 1000; key++)
407 assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
409 for (key = 1; key <= 1000; key++) {
410 if (bpf_map_lookup_elem(map_fd, &key, value))
416 printf("nr_losses:%d(/1000)\n", nr_losses);
419 static void do_test_parallel_lru_loss(int task, void *data)
421 const unsigned int nr_stable_elems = 1000;
422 const unsigned int nr_repeats = 100000;
424 int map_fd = *(int *)data;
425 unsigned long long stable_base;
426 unsigned long long key, value[nr_cpus];
427 unsigned long long next_ins_key;
428 unsigned int nr_losses = 0;
431 stable_base = task * nr_repeats * 2 + 1;
432 next_ins_key = stable_base;
434 for (i = 0; i < nr_stable_elems; i++) {
435 assert(bpf_map_update_elem(map_fd, &next_ins_key, value,
440 for (i = 0; i < nr_repeats; i++) {
446 key = rn % nr_stable_elems + stable_base;
447 bpf_map_lookup_elem(map_fd, &key, value);
449 bpf_map_update_elem(map_fd, &next_ins_key, value,
456 for (i = 0; i < nr_stable_elems; i++) {
457 if (bpf_map_lookup_elem(map_fd, &key, value))
462 printf(" task:%d nr_losses:%u\n", task, nr_losses);
465 static void test_parallel_lru_loss(int map_type, int map_flags, int nr_tasks)
469 printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
472 /* Give 20% more than the active working set */
473 if (map_flags & BPF_F_NO_COMMON_LRU)
474 map_fd = create_map(map_type, map_flags,
475 nr_cpus * (1000 + 200));
477 map_fd = create_map(map_type, map_flags,
478 nr_tasks * (1000 + 200));
480 assert(map_fd != -1);
482 run_parallel(nr_tasks, do_test_parallel_lru_loss, &map_fd);
487 int main(int argc, char **argv)
489 int map_flags[] = {0, BPF_F_NO_COMMON_LRU};
490 const char *dist_file;
496 printf("Usage: %s <dist-file> <lru-size> <nr-tasks>\n",
502 lru_size = atoi(argv[2]);
503 nr_tasks = atoi(argv[3]);
505 setbuf(stdout, NULL);
509 nr_cpus = bpf_num_possible_cpus();
510 assert(nr_cpus != -1);
511 printf("nr_cpus:%d\n\n", nr_cpus);
513 nr_tasks = min(nr_tasks, nr_cpus);
515 dist_key_counts = read_keys(dist_file, &dist_keys);
516 if (!dist_key_counts) {
517 printf("%s has no key\n", dist_file);
521 for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
522 test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
523 test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
524 test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
526 test_parallel_lru_dist(BPF_MAP_TYPE_LRU_HASH, map_flags[f],