blob: afedd37705620b5ae5f8416d1f25b1eb12ed709e [file] [log] [blame]
Michel Lespinasse910a7422012-10-08 16:30:39 -07001#include <linux/module.h>
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -07002#include <linux/moduleparam.h>
Michel Lespinasse9c079ad2012-10-08 16:31:33 -07003#include <linux/rbtree_augmented.h>
Michel Lespinasse910a7422012-10-08 16:30:39 -07004#include <linux/random.h>
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -07005#include <linux/slab.h>
Michel Lespinasse910a7422012-10-08 16:30:39 -07006#include <asm/timex.h>
7
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -07008#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");
Davidlohr Buesoc07f4062017-11-17 15:28:27 -080014__param(int, perf_loops, 1000, "Number of iterations modifying the rb-tree");
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -070015__param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree");
Michel Lespinasse910a7422012-10-08 16:30:39 -070016
17struct test_node {
Michel Lespinasse910a7422012-10-08 16:30:39 -070018 u32 key;
Cody P Schaferdbf128c2014-01-23 15:56:05 -080019 struct rb_node rb;
Michel Lespinassedadf9352012-10-08 16:31:15 -070020
21 /* following fields used for testing augmented rbtree functionality */
22 u32 val;
23 u32 augmented;
Michel Lespinasse910a7422012-10-08 16:30:39 -070024};
25
26static struct rb_root root = RB_ROOT;
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -070027static struct test_node *nodes = NULL;
Michel Lespinasse910a7422012-10-08 16:30:39 -070028
29static struct rnd_state rnd;
30
31static void insert(struct test_node *node, struct rb_root *root)
32{
33 struct rb_node **new = &root->rb_node, *parent = NULL;
Michel Lespinassedadf9352012-10-08 16:31:15 -070034 u32 key = node->key;
Michel Lespinasse910a7422012-10-08 16:30:39 -070035
36 while (*new) {
37 parent = *new;
Michel Lespinassedadf9352012-10-08 16:31:15 -070038 if (key < rb_entry(parent, struct test_node, rb)->key)
Michel Lespinasse910a7422012-10-08 16:30:39 -070039 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
48static inline void erase(struct test_node *node, struct rb_root *root)
49{
50 rb_erase(&node->rb, root);
51}
52
Michel Lespinassedadf9352012-10-08 16:31:15 -070053static 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 Lespinasse39088362012-10-08 16:31:21 -070071RB_DECLARE_CALLBACKS(static, augment_callbacks, struct test_node, rb,
72 u32, augmented, augment_recompute)
Michel Lespinasse14b94af2012-10-08 16:31:17 -070073
Michel Lespinassedadf9352012-10-08 16:31:15 -070074static void insert_augmented(struct test_node *node, struct rb_root *root)
75{
Michel Lespinasse14b94af2012-10-08 16:31:17 -070076 struct rb_node **new = &root->rb_node, *rb_parent = NULL;
Michel Lespinassedadf9352012-10-08 16:31:15 -070077 u32 key = node->key;
Michel Lespinasse14b94af2012-10-08 16:31:17 -070078 u32 val = node->val;
79 struct test_node *parent;
Michel Lespinassedadf9352012-10-08 16:31:15 -070080
81 while (*new) {
Michel Lespinasse14b94af2012-10-08 16:31:17 -070082 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 Lespinassedadf9352012-10-08 16:31:15 -070088 else
Michel Lespinasse14b94af2012-10-08 16:31:17 -070089 new = &parent->rb.rb_right;
Michel Lespinassedadf9352012-10-08 16:31:15 -070090 }
91
Michel Lespinasse14b94af2012-10-08 16:31:17 -070092 node->augmented = val;
93 rb_link_node(&node->rb, rb_parent, new);
94 rb_insert_augmented(&node->rb, root, &augment_callbacks);
Michel Lespinassedadf9352012-10-08 16:31:15 -070095}
96
97static void erase_augmented(struct test_node *node, struct rb_root *root)
98{
Michel Lespinasse14b94af2012-10-08 16:31:17 -070099 rb_erase_augmented(&node->rb, root, &augment_callbacks);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700100}
101
Michel Lespinasse910a7422012-10-08 16:30:39 -0700102static void init(void)
103{
104 int i;
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700105 for (i = 0; i < nnodes; i++) {
Akinobu Mita496f2f92012-12-17 16:04:23 -0800106 nodes[i].key = prandom_u32_state(&rnd);
107 nodes[i].val = prandom_u32_state(&rnd);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700108 }
Michel Lespinasse910a7422012-10-08 16:30:39 -0700109}
110
111static bool is_red(struct rb_node *rb)
112{
113 return !(rb->__rb_parent_color & 1);
114}
115
116static 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 Schafer964fe942014-01-23 15:56:06 -0800124static 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 Schafera791a622013-09-11 14:25:17 -0700134static 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 Lespinasse910a7422012-10-08 16:30:39 -0700144static void check(int nr_nodes)
145{
146 struct rb_node *rb;
Davidlohr Bueso4130f0e2013-04-30 15:28:24 -0700147 int count = 0, blacks = 0;
Michel Lespinasse910a7422012-10-08 16:30:39 -0700148 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 Bueso4130f0e2013-04-30 15:28:24 -0700163
Michel Lespinasse910a7422012-10-08 16:30:39 -0700164 WARN_ON_ONCE(count != nr_nodes);
Davidlohr Bueso4130f0e2013-04-30 15:28:24 -0700165 WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1);
Cody P Schafera791a622013-09-11 14:25:17 -0700166
167 check_postorder(nr_nodes);
Cody P Schafer964fe942014-01-23 15:56:06 -0800168 check_postorder_foreach(nr_nodes);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700169}
170
Michel Lespinassedadf9352012-10-08 16:31:15 -0700171static 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 Buesoc75aaa82013-04-30 15:28:25 -0700182static int __init rbtree_test_init(void)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700183{
184 int i, j;
185 cycles_t time1, time2, time;
186
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700187 nodes = kmalloc(nnodes * sizeof(*nodes), GFP_KERNEL);
188 if (!nodes)
189 return -ENOMEM;
190
Michel Lespinasse910a7422012-10-08 16:30:39 -0700191 printk(KERN_ALERT "rbtree testing");
192
Akinobu Mita496f2f92012-12-17 16:04:23 -0800193 prandom_seed_state(&rnd, 3141592653589793238ULL);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700194 init();
195
196 time1 = get_cycles();
197
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700198 for (i = 0; i < perf_loops; i++) {
199 for (j = 0; j < nnodes; j++)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700200 insert(nodes + j, &root);
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700201 for (j = 0; j < nnodes; j++)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700202 erase(nodes + j, &root);
203 }
204
205 time2 = get_cycles();
206 time = time2 - time1;
207
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700208 time = div_u64(time, perf_loops);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700209 printk(" -> %llu cycles\n", (unsigned long long)time);
210
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700211 for (i = 0; i < check_loops; i++) {
Michel Lespinasse910a7422012-10-08 16:30:39 -0700212 init();
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700213 for (j = 0; j < nnodes; j++) {
Michel Lespinasse910a7422012-10-08 16:30:39 -0700214 check(j);
215 insert(nodes + j, &root);
216 }
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700217 for (j = 0; j < nnodes; j++) {
218 check(nnodes - j);
Michel Lespinasse910a7422012-10-08 16:30:39 -0700219 erase(nodes + j, &root);
220 }
221 check(0);
222 }
223
Michel Lespinassedadf9352012-10-08 16:31:15 -0700224 printk(KERN_ALERT "augmented rbtree testing");
225
226 init();
227
228 time1 = get_cycles();
229
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700230 for (i = 0; i < perf_loops; i++) {
231 for (j = 0; j < nnodes; j++)
Michel Lespinassedadf9352012-10-08 16:31:15 -0700232 insert_augmented(nodes + j, &root);
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700233 for (j = 0; j < nnodes; j++)
Michel Lespinassedadf9352012-10-08 16:31:15 -0700234 erase_augmented(nodes + j, &root);
235 }
236
237 time2 = get_cycles();
238 time = time2 - time1;
239
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700240 time = div_u64(time, perf_loops);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700241 printk(" -> %llu cycles\n", (unsigned long long)time);
242
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700243 for (i = 0; i < check_loops; i++) {
Michel Lespinassedadf9352012-10-08 16:31:15 -0700244 init();
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700245 for (j = 0; j < nnodes; j++) {
Michel Lespinassedadf9352012-10-08 16:31:15 -0700246 check_augmented(j);
247 insert_augmented(nodes + j, &root);
248 }
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700249 for (j = 0; j < nnodes; j++) {
250 check_augmented(nnodes - j);
Michel Lespinassedadf9352012-10-08 16:31:15 -0700251 erase_augmented(nodes + j, &root);
252 }
253 check_augmented(0);
254 }
255
Davidlohr Buesob7d8b9c2017-09-08 16:14:46 -0700256 kfree(nodes);
257
Michel Lespinasse910a7422012-10-08 16:30:39 -0700258 return -EAGAIN; /* Fail will directly unload the module */
259}
260
Davidlohr Buesoc75aaa82013-04-30 15:28:25 -0700261static void __exit rbtree_test_exit(void)
Michel Lespinasse910a7422012-10-08 16:30:39 -0700262{
263 printk(KERN_ALERT "test exit\n");
264}
265
266module_init(rbtree_test_init)
267module_exit(rbtree_test_exit)
268
269MODULE_LICENSE("GPL");
270MODULE_AUTHOR("Michel Lespinasse");
271MODULE_DESCRIPTION("Red Black Tree test");