11366c37eSMatthew Wilcox #include <stdio.h> 21366c37eSMatthew Wilcox #include <stdlib.h> 31366c37eSMatthew Wilcox #include <unistd.h> 41366c37eSMatthew Wilcox #include <time.h> 51366c37eSMatthew Wilcox #include <assert.h> 61366c37eSMatthew Wilcox 71366c37eSMatthew Wilcox #include <linux/slab.h> 81366c37eSMatthew Wilcox #include <linux/radix-tree.h> 91366c37eSMatthew Wilcox 101366c37eSMatthew Wilcox #include "test.h" 111366c37eSMatthew Wilcox #include "regression.h" 121366c37eSMatthew Wilcox 131366c37eSMatthew Wilcox void __gang_check(unsigned long middle, long down, long up, int chunk, int hop) 141366c37eSMatthew Wilcox { 151366c37eSMatthew Wilcox long idx; 161366c37eSMatthew Wilcox RADIX_TREE(tree, GFP_KERNEL); 171366c37eSMatthew Wilcox 181366c37eSMatthew Wilcox middle = 1 << 30; 191366c37eSMatthew Wilcox 201366c37eSMatthew Wilcox for (idx = -down; idx < up; idx++) 211366c37eSMatthew Wilcox item_insert(&tree, middle + idx); 221366c37eSMatthew Wilcox 231366c37eSMatthew Wilcox item_check_absent(&tree, middle - down - 1); 241366c37eSMatthew Wilcox for (idx = -down; idx < up; idx++) 251366c37eSMatthew Wilcox item_check_present(&tree, middle + idx); 261366c37eSMatthew Wilcox item_check_absent(&tree, middle + up); 271366c37eSMatthew Wilcox 281366c37eSMatthew Wilcox item_gang_check_present(&tree, middle - down, 291366c37eSMatthew Wilcox up + down, chunk, hop); 301366c37eSMatthew Wilcox item_full_scan(&tree, middle - down, down + up, chunk); 311366c37eSMatthew Wilcox item_kill_tree(&tree); 321366c37eSMatthew Wilcox } 331366c37eSMatthew Wilcox 341366c37eSMatthew Wilcox void gang_check(void) 351366c37eSMatthew Wilcox { 361366c37eSMatthew Wilcox __gang_check(1 << 30, 128, 128, 35, 2); 371366c37eSMatthew Wilcox __gang_check(1 << 31, 128, 128, 32, 32); 381366c37eSMatthew Wilcox __gang_check(1 << 31, 128, 128, 32, 100); 391366c37eSMatthew Wilcox __gang_check(1 << 31, 128, 128, 17, 7); 401366c37eSMatthew Wilcox __gang_check(0xffff0000, 0, 65536, 17, 7); 411366c37eSMatthew Wilcox __gang_check(0xfffffffe, 1, 1, 17, 7); 421366c37eSMatthew Wilcox } 431366c37eSMatthew Wilcox 441366c37eSMatthew Wilcox void __big_gang_check(void) 451366c37eSMatthew Wilcox { 461366c37eSMatthew Wilcox unsigned long start; 471366c37eSMatthew Wilcox int wrapped = 0; 481366c37eSMatthew Wilcox 491366c37eSMatthew Wilcox start = 0; 501366c37eSMatthew Wilcox do { 511366c37eSMatthew Wilcox unsigned long old_start; 521366c37eSMatthew Wilcox 531366c37eSMatthew Wilcox // printf("0x%08lx\n", start); 541366c37eSMatthew Wilcox __gang_check(start, rand() % 113 + 1, rand() % 71, 551366c37eSMatthew Wilcox rand() % 157, rand() % 91 + 1); 561366c37eSMatthew Wilcox old_start = start; 571366c37eSMatthew Wilcox start += rand() % 1000000; 581366c37eSMatthew Wilcox start %= 1ULL << 33; 591366c37eSMatthew Wilcox if (start < old_start) 601366c37eSMatthew Wilcox wrapped = 1; 611366c37eSMatthew Wilcox } while (!wrapped); 621366c37eSMatthew Wilcox } 631366c37eSMatthew Wilcox 641366c37eSMatthew Wilcox void big_gang_check(void) 651366c37eSMatthew Wilcox { 661366c37eSMatthew Wilcox int i; 671366c37eSMatthew Wilcox 681366c37eSMatthew Wilcox for (i = 0; i < 1000; i++) { 691366c37eSMatthew Wilcox __big_gang_check(); 701366c37eSMatthew Wilcox srand(time(0)); 711366c37eSMatthew Wilcox printf("%d ", i); 721366c37eSMatthew Wilcox fflush(stdout); 731366c37eSMatthew Wilcox } 741366c37eSMatthew Wilcox } 751366c37eSMatthew Wilcox 761366c37eSMatthew Wilcox void add_and_check(void) 771366c37eSMatthew Wilcox { 781366c37eSMatthew Wilcox RADIX_TREE(tree, GFP_KERNEL); 791366c37eSMatthew Wilcox 801366c37eSMatthew Wilcox item_insert(&tree, 44); 811366c37eSMatthew Wilcox item_check_present(&tree, 44); 821366c37eSMatthew Wilcox item_check_absent(&tree, 43); 831366c37eSMatthew Wilcox item_kill_tree(&tree); 841366c37eSMatthew Wilcox } 851366c37eSMatthew Wilcox 861366c37eSMatthew Wilcox void dynamic_height_check(void) 871366c37eSMatthew Wilcox { 881366c37eSMatthew Wilcox int i; 891366c37eSMatthew Wilcox RADIX_TREE(tree, GFP_KERNEL); 901366c37eSMatthew Wilcox tree_verify_min_height(&tree, 0); 911366c37eSMatthew Wilcox 921366c37eSMatthew Wilcox item_insert(&tree, 42); 931366c37eSMatthew Wilcox tree_verify_min_height(&tree, 42); 941366c37eSMatthew Wilcox 951366c37eSMatthew Wilcox item_insert(&tree, 1000000); 961366c37eSMatthew Wilcox tree_verify_min_height(&tree, 1000000); 971366c37eSMatthew Wilcox 981366c37eSMatthew Wilcox assert(item_delete(&tree, 1000000)); 991366c37eSMatthew Wilcox tree_verify_min_height(&tree, 42); 1001366c37eSMatthew Wilcox 1011366c37eSMatthew Wilcox assert(item_delete(&tree, 42)); 1021366c37eSMatthew Wilcox tree_verify_min_height(&tree, 0); 1031366c37eSMatthew Wilcox 1041366c37eSMatthew Wilcox for (i = 0; i < 1000; i++) { 1051366c37eSMatthew Wilcox item_insert(&tree, i); 1061366c37eSMatthew Wilcox tree_verify_min_height(&tree, i); 1071366c37eSMatthew Wilcox } 1081366c37eSMatthew Wilcox 1091366c37eSMatthew Wilcox i--; 1101366c37eSMatthew Wilcox for (;;) { 1111366c37eSMatthew Wilcox assert(item_delete(&tree, i)); 1121366c37eSMatthew Wilcox if (i == 0) { 1131366c37eSMatthew Wilcox tree_verify_min_height(&tree, 0); 1141366c37eSMatthew Wilcox break; 1151366c37eSMatthew Wilcox } 1161366c37eSMatthew Wilcox i--; 1171366c37eSMatthew Wilcox tree_verify_min_height(&tree, i); 1181366c37eSMatthew Wilcox } 1191366c37eSMatthew Wilcox 1201366c37eSMatthew Wilcox item_kill_tree(&tree); 1211366c37eSMatthew Wilcox } 1221366c37eSMatthew Wilcox 1231366c37eSMatthew 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) 1241366c37eSMatthew Wilcox { 1251366c37eSMatthew Wilcox int i; 1261366c37eSMatthew Wilcox 1271366c37eSMatthew Wilcox for (i = 0; i < count; i++) { 1281366c37eSMatthew Wilcox /* if (i % 1000 == 0) 1291366c37eSMatthew Wilcox putchar('.'); */ 1301366c37eSMatthew Wilcox if (idx[i] < start || idx[i] > end) { 1311366c37eSMatthew Wilcox if (item_tag_get(tree, idx[i], totag)) { 1321366c37eSMatthew Wilcox 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)); 1331366c37eSMatthew Wilcox } 1341366c37eSMatthew Wilcox assert(!item_tag_get(tree, idx[i], totag)); 1351366c37eSMatthew Wilcox continue; 1361366c37eSMatthew Wilcox } 1371366c37eSMatthew Wilcox if (item_tag_get(tree, idx[i], fromtag) ^ 1381366c37eSMatthew Wilcox item_tag_get(tree, idx[i], totag)) { 1391366c37eSMatthew Wilcox 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)); 1401366c37eSMatthew Wilcox } 1411366c37eSMatthew Wilcox assert(!(item_tag_get(tree, idx[i], fromtag) ^ 1421366c37eSMatthew Wilcox item_tag_get(tree, idx[i], totag))); 1431366c37eSMatthew Wilcox } 1441366c37eSMatthew Wilcox } 1451366c37eSMatthew Wilcox 1461366c37eSMatthew Wilcox #define ITEMS 50000 1471366c37eSMatthew Wilcox 1481366c37eSMatthew Wilcox void copy_tag_check(void) 1491366c37eSMatthew Wilcox { 1501366c37eSMatthew Wilcox RADIX_TREE(tree, GFP_KERNEL); 1511366c37eSMatthew Wilcox unsigned long idx[ITEMS]; 1521366c37eSMatthew Wilcox unsigned long start, end, count = 0, tagged, cur, tmp; 1531366c37eSMatthew Wilcox int i; 1541366c37eSMatthew Wilcox 1551366c37eSMatthew Wilcox // printf("generating radix tree indices...\n"); 1561366c37eSMatthew Wilcox start = rand(); 1571366c37eSMatthew Wilcox end = rand(); 1581366c37eSMatthew Wilcox if (start > end && (rand() % 10)) { 1591366c37eSMatthew Wilcox cur = start; 1601366c37eSMatthew Wilcox start = end; 1611366c37eSMatthew Wilcox end = cur; 1621366c37eSMatthew Wilcox } 1631366c37eSMatthew Wilcox /* Specifically create items around the start and the end of the range 1641366c37eSMatthew Wilcox * with high probability to check for off by one errors */ 1651366c37eSMatthew Wilcox cur = rand(); 1661366c37eSMatthew Wilcox if (cur & 1) { 1671366c37eSMatthew Wilcox item_insert(&tree, start); 1681366c37eSMatthew Wilcox if (cur & 2) { 1691366c37eSMatthew Wilcox if (start <= end) 1701366c37eSMatthew Wilcox count++; 1711366c37eSMatthew Wilcox item_tag_set(&tree, start, 0); 1721366c37eSMatthew Wilcox } 1731366c37eSMatthew Wilcox } 1741366c37eSMatthew Wilcox if (cur & 4) { 1751366c37eSMatthew Wilcox item_insert(&tree, start-1); 1761366c37eSMatthew Wilcox if (cur & 8) 1771366c37eSMatthew Wilcox item_tag_set(&tree, start-1, 0); 1781366c37eSMatthew Wilcox } 1791366c37eSMatthew Wilcox if (cur & 16) { 1801366c37eSMatthew Wilcox item_insert(&tree, end); 1811366c37eSMatthew Wilcox if (cur & 32) { 1821366c37eSMatthew Wilcox if (start <= end) 1831366c37eSMatthew Wilcox count++; 1841366c37eSMatthew Wilcox item_tag_set(&tree, end, 0); 1851366c37eSMatthew Wilcox } 1861366c37eSMatthew Wilcox } 1871366c37eSMatthew Wilcox if (cur & 64) { 1881366c37eSMatthew Wilcox item_insert(&tree, end+1); 1891366c37eSMatthew Wilcox if (cur & 128) 1901366c37eSMatthew Wilcox item_tag_set(&tree, end+1, 0); 1911366c37eSMatthew Wilcox } 1921366c37eSMatthew Wilcox 1931366c37eSMatthew Wilcox for (i = 0; i < ITEMS; i++) { 1941366c37eSMatthew Wilcox do { 1951366c37eSMatthew Wilcox idx[i] = rand(); 1961366c37eSMatthew Wilcox } while (item_lookup(&tree, idx[i])); 1971366c37eSMatthew Wilcox 1981366c37eSMatthew Wilcox item_insert(&tree, idx[i]); 1991366c37eSMatthew Wilcox if (rand() & 1) { 2001366c37eSMatthew Wilcox item_tag_set(&tree, idx[i], 0); 2011366c37eSMatthew Wilcox if (idx[i] >= start && idx[i] <= end) 2021366c37eSMatthew Wilcox count++; 2031366c37eSMatthew Wilcox } 2041366c37eSMatthew Wilcox /* if (i % 1000 == 0) 2051366c37eSMatthew Wilcox putchar('.'); */ 2061366c37eSMatthew Wilcox } 2071366c37eSMatthew Wilcox 2081366c37eSMatthew Wilcox // printf("\ncopying tags...\n"); 2091366c37eSMatthew Wilcox cur = start; 2101366c37eSMatthew Wilcox tagged = radix_tree_range_tag_if_tagged(&tree, &cur, end, ITEMS, 0, 1); 2111366c37eSMatthew Wilcox 2121366c37eSMatthew Wilcox // printf("checking copied tags\n"); 2131366c37eSMatthew Wilcox assert(tagged == count); 2141366c37eSMatthew Wilcox check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1); 2151366c37eSMatthew Wilcox 2161366c37eSMatthew Wilcox /* Copy tags in several rounds */ 2171366c37eSMatthew Wilcox // printf("\ncopying tags...\n"); 2181366c37eSMatthew Wilcox cur = start; 2191366c37eSMatthew Wilcox do { 2201366c37eSMatthew Wilcox tmp = rand() % (count/10+2); 2211366c37eSMatthew Wilcox tagged = radix_tree_range_tag_if_tagged(&tree, &cur, end, tmp, 0, 2); 2221366c37eSMatthew Wilcox } while (tmp == tagged); 2231366c37eSMatthew Wilcox 2241366c37eSMatthew Wilcox // printf("%lu %lu %lu\n", tagged, tmp, count); 2251366c37eSMatthew Wilcox // printf("checking copied tags\n"); 2261366c37eSMatthew Wilcox check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2); 2271366c37eSMatthew Wilcox assert(tagged < tmp); 2281366c37eSMatthew Wilcox verify_tag_consistency(&tree, 0); 2291366c37eSMatthew Wilcox verify_tag_consistency(&tree, 1); 2301366c37eSMatthew Wilcox verify_tag_consistency(&tree, 2); 2311366c37eSMatthew Wilcox // printf("\n"); 2321366c37eSMatthew Wilcox item_kill_tree(&tree); 2331366c37eSMatthew Wilcox } 2341366c37eSMatthew Wilcox 2351366c37eSMatthew Wilcox static void single_thread_tests(void) 2361366c37eSMatthew Wilcox { 2371366c37eSMatthew Wilcox int i; 2381366c37eSMatthew Wilcox 2391366c37eSMatthew Wilcox tag_check(); 2401366c37eSMatthew Wilcox printf("after tag_check: %d allocated\n", nr_allocated); 2411366c37eSMatthew Wilcox gang_check(); 2421366c37eSMatthew Wilcox printf("after gang_check: %d allocated\n", nr_allocated); 2431366c37eSMatthew Wilcox add_and_check(); 2441366c37eSMatthew Wilcox printf("after add_and_check: %d allocated\n", nr_allocated); 2451366c37eSMatthew Wilcox dynamic_height_check(); 2461366c37eSMatthew Wilcox printf("after dynamic_height_check: %d allocated\n", nr_allocated); 2471366c37eSMatthew Wilcox big_gang_check(); 2481366c37eSMatthew Wilcox printf("after big_gang_check: %d allocated\n", nr_allocated); 2491366c37eSMatthew Wilcox for (i = 0; i < 2000; i++) { 2501366c37eSMatthew Wilcox copy_tag_check(); 2511366c37eSMatthew Wilcox printf("%d ", i); 2521366c37eSMatthew Wilcox fflush(stdout); 2531366c37eSMatthew Wilcox } 2541366c37eSMatthew Wilcox printf("after copy_tag_check: %d allocated\n", nr_allocated); 2551366c37eSMatthew Wilcox } 2561366c37eSMatthew Wilcox 2571366c37eSMatthew Wilcox int main(void) 2581366c37eSMatthew Wilcox { 2591366c37eSMatthew Wilcox rcu_register_thread(); 2601366c37eSMatthew Wilcox radix_tree_init(); 2611366c37eSMatthew Wilcox 2621366c37eSMatthew Wilcox regression1_test(); 2631366c37eSMatthew Wilcox regression2_test(); 264*2d6f45b8SKonstantin Khlebnikov regression3_test(); 2651366c37eSMatthew Wilcox single_thread_tests(); 2661366c37eSMatthew Wilcox 2671366c37eSMatthew Wilcox sleep(1); 2681366c37eSMatthew Wilcox printf("after sleep(1): %d allocated\n", nr_allocated); 2691366c37eSMatthew Wilcox rcu_unregister_thread(); 2701366c37eSMatthew Wilcox 2711366c37eSMatthew Wilcox exit(0); 2721366c37eSMatthew Wilcox } 273