xref: /openbmc/linux/tools/testing/radix-tree/main.c (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21366c37eSMatthew Wilcox #include <stdio.h>
31366c37eSMatthew Wilcox #include <stdlib.h>
41366c37eSMatthew Wilcox #include <unistd.h>
51366c37eSMatthew Wilcox #include <time.h>
61366c37eSMatthew Wilcox #include <assert.h>
70a835c4fSMatthew Wilcox #include <limits.h>
81366c37eSMatthew Wilcox 
91366c37eSMatthew Wilcox #include <linux/slab.h>
101366c37eSMatthew Wilcox #include <linux/radix-tree.h>
111366c37eSMatthew Wilcox 
121366c37eSMatthew Wilcox #include "test.h"
131366c37eSMatthew Wilcox #include "regression.h"
141366c37eSMatthew Wilcox 
__gang_check(unsigned long middle,long down,long up,int chunk,int hop)151366c37eSMatthew Wilcox void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)
161366c37eSMatthew Wilcox {
171366c37eSMatthew Wilcox 	long idx;
181366c37eSMatthew Wilcox 	RADIX_TREE(tree, GFP_KERNEL);
191366c37eSMatthew Wilcox 
201366c37eSMatthew Wilcox 	middle = 1 << 30;
211366c37eSMatthew Wilcox 
221366c37eSMatthew Wilcox 	for (idx = -down; idx < up; idx++)
231366c37eSMatthew Wilcox 		item_insert(&tree, middle + idx);
241366c37eSMatthew Wilcox 
251366c37eSMatthew Wilcox 	item_check_absent(&tree, middle - down - 1);
261366c37eSMatthew Wilcox 	for (idx = -down; idx < up; idx++)
271366c37eSMatthew Wilcox 		item_check_present(&tree, middle + idx);
281366c37eSMatthew Wilcox 	item_check_absent(&tree, middle + up);
291366c37eSMatthew Wilcox 
30d1c0d5e3SMatthew Wilcox 	if (chunk > 0) {
31d1c0d5e3SMatthew Wilcox 		item_gang_check_present(&tree, middle - down, up + down,
32d1c0d5e3SMatthew Wilcox 				chunk, hop);
331366c37eSMatthew Wilcox 		item_full_scan(&tree, middle - down, down + up, chunk);
34d1c0d5e3SMatthew Wilcox 	}
351366c37eSMatthew Wilcox 	item_kill_tree(&tree);
361366c37eSMatthew Wilcox }
371366c37eSMatthew Wilcox 
gang_check(void)381366c37eSMatthew Wilcox void gang_check(void)
391366c37eSMatthew Wilcox {
40d1c0d5e3SMatthew Wilcox 	__gang_check(1UL << 30, 128, 128, 35, 2);
41d1c0d5e3SMatthew Wilcox 	__gang_check(1UL << 31, 128, 128, 32, 32);
42d1c0d5e3SMatthew Wilcox 	__gang_check(1UL << 31, 128, 128, 32, 100);
43d1c0d5e3SMatthew Wilcox 	__gang_check(1UL << 31, 128, 128, 17, 7);
44d1c0d5e3SMatthew Wilcox 	__gang_check(0xffff0000UL, 0, 65536, 17, 7);
45d1c0d5e3SMatthew Wilcox 	__gang_check(0xfffffffeUL, 1, 1, 17, 7);
461366c37eSMatthew Wilcox }
471366c37eSMatthew Wilcox 
__big_gang_check(void)481366c37eSMatthew Wilcox void __big_gang_check(void)
491366c37eSMatthew Wilcox {
501366c37eSMatthew Wilcox 	unsigned long start;
511366c37eSMatthew Wilcox 	int wrapped = 0;
521366c37eSMatthew Wilcox 
531366c37eSMatthew Wilcox 	start = 0;
541366c37eSMatthew Wilcox 	do {
551366c37eSMatthew Wilcox 		unsigned long old_start;
561366c37eSMatthew Wilcox 
571366c37eSMatthew Wilcox //		printf("0x%08lx\n", start);
581366c37eSMatthew Wilcox 		__gang_check(start, rand() % 113 + 1, rand() % 71,
591366c37eSMatthew Wilcox 				rand() % 157, rand() % 91 + 1);
601366c37eSMatthew Wilcox 		old_start = start;
611366c37eSMatthew Wilcox 		start += rand() % 1000000;
621366c37eSMatthew Wilcox 		start %= 1ULL << 33;
631366c37eSMatthew Wilcox 		if (start < old_start)
641366c37eSMatthew Wilcox 			wrapped = 1;
651366c37eSMatthew Wilcox 	} while (!wrapped);
661366c37eSMatthew Wilcox }
671366c37eSMatthew Wilcox 
big_gang_check(bool long_run)68aa1d62d8SRoss Zwisler void big_gang_check(bool long_run)
691366c37eSMatthew Wilcox {
701366c37eSMatthew Wilcox 	int i;
711366c37eSMatthew Wilcox 
72aa1d62d8SRoss Zwisler 	for (i = 0; i < (long_run ? 1000 : 3); i++) {
731366c37eSMatthew Wilcox 		__big_gang_check();
7473bc029bSRehas Sachdeva 		printv(2, "%d ", i);
751366c37eSMatthew Wilcox 		fflush(stdout);
761366c37eSMatthew Wilcox 	}
771366c37eSMatthew Wilcox }
781366c37eSMatthew Wilcox 
add_and_check(void)791366c37eSMatthew Wilcox void add_and_check(void)
801366c37eSMatthew Wilcox {
811366c37eSMatthew Wilcox 	RADIX_TREE(tree, GFP_KERNEL);
821366c37eSMatthew Wilcox 
831366c37eSMatthew Wilcox 	item_insert(&tree, 44);
841366c37eSMatthew Wilcox 	item_check_present(&tree, 44);
851366c37eSMatthew Wilcox 	item_check_absent(&tree, 43);
861366c37eSMatthew Wilcox 	item_kill_tree(&tree);
871366c37eSMatthew Wilcox }
881366c37eSMatthew Wilcox 
dynamic_height_check(void)891366c37eSMatthew Wilcox void dynamic_height_check(void)
901366c37eSMatthew Wilcox {
911366c37eSMatthew Wilcox 	int i;
921366c37eSMatthew Wilcox 	RADIX_TREE(tree, GFP_KERNEL);
931366c37eSMatthew Wilcox 	tree_verify_min_height(&tree, 0);
941366c37eSMatthew Wilcox 
951366c37eSMatthew Wilcox 	item_insert(&tree, 42);
961366c37eSMatthew Wilcox 	tree_verify_min_height(&tree, 42);
971366c37eSMatthew Wilcox 
981366c37eSMatthew Wilcox 	item_insert(&tree, 1000000);
991366c37eSMatthew Wilcox 	tree_verify_min_height(&tree, 1000000);
1001366c37eSMatthew Wilcox 
1011366c37eSMatthew Wilcox 	assert(item_delete(&tree, 1000000));
1021366c37eSMatthew Wilcox 	tree_verify_min_height(&tree, 42);
1031366c37eSMatthew Wilcox 
1041366c37eSMatthew Wilcox 	assert(item_delete(&tree, 42));
1051366c37eSMatthew Wilcox 	tree_verify_min_height(&tree, 0);
1061366c37eSMatthew Wilcox 
1071366c37eSMatthew Wilcox 	for (i = 0; i < 1000; i++) {
1081366c37eSMatthew Wilcox 		item_insert(&tree, i);
1091366c37eSMatthew Wilcox 		tree_verify_min_height(&tree, i);
1101366c37eSMatthew Wilcox 	}
1111366c37eSMatthew Wilcox 
1121366c37eSMatthew Wilcox 	i--;
1131366c37eSMatthew Wilcox 	for (;;) {
1141366c37eSMatthew Wilcox 		assert(item_delete(&tree, i));
1151366c37eSMatthew Wilcox 		if (i == 0) {
1161366c37eSMatthew Wilcox 			tree_verify_min_height(&tree, 0);
1171366c37eSMatthew Wilcox 			break;
1181366c37eSMatthew Wilcox 		}
1191366c37eSMatthew Wilcox 		i--;
1201366c37eSMatthew Wilcox 		tree_verify_min_height(&tree, i);
1211366c37eSMatthew Wilcox 	}
1221366c37eSMatthew Wilcox 
1231366c37eSMatthew Wilcox 	item_kill_tree(&tree);
1241366c37eSMatthew Wilcox }
1251366c37eSMatthew Wilcox 
check_copied_tags(struct radix_tree_root * tree,unsigned long start,unsigned long end,unsigned long * idx,int count,int fromtag,int totag)1261366c37eSMatthew Wilcox void check_copied_tags(struct radix_tree_root *tree, unsigned long start, unsigned long end, unsigned long *idx, int count, int fromtag, int totag)
1271366c37eSMatthew Wilcox {
1281366c37eSMatthew Wilcox 	int i;
1291366c37eSMatthew Wilcox 
1301366c37eSMatthew Wilcox 	for (i = 0; i < count; i++) {
1311366c37eSMatthew Wilcox /*		if (i % 1000 == 0)
1321366c37eSMatthew Wilcox 			putchar('.'); */
1331366c37eSMatthew Wilcox 		if (idx[i] < start || idx[i] > end) {
1341366c37eSMatthew Wilcox 			if (item_tag_get(tree, idx[i], totag)) {
13573bc029bSRehas Sachdeva 				printv(2, "%lu-%lu: %lu, tags %d-%d\n", start,
13673bc029bSRehas Sachdeva 				       end, idx[i], item_tag_get(tree, idx[i],
13773bc029bSRehas Sachdeva 								 fromtag),
13873bc029bSRehas Sachdeva 				       item_tag_get(tree, idx[i], totag));
1391366c37eSMatthew Wilcox 			}
1401366c37eSMatthew Wilcox 			assert(!item_tag_get(tree, idx[i], totag));
1411366c37eSMatthew Wilcox 			continue;
1421366c37eSMatthew Wilcox 		}
1431366c37eSMatthew Wilcox 		if (item_tag_get(tree, idx[i], fromtag) ^
1441366c37eSMatthew Wilcox 			item_tag_get(tree, idx[i], totag)) {
14573bc029bSRehas Sachdeva 			printv(2, "%lu-%lu: %lu, tags %d-%d\n", start, end,
14673bc029bSRehas Sachdeva 			       idx[i], item_tag_get(tree, idx[i], fromtag),
14773bc029bSRehas Sachdeva 			       item_tag_get(tree, idx[i], totag));
1481366c37eSMatthew Wilcox 		}
1491366c37eSMatthew Wilcox 		assert(!(item_tag_get(tree, idx[i], fromtag) ^
1501366c37eSMatthew Wilcox 			 item_tag_get(tree, idx[i], totag)));
1511366c37eSMatthew Wilcox 	}
1521366c37eSMatthew Wilcox }
1531366c37eSMatthew Wilcox 
1541366c37eSMatthew Wilcox #define ITEMS 50000
1551366c37eSMatthew Wilcox 
copy_tag_check(void)1561366c37eSMatthew Wilcox void copy_tag_check(void)
1571366c37eSMatthew Wilcox {
1581366c37eSMatthew Wilcox 	RADIX_TREE(tree, GFP_KERNEL);
1591366c37eSMatthew Wilcox 	unsigned long idx[ITEMS];
1601366c37eSMatthew Wilcox 	unsigned long start, end, count = 0, tagged, cur, tmp;
1611366c37eSMatthew Wilcox 	int i;
1621366c37eSMatthew Wilcox 
1631366c37eSMatthew Wilcox //	printf("generating radix tree indices...\n");
1641366c37eSMatthew Wilcox 	start = rand();
1651366c37eSMatthew Wilcox 	end = rand();
1661366c37eSMatthew Wilcox 	if (start > end && (rand() % 10)) {
1671366c37eSMatthew Wilcox 		cur = start;
1681366c37eSMatthew Wilcox 		start = end;
1691366c37eSMatthew Wilcox 		end = cur;
1701366c37eSMatthew Wilcox 	}
1711366c37eSMatthew Wilcox 	/* Specifically create items around the start and the end of the range
1721366c37eSMatthew Wilcox 	 * with high probability to check for off by one errors */
1731366c37eSMatthew Wilcox 	cur = rand();
1741366c37eSMatthew Wilcox 	if (cur & 1) {
1751366c37eSMatthew Wilcox 		item_insert(&tree, start);
1761366c37eSMatthew Wilcox 		if (cur & 2) {
1771366c37eSMatthew Wilcox 			if (start <= end)
1781366c37eSMatthew Wilcox 				count++;
1791366c37eSMatthew Wilcox 			item_tag_set(&tree, start, 0);
1801366c37eSMatthew Wilcox 		}
1811366c37eSMatthew Wilcox 	}
1821366c37eSMatthew Wilcox 	if (cur & 4) {
1831366c37eSMatthew Wilcox 		item_insert(&tree, start-1);
1841366c37eSMatthew Wilcox 		if (cur & 8)
1851366c37eSMatthew Wilcox 			item_tag_set(&tree, start-1, 0);
1861366c37eSMatthew Wilcox 	}
1871366c37eSMatthew Wilcox 	if (cur & 16) {
1881366c37eSMatthew Wilcox 		item_insert(&tree, end);
1891366c37eSMatthew Wilcox 		if (cur & 32) {
1901366c37eSMatthew Wilcox 			if (start <= end)
1911366c37eSMatthew Wilcox 				count++;
1921366c37eSMatthew Wilcox 			item_tag_set(&tree, end, 0);
1931366c37eSMatthew Wilcox 		}
1941366c37eSMatthew Wilcox 	}
1951366c37eSMatthew Wilcox 	if (cur & 64) {
1961366c37eSMatthew Wilcox 		item_insert(&tree, end+1);
1971366c37eSMatthew Wilcox 		if (cur & 128)
1981366c37eSMatthew Wilcox 			item_tag_set(&tree, end+1, 0);
1991366c37eSMatthew Wilcox 	}
2001366c37eSMatthew Wilcox 
2011366c37eSMatthew Wilcox 	for (i = 0; i < ITEMS; i++) {
2021366c37eSMatthew Wilcox 		do {
2031366c37eSMatthew Wilcox 			idx[i] = rand();
2041366c37eSMatthew Wilcox 		} while (item_lookup(&tree, idx[i]));
2051366c37eSMatthew Wilcox 
2061366c37eSMatthew Wilcox 		item_insert(&tree, idx[i]);
2071366c37eSMatthew Wilcox 		if (rand() & 1) {
2081366c37eSMatthew Wilcox 			item_tag_set(&tree, idx[i], 0);
2091366c37eSMatthew Wilcox 			if (idx[i] >= start && idx[i] <= end)
2101366c37eSMatthew Wilcox 				count++;
2111366c37eSMatthew Wilcox 		}
2121366c37eSMatthew Wilcox /*		if (i % 1000 == 0)
2131366c37eSMatthew Wilcox 			putchar('.'); */
2141366c37eSMatthew Wilcox 	}
2151366c37eSMatthew Wilcox 
2161366c37eSMatthew Wilcox //	printf("\ncopying tags...\n");
217372266baSMatthew Wilcox 	tagged = tag_tagged_items(&tree, start, end, ITEMS, XA_MARK_0, XA_MARK_1);
2181366c37eSMatthew Wilcox 
2191366c37eSMatthew Wilcox //	printf("checking copied tags\n");
2201366c37eSMatthew Wilcox 	assert(tagged == count);
2211366c37eSMatthew Wilcox 	check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
2221366c37eSMatthew Wilcox 
2231366c37eSMatthew Wilcox 	/* Copy tags in several rounds */
2241366c37eSMatthew Wilcox //	printf("\ncopying tags...\n");
2251366c37eSMatthew Wilcox 	tmp = rand() % (count / 10 + 2);
226372266baSMatthew Wilcox 	tagged = tag_tagged_items(&tree, start, end, tmp, XA_MARK_0, XA_MARK_2);
227268f42deSMatthew Wilcox 	assert(tagged == count);
2281366c37eSMatthew Wilcox 
2291366c37eSMatthew Wilcox //	printf("%lu %lu %lu\n", tagged, tmp, count);
2301366c37eSMatthew Wilcox //	printf("checking copied tags\n");
2311366c37eSMatthew Wilcox 	check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
2321366c37eSMatthew Wilcox 	verify_tag_consistency(&tree, 0);
2331366c37eSMatthew Wilcox 	verify_tag_consistency(&tree, 1);
2341366c37eSMatthew Wilcox 	verify_tag_consistency(&tree, 2);
2351366c37eSMatthew Wilcox //	printf("\n");
2361366c37eSMatthew Wilcox 	item_kill_tree(&tree);
2371366c37eSMatthew Wilcox }
2381366c37eSMatthew Wilcox 
single_thread_tests(bool long_run)239aa1d62d8SRoss Zwisler static void single_thread_tests(bool long_run)
2401366c37eSMatthew Wilcox {
2411366c37eSMatthew Wilcox 	int i;
2421366c37eSMatthew Wilcox 
24373bc029bSRehas Sachdeva 	printv(1, "starting single_thread_tests: %d allocated, preempt %d\n",
244847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2454f3755d1SMatthew Wilcox 	multiorder_checks();
246af1c5ccaSMatthew Wilcox 	rcu_barrier();
24773bc029bSRehas Sachdeva 	printv(2, "after multiorder_check: %d allocated, preempt %d\n",
248847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2491366c37eSMatthew Wilcox 	tag_check();
250af1c5ccaSMatthew Wilcox 	rcu_barrier();
25173bc029bSRehas Sachdeva 	printv(2, "after tag_check: %d allocated, preempt %d\n",
252847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2531366c37eSMatthew Wilcox 	gang_check();
254af1c5ccaSMatthew Wilcox 	rcu_barrier();
25573bc029bSRehas Sachdeva 	printv(2, "after gang_check: %d allocated, preempt %d\n",
256847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2571366c37eSMatthew Wilcox 	add_and_check();
258af1c5ccaSMatthew Wilcox 	rcu_barrier();
25973bc029bSRehas Sachdeva 	printv(2, "after add_and_check: %d allocated, preempt %d\n",
260847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2611366c37eSMatthew Wilcox 	dynamic_height_check();
262af1c5ccaSMatthew Wilcox 	rcu_barrier();
26373bc029bSRehas Sachdeva 	printv(2, "after dynamic_height_check: %d allocated, preempt %d\n",
264847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2650a835c4fSMatthew Wilcox 	idr_checks();
2668ab8ba38SMatthew Wilcox 	ida_tests();
2670a835c4fSMatthew Wilcox 	rcu_barrier();
26873bc029bSRehas Sachdeva 	printv(2, "after idr_checks: %d allocated, preempt %d\n",
2690a835c4fSMatthew Wilcox 		nr_allocated, preempt_count);
270aa1d62d8SRoss Zwisler 	big_gang_check(long_run);
271af1c5ccaSMatthew Wilcox 	rcu_barrier();
27273bc029bSRehas Sachdeva 	printv(2, "after big_gang_check: %d allocated, preempt %d\n",
273847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
274aa1d62d8SRoss Zwisler 	for (i = 0; i < (long_run ? 2000 : 3); i++) {
2751366c37eSMatthew Wilcox 		copy_tag_check();
27673bc029bSRehas Sachdeva 		printv(2, "%d ", i);
2771366c37eSMatthew Wilcox 		fflush(stdout);
2781366c37eSMatthew Wilcox 	}
279af1c5ccaSMatthew Wilcox 	rcu_barrier();
28073bc029bSRehas Sachdeva 	printv(2, "after copy_tag_check: %d allocated, preempt %d\n",
281847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
2821366c37eSMatthew Wilcox }
2831366c37eSMatthew Wilcox 
main(int argc,char ** argv)284aa1d62d8SRoss Zwisler int main(int argc, char **argv)
2851366c37eSMatthew Wilcox {
286aa1d62d8SRoss Zwisler 	bool long_run = false;
287aa1d62d8SRoss Zwisler 	int opt;
288061ef393SMatthew Wilcox 	unsigned int seed = time(NULL);
289aa1d62d8SRoss Zwisler 
29073bc029bSRehas Sachdeva 	while ((opt = getopt(argc, argv, "ls:v")) != -1) {
291aa1d62d8SRoss Zwisler 		if (opt == 'l')
292aa1d62d8SRoss Zwisler 			long_run = true;
293061ef393SMatthew Wilcox 		else if (opt == 's')
294061ef393SMatthew Wilcox 			seed = strtoul(optarg, NULL, 0);
29573bc029bSRehas Sachdeva 		else if (opt == 'v')
29673bc029bSRehas Sachdeva 			test_verbose++;
297aa1d62d8SRoss Zwisler 	}
298aa1d62d8SRoss Zwisler 
299061ef393SMatthew Wilcox 	printf("random seed %u\n", seed);
300061ef393SMatthew Wilcox 	srand(seed);
301061ef393SMatthew Wilcox 
30273bc029bSRehas Sachdeva 	printf("running tests\n");
30373bc029bSRehas Sachdeva 
3041366c37eSMatthew Wilcox 	rcu_register_thread();
3051366c37eSMatthew Wilcox 	radix_tree_init();
3061366c37eSMatthew Wilcox 
307ad3d6c72SMatthew Wilcox 	xarray_tests();
3081366c37eSMatthew Wilcox 	regression1_test();
3091366c37eSMatthew Wilcox 	regression2_test();
3102d6f45b8SKonstantin Khlebnikov 	regression3_test();
311eff3860bSMatthew Wilcox 	regression4_test();
312c0cdbf81SMatthew Wilcox 	iteration_test(0, 10 + 90 * long_run);
313c0cdbf81SMatthew Wilcox 	iteration_test(7, 10 + 90 * long_run);
314*7e934cf5SMatthew Wilcox (Oracle) 	iteration_test2(10 + 90 * long_run);
315aa1d62d8SRoss Zwisler 	single_thread_tests(long_run);
3161366c37eSMatthew Wilcox 
3176df5ee78SMatthew Wilcox 	/* Free any remaining preallocated nodes */
3186df5ee78SMatthew Wilcox 	radix_tree_cpu_dead(0);
3196df5ee78SMatthew Wilcox 
320cfa40bcfSKonstantin Khlebnikov 	benchmark();
321cfa40bcfSKonstantin Khlebnikov 
322af1c5ccaSMatthew Wilcox 	rcu_barrier();
32373bc029bSRehas Sachdeva 	printv(2, "after rcu_barrier: %d allocated, preempt %d\n",
324847d3576SMatthew Wilcox 		nr_allocated, preempt_count);
3251366c37eSMatthew Wilcox 	rcu_unregister_thread();
3261366c37eSMatthew Wilcox 
32773bc029bSRehas Sachdeva 	printf("tests completed\n");
32873bc029bSRehas Sachdeva 
3291366c37eSMatthew Wilcox 	exit(0);
3301366c37eSMatthew Wilcox }
331