test_lru_dist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016 Facebook
  4. */
  5. #define _GNU_SOURCE
  6. #include <linux/types.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <linux/bpf.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <sched.h>
  14. #include <sys/wait.h>
  15. #include <sys/stat.h>
  16. #include <sys/resource.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <bpf/bpf.h>
  21. #include "bpf_util.h"
  22. #define min(a, b) ((a) < (b) ? (a) : (b))
  23. #ifndef offsetof
  24. # define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
  25. #endif
  26. #define container_of(ptr, type, member) ({ \
  27. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  28. (type *)( (char *)__mptr - offsetof(type,member) );})
  29. static int nr_cpus;
  30. static unsigned long long *dist_keys;
  31. static unsigned int dist_key_counts;
  32. struct list_head {
  33. struct list_head *next, *prev;
  34. };
  35. static inline void INIT_LIST_HEAD(struct list_head *list)
  36. {
  37. list->next = list;
  38. list->prev = list;
  39. }
  40. static inline int list_empty(const struct list_head *head)
  41. {
  42. return head->next == head;
  43. }
  44. static inline void __list_add(struct list_head *new,
  45. struct list_head *prev,
  46. struct list_head *next)
  47. {
  48. next->prev = new;
  49. new->next = next;
  50. new->prev = prev;
  51. prev->next = new;
  52. }
  53. static inline void list_add(struct list_head *new, struct list_head *head)
  54. {
  55. __list_add(new, head, head->next);
  56. }
  57. static inline void __list_del(struct list_head *prev, struct list_head *next)
  58. {
  59. next->prev = prev;
  60. prev->next = next;
  61. }
  62. static inline void __list_del_entry(struct list_head *entry)
  63. {
  64. __list_del(entry->prev, entry->next);
  65. }
  66. static inline void list_move(struct list_head *list, struct list_head *head)
  67. {
  68. __list_del_entry(list);
  69. list_add(list, head);
  70. }
  71. #define list_entry(ptr, type, member) \
  72. container_of(ptr, type, member)
  73. #define list_last_entry(ptr, type, member) \
  74. list_entry((ptr)->prev, type, member)
  75. struct pfect_lru_node {
  76. struct list_head list;
  77. unsigned long long key;
  78. };
  79. struct pfect_lru {
  80. struct list_head list;
  81. struct pfect_lru_node *free_nodes;
  82. unsigned int cur_size;
  83. unsigned int lru_size;
  84. unsigned int nr_unique;
  85. unsigned int nr_misses;
  86. unsigned int total;
  87. int map_fd;
  88. };
  89. static void pfect_lru_init(struct pfect_lru *lru, unsigned int lru_size,
  90. unsigned int nr_possible_elems)
  91. {
  92. lru->map_fd = bpf_create_map(BPF_MAP_TYPE_HASH,
  93. sizeof(unsigned long long),
  94. sizeof(struct pfect_lru_node *),
  95. nr_possible_elems, 0);
  96. assert(lru->map_fd != -1);
  97. lru->free_nodes = malloc(lru_size * sizeof(struct pfect_lru_node));
  98. assert(lru->free_nodes);
  99. INIT_LIST_HEAD(&lru->list);
  100. lru->cur_size = 0;
  101. lru->lru_size = lru_size;
  102. lru->nr_unique = lru->nr_misses = lru->total = 0;
  103. }
  104. static void pfect_lru_destroy(struct pfect_lru *lru)
  105. {
  106. close(lru->map_fd);
  107. free(lru->free_nodes);
  108. }
  109. static int pfect_lru_lookup_or_insert(struct pfect_lru *lru,
  110. unsigned long long key)
  111. {
  112. struct pfect_lru_node *node = NULL;
  113. int seen = 0;
  114. lru->total++;
  115. if (!bpf_map_lookup_elem(lru->map_fd, &key, &node)) {
  116. if (node) {
  117. list_move(&node->list, &lru->list);
  118. return 1;
  119. }
  120. seen = 1;
  121. }
  122. if (lru->cur_size < lru->lru_size) {
  123. node = &lru->free_nodes[lru->cur_size++];
  124. INIT_LIST_HEAD(&node->list);
  125. } else {
  126. struct pfect_lru_node *null_node = NULL;
  127. node = list_last_entry(&lru->list,
  128. struct pfect_lru_node,
  129. list);
  130. bpf_map_update_elem(lru->map_fd, &node->key, &null_node, BPF_EXIST);
  131. }
  132. node->key = key;
  133. list_move(&node->list, &lru->list);
  134. lru->nr_misses++;
  135. if (seen) {
  136. assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_EXIST));
  137. } else {
  138. lru->nr_unique++;
  139. assert(!bpf_map_update_elem(lru->map_fd, &key, &node, BPF_NOEXIST));
  140. }
  141. return seen;
  142. }
  143. static unsigned int read_keys(const char *dist_file,
  144. unsigned long long **keys)
  145. {
  146. struct stat fst;
  147. unsigned long long *retkeys;
  148. unsigned int counts = 0;
  149. int dist_fd;
  150. char *b, *l;
  151. int i;
  152. dist_fd = open(dist_file, 0);
  153. assert(dist_fd != -1);
  154. assert(fstat(dist_fd, &fst) == 0);
  155. b = malloc(fst.st_size);
  156. assert(b);
  157. assert(read(dist_fd, b, fst.st_size) == fst.st_size);
  158. close(dist_fd);
  159. for (i = 0; i < fst.st_size; i++) {
  160. if (b[i] == '\n')
  161. counts++;
  162. }
  163. counts++; /* in case the last line has no \n */
  164. retkeys = malloc(counts * sizeof(unsigned long long));
  165. assert(retkeys);
  166. counts = 0;
  167. for (l = strtok(b, "\n"); l; l = strtok(NULL, "\n"))
  168. retkeys[counts++] = strtoull(l, NULL, 10);
  169. free(b);
  170. *keys = retkeys;
  171. return counts;
  172. }
  173. static int create_map(int map_type, int map_flags, unsigned int size)
  174. {
  175. int map_fd;
  176. map_fd = bpf_create_map(map_type, sizeof(unsigned long long),
  177. sizeof(unsigned long long), size, map_flags);
  178. if (map_fd == -1)
  179. perror("bpf_create_map");
  180. return map_fd;
  181. }
  182. static int sched_next_online(int pid, int next_to_try)
  183. {
  184. cpu_set_t cpuset;
  185. if (next_to_try == nr_cpus)
  186. return -1;
  187. while (next_to_try < nr_cpus) {
  188. CPU_ZERO(&cpuset);
  189. CPU_SET(next_to_try++, &cpuset);
  190. if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset))
  191. break;
  192. }
  193. return next_to_try;
  194. }
  195. static void run_parallel(unsigned int tasks, void (*fn)(int i, void *data),
  196. void *data)
  197. {
  198. int next_sched_cpu = 0;
  199. pid_t pid[tasks];
  200. int i;
  201. for (i = 0; i < tasks; i++) {
  202. pid[i] = fork();
  203. if (pid[i] == 0) {
  204. next_sched_cpu = sched_next_online(0, next_sched_cpu);
  205. fn(i, data);
  206. exit(0);
  207. } else if (pid[i] == -1) {
  208. printf("couldn't spawn #%d process\n", i);
  209. exit(1);
  210. }
  211. /* It is mostly redundant and just allow the parent
  212. * process to update next_shced_cpu for the next child
  213. * process
  214. */
  215. next_sched_cpu = sched_next_online(pid[i], next_sched_cpu);
  216. }
  217. for (i = 0; i < tasks; i++) {
  218. int status;
  219. assert(waitpid(pid[i], &status, 0) == pid[i]);
  220. assert(status == 0);
  221. }
  222. }
  223. static void do_test_lru_dist(int task, void *data)
  224. {
  225. unsigned int nr_misses = 0;
  226. struct pfect_lru pfect_lru;
  227. unsigned long long key, value = 1234;
  228. unsigned int i;
  229. unsigned int lru_map_fd = ((unsigned int *)data)[0];
  230. unsigned int lru_size = ((unsigned int *)data)[1];
  231. unsigned long long key_offset = task * dist_key_counts;
  232. pfect_lru_init(&pfect_lru, lru_size, dist_key_counts);
  233. for (i = 0; i < dist_key_counts; i++) {
  234. key = dist_keys[i] + key_offset;
  235. pfect_lru_lookup_or_insert(&pfect_lru, key);
  236. if (!bpf_map_lookup_elem(lru_map_fd, &key, &value))
  237. continue;
  238. if (bpf_map_update_elem(lru_map_fd, &key, &value, BPF_NOEXIST)) {
  239. printf("bpf_map_update_elem(lru_map_fd, %llu): errno:%d\n",
  240. key, errno);
  241. assert(0);
  242. }
  243. nr_misses++;
  244. }
  245. printf(" task:%d BPF LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
  246. task, pfect_lru.nr_unique, dist_key_counts, nr_misses,
  247. dist_key_counts);
  248. printf(" task:%d Perfect LRU: nr_unique:%u(/%u) nr_misses:%u(/%u)\n",
  249. task, pfect_lru.nr_unique, pfect_lru.total,
  250. pfect_lru.nr_misses, pfect_lru.total);
  251. pfect_lru_destroy(&pfect_lru);
  252. close(lru_map_fd);
  253. }
  254. static void test_parallel_lru_dist(int map_type, int map_flags,
  255. int nr_tasks, unsigned int lru_size)
  256. {
  257. int child_data[2];
  258. int lru_map_fd;
  259. printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
  260. map_flags);
  261. if (map_flags & BPF_F_NO_COMMON_LRU)
  262. lru_map_fd = create_map(map_type, map_flags,
  263. nr_cpus * lru_size);
  264. else
  265. lru_map_fd = create_map(map_type, map_flags,
  266. nr_tasks * lru_size);
  267. assert(lru_map_fd != -1);
  268. child_data[0] = lru_map_fd;
  269. child_data[1] = lru_size;
  270. run_parallel(nr_tasks, do_test_lru_dist, child_data);
  271. close(lru_map_fd);
  272. }
  273. static void test_lru_loss0(int map_type, int map_flags)
  274. {
  275. unsigned long long key, value[nr_cpus];
  276. unsigned int old_unused_losses = 0;
  277. unsigned int new_unused_losses = 0;
  278. unsigned int used_losses = 0;
  279. int map_fd;
  280. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  281. map_flags);
  282. assert(sched_next_online(0, 0) != -1);
  283. if (map_flags & BPF_F_NO_COMMON_LRU)
  284. map_fd = create_map(map_type, map_flags, 900 * nr_cpus);
  285. else
  286. map_fd = create_map(map_type, map_flags, 900);
  287. assert(map_fd != -1);
  288. value[0] = 1234;
  289. for (key = 1; key <= 1000; key++) {
  290. int start_key, end_key;
  291. assert(bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST) == 0);
  292. start_key = 101;
  293. end_key = min(key, 900);
  294. while (start_key <= end_key) {
  295. bpf_map_lookup_elem(map_fd, &start_key, value);
  296. start_key++;
  297. }
  298. }
  299. for (key = 1; key <= 1000; key++) {
  300. if (bpf_map_lookup_elem(map_fd, &key, value)) {
  301. if (key <= 100)
  302. old_unused_losses++;
  303. else if (key <= 900)
  304. used_losses++;
  305. else
  306. new_unused_losses++;
  307. }
  308. }
  309. close(map_fd);
  310. printf("older-elem-losses:%d(/100) active-elem-losses:%d(/800) "
  311. "newer-elem-losses:%d(/100)\n",
  312. old_unused_losses, used_losses, new_unused_losses);
  313. }
  314. static void test_lru_loss1(int map_type, int map_flags)
  315. {
  316. unsigned long long key, value[nr_cpus];
  317. int map_fd;
  318. unsigned int nr_losses = 0;
  319. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  320. map_flags);
  321. assert(sched_next_online(0, 0) != -1);
  322. if (map_flags & BPF_F_NO_COMMON_LRU)
  323. map_fd = create_map(map_type, map_flags, 1000 * nr_cpus);
  324. else
  325. map_fd = create_map(map_type, map_flags, 1000);
  326. assert(map_fd != -1);
  327. value[0] = 1234;
  328. for (key = 1; key <= 1000; key++)
  329. assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
  330. for (key = 1; key <= 1000; key++) {
  331. if (bpf_map_lookup_elem(map_fd, &key, value))
  332. nr_losses++;
  333. }
  334. close(map_fd);
  335. printf("nr_losses:%d(/1000)\n", nr_losses);
  336. }
  337. static void do_test_parallel_lru_loss(int task, void *data)
  338. {
  339. const unsigned int nr_stable_elems = 1000;
  340. const unsigned int nr_repeats = 100000;
  341. int map_fd = *(int *)data;
  342. unsigned long long stable_base;
  343. unsigned long long key, value[nr_cpus];
  344. unsigned long long next_ins_key;
  345. unsigned int nr_losses = 0;
  346. unsigned int i;
  347. stable_base = task * nr_repeats * 2 + 1;
  348. next_ins_key = stable_base;
  349. value[0] = 1234;
  350. for (i = 0; i < nr_stable_elems; i++) {
  351. assert(bpf_map_update_elem(map_fd, &next_ins_key, value,
  352. BPF_NOEXIST) == 0);
  353. next_ins_key++;
  354. }
  355. for (i = 0; i < nr_repeats; i++) {
  356. int rn;
  357. rn = rand();
  358. if (rn % 10) {
  359. key = rn % nr_stable_elems + stable_base;
  360. bpf_map_lookup_elem(map_fd, &key, value);
  361. } else {
  362. bpf_map_update_elem(map_fd, &next_ins_key, value,
  363. BPF_NOEXIST);
  364. next_ins_key++;
  365. }
  366. }
  367. key = stable_base;
  368. for (i = 0; i < nr_stable_elems; i++) {
  369. if (bpf_map_lookup_elem(map_fd, &key, value))
  370. nr_losses++;
  371. key++;
  372. }
  373. printf(" task:%d nr_losses:%u\n", task, nr_losses);
  374. }
  375. static void test_parallel_lru_loss(int map_type, int map_flags, int nr_tasks)
  376. {
  377. int map_fd;
  378. printf("%s (map_type:%d map_flags:0x%X):\n", __func__, map_type,
  379. map_flags);
  380. /* Give 20% more than the active working set */
  381. if (map_flags & BPF_F_NO_COMMON_LRU)
  382. map_fd = create_map(map_type, map_flags,
  383. nr_cpus * (1000 + 200));
  384. else
  385. map_fd = create_map(map_type, map_flags,
  386. nr_tasks * (1000 + 200));
  387. assert(map_fd != -1);
  388. run_parallel(nr_tasks, do_test_parallel_lru_loss, &map_fd);
  389. close(map_fd);
  390. }
  391. int main(int argc, char **argv)
  392. {
  393. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  394. int map_flags[] = {0, BPF_F_NO_COMMON_LRU};
  395. const char *dist_file;
  396. int nr_tasks = 1;
  397. int lru_size;
  398. int f;
  399. if (argc < 4) {
  400. printf("Usage: %s <dist-file> <lru-size> <nr-tasks>\n",
  401. argv[0]);
  402. return -1;
  403. }
  404. dist_file = argv[1];
  405. lru_size = atoi(argv[2]);
  406. nr_tasks = atoi(argv[3]);
  407. setbuf(stdout, NULL);
  408. assert(!setrlimit(RLIMIT_MEMLOCK, &r));
  409. srand(time(NULL));
  410. nr_cpus = bpf_num_possible_cpus();
  411. assert(nr_cpus != -1);
  412. printf("nr_cpus:%d\n\n", nr_cpus);
  413. nr_tasks = min(nr_tasks, nr_cpus);
  414. dist_key_counts = read_keys(dist_file, &dist_keys);
  415. if (!dist_key_counts) {
  416. printf("%s has no key\n", dist_file);
  417. return -1;
  418. }
  419. for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
  420. test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
  421. test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
  422. test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
  423. nr_tasks);
  424. test_parallel_lru_dist(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
  425. nr_tasks, lru_size);
  426. printf("\n");
  427. }
  428. free(dist_keys);
  429. return 0;
  430. }