rbtree_test.c 5.5 KB

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