rbtree_test.c 9.4 KB

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