rbtree_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/rbtree_augmented.h>
  4. #include <linux/random.h>
  5. #include <linux/slab.h>
  6. #include <asm/timex.h>
  7. #define __param(type, name, init, msg) \
  8. static type name = init; \
  9. module_param(name, type, 0444); \
  10. MODULE_PARM_DESC(name, msg);
  11. __param(int, nnodes, 100, "Number of nodes in the rb-tree");
  12. __param(int, perf_loops, 1000, "Number of iterations modifying the rb-tree");
  13. __param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree");
  14. struct test_node {
  15. u32 key;
  16. struct rb_node rb;
  17. /* following fields used for testing augmented rbtree functionality */
  18. u32 val;
  19. u32 augmented;
  20. };
  21. static struct rb_root root = RB_ROOT;
  22. static struct test_node *nodes = NULL;
  23. static struct rnd_state rnd;
  24. static void insert(struct test_node *node, struct rb_root *root)
  25. {
  26. struct rb_node **new = &root->rb_node, *parent = NULL;
  27. u32 key = node->key;
  28. while (*new) {
  29. parent = *new;
  30. if (key < rb_entry(parent, struct test_node, rb)->key)
  31. new = &parent->rb_left;
  32. else
  33. new = &parent->rb_right;
  34. }
  35. rb_link_node(&node->rb, parent, new);
  36. rb_insert_color(&node->rb, root);
  37. }
  38. static inline void erase(struct test_node *node, struct rb_root *root)
  39. {
  40. rb_erase(&node->rb, root);
  41. }
  42. static inline u32 augment_recompute(struct test_node *node)
  43. {
  44. u32 max = node->val, child_augmented;
  45. if (node->rb.rb_left) {
  46. child_augmented = rb_entry(node->rb.rb_left, struct test_node,
  47. rb)->augmented;
  48. if (max < child_augmented)
  49. max = child_augmented;
  50. }
  51. if (node->rb.rb_right) {
  52. child_augmented = rb_entry(node->rb.rb_right, struct test_node,
  53. rb)->augmented;
  54. if (max < child_augmented)
  55. max = child_augmented;
  56. }
  57. return max;
  58. }
  59. RB_DECLARE_CALLBACKS(static, augment_callbacks, struct test_node, rb,
  60. u32, augmented, augment_recompute)
  61. static void insert_augmented(struct test_node *node, struct rb_root *root)
  62. {
  63. struct rb_node **new = &root->rb_node, *rb_parent = NULL;
  64. u32 key = node->key;
  65. u32 val = node->val;
  66. struct test_node *parent;
  67. while (*new) {
  68. rb_parent = *new;
  69. parent = rb_entry(rb_parent, struct test_node, rb);
  70. if (parent->augmented < val)
  71. parent->augmented = val;
  72. if (key < parent->key)
  73. new = &parent->rb.rb_left;
  74. else
  75. new = &parent->rb.rb_right;
  76. }
  77. node->augmented = val;
  78. rb_link_node(&node->rb, rb_parent, new);
  79. rb_insert_augmented(&node->rb, root, &augment_callbacks);
  80. }
  81. static void erase_augmented(struct test_node *node, struct rb_root *root)
  82. {
  83. rb_erase_augmented(&node->rb, root, &augment_callbacks);
  84. }
  85. static void init(void)
  86. {
  87. int i;
  88. for (i = 0; i < nnodes; i++) {
  89. nodes[i].key = prandom_u32_state(&rnd);
  90. nodes[i].val = prandom_u32_state(&rnd);
  91. }
  92. }
  93. static bool is_red(struct rb_node *rb)
  94. {
  95. return !(rb->__rb_parent_color & 1);
  96. }
  97. static int black_path_count(struct rb_node *rb)
  98. {
  99. int count;
  100. for (count = 0; rb; rb = rb_parent(rb))
  101. count += !is_red(rb);
  102. return count;
  103. }
  104. static void check_postorder_foreach(int nr_nodes)
  105. {
  106. struct test_node *cur, *n;
  107. int count = 0;
  108. rbtree_postorder_for_each_entry_safe(cur, n, &root, rb)
  109. count++;
  110. WARN_ON_ONCE(count != nr_nodes);
  111. }
  112. static void check_postorder(int nr_nodes)
  113. {
  114. struct rb_node *rb;
  115. int count = 0;
  116. for (rb = rb_first_postorder(&root); rb; rb = rb_next_postorder(rb))
  117. count++;
  118. WARN_ON_ONCE(count != nr_nodes);
  119. }
  120. static void check(int nr_nodes)
  121. {
  122. struct rb_node *rb;
  123. int count = 0, blacks = 0;
  124. u32 prev_key = 0;
  125. for (rb = rb_first(&root); rb; rb = rb_next(rb)) {
  126. struct test_node *node = rb_entry(rb, struct test_node, rb);
  127. WARN_ON_ONCE(node->key < prev_key);
  128. WARN_ON_ONCE(is_red(rb) &&
  129. (!rb_parent(rb) || is_red(rb_parent(rb))));
  130. if (!count)
  131. blacks = black_path_count(rb);
  132. else
  133. WARN_ON_ONCE((!rb->rb_left || !rb->rb_right) &&
  134. blacks != black_path_count(rb));
  135. prev_key = node->key;
  136. count++;
  137. }
  138. WARN_ON_ONCE(count != nr_nodes);
  139. WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1);
  140. check_postorder(nr_nodes);
  141. check_postorder_foreach(nr_nodes);
  142. }
  143. static void check_augmented(int nr_nodes)
  144. {
  145. struct rb_node *rb;
  146. check(nr_nodes);
  147. for (rb = rb_first(&root); rb; rb = rb_next(rb)) {
  148. struct test_node *node = rb_entry(rb, struct test_node, rb);
  149. WARN_ON_ONCE(node->augmented != augment_recompute(node));
  150. }
  151. }
  152. static int __init rbtree_test_init(void)
  153. {
  154. int i, j;
  155. cycles_t time1, time2, time;
  156. nodes = kmalloc(nnodes * sizeof(*nodes), GFP_KERNEL);
  157. if (!nodes)
  158. return -ENOMEM;
  159. printk(KERN_ALERT "rbtree testing");
  160. prandom_seed_state(&rnd, 3141592653589793238ULL);
  161. init();
  162. time1 = get_cycles();
  163. for (i = 0; i < perf_loops; i++) {
  164. for (j = 0; j < nnodes; j++)
  165. insert(nodes + j, &root);
  166. for (j = 0; j < nnodes; j++)
  167. erase(nodes + j, &root);
  168. }
  169. time2 = get_cycles();
  170. time = time2 - time1;
  171. time = div_u64(time, perf_loops);
  172. printk(" -> %llu cycles\n", (unsigned long long)time);
  173. for (i = 0; i < check_loops; i++) {
  174. init();
  175. for (j = 0; j < nnodes; j++) {
  176. check(j);
  177. insert(nodes + j, &root);
  178. }
  179. for (j = 0; j < nnodes; j++) {
  180. check(nnodes - j);
  181. erase(nodes + j, &root);
  182. }
  183. check(0);
  184. }
  185. printk(KERN_ALERT "augmented rbtree testing");
  186. init();
  187. time1 = get_cycles();
  188. for (i = 0; i < perf_loops; i++) {
  189. for (j = 0; j < nnodes; j++)
  190. insert_augmented(nodes + j, &root);
  191. for (j = 0; j < nnodes; j++)
  192. erase_augmented(nodes + j, &root);
  193. }
  194. time2 = get_cycles();
  195. time = time2 - time1;
  196. time = div_u64(time, perf_loops);
  197. printk(" -> %llu cycles\n", (unsigned long long)time);
  198. for (i = 0; i < check_loops; i++) {
  199. init();
  200. for (j = 0; j < nnodes; j++) {
  201. check_augmented(j);
  202. insert_augmented(nodes + j, &root);
  203. }
  204. for (j = 0; j < nnodes; j++) {
  205. check_augmented(nnodes - j);
  206. erase_augmented(nodes + j, &root);
  207. }
  208. check_augmented(0);
  209. }
  210. kfree(nodes);
  211. return -EAGAIN; /* Fail will directly unload the module */
  212. }
  213. static void __exit rbtree_test_exit(void)
  214. {
  215. printk(KERN_ALERT "test exit\n");
  216. }
  217. module_init(rbtree_test_init)
  218. module_exit(rbtree_test_exit)
  219. MODULE_LICENSE("GPL");
  220. MODULE_AUTHOR("Michel Lespinasse");
  221. MODULE_DESCRIPTION("Red Black Tree test");