Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 1 | #include <linux/module.h> |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 2 | #include <linux/moduleparam.h> |
Michel Lespinasse | 9c079ad | 2012-10-08 16:31:33 -0700 | [diff] [blame] | 3 | #include <linux/rbtree_augmented.h> |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 4 | #include <linux/random.h> |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 5 | #include <linux/slab.h> |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 6 | #include <asm/timex.h> |
| 7 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 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 | |
| 13 | __param(int, nnodes, 100, "Number of nodes in the rb-tree"); |
| 14 | __param(int, perf_loops, 100000, "Number of iterations modifying the rb-tree"); |
| 15 | __param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree"); |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 16 | |
| 17 | struct test_node { |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 18 | u32 key; |
Cody P Schafer | dbf128c | 2014-01-23 15:56:05 -0800 | [diff] [blame] | 19 | struct rb_node rb; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 20 | |
| 21 | /* following fields used for testing augmented rbtree functionality */ |
| 22 | u32 val; |
| 23 | u32 augmented; |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static struct rb_root root = RB_ROOT; |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 27 | static struct test_node *nodes = NULL; |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 28 | |
| 29 | static struct rnd_state rnd; |
| 30 | |
| 31 | static void insert(struct test_node *node, struct rb_root *root) |
| 32 | { |
| 33 | struct rb_node **new = &root->rb_node, *parent = NULL; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 34 | u32 key = node->key; |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 35 | |
| 36 | while (*new) { |
| 37 | parent = *new; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 38 | if (key < rb_entry(parent, struct test_node, rb)->key) |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 39 | new = &parent->rb_left; |
| 40 | else |
| 41 | new = &parent->rb_right; |
| 42 | } |
| 43 | |
| 44 | rb_link_node(&node->rb, parent, new); |
| 45 | rb_insert_color(&node->rb, root); |
| 46 | } |
| 47 | |
| 48 | static inline void erase(struct test_node *node, struct rb_root *root) |
| 49 | { |
| 50 | rb_erase(&node->rb, root); |
| 51 | } |
| 52 | |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 53 | static inline u32 augment_recompute(struct test_node *node) |
| 54 | { |
| 55 | u32 max = node->val, child_augmented; |
| 56 | if (node->rb.rb_left) { |
| 57 | child_augmented = rb_entry(node->rb.rb_left, struct test_node, |
| 58 | rb)->augmented; |
| 59 | if (max < child_augmented) |
| 60 | max = child_augmented; |
| 61 | } |
| 62 | if (node->rb.rb_right) { |
| 63 | child_augmented = rb_entry(node->rb.rb_right, struct test_node, |
| 64 | rb)->augmented; |
| 65 | if (max < child_augmented) |
| 66 | max = child_augmented; |
| 67 | } |
| 68 | return max; |
| 69 | } |
| 70 | |
Michel Lespinasse | 3908836 | 2012-10-08 16:31:21 -0700 | [diff] [blame] | 71 | RB_DECLARE_CALLBACKS(static, augment_callbacks, struct test_node, rb, |
| 72 | u32, augmented, augment_recompute) |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 73 | |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 74 | static void insert_augmented(struct test_node *node, struct rb_root *root) |
| 75 | { |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 76 | struct rb_node **new = &root->rb_node, *rb_parent = NULL; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 77 | u32 key = node->key; |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 78 | u32 val = node->val; |
| 79 | struct test_node *parent; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 80 | |
| 81 | while (*new) { |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 82 | rb_parent = *new; |
| 83 | parent = rb_entry(rb_parent, struct test_node, rb); |
| 84 | if (parent->augmented < val) |
| 85 | parent->augmented = val; |
| 86 | if (key < parent->key) |
| 87 | new = &parent->rb.rb_left; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 88 | else |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 89 | new = &parent->rb.rb_right; |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 92 | node->augmented = val; |
| 93 | rb_link_node(&node->rb, rb_parent, new); |
| 94 | rb_insert_augmented(&node->rb, root, &augment_callbacks); |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void erase_augmented(struct test_node *node, struct rb_root *root) |
| 98 | { |
Michel Lespinasse | 14b94af | 2012-10-08 16:31:17 -0700 | [diff] [blame] | 99 | rb_erase_augmented(&node->rb, root, &augment_callbacks); |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 102 | static void init(void) |
| 103 | { |
| 104 | int i; |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 105 | for (i = 0; i < nnodes; i++) { |
Akinobu Mita | 496f2f9 | 2012-12-17 16:04:23 -0800 | [diff] [blame] | 106 | nodes[i].key = prandom_u32_state(&rnd); |
| 107 | nodes[i].val = prandom_u32_state(&rnd); |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 108 | } |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static bool is_red(struct rb_node *rb) |
| 112 | { |
| 113 | return !(rb->__rb_parent_color & 1); |
| 114 | } |
| 115 | |
| 116 | static int black_path_count(struct rb_node *rb) |
| 117 | { |
| 118 | int count; |
| 119 | for (count = 0; rb; rb = rb_parent(rb)) |
| 120 | count += !is_red(rb); |
| 121 | return count; |
| 122 | } |
| 123 | |
Cody P Schafer | 964fe94 | 2014-01-23 15:56:06 -0800 | [diff] [blame] | 124 | static void check_postorder_foreach(int nr_nodes) |
| 125 | { |
| 126 | struct test_node *cur, *n; |
| 127 | int count = 0; |
| 128 | rbtree_postorder_for_each_entry_safe(cur, n, &root, rb) |
| 129 | count++; |
| 130 | |
| 131 | WARN_ON_ONCE(count != nr_nodes); |
| 132 | } |
| 133 | |
Cody P Schafer | a791a62 | 2013-09-11 14:25:17 -0700 | [diff] [blame] | 134 | static void check_postorder(int nr_nodes) |
| 135 | { |
| 136 | struct rb_node *rb; |
| 137 | int count = 0; |
| 138 | for (rb = rb_first_postorder(&root); rb; rb = rb_next_postorder(rb)) |
| 139 | count++; |
| 140 | |
| 141 | WARN_ON_ONCE(count != nr_nodes); |
| 142 | } |
| 143 | |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 144 | static void check(int nr_nodes) |
| 145 | { |
| 146 | struct rb_node *rb; |
Davidlohr Bueso | 4130f0e | 2013-04-30 15:28:24 -0700 | [diff] [blame] | 147 | int count = 0, blacks = 0; |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 148 | u32 prev_key = 0; |
| 149 | |
| 150 | for (rb = rb_first(&root); rb; rb = rb_next(rb)) { |
| 151 | struct test_node *node = rb_entry(rb, struct test_node, rb); |
| 152 | WARN_ON_ONCE(node->key < prev_key); |
| 153 | WARN_ON_ONCE(is_red(rb) && |
| 154 | (!rb_parent(rb) || is_red(rb_parent(rb)))); |
| 155 | if (!count) |
| 156 | blacks = black_path_count(rb); |
| 157 | else |
| 158 | WARN_ON_ONCE((!rb->rb_left || !rb->rb_right) && |
| 159 | blacks != black_path_count(rb)); |
| 160 | prev_key = node->key; |
| 161 | count++; |
| 162 | } |
Davidlohr Bueso | 4130f0e | 2013-04-30 15:28:24 -0700 | [diff] [blame] | 163 | |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 164 | WARN_ON_ONCE(count != nr_nodes); |
Davidlohr Bueso | 4130f0e | 2013-04-30 15:28:24 -0700 | [diff] [blame] | 165 | WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1); |
Cody P Schafer | a791a62 | 2013-09-11 14:25:17 -0700 | [diff] [blame] | 166 | |
| 167 | check_postorder(nr_nodes); |
Cody P Schafer | 964fe94 | 2014-01-23 15:56:06 -0800 | [diff] [blame] | 168 | check_postorder_foreach(nr_nodes); |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 171 | static void check_augmented(int nr_nodes) |
| 172 | { |
| 173 | struct rb_node *rb; |
| 174 | |
| 175 | check(nr_nodes); |
| 176 | for (rb = rb_first(&root); rb; rb = rb_next(rb)) { |
| 177 | struct test_node *node = rb_entry(rb, struct test_node, rb); |
| 178 | WARN_ON_ONCE(node->augmented != augment_recompute(node)); |
| 179 | } |
| 180 | } |
| 181 | |
Davidlohr Bueso | c75aaa8 | 2013-04-30 15:28:25 -0700 | [diff] [blame] | 182 | static int __init rbtree_test_init(void) |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 183 | { |
| 184 | int i, j; |
| 185 | cycles_t time1, time2, time; |
Davidlohr Bueso | 977bd8d | 2017-09-08 16:14:49 -0700 | [diff] [blame^] | 186 | struct rb_node *node; |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 187 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 188 | nodes = kmalloc(nnodes * sizeof(*nodes), GFP_KERNEL); |
| 189 | if (!nodes) |
| 190 | return -ENOMEM; |
| 191 | |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 192 | printk(KERN_ALERT "rbtree testing"); |
| 193 | |
Akinobu Mita | 496f2f9 | 2012-12-17 16:04:23 -0800 | [diff] [blame] | 194 | prandom_seed_state(&rnd, 3141592653589793238ULL); |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 195 | init(); |
| 196 | |
| 197 | time1 = get_cycles(); |
| 198 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 199 | for (i = 0; i < perf_loops; i++) { |
| 200 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 201 | insert(nodes + j, &root); |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 202 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 203 | erase(nodes + j, &root); |
| 204 | } |
| 205 | |
| 206 | time2 = get_cycles(); |
| 207 | time = time2 - time1; |
| 208 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 209 | time = div_u64(time, perf_loops); |
Davidlohr Bueso | 977bd8d | 2017-09-08 16:14:49 -0700 | [diff] [blame^] | 210 | printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n", (unsigned long long)time); |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 211 | |
Davidlohr Bueso | 977bd8d | 2017-09-08 16:14:49 -0700 | [diff] [blame^] | 212 | for (i = 0; i < nnodes; i++) |
| 213 | insert(nodes + i, &root); |
| 214 | |
| 215 | time1 = get_cycles(); |
| 216 | |
| 217 | for (i = 0; i < perf_loops; i++) { |
| 218 | for (node = rb_first(&root); node; node = rb_next(node)) |
| 219 | ; |
| 220 | } |
| 221 | |
| 222 | time2 = get_cycles(); |
| 223 | time = time2 - time1; |
| 224 | |
| 225 | time = div_u64(time, perf_loops); |
| 226 | printk(" -> test 2 (latency of inorder traversal): %llu cycles\n", (unsigned long long)time); |
| 227 | |
| 228 | for (i = 0; i < nnodes; i++) |
| 229 | erase(nodes + i, &root); |
| 230 | |
| 231 | /* run checks */ |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 232 | for (i = 0; i < check_loops; i++) { |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 233 | init(); |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 234 | for (j = 0; j < nnodes; j++) { |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 235 | check(j); |
| 236 | insert(nodes + j, &root); |
| 237 | } |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 238 | for (j = 0; j < nnodes; j++) { |
| 239 | check(nnodes - j); |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 240 | erase(nodes + j, &root); |
| 241 | } |
| 242 | check(0); |
| 243 | } |
| 244 | |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 245 | printk(KERN_ALERT "augmented rbtree testing"); |
| 246 | |
| 247 | init(); |
| 248 | |
| 249 | time1 = get_cycles(); |
| 250 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 251 | for (i = 0; i < perf_loops; i++) { |
| 252 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 253 | insert_augmented(nodes + j, &root); |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 254 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 255 | erase_augmented(nodes + j, &root); |
| 256 | } |
| 257 | |
| 258 | time2 = get_cycles(); |
| 259 | time = time2 - time1; |
| 260 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 261 | time = div_u64(time, perf_loops); |
Davidlohr Bueso | 977bd8d | 2017-09-08 16:14:49 -0700 | [diff] [blame^] | 262 | printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n", (unsigned long long)time); |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 263 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 264 | for (i = 0; i < check_loops; i++) { |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 265 | init(); |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 266 | for (j = 0; j < nnodes; j++) { |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 267 | check_augmented(j); |
| 268 | insert_augmented(nodes + j, &root); |
| 269 | } |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 270 | for (j = 0; j < nnodes; j++) { |
| 271 | check_augmented(nnodes - j); |
Michel Lespinasse | dadf935 | 2012-10-08 16:31:15 -0700 | [diff] [blame] | 272 | erase_augmented(nodes + j, &root); |
| 273 | } |
| 274 | check_augmented(0); |
| 275 | } |
| 276 | |
Davidlohr Bueso | 223f891 | 2017-09-08 16:14:46 -0700 | [diff] [blame] | 277 | kfree(nodes); |
| 278 | |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 279 | return -EAGAIN; /* Fail will directly unload the module */ |
| 280 | } |
| 281 | |
Davidlohr Bueso | c75aaa8 | 2013-04-30 15:28:25 -0700 | [diff] [blame] | 282 | static void __exit rbtree_test_exit(void) |
Michel Lespinasse | 910a742 | 2012-10-08 16:30:39 -0700 | [diff] [blame] | 283 | { |
| 284 | printk(KERN_ALERT "test exit\n"); |
| 285 | } |
| 286 | |
| 287 | module_init(rbtree_test_init) |
| 288 | module_exit(rbtree_test_exit) |
| 289 | |
| 290 | MODULE_LICENSE("GPL"); |
| 291 | MODULE_AUTHOR("Michel Lespinasse"); |
| 292 | MODULE_DESCRIPTION("Red Black Tree test"); |