8 #include <linux/slab.h>
9 #include <linux/radix-tree.h>
12 #include "regression.h"
14 void __gang_check(unsigned long middle
, long down
, long up
, int chunk
, int hop
)
17 RADIX_TREE(tree
, GFP_KERNEL
);
21 for (idx
= -down
; idx
< up
; idx
++)
22 item_insert(&tree
, middle
+ idx
);
24 item_check_absent(&tree
, middle
- down
- 1);
25 for (idx
= -down
; idx
< up
; idx
++)
26 item_check_present(&tree
, middle
+ idx
);
27 item_check_absent(&tree
, middle
+ up
);
29 item_gang_check_present(&tree
, middle
- down
,
30 up
+ down
, chunk
, hop
);
31 item_full_scan(&tree
, middle
- down
, down
+ up
, chunk
);
32 item_kill_tree(&tree
);
37 __gang_check(1 << 30, 128, 128, 35, 2);
38 __gang_check(1 << 31, 128, 128, 32, 32);
39 __gang_check(1 << 31, 128, 128, 32, 100);
40 __gang_check(1 << 31, 128, 128, 17, 7);
41 __gang_check(0xffff0000, 0, 65536, 17, 7);
42 __gang_check(0xfffffffe, 1, 1, 17, 7);
45 void __big_gang_check(void)
52 unsigned long old_start
;
54 // printf("0x%08lx\n", start);
55 __gang_check(start
, rand() % 113 + 1, rand() % 71,
56 rand() % 157, rand() % 91 + 1);
58 start
+= rand() % 1000000;
60 if (start
< old_start
)
65 void big_gang_check(bool long_run
)
69 for (i
= 0; i
< (long_run
? 1000 : 3); i
++) {
76 void add_and_check(void)
78 RADIX_TREE(tree
, GFP_KERNEL
);
80 item_insert(&tree
, 44);
81 item_check_present(&tree
, 44);
82 item_check_absent(&tree
, 43);
83 item_kill_tree(&tree
);
86 void dynamic_height_check(void)
89 RADIX_TREE(tree
, GFP_KERNEL
);
90 tree_verify_min_height(&tree
, 0);
92 item_insert(&tree
, 42);
93 tree_verify_min_height(&tree
, 42);
95 item_insert(&tree
, 1000000);
96 tree_verify_min_height(&tree
, 1000000);
98 assert(item_delete(&tree
, 1000000));
99 tree_verify_min_height(&tree
, 42);
101 assert(item_delete(&tree
, 42));
102 tree_verify_min_height(&tree
, 0);
104 for (i
= 0; i
< 1000; i
++) {
105 item_insert(&tree
, i
);
106 tree_verify_min_height(&tree
, i
);
111 assert(item_delete(&tree
, i
));
113 tree_verify_min_height(&tree
, 0);
117 tree_verify_min_height(&tree
, i
);
120 item_kill_tree(&tree
);
123 void check_copied_tags(struct radix_tree_root
*tree
, unsigned long start
, unsigned long end
, unsigned long *idx
, int count
, int fromtag
, int totag
)
127 for (i
= 0; i
< count
; i
++) {
128 /* if (i % 1000 == 0)
130 if (idx
[i
] < start
|| idx
[i
] > end
) {
131 if (item_tag_get(tree
, idx
[i
], totag
)) {
132 printv(2, "%lu-%lu: %lu, tags %d-%d\n", start
,
133 end
, idx
[i
], item_tag_get(tree
, idx
[i
],
135 item_tag_get(tree
, idx
[i
], totag
));
137 assert(!item_tag_get(tree
, idx
[i
], totag
));
140 if (item_tag_get(tree
, idx
[i
], fromtag
) ^
141 item_tag_get(tree
, idx
[i
], totag
)) {
142 printv(2, "%lu-%lu: %lu, tags %d-%d\n", start
, end
,
143 idx
[i
], item_tag_get(tree
, idx
[i
], fromtag
),
144 item_tag_get(tree
, idx
[i
], totag
));
146 assert(!(item_tag_get(tree
, idx
[i
], fromtag
) ^
147 item_tag_get(tree
, idx
[i
], totag
)));
153 void copy_tag_check(void)
155 RADIX_TREE(tree
, GFP_KERNEL
);
156 unsigned long idx
[ITEMS
];
157 unsigned long start
, end
, count
= 0, tagged
, cur
, tmp
;
160 // printf("generating radix tree indices...\n");
163 if (start
> end
&& (rand() % 10)) {
168 /* Specifically create items around the start and the end of the range
169 * with high probability to check for off by one errors */
172 item_insert(&tree
, start
);
176 item_tag_set(&tree
, start
, 0);
180 item_insert(&tree
, start
-1);
182 item_tag_set(&tree
, start
-1, 0);
185 item_insert(&tree
, end
);
189 item_tag_set(&tree
, end
, 0);
193 item_insert(&tree
, end
+1);
195 item_tag_set(&tree
, end
+1, 0);
198 for (i
= 0; i
< ITEMS
; i
++) {
201 } while (item_lookup(&tree
, idx
[i
]));
203 item_insert(&tree
, idx
[i
]);
205 item_tag_set(&tree
, idx
[i
], 0);
206 if (idx
[i
] >= start
&& idx
[i
] <= end
)
209 /* if (i % 1000 == 0)
213 // printf("\ncopying tags...\n");
214 tagged
= tag_tagged_items(&tree
, NULL
, start
, end
, ITEMS
, 0, 1);
216 // printf("checking copied tags\n");
217 assert(tagged
== count
);
218 check_copied_tags(&tree
, start
, end
, idx
, ITEMS
, 0, 1);
220 /* Copy tags in several rounds */
221 // printf("\ncopying tags...\n");
222 tmp
= rand() % (count
/ 10 + 2);
223 tagged
= tag_tagged_items(&tree
, NULL
, start
, end
, tmp
, 0, 2);
224 assert(tagged
== count
);
226 // printf("%lu %lu %lu\n", tagged, tmp, count);
227 // printf("checking copied tags\n");
228 check_copied_tags(&tree
, start
, end
, idx
, ITEMS
, 0, 2);
229 verify_tag_consistency(&tree
, 0);
230 verify_tag_consistency(&tree
, 1);
231 verify_tag_consistency(&tree
, 2);
233 item_kill_tree(&tree
);
236 static void __locate_check(struct radix_tree_root
*tree
, unsigned long index
,
240 unsigned long index2
;
242 item_insert_order(tree
, index
, order
);
243 item
= item_lookup(tree
, index
);
244 index2
= find_item(tree
, item
);
245 if (index
!= index2
) {
246 printv(2, "index %ld order %d inserted; found %ld\n",
247 index
, order
, index2
);
252 static void __order_0_locate_check(void)
254 RADIX_TREE(tree
, GFP_KERNEL
);
257 for (i
= 0; i
< 50; i
++)
258 __locate_check(&tree
, rand() % INT_MAX
, 0);
260 item_kill_tree(&tree
);
263 static void locate_check(void)
265 RADIX_TREE(tree
, GFP_KERNEL
);
267 unsigned long offset
, index
;
269 __order_0_locate_check();
271 for (order
= 0; order
< 20; order
++) {
272 for (offset
= 0; offset
< (1 << (order
+ 3));
273 offset
+= (1UL << order
)) {
274 for (index
= 0; index
< (1UL << (order
+ 5));
275 index
+= (1UL << order
)) {
276 __locate_check(&tree
, index
+ offset
, order
);
278 if (find_item(&tree
, &tree
) != -1)
281 item_kill_tree(&tree
);
285 if (find_item(&tree
, &tree
) != -1)
287 __locate_check(&tree
, -1, 0);
288 if (find_item(&tree
, &tree
) != -1)
290 item_kill_tree(&tree
);
293 static void single_thread_tests(bool long_run
)
297 printv(1, "starting single_thread_tests: %d allocated, preempt %d\n",
298 nr_allocated
, preempt_count
);
301 printv(2, "after multiorder_check: %d allocated, preempt %d\n",
302 nr_allocated
, preempt_count
);
305 printv(2, "after locate_check: %d allocated, preempt %d\n",
306 nr_allocated
, preempt_count
);
309 printv(2, "after tag_check: %d allocated, preempt %d\n",
310 nr_allocated
, preempt_count
);
313 printv(2, "after gang_check: %d allocated, preempt %d\n",
314 nr_allocated
, preempt_count
);
317 printv(2, "after add_and_check: %d allocated, preempt %d\n",
318 nr_allocated
, preempt_count
);
319 dynamic_height_check();
321 printv(2, "after dynamic_height_check: %d allocated, preempt %d\n",
322 nr_allocated
, preempt_count
);
326 printv(2, "after idr_checks: %d allocated, preempt %d\n",
327 nr_allocated
, preempt_count
);
328 big_gang_check(long_run
);
330 printv(2, "after big_gang_check: %d allocated, preempt %d\n",
331 nr_allocated
, preempt_count
);
332 for (i
= 0; i
< (long_run
? 2000 : 3); i
++) {
338 printv(2, "after copy_tag_check: %d allocated, preempt %d\n",
339 nr_allocated
, preempt_count
);
342 int main(int argc
, char **argv
)
344 bool long_run
= false;
346 unsigned int seed
= time(NULL
);
348 while ((opt
= getopt(argc
, argv
, "ls:v")) != -1) {
352 seed
= strtoul(optarg
, NULL
, 0);
357 printf("random seed %u\n", seed
);
360 printf("running tests\n");
362 rcu_register_thread();
368 iteration_test(0, 10 + 90 * long_run
);
369 iteration_test(7, 10 + 90 * long_run
);
370 single_thread_tests(long_run
);
373 /* Free any remaining preallocated nodes */
374 radix_tree_cpu_dead(0);
379 printv(2, "after rcu_barrier: %d allocated, preempt %d\n",
380 nr_allocated
, preempt_count
);
381 rcu_unregister_thread();
383 printf("tests completed\n");