rbtree_test.c 9.4 KB

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