rbtree_test.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/rbtree_augmented.h>
  5. #include <linux/prandom.h>
  6. #include <linux/slab.h>
  7. #include <asm/timex.h>
  8. #define __param(type, name, init, msg) \
  9. static type name = init; \
  10. module_param(name, type, 0444); \
  11. MODULE_PARM_DESC(name, msg);
  12. __param(int, nnodes, 100, "Number of nodes in the rb-tree");
  13. __param(int, perf_loops, 1000, "Number of iterations modifying the rb-tree");
  14. __param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree");
  15. __param(ullong, seed, 3141592653589793238ULL, "Random seed");
  16. struct test_node {
  17. u32 key;
  18. struct rb_node rb;
  19. /* following fields used for testing augmented rbtree functionality */
  20. u32 val;
  21. u32 augmented;
  22. };
  23. static struct rb_root_cached root = RB_ROOT_CACHED;
  24. static struct test_node *nodes = NULL;
  25. static struct rnd_state rnd;
  26. static void insert(struct test_node *node, struct rb_root_cached *root)
  27. {
  28. struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
  29. u32 key = node->key;
  30. while (*new) {
  31. parent = *new;
  32. if (key < rb_entry(parent, struct test_node, rb)->key)
  33. new = &parent->rb_left;
  34. else
  35. new = &parent->rb_right;
  36. }
  37. rb_link_node(&node->rb, parent, new);
  38. rb_insert_color(&node->rb, &root->rb_root);
  39. }
  40. static void insert_cached(struct test_node *node, struct rb_root_cached *root)
  41. {
  42. struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
  43. u32 key = node->key;
  44. bool leftmost = true;
  45. while (*new) {
  46. parent = *new;
  47. if (key < rb_entry(parent, struct test_node, rb)->key)
  48. new = &parent->rb_left;
  49. else {
  50. new = &parent->rb_right;
  51. leftmost = false;
  52. }
  53. }
  54. rb_link_node(&node->rb, parent, new);
  55. rb_insert_color_cached(&node->rb, root, leftmost);
  56. }
  57. static inline void erase(struct test_node *node, struct rb_root_cached *root)
  58. {
  59. rb_erase(&node->rb, &root->rb_root);
  60. }
  61. static inline void erase_cached(struct test_node *node, struct rb_root_cached *root)
  62. {
  63. rb_erase_cached(&node->rb, root);
  64. }
  65. #define NODE_VAL(node) ((node)->val)
  66. RB_DECLARE_CALLBACKS_MAX(static, augment_callbacks,
  67. struct test_node, rb, u32, augmented, NODE_VAL)
  68. static void insert_augmented(struct test_node *node,
  69. struct rb_root_cached *root)
  70. {
  71. struct rb_node **new = &root->rb_root.rb_node, *rb_parent = NULL;
  72. u32 key = node->key;
  73. u32 val = node->val;
  74. struct test_node *parent;
  75. while (*new) {
  76. rb_parent = *new;
  77. parent = rb_entry(rb_parent, struct test_node, rb);
  78. if (parent->augmented < val)
  79. parent->augmented = val;
  80. if (key < parent->key)
  81. new = &parent->rb.rb_left;
  82. else
  83. new = &parent->rb.rb_right;
  84. }
  85. node->augmented = val;
  86. rb_link_node(&node->rb, rb_parent, new);
  87. rb_insert_augmented(&node->rb, &root->rb_root, &augment_callbacks);
  88. }
  89. static void insert_augmented_cached(struct test_node *node,
  90. struct rb_root_cached *root)
  91. {
  92. struct rb_node **new = &root->rb_root.rb_node, *rb_parent = NULL;
  93. u32 key = node->key;
  94. u32 val = node->val;
  95. struct test_node *parent;
  96. bool leftmost = true;
  97. while (*new) {
  98. rb_parent = *new;
  99. parent = rb_entry(rb_parent, struct test_node, rb);
  100. if (parent->augmented < val)
  101. parent->augmented = val;
  102. if (key < parent->key)
  103. new = &parent->rb.rb_left;
  104. else {
  105. new = &parent->rb.rb_right;
  106. leftmost = false;
  107. }
  108. }
  109. node->augmented = val;
  110. rb_link_node(&node->rb, rb_parent, new);
  111. rb_insert_augmented_cached(&node->rb, root,
  112. leftmost, &augment_callbacks);
  113. }
  114. static void erase_augmented(struct test_node *node, struct rb_root_cached *root)
  115. {
  116. rb_erase_augmented(&node->rb, &root->rb_root, &augment_callbacks);
  117. }
  118. static void erase_augmented_cached(struct test_node *node,
  119. struct rb_root_cached *root)
  120. {
  121. rb_erase_augmented_cached(&node->rb, root, &augment_callbacks);
  122. }
  123. static void init(void)
  124. {
  125. int i;
  126. for (i = 0; i < nnodes; i++) {
  127. nodes[i].key = prandom_u32_state(&rnd);
  128. nodes[i].val = prandom_u32_state(&rnd);
  129. }
  130. }
  131. static bool is_red(struct rb_node *rb)
  132. {
  133. return !(rb->__rb_parent_color & 1);
  134. }
  135. static int black_path_count(struct rb_node *rb)
  136. {
  137. int count;
  138. for (count = 0; rb; rb = rb_parent(rb))
  139. count += !is_red(rb);
  140. return count;
  141. }
  142. static void check_postorder_foreach(int nr_nodes)
  143. {
  144. struct test_node *cur, *n;
  145. int count = 0;
  146. rbtree_postorder_for_each_entry_safe(cur, n, &root.rb_root, rb)
  147. count++;
  148. WARN_ON_ONCE(count != nr_nodes);
  149. }
  150. static void check_postorder(int nr_nodes)
  151. {
  152. struct rb_node *rb;
  153. int count = 0;
  154. for (rb = rb_first_postorder(&root.rb_root); rb; rb = rb_next_postorder(rb))
  155. count++;
  156. WARN_ON_ONCE(count != nr_nodes);
  157. }
  158. static void check(int nr_nodes)
  159. {
  160. struct rb_node *rb;
  161. int count = 0, blacks = 0;
  162. u32 prev_key = 0;
  163. for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) {
  164. struct test_node *node = rb_entry(rb, struct test_node, rb);
  165. WARN_ON_ONCE(node->key < prev_key);
  166. WARN_ON_ONCE(is_red(rb) &&
  167. (!rb_parent(rb) || is_red(rb_parent(rb))));
  168. if (!count)
  169. blacks = black_path_count(rb);
  170. else
  171. WARN_ON_ONCE((!rb->rb_left || !rb->rb_right) &&
  172. blacks != black_path_count(rb));
  173. prev_key = node->key;
  174. count++;
  175. }
  176. WARN_ON_ONCE(count != nr_nodes);
  177. WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root.rb_root))) - 1);
  178. check_postorder(nr_nodes);
  179. check_postorder_foreach(nr_nodes);
  180. }
  181. static void check_augmented(int nr_nodes)
  182. {
  183. struct rb_node *rb;
  184. check(nr_nodes);
  185. for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) {
  186. struct test_node *node = rb_entry(rb, struct test_node, rb);
  187. u32 subtree, max = node->val;
  188. if (node->rb.rb_left) {
  189. subtree = rb_entry(node->rb.rb_left, struct test_node,
  190. rb)->augmented;
  191. if (max < subtree)
  192. max = subtree;
  193. }
  194. if (node->rb.rb_right) {
  195. subtree = rb_entry(node->rb.rb_right, struct test_node,
  196. rb)->augmented;
  197. if (max < subtree)
  198. max = subtree;
  199. }
  200. WARN_ON_ONCE(node->augmented != max);
  201. }
  202. }
  203. static int basic_check(void)
  204. {
  205. int i, j;
  206. cycles_t time1, time2, time;
  207. struct rb_node *node;
  208. printk(KERN_ALERT "rbtree testing");
  209. init();
  210. time1 = get_cycles();
  211. for (i = 0; i < perf_loops; i++) {
  212. for (j = 0; j < nnodes; j++)
  213. insert(nodes + j, &root);
  214. for (j = 0; j < nnodes; j++)
  215. erase(nodes + j, &root);
  216. }
  217. time2 = get_cycles();
  218. time = time2 - time1;
  219. time = div_u64(time, perf_loops);
  220. printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n",
  221. (unsigned long long)time);
  222. time1 = get_cycles();
  223. for (i = 0; i < perf_loops; i++) {
  224. for (j = 0; j < nnodes; j++)
  225. insert_cached(nodes + j, &root);
  226. for (j = 0; j < nnodes; j++)
  227. erase_cached(nodes + j, &root);
  228. }
  229. time2 = get_cycles();
  230. time = time2 - time1;
  231. time = div_u64(time, perf_loops);
  232. printk(" -> test 2 (latency of nnodes cached insert+delete): %llu cycles\n",
  233. (unsigned long long)time);
  234. for (i = 0; i < nnodes; i++)
  235. insert(nodes + i, &root);
  236. time1 = get_cycles();
  237. for (i = 0; i < perf_loops; i++) {
  238. for (node = rb_first(&root.rb_root); node; node = rb_next(node))
  239. ;
  240. }
  241. time2 = get_cycles();
  242. time = time2 - time1;
  243. time = div_u64(time, perf_loops);
  244. printk(" -> test 3 (latency of inorder traversal): %llu cycles\n",
  245. (unsigned long long)time);
  246. time1 = get_cycles();
  247. for (i = 0; i < perf_loops; i++)
  248. node = rb_first(&root.rb_root);
  249. time2 = get_cycles();
  250. time = time2 - time1;
  251. time = div_u64(time, perf_loops);
  252. printk(" -> test 4 (latency to fetch first node)\n");
  253. printk(" non-cached: %llu cycles\n", (unsigned long long)time);
  254. time1 = get_cycles();
  255. for (i = 0; i < perf_loops; i++)
  256. node = rb_first_cached(&root);
  257. time2 = get_cycles();
  258. time = time2 - time1;
  259. time = div_u64(time, perf_loops);
  260. printk(" cached: %llu cycles\n", (unsigned long long)time);
  261. for (i = 0; i < nnodes; i++)
  262. erase(nodes + i, &root);
  263. /* run checks */
  264. for (i = 0; i < check_loops; i++) {
  265. init();
  266. for (j = 0; j < nnodes; j++) {
  267. check(j);
  268. insert(nodes + j, &root);
  269. }
  270. for (j = 0; j < nnodes; j++) {
  271. check(nnodes - j);
  272. erase(nodes + j, &root);
  273. }
  274. check(0);
  275. }
  276. return 0;
  277. }
  278. static int augmented_check(void)
  279. {
  280. int i, j;
  281. cycles_t time1, time2, time;
  282. printk(KERN_ALERT "augmented rbtree testing");
  283. init();
  284. time1 = get_cycles();
  285. for (i = 0; i < perf_loops; i++) {
  286. for (j = 0; j < nnodes; j++)
  287. insert_augmented(nodes + j, &root);
  288. for (j = 0; j < nnodes; j++)
  289. erase_augmented(nodes + j, &root);
  290. }
  291. time2 = get_cycles();
  292. time = time2 - time1;
  293. time = div_u64(time, perf_loops);
  294. printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n", (unsigned long long)time);
  295. time1 = get_cycles();
  296. for (i = 0; i < perf_loops; i++) {
  297. for (j = 0; j < nnodes; j++)
  298. insert_augmented_cached(nodes + j, &root);
  299. for (j = 0; j < nnodes; j++)
  300. erase_augmented_cached(nodes + j, &root);
  301. }
  302. time2 = get_cycles();
  303. time = time2 - time1;
  304. time = div_u64(time, perf_loops);
  305. printk(" -> test 2 (latency of nnodes cached insert+delete): %llu cycles\n", (unsigned long long)time);
  306. for (i = 0; i < check_loops; i++) {
  307. init();
  308. for (j = 0; j < nnodes; j++) {
  309. check_augmented(j);
  310. insert_augmented(nodes + j, &root);
  311. }
  312. for (j = 0; j < nnodes; j++) {
  313. check_augmented(nnodes - j);
  314. erase_augmented(nodes + j, &root);
  315. }
  316. check_augmented(0);
  317. }
  318. return 0;
  319. }
  320. static int __init rbtree_test_init(void)
  321. {
  322. nodes = kmalloc_objs(*nodes, nnodes);
  323. if (!nodes)
  324. return -ENOMEM;
  325. prandom_seed_state(&rnd, seed);
  326. basic_check();
  327. augmented_check();
  328. kfree(nodes);
  329. return -EAGAIN; /* Fail will directly unload the module */
  330. }
  331. static void __exit rbtree_test_exit(void)
  332. {
  333. printk(KERN_ALERT "test exit\n");
  334. }
  335. module_init(rbtree_test_init)
  336. module_exit(rbtree_test_exit)
  337. MODULE_LICENSE("GPL");
  338. MODULE_AUTHOR("Michel Lespinasse");
  339. MODULE_DESCRIPTION("Red Black Tree test");