1 #include <linux/module.h>
2 #include <linux/rbtree_augmented.h>
3 #include <linux/random.h>
7 #define PERF_LOOPS 100000
8 #define CHECK_LOOPS 100
14 /* following fields used for testing augmented rbtree functionality */
19 static struct rb_root root
= RB_ROOT
;
20 static struct test_node nodes
[NODES
];
22 static struct rnd_state rnd
;
24 static void insert(struct test_node
*node
, struct rb_root
*root
)
26 struct rb_node
**new = &root
->rb_node
, *parent
= NULL
;
31 if (key
< rb_entry(parent
, struct test_node
, rb
)->key
)
32 new = &parent
->rb_left
;
34 new = &parent
->rb_right
;
37 rb_link_node(&node
->rb
, parent
, new);
38 rb_insert_color(&node
->rb
, root
);
41 static inline void erase(struct test_node
*node
, struct rb_root
*root
)
43 rb_erase(&node
->rb
, root
);
46 static inline u32
augment_recompute(struct test_node
*node
)
48 u32 max
= node
->val
, child_augmented
;
49 if (node
->rb
.rb_left
) {
50 child_augmented
= rb_entry(node
->rb
.rb_left
, struct test_node
,
52 if (max
< child_augmented
)
53 max
= child_augmented
;
55 if (node
->rb
.rb_right
) {
56 child_augmented
= rb_entry(node
->rb
.rb_right
, struct test_node
,
58 if (max
< child_augmented
)
59 max
= child_augmented
;
64 RB_DECLARE_CALLBACKS(static, augment_callbacks
, struct test_node
, rb
,
65 u32
, augmented
, augment_recompute
)
67 static void insert_augmented(struct test_node
*node
, struct rb_root
*root
)
69 struct rb_node
**new = &root
->rb_node
, *rb_parent
= NULL
;
72 struct test_node
*parent
;
76 parent
= rb_entry(rb_parent
, struct test_node
, rb
);
77 if (parent
->augmented
< val
)
78 parent
->augmented
= val
;
79 if (key
< parent
->key
)
80 new = &parent
->rb
.rb_left
;
82 new = &parent
->rb
.rb_right
;
85 node
->augmented
= val
;
86 rb_link_node(&node
->rb
, rb_parent
, new);
87 rb_insert_augmented(&node
->rb
, root
, &augment_callbacks
);
90 static void erase_augmented(struct test_node
*node
, struct rb_root
*root
)
92 rb_erase_augmented(&node
->rb
, root
, &augment_callbacks
);
95 static void init(void)
98 for (i
= 0; i
< NODES
; i
++) {
99 nodes
[i
].key
= prandom_u32_state(&rnd
);
100 nodes
[i
].val
= prandom_u32_state(&rnd
);
104 static bool is_red(struct rb_node
*rb
)
106 return !(rb
->__rb_parent_color
& 1);
109 static int black_path_count(struct rb_node
*rb
)
112 for (count
= 0; rb
; rb
= rb_parent(rb
))
113 count
+= !is_red(rb
);
117 static void check_postorder_foreach(int nr_nodes
)
119 struct test_node
*cur
, *n
;
121 rbtree_postorder_for_each_entry_safe(cur
, n
, &root
, rb
)
124 WARN_ON_ONCE(count
!= nr_nodes
);
127 static void check_postorder(int nr_nodes
)
131 for (rb
= rb_first_postorder(&root
); rb
; rb
= rb_next_postorder(rb
))
134 WARN_ON_ONCE(count
!= nr_nodes
);
137 static void check(int nr_nodes
)
140 int count
= 0, blacks
= 0;
143 for (rb
= rb_first(&root
); rb
; rb
= rb_next(rb
)) {
144 struct test_node
*node
= rb_entry(rb
, struct test_node
, rb
);
145 WARN_ON_ONCE(node
->key
< prev_key
);
146 WARN_ON_ONCE(is_red(rb
) &&
147 (!rb_parent(rb
) || is_red(rb_parent(rb
))));
149 blacks
= black_path_count(rb
);
151 WARN_ON_ONCE((!rb
->rb_left
|| !rb
->rb_right
) &&
152 blacks
!= black_path_count(rb
));
153 prev_key
= node
->key
;
157 WARN_ON_ONCE(count
!= nr_nodes
);
158 WARN_ON_ONCE(count
< (1 << black_path_count(rb_last(&root
))) - 1);
160 check_postorder(nr_nodes
);
161 check_postorder_foreach(nr_nodes
);
164 static void check_augmented(int nr_nodes
)
169 for (rb
= rb_first(&root
); rb
; rb
= rb_next(rb
)) {
170 struct test_node
*node
= rb_entry(rb
, struct test_node
, rb
);
171 WARN_ON_ONCE(node
->augmented
!= augment_recompute(node
));
175 static int __init
rbtree_test_init(void)
178 cycles_t time1
, time2
, time
;
180 printk(KERN_ALERT
"rbtree testing");
182 prandom_seed_state(&rnd
, 3141592653589793238ULL);
185 time1
= get_cycles();
187 for (i
= 0; i
< PERF_LOOPS
; i
++) {
188 for (j
= 0; j
< NODES
; j
++)
189 insert(nodes
+ j
, &root
);
190 for (j
= 0; j
< NODES
; j
++)
191 erase(nodes
+ j
, &root
);
194 time2
= get_cycles();
195 time
= time2
- time1
;
197 time
= div_u64(time
, PERF_LOOPS
);
198 printk(" -> %llu cycles\n", (unsigned long long)time
);
200 for (i
= 0; i
< CHECK_LOOPS
; i
++) {
202 for (j
= 0; j
< NODES
; j
++) {
204 insert(nodes
+ j
, &root
);
206 for (j
= 0; j
< NODES
; j
++) {
208 erase(nodes
+ j
, &root
);
213 printk(KERN_ALERT
"augmented rbtree testing");
217 time1
= get_cycles();
219 for (i
= 0; i
< PERF_LOOPS
; i
++) {
220 for (j
= 0; j
< NODES
; j
++)
221 insert_augmented(nodes
+ j
, &root
);
222 for (j
= 0; j
< NODES
; j
++)
223 erase_augmented(nodes
+ j
, &root
);
226 time2
= get_cycles();
227 time
= time2
- time1
;
229 time
= div_u64(time
, PERF_LOOPS
);
230 printk(" -> %llu cycles\n", (unsigned long long)time
);
232 for (i
= 0; i
< CHECK_LOOPS
; i
++) {
234 for (j
= 0; j
< NODES
; j
++) {
236 insert_augmented(nodes
+ j
, &root
);
238 for (j
= 0; j
< NODES
; j
++) {
239 check_augmented(NODES
- j
);
240 erase_augmented(nodes
+ j
, &root
);
245 return -EAGAIN
; /* Fail will directly unload the module */
248 static void __exit
rbtree_test_exit(void)
250 printk(KERN_ALERT
"test exit\n");
253 module_init(rbtree_test_init
)
254 module_exit(rbtree_test_exit
)
256 MODULE_LICENSE("GPL");
257 MODULE_AUTHOR("Michel Lespinasse");
258 MODULE_DESCRIPTION("Red Black Tree test");