blob: ddd90a11db3f4a548c41a6460c8dce26d45ee4e5 [file] [log] [blame]
Matthew Wilcox1366c372016-03-17 14:21:45 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <time.h>
5#include <assert.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -05006#include <limits.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -07007
8#include <linux/slab.h>
9#include <linux/radix-tree.h>
10
11#include "test.h"
12#include "regression.h"
13
14void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)
15{
16 long idx;
17 RADIX_TREE(tree, GFP_KERNEL);
18
19 middle = 1 << 30;
20
21 for (idx = -down; idx < up; idx++)
22 item_insert(&tree, middle + idx);
23
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);
28
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);
33}
34
35void gang_check(void)
36{
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);
43}
44
45void __big_gang_check(void)
46{
47 unsigned long start;
48 int wrapped = 0;
49
50 start = 0;
51 do {
52 unsigned long old_start;
53
54// printf("0x%08lx\n", start);
55 __gang_check(start, rand() % 113 + 1, rand() % 71,
56 rand() % 157, rand() % 91 + 1);
57 old_start = start;
58 start += rand() % 1000000;
59 start %= 1ULL << 33;
60 if (start < old_start)
61 wrapped = 1;
62 } while (!wrapped);
63}
64
Ross Zwisleraa1d62d2016-05-20 17:01:45 -070065void big_gang_check(bool long_run)
Matthew Wilcox1366c372016-03-17 14:21:45 -070066{
67 int i;
68
Ross Zwisleraa1d62d2016-05-20 17:01:45 -070069 for (i = 0; i < (long_run ? 1000 : 3); i++) {
Matthew Wilcox1366c372016-03-17 14:21:45 -070070 __big_gang_check();
Matthew Wilcox1366c372016-03-17 14:21:45 -070071 printf("%d ", i);
72 fflush(stdout);
73 }
74}
75
76void add_and_check(void)
77{
78 RADIX_TREE(tree, GFP_KERNEL);
79
80 item_insert(&tree, 44);
81 item_check_present(&tree, 44);
82 item_check_absent(&tree, 43);
83 item_kill_tree(&tree);
84}
85
86void dynamic_height_check(void)
87{
88 int i;
89 RADIX_TREE(tree, GFP_KERNEL);
90 tree_verify_min_height(&tree, 0);
91
92 item_insert(&tree, 42);
93 tree_verify_min_height(&tree, 42);
94
95 item_insert(&tree, 1000000);
96 tree_verify_min_height(&tree, 1000000);
97
98 assert(item_delete(&tree, 1000000));
99 tree_verify_min_height(&tree, 42);
100
101 assert(item_delete(&tree, 42));
102 tree_verify_min_height(&tree, 0);
103
104 for (i = 0; i < 1000; i++) {
105 item_insert(&tree, i);
106 tree_verify_min_height(&tree, i);
107 }
108
109 i--;
110 for (;;) {
111 assert(item_delete(&tree, i));
112 if (i == 0) {
113 tree_verify_min_height(&tree, 0);
114 break;
115 }
116 i--;
117 tree_verify_min_height(&tree, i);
118 }
119
120 item_kill_tree(&tree);
121}
122
123void check_copied_tags(struct radix_tree_root *tree, unsigned long start, unsigned long end, unsigned long *idx, int count, int fromtag, int totag)
124{
125 int i;
126
127 for (i = 0; i < count; i++) {
128/* if (i % 1000 == 0)
129 putchar('.'); */
130 if (idx[i] < start || idx[i] > end) {
131 if (item_tag_get(tree, idx[i], totag)) {
132 printf("%lu-%lu: %lu, tags %d-%d\n", start, end, idx[i], item_tag_get(tree, idx[i], fromtag), item_tag_get(tree, idx[i], totag));
133 }
134 assert(!item_tag_get(tree, idx[i], totag));
135 continue;
136 }
137 if (item_tag_get(tree, idx[i], fromtag) ^
138 item_tag_get(tree, idx[i], totag)) {
139 printf("%lu-%lu: %lu, tags %d-%d\n", start, end, idx[i], item_tag_get(tree, idx[i], fromtag), item_tag_get(tree, idx[i], totag));
140 }
141 assert(!(item_tag_get(tree, idx[i], fromtag) ^
142 item_tag_get(tree, idx[i], totag)));
143 }
144}
145
146#define ITEMS 50000
147
148void copy_tag_check(void)
149{
150 RADIX_TREE(tree, GFP_KERNEL);
151 unsigned long idx[ITEMS];
152 unsigned long start, end, count = 0, tagged, cur, tmp;
153 int i;
154
155// printf("generating radix tree indices...\n");
156 start = rand();
157 end = rand();
158 if (start > end && (rand() % 10)) {
159 cur = start;
160 start = end;
161 end = cur;
162 }
163 /* Specifically create items around the start and the end of the range
164 * with high probability to check for off by one errors */
165 cur = rand();
166 if (cur & 1) {
167 item_insert(&tree, start);
168 if (cur & 2) {
169 if (start <= end)
170 count++;
171 item_tag_set(&tree, start, 0);
172 }
173 }
174 if (cur & 4) {
175 item_insert(&tree, start-1);
176 if (cur & 8)
177 item_tag_set(&tree, start-1, 0);
178 }
179 if (cur & 16) {
180 item_insert(&tree, end);
181 if (cur & 32) {
182 if (start <= end)
183 count++;
184 item_tag_set(&tree, end, 0);
185 }
186 }
187 if (cur & 64) {
188 item_insert(&tree, end+1);
189 if (cur & 128)
190 item_tag_set(&tree, end+1, 0);
191 }
192
193 for (i = 0; i < ITEMS; i++) {
194 do {
195 idx[i] = rand();
196 } while (item_lookup(&tree, idx[i]));
197
198 item_insert(&tree, idx[i]);
199 if (rand() & 1) {
200 item_tag_set(&tree, idx[i], 0);
201 if (idx[i] >= start && idx[i] <= end)
202 count++;
203 }
204/* if (i % 1000 == 0)
205 putchar('.'); */
206 }
207
208// printf("\ncopying tags...\n");
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800209 tagged = tag_tagged_items(&tree, NULL, start, end, ITEMS, 0, 1);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700210
211// printf("checking copied tags\n");
212 assert(tagged == count);
213 check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
214
215 /* Copy tags in several rounds */
216// printf("\ncopying tags...\n");
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800217 tmp = rand() % (count / 10 + 2);
218 tagged = tag_tagged_items(&tree, NULL, start, end, tmp, 0, 2);
219 assert(tagged == count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700220
221// printf("%lu %lu %lu\n", tagged, tmp, count);
222// printf("checking copied tags\n");
223 check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700224 verify_tag_consistency(&tree, 0);
225 verify_tag_consistency(&tree, 1);
226 verify_tag_consistency(&tree, 2);
227// printf("\n");
228 item_kill_tree(&tree);
229}
230
Ross Zwislereb73f7f2016-05-20 17:02:49 -0700231static void __locate_check(struct radix_tree_root *tree, unsigned long index,
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700232 unsigned order)
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700233{
234 struct item *item;
235 unsigned long index2;
236
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700237 item_insert_order(tree, index, order);
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700238 item = item_lookup(tree, index);
Matthew Wilcox478922e2016-12-14 15:08:52 -0800239 index2 = find_item(tree, item);
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700240 if (index != index2) {
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700241 printf("index %ld order %d inserted; found %ld\n",
242 index, order, index2);
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700243 abort();
244 }
245}
246
Ross Zwislereb73f7f2016-05-20 17:02:49 -0700247static void __order_0_locate_check(void)
248{
249 RADIX_TREE(tree, GFP_KERNEL);
250 int i;
251
252 for (i = 0; i < 50; i++)
253 __locate_check(&tree, rand() % INT_MAX, 0);
254
255 item_kill_tree(&tree);
256}
257
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700258static void locate_check(void)
259{
260 RADIX_TREE(tree, GFP_KERNEL);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700261 unsigned order;
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700262 unsigned long offset, index;
263
Ross Zwislereb73f7f2016-05-20 17:02:49 -0700264 __order_0_locate_check();
265
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700266 for (order = 0; order < 20; order++) {
267 for (offset = 0; offset < (1 << (order + 3));
268 offset += (1UL << order)) {
269 for (index = 0; index < (1UL << (order + 5));
270 index += (1UL << order)) {
271 __locate_check(&tree, index + offset, order);
272 }
Matthew Wilcox478922e2016-12-14 15:08:52 -0800273 if (find_item(&tree, &tree) != -1)
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700274 abort();
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700275
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700276 item_kill_tree(&tree);
277 }
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700278 }
279
Matthew Wilcox478922e2016-12-14 15:08:52 -0800280 if (find_item(&tree, &tree) != -1)
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700281 abort();
Matthew Wilcox0a2efc62016-05-20 17:02:46 -0700282 __locate_check(&tree, -1, 0);
Matthew Wilcox478922e2016-12-14 15:08:52 -0800283 if (find_item(&tree, &tree) != -1)
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700284 abort();
285 item_kill_tree(&tree);
286}
287
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700288static void single_thread_tests(bool long_run)
Matthew Wilcox1366c372016-03-17 14:21:45 -0700289{
290 int i;
291
Matthew Wilcox847d3572016-12-14 15:08:02 -0800292 printf("starting single_thread_tests: %d allocated, preempt %d\n",
293 nr_allocated, preempt_count);
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700294 multiorder_checks();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800295 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800296 printf("after multiorder_check: %d allocated, preempt %d\n",
297 nr_allocated, preempt_count);
Matthew Wilcoxd42cb1a2016-05-20 17:01:39 -0700298 locate_check();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800299 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800300 printf("after locate_check: %d allocated, preempt %d\n",
301 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700302 tag_check();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800303 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800304 printf("after tag_check: %d allocated, preempt %d\n",
305 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700306 gang_check();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800307 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800308 printf("after gang_check: %d allocated, preempt %d\n",
309 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700310 add_and_check();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800311 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800312 printf("after add_and_check: %d allocated, preempt %d\n",
313 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700314 dynamic_height_check();
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800315 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800316 printf("after dynamic_height_check: %d allocated, preempt %d\n",
317 nr_allocated, preempt_count);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500318 idr_checks();
319 ida_checks();
320 rcu_barrier();
321 printf("after idr_checks: %d allocated, preempt %d\n",
322 nr_allocated, preempt_count);
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700323 big_gang_check(long_run);
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800324 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800325 printf("after big_gang_check: %d allocated, preempt %d\n",
326 nr_allocated, preempt_count);
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700327 for (i = 0; i < (long_run ? 2000 : 3); i++) {
Matthew Wilcox1366c372016-03-17 14:21:45 -0700328 copy_tag_check();
329 printf("%d ", i);
330 fflush(stdout);
331 }
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800332 rcu_barrier();
Matthew Wilcox847d3572016-12-14 15:08:02 -0800333 printf("after copy_tag_check: %d allocated, preempt %d\n",
334 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700335}
336
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700337int main(int argc, char **argv)
Matthew Wilcox1366c372016-03-17 14:21:45 -0700338{
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700339 bool long_run = false;
340 int opt;
Matthew Wilcox061ef392016-12-14 15:08:08 -0800341 unsigned int seed = time(NULL);
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700342
Matthew Wilcox061ef392016-12-14 15:08:08 -0800343 while ((opt = getopt(argc, argv, "ls:")) != -1) {
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700344 if (opt == 'l')
345 long_run = true;
Matthew Wilcox061ef392016-12-14 15:08:08 -0800346 else if (opt == 's')
347 seed = strtoul(optarg, NULL, 0);
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700348 }
349
Matthew Wilcox061ef392016-12-14 15:08:08 -0800350 printf("random seed %u\n", seed);
351 srand(seed);
352
Matthew Wilcox1366c372016-03-17 14:21:45 -0700353 rcu_register_thread();
354 radix_tree_init();
355
356 regression1_test();
357 regression2_test();
Konstantin Khlebnikov2d6f45b2016-03-17 14:22:08 -0700358 regression3_test();
Matthew Wilcox3e3cdc62016-12-14 15:09:10 -0800359 iteration_test(0, 10);
360 iteration_test(7, 20);
Ross Zwisleraa1d62d2016-05-20 17:01:45 -0700361 single_thread_tests(long_run);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700362
Matthew Wilcox6df5ee72016-12-14 15:08:05 -0800363 /* Free any remaining preallocated nodes */
364 radix_tree_cpu_dead(0);
365
Konstantin Khlebnikovcfa40bc2016-12-14 15:08:14 -0800366 benchmark();
367
Matthew Wilcoxaf1c5cc2016-12-14 15:08:17 -0800368 rcu_barrier();
369 printf("after rcu_barrier: %d allocated, preempt %d\n",
Matthew Wilcox847d3572016-12-14 15:08:02 -0800370 nr_allocated, preempt_count);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700371 rcu_unregister_thread();
372
373 exit(0);
374}