blob: 41ae3c7570d3971478be9a396557cac8f1ab4dc3 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Michel Lespinasse910a7422012-10-08 16:30:39 -07002#include <linux/module.h>
Davidlohr Bueso223f8912017-09-08 16:14:46 -07003#include <linux/moduleparam.h>
Michel Lespinasse9c079ad2012-10-08 16:31:33 -07004#include <linux/rbtree_augmented.h>
Michel Lespinasse910a7422012-10-08 16:30:39 -07005#include <linux/random.h>
Davidlohr Bueso223f8912017-09-08 16:14:46 -07006#include <linux/slab.h>
Michel Lespinasse910a7422012-10-08 16:30:39 -07007#include <asm/timex.h>
8
Davidlohr Bueso223f8912017-09-08 16:14:46 -07009#define __param(type, name, init, msg) \
10 static type name = init; \
11 module_param(name, type, 0444); \
12 MODULE_PARM_DESC(name, msg);
13
14__param(int, nnodes, 100, "Number of nodes in the rb-tree");
Davidlohr Bueso0b548e32017-11-17 15:28:27 -080015__param(int, perf_loops, 1000, "Number of iterations modifying the rb-tree");
Davidlohr Bueso223f8912017-09-08 16:14:46 -070016__param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree");
Michel Lespinasse910a7422012-10-08 16:30:39 -070017
18struct test_node {
Michel Lespinasse910a7422012-10-08 16:30:39 -070019 u32 key;
Cody P Schaferdbf128c2014-01-23 15:56:05 -080020 struct rb_node rb;
Michel Lespinassedadf9352012-10-08 16:31:15 -070021
22 /* following fields used for testing augmented rbtree functionality */
23 u32 val;
24 u32 augmented;
Michel Lespinasse910a7422012-10-08 16:30:39 -070025};
26
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070027static struct rb_root_cached root = RB_ROOT_CACHED;
Davidlohr Bueso223f8912017-09-08 16:14:46 -070028static struct test_node *nodes = NULL;
Michel Lespinasse910a7422012-10-08 16:30:39 -070029
30static struct rnd_state rnd;
31
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070032static void insert(struct test_node *node, struct rb_root_cached *root)
Michel Lespinasse910a7422012-10-08 16:30:39 -070033{
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070034 struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
Michel Lespinassedadf9352012-10-08 16:31:15 -070035 u32 key = node->key;
Michel Lespinasse910a7422012-10-08 16:30:39 -070036
37 while (*new) {
38 parent = *new;
Michel Lespinassedadf9352012-10-08 16:31:15 -070039 if (key < rb_entry(parent, struct test_node, rb)->key)
Michel Lespinasse910a7422012-10-08 16:30:39 -070040 new = &parent->rb_left;
41 else
42 new = &parent->rb_right;
43 }
44
45 rb_link_node(&node->rb, parent, new);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070046 rb_insert_color(&node->rb, &root->rb_root);
Michel Lespinasse910a7422012-10-08 16:30:39 -070047}
48
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070049static void insert_cached(struct test_node *node, struct rb_root_cached *root)
Michel Lespinasse910a7422012-10-08 16:30:39 -070050{
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070051 struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
52 u32 key = node->key;
53 bool leftmost = true;
54
55 while (*new) {
56 parent = *new;
57 if (key < rb_entry(parent, struct test_node, rb)->key)
58 new = &parent->rb_left;
59 else {
60 new = &parent->rb_right;
61 leftmost = false;
62 }
63 }
64
65 rb_link_node(&node->rb, parent, new);
66 rb_insert_color_cached(&node->rb, root, leftmost);
Michel Lespinasse910a7422012-10-08 16:30:39 -070067}
68
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070069static inline void erase(struct test_node *node, struct rb_root_cached *root)
70{
71 rb_erase(&node->rb, &root->rb_root);
72}
73
74static inline void erase_cached(struct test_node *node, struct rb_root_cached *root)
75{
76 rb_erase_cached(&node->rb, root);
77}
78
79
Michel Lespinasse315cc062019-09-25 16:46:07 -070080#define NODE_VAL(node) ((node)->val)
Michel Lespinassedadf9352012-10-08 16:31:15 -070081
Michel Lespinasse315cc062019-09-25 16:46:07 -070082RB_DECLARE_CALLBACKS_MAX(static, augment_callbacks,
83 struct test_node, rb, u32, augmented, NODE_VAL)
Michel Lespinasse14b94af2012-10-08 16:31:17 -070084
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070085static void insert_augmented(struct test_node *node,
86 struct rb_root_cached *root)
Michel Lespinassedadf9352012-10-08 16:31:15 -070087{
Davidlohr Buesob10d43f2017-09-08 16:14:52 -070088 struct rb_node **new = &root->rb_root.rb_node, *rb_parent = NULL;
Michel Lespinassedadf9352012-10-08 16:31:15 -070089 u32 key = node->key;
Michel Lespinasse14b94af2012-10-08 16:31:17 -070090 u32 val = node->val;
91 struct test_node *parent;
Michel Lespinassedadf9352012-10-08 16:31:15 -070092
93 while (*new) {
Michel Lespinasse14b94af2012-10-08 16:31:17 -070094 rb_parent = *new;
95 parent = rb_entry(rb_parent, struct test_node, rb);
96 if (parent->augmented < val)
97 parent->augmented = val;
98 if (key < parent->key)
99 new = &parent->rb.rb_left;
Michel Lespinassedadf9352012-10-08 16:31:15 -0700100 else
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700101 new = &parent->rb.rb_right;
Michel Lespinassedadf9352012-10-08 16:31:15 -0700102 }
103
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700104 node->augmented = val;
105 rb_link_node(&node->rb, rb_parent, new);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700106 rb_insert_augmented(&node->rb, &root->rb_root, &augment_callbacks);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700107}
108
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700109static void insert_augmented_cached(struct test_node *node,
110 struct rb_root_cached *root)
Michel Lespinassedadf9352012-10-08 16:31:15 -0700111{
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700112 struct rb_node **new = &root->rb_root.rb_node, *rb_parent = NULL;
113 u32 key = node->key;
114 u32 val = node->val;
115 struct test_node *parent;
116 bool leftmost = true;
117
118 while (*new) {
119 rb_parent = *new;
120 parent = rb_entry(rb_parent, struct test_node, rb);
121 if (parent->augmented < val)
122 parent->augmented = val;
123 if (key < parent->key)
124 new = &parent->rb.rb_left;
125 else {
126 new = &parent->rb.rb_right;
127 leftmost = false;
128 }
129 }
130
131 node->augmented = val;
132 rb_link_node(&node->rb, rb_parent, new);
133 rb_insert_augmented_cached(&node->rb, root,
134 leftmost, &augment_callbacks);
135}
136
137
138static void erase_augmented(struct test_node *node, struct rb_root_cached *root)
139{
140 rb_erase_augmented(&node->rb, &root->rb_root, &augment_callbacks);
141}
142
143static void erase_augmented_cached(struct test_node *node,
144 struct rb_root_cached *root)
145{
146 rb_erase_augmented_cached(&node->rb, root, &augment_callbacks);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700147}
148
Michel Lespinasse910a7422012-10-08 16:30:39 -0700149static void init(void)
150{
151 int i;
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700152 for (i = 0; i < nnodes; i++) {
Akinobu Mita496f2f92012-12-17 16:04:23 -0800153 nodes[i].key = prandom_u32_state(&rnd);
154 nodes[i].val = prandom_u32_state(&rnd);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700155 }
Michel Lespinasse910a7422012-10-08 16:30:39 -0700156}
157
158static bool is_red(struct rb_node *rb)
159{
160 return !(rb->__rb_parent_color & 1);
161}
162
163static int black_path_count(struct rb_node *rb)
164{
165 int count;
166 for (count = 0; rb; rb = rb_parent(rb))
167 count += !is_red(rb);
168 return count;
169}
170
Cody P Schafer964fe942014-01-23 15:56:06 -0800171static void check_postorder_foreach(int nr_nodes)
172{
173 struct test_node *cur, *n;
174 int count = 0;
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700175 rbtree_postorder_for_each_entry_safe(cur, n, &root.rb_root, rb)
Cody P Schafer964fe942014-01-23 15:56:06 -0800176 count++;
177
178 WARN_ON_ONCE(count != nr_nodes);
179}
180
Cody P Schafera791a622013-09-11 14:25:17 -0700181static void check_postorder(int nr_nodes)
182{
183 struct rb_node *rb;
184 int count = 0;
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700185 for (rb = rb_first_postorder(&root.rb_root); rb; rb = rb_next_postorder(rb))
Cody P Schafera791a622013-09-11 14:25:17 -0700186 count++;
187
188 WARN_ON_ONCE(count != nr_nodes);
189}
190
Michel Lespinasse910a7422012-10-08 16:30:39 -0700191static void check(int nr_nodes)
192{
193 struct rb_node *rb;
Davidlohr Bueso4130f0e2013-04-30 15:28:24 -0700194 int count = 0, blacks = 0;
Michel Lespinasse910a7422012-10-08 16:30:39 -0700195 u32 prev_key = 0;
196
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700197 for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) {
Michel Lespinasse910a7422012-10-08 16:30:39 -0700198 struct test_node *node = rb_entry(rb, struct test_node, rb);
199 WARN_ON_ONCE(node->key < prev_key);
200 WARN_ON_ONCE(is_red(rb) &&
201 (!rb_parent(rb) || is_red(rb_parent(rb))));
202 if (!count)
203 blacks = black_path_count(rb);
204 else
205 WARN_ON_ONCE((!rb->rb_left || !rb->rb_right) &&
206 blacks != black_path_count(rb));
207 prev_key = node->key;
208 count++;
209 }
Davidlohr Bueso4130f0e2013-04-30 15:28:24 -0700210
Michel Lespinasse910a7422012-10-08 16:30:39 -0700211 WARN_ON_ONCE(count != nr_nodes);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700212 WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root.rb_root))) - 1);
Cody P Schafera791a622013-09-11 14:25:17 -0700213
214 check_postorder(nr_nodes);
Cody P Schafer964fe942014-01-23 15:56:06 -0800215 check_postorder_foreach(nr_nodes);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700216}
217
Michel Lespinassedadf9352012-10-08 16:31:15 -0700218static void check_augmented(int nr_nodes)
219{
220 struct rb_node *rb;
221
222 check(nr_nodes);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700223 for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) {
Michel Lespinassedadf9352012-10-08 16:31:15 -0700224 struct test_node *node = rb_entry(rb, struct test_node, rb);
Michel Lespinasse315cc062019-09-25 16:46:07 -0700225 u32 subtree, max = node->val;
226 if (node->rb.rb_left) {
227 subtree = rb_entry(node->rb.rb_left, struct test_node,
228 rb)->augmented;
229 if (max < subtree)
230 max = subtree;
231 }
232 if (node->rb.rb_right) {
233 subtree = rb_entry(node->rb.rb_right, struct test_node,
234 rb)->augmented;
235 if (max < subtree)
236 max = subtree;
237 }
238 WARN_ON_ONCE(node->augmented != max);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700239 }
240}
241
Davidlohr Buesoc75aaa82013-04-30 15:28:25 -0700242static int __init rbtree_test_init(void)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700243{
244 int i, j;
245 cycles_t time1, time2, time;
Davidlohr Bueso977bd8d2017-09-08 16:14:49 -0700246 struct rb_node *node;
Michel Lespinasse910a7422012-10-08 16:30:39 -0700247
Kees Cook6da2ec52018-06-12 13:55:00 -0700248 nodes = kmalloc_array(nnodes, sizeof(*nodes), GFP_KERNEL);
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700249 if (!nodes)
250 return -ENOMEM;
251
Michel Lespinasse910a7422012-10-08 16:30:39 -0700252 printk(KERN_ALERT "rbtree testing");
253
Akinobu Mita496f2f92012-12-17 16:04:23 -0800254 prandom_seed_state(&rnd, 3141592653589793238ULL);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700255 init();
256
257 time1 = get_cycles();
258
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700259 for (i = 0; i < perf_loops; i++) {
260 for (j = 0; j < nnodes; j++)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700261 insert(nodes + j, &root);
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700262 for (j = 0; j < nnodes; j++)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700263 erase(nodes + j, &root);
264 }
265
266 time2 = get_cycles();
267 time = time2 - time1;
268
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700269 time = div_u64(time, perf_loops);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700270 printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n",
271 (unsigned long long)time);
272
273 time1 = get_cycles();
274
275 for (i = 0; i < perf_loops; i++) {
276 for (j = 0; j < nnodes; j++)
277 insert_cached(nodes + j, &root);
278 for (j = 0; j < nnodes; j++)
279 erase_cached(nodes + j, &root);
280 }
281
282 time2 = get_cycles();
283 time = time2 - time1;
284
285 time = div_u64(time, perf_loops);
286 printk(" -> test 2 (latency of nnodes cached insert+delete): %llu cycles\n",
287 (unsigned long long)time);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700288
Davidlohr Bueso977bd8d2017-09-08 16:14:49 -0700289 for (i = 0; i < nnodes; i++)
290 insert(nodes + i, &root);
291
292 time1 = get_cycles();
293
294 for (i = 0; i < perf_loops; i++) {
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700295 for (node = rb_first(&root.rb_root); node; node = rb_next(node))
Davidlohr Bueso977bd8d2017-09-08 16:14:49 -0700296 ;
297 }
298
299 time2 = get_cycles();
300 time = time2 - time1;
301
302 time = div_u64(time, perf_loops);
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700303 printk(" -> test 3 (latency of inorder traversal): %llu cycles\n",
304 (unsigned long long)time);
305
306 time1 = get_cycles();
307
308 for (i = 0; i < perf_loops; i++)
309 node = rb_first(&root.rb_root);
310
311 time2 = get_cycles();
312 time = time2 - time1;
313
314 time = div_u64(time, perf_loops);
315 printk(" -> test 4 (latency to fetch first node)\n");
316 printk(" non-cached: %llu cycles\n", (unsigned long long)time);
317
318 time1 = get_cycles();
319
320 for (i = 0; i < perf_loops; i++)
321 node = rb_first_cached(&root);
322
323 time2 = get_cycles();
324 time = time2 - time1;
325
326 time = div_u64(time, perf_loops);
327 printk(" cached: %llu cycles\n", (unsigned long long)time);
Davidlohr Bueso977bd8d2017-09-08 16:14:49 -0700328
329 for (i = 0; i < nnodes; i++)
330 erase(nodes + i, &root);
331
332 /* run checks */
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700333 for (i = 0; i < check_loops; i++) {
Michel Lespinasse910a7422012-10-08 16:30:39 -0700334 init();
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700335 for (j = 0; j < nnodes; j++) {
Michel Lespinasse910a7422012-10-08 16:30:39 -0700336 check(j);
337 insert(nodes + j, &root);
338 }
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700339 for (j = 0; j < nnodes; j++) {
340 check(nnodes - j);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700341 erase(nodes + j, &root);
342 }
343 check(0);
344 }
345
Michel Lespinassedadf9352012-10-08 16:31:15 -0700346 printk(KERN_ALERT "augmented rbtree testing");
347
348 init();
349
350 time1 = get_cycles();
351
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700352 for (i = 0; i < perf_loops; i++) {
353 for (j = 0; j < nnodes; j++)
Michel Lespinassedadf9352012-10-08 16:31:15 -0700354 insert_augmented(nodes + j, &root);
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700355 for (j = 0; j < nnodes; j++)
Michel Lespinassedadf9352012-10-08 16:31:15 -0700356 erase_augmented(nodes + j, &root);
357 }
358
359 time2 = get_cycles();
360 time = time2 - time1;
361
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700362 time = div_u64(time, perf_loops);
Davidlohr Bueso977bd8d2017-09-08 16:14:49 -0700363 printk(" -> test 1 (latency of nnodes insert+delete): %llu cycles\n", (unsigned long long)time);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700364
Davidlohr Buesob10d43f2017-09-08 16:14:52 -0700365 time1 = get_cycles();
366
367 for (i = 0; i < perf_loops; i++) {
368 for (j = 0; j < nnodes; j++)
369 insert_augmented_cached(nodes + j, &root);
370 for (j = 0; j < nnodes; j++)
371 erase_augmented_cached(nodes + j, &root);
372 }
373
374 time2 = get_cycles();
375 time = time2 - time1;
376
377 time = div_u64(time, perf_loops);
378 printk(" -> test 2 (latency of nnodes cached insert+delete): %llu cycles\n", (unsigned long long)time);
379
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700380 for (i = 0; i < check_loops; i++) {
Michel Lespinassedadf9352012-10-08 16:31:15 -0700381 init();
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700382 for (j = 0; j < nnodes; j++) {
Michel Lespinassedadf9352012-10-08 16:31:15 -0700383 check_augmented(j);
384 insert_augmented(nodes + j, &root);
385 }
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700386 for (j = 0; j < nnodes; j++) {
387 check_augmented(nnodes - j);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700388 erase_augmented(nodes + j, &root);
389 }
390 check_augmented(0);
391 }
392
Davidlohr Bueso223f8912017-09-08 16:14:46 -0700393 kfree(nodes);
394
Michel Lespinasse910a7422012-10-08 16:30:39 -0700395 return -EAGAIN; /* Fail will directly unload the module */
396}
397
Davidlohr Buesoc75aaa82013-04-30 15:28:25 -0700398static void __exit rbtree_test_exit(void)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700399{
400 printk(KERN_ALERT "test exit\n");
401}
402
403module_init(rbtree_test_init)
404module_exit(rbtree_test_exit)
405
406MODULE_LICENSE("GPL");
407MODULE_AUTHOR("Michel Lespinasse");
408MODULE_DESCRIPTION("Red Black Tree test");