main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include <linux/slab.h>
  7. #include <linux/radix-tree.h>
  8. #include "test.h"
  9. #include "regression.h"
  10. void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)
  11. {
  12. long idx;
  13. RADIX_TREE(tree, GFP_KERNEL);
  14. middle = 1 << 30;
  15. for (idx = -down; idx < up; idx++)
  16. item_insert(&tree, middle + idx);
  17. item_check_absent(&tree, middle - down - 1);
  18. for (idx = -down; idx < up; idx++)
  19. item_check_present(&tree, middle + idx);
  20. item_check_absent(&tree, middle + up);
  21. item_gang_check_present(&tree, middle - down,
  22. up + down, chunk, hop);
  23. item_full_scan(&tree, middle - down, down + up, chunk);
  24. item_kill_tree(&tree);
  25. }
  26. void gang_check(void)
  27. {
  28. __gang_check(1 << 30, 128, 128, 35, 2);
  29. __gang_check(1 << 31, 128, 128, 32, 32);
  30. __gang_check(1 << 31, 128, 128, 32, 100);
  31. __gang_check(1 << 31, 128, 128, 17, 7);
  32. __gang_check(0xffff0000, 0, 65536, 17, 7);
  33. __gang_check(0xfffffffe, 1, 1, 17, 7);
  34. }
  35. void __big_gang_check(void)
  36. {
  37. unsigned long start;
  38. int wrapped = 0;
  39. start = 0;
  40. do {
  41. unsigned long old_start;
  42. // printf("0x%08lx\n", start);
  43. __gang_check(start, rand() % 113 + 1, rand() % 71,
  44. rand() % 157, rand() % 91 + 1);
  45. old_start = start;
  46. start += rand() % 1000000;
  47. start %= 1ULL << 33;
  48. if (start < old_start)
  49. wrapped = 1;
  50. } while (!wrapped);
  51. }
  52. void big_gang_check(bool long_run)
  53. {
  54. int i;
  55. for (i = 0; i < (long_run ? 1000 : 3); i++) {
  56. __big_gang_check();
  57. srand(time(0));
  58. printf("%d ", i);
  59. fflush(stdout);
  60. }
  61. }
  62. void add_and_check(void)
  63. {
  64. RADIX_TREE(tree, GFP_KERNEL);
  65. item_insert(&tree, 44);
  66. item_check_present(&tree, 44);
  67. item_check_absent(&tree, 43);
  68. item_kill_tree(&tree);
  69. }
  70. void dynamic_height_check(void)
  71. {
  72. int i;
  73. RADIX_TREE(tree, GFP_KERNEL);
  74. tree_verify_min_height(&tree, 0);
  75. item_insert(&tree, 42);
  76. tree_verify_min_height(&tree, 42);
  77. item_insert(&tree, 1000000);
  78. tree_verify_min_height(&tree, 1000000);
  79. assert(item_delete(&tree, 1000000));
  80. tree_verify_min_height(&tree, 42);
  81. assert(item_delete(&tree, 42));
  82. tree_verify_min_height(&tree, 0);
  83. for (i = 0; i < 1000; i++) {
  84. item_insert(&tree, i);
  85. tree_verify_min_height(&tree, i);
  86. }
  87. i--;
  88. for (;;) {
  89. assert(item_delete(&tree, i));
  90. if (i == 0) {
  91. tree_verify_min_height(&tree, 0);
  92. break;
  93. }
  94. i--;
  95. tree_verify_min_height(&tree, i);
  96. }
  97. item_kill_tree(&tree);
  98. }
  99. void check_copied_tags(struct radix_tree_root *tree, unsigned long start, unsigned long end, unsigned long *idx, int count, int fromtag, int totag)
  100. {
  101. int i;
  102. for (i = 0; i < count; i++) {
  103. /* if (i % 1000 == 0)
  104. putchar('.'); */
  105. if (idx[i] < start || idx[i] > end) {
  106. if (item_tag_get(tree, idx[i], totag)) {
  107. 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));
  108. }
  109. assert(!item_tag_get(tree, idx[i], totag));
  110. continue;
  111. }
  112. if (item_tag_get(tree, idx[i], fromtag) ^
  113. item_tag_get(tree, idx[i], totag)) {
  114. 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));
  115. }
  116. assert(!(item_tag_get(tree, idx[i], fromtag) ^
  117. item_tag_get(tree, idx[i], totag)));
  118. }
  119. }
  120. #define ITEMS 50000
  121. void copy_tag_check(void)
  122. {
  123. RADIX_TREE(tree, GFP_KERNEL);
  124. unsigned long idx[ITEMS];
  125. unsigned long start, end, count = 0, tagged, cur, tmp;
  126. int i;
  127. // printf("generating radix tree indices...\n");
  128. start = rand();
  129. end = rand();
  130. if (start > end && (rand() % 10)) {
  131. cur = start;
  132. start = end;
  133. end = cur;
  134. }
  135. /* Specifically create items around the start and the end of the range
  136. * with high probability to check for off by one errors */
  137. cur = rand();
  138. if (cur & 1) {
  139. item_insert(&tree, start);
  140. if (cur & 2) {
  141. if (start <= end)
  142. count++;
  143. item_tag_set(&tree, start, 0);
  144. }
  145. }
  146. if (cur & 4) {
  147. item_insert(&tree, start-1);
  148. if (cur & 8)
  149. item_tag_set(&tree, start-1, 0);
  150. }
  151. if (cur & 16) {
  152. item_insert(&tree, end);
  153. if (cur & 32) {
  154. if (start <= end)
  155. count++;
  156. item_tag_set(&tree, end, 0);
  157. }
  158. }
  159. if (cur & 64) {
  160. item_insert(&tree, end+1);
  161. if (cur & 128)
  162. item_tag_set(&tree, end+1, 0);
  163. }
  164. for (i = 0; i < ITEMS; i++) {
  165. do {
  166. idx[i] = rand();
  167. } while (item_lookup(&tree, idx[i]));
  168. item_insert(&tree, idx[i]);
  169. if (rand() & 1) {
  170. item_tag_set(&tree, idx[i], 0);
  171. if (idx[i] >= start && idx[i] <= end)
  172. count++;
  173. }
  174. /* if (i % 1000 == 0)
  175. putchar('.'); */
  176. }
  177. // printf("\ncopying tags...\n");
  178. cur = start;
  179. tagged = radix_tree_range_tag_if_tagged(&tree, &cur, end, ITEMS, 0, 1);
  180. // printf("checking copied tags\n");
  181. assert(tagged == count);
  182. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
  183. /* Copy tags in several rounds */
  184. // printf("\ncopying tags...\n");
  185. cur = start;
  186. do {
  187. tmp = rand() % (count/10+2);
  188. tagged = radix_tree_range_tag_if_tagged(&tree, &cur, end, tmp, 0, 2);
  189. } while (tmp == tagged);
  190. // printf("%lu %lu %lu\n", tagged, tmp, count);
  191. // printf("checking copied tags\n");
  192. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
  193. assert(tagged < tmp);
  194. verify_tag_consistency(&tree, 0);
  195. verify_tag_consistency(&tree, 1);
  196. verify_tag_consistency(&tree, 2);
  197. // printf("\n");
  198. item_kill_tree(&tree);
  199. }
  200. static void __locate_check(struct radix_tree_root *tree, unsigned long index,
  201. unsigned order)
  202. {
  203. struct item *item;
  204. unsigned long index2;
  205. item_insert_order(tree, index, order);
  206. item = item_lookup(tree, index);
  207. index2 = radix_tree_locate_item(tree, item);
  208. if (index != index2) {
  209. printf("index %ld order %d inserted; found %ld\n",
  210. index, order, index2);
  211. abort();
  212. }
  213. }
  214. static void __order_0_locate_check(void)
  215. {
  216. RADIX_TREE(tree, GFP_KERNEL);
  217. int i;
  218. for (i = 0; i < 50; i++)
  219. __locate_check(&tree, rand() % INT_MAX, 0);
  220. item_kill_tree(&tree);
  221. }
  222. static void locate_check(void)
  223. {
  224. RADIX_TREE(tree, GFP_KERNEL);
  225. unsigned order;
  226. unsigned long offset, index;
  227. __order_0_locate_check();
  228. for (order = 0; order < 20; order++) {
  229. for (offset = 0; offset < (1 << (order + 3));
  230. offset += (1UL << order)) {
  231. for (index = 0; index < (1UL << (order + 5));
  232. index += (1UL << order)) {
  233. __locate_check(&tree, index + offset, order);
  234. }
  235. if (radix_tree_locate_item(&tree, &tree) != -1)
  236. abort();
  237. item_kill_tree(&tree);
  238. }
  239. }
  240. if (radix_tree_locate_item(&tree, &tree) != -1)
  241. abort();
  242. __locate_check(&tree, -1, 0);
  243. if (radix_tree_locate_item(&tree, &tree) != -1)
  244. abort();
  245. item_kill_tree(&tree);
  246. }
  247. static void single_thread_tests(bool long_run)
  248. {
  249. int i;
  250. printf("starting single_thread_tests: %d allocated\n", nr_allocated);
  251. multiorder_checks();
  252. printf("after multiorder_check: %d allocated\n", nr_allocated);
  253. locate_check();
  254. printf("after locate_check: %d allocated\n", nr_allocated);
  255. tag_check();
  256. printf("after tag_check: %d allocated\n", nr_allocated);
  257. gang_check();
  258. printf("after gang_check: %d allocated\n", nr_allocated);
  259. add_and_check();
  260. printf("after add_and_check: %d allocated\n", nr_allocated);
  261. dynamic_height_check();
  262. printf("after dynamic_height_check: %d allocated\n", nr_allocated);
  263. big_gang_check(long_run);
  264. printf("after big_gang_check: %d allocated\n", nr_allocated);
  265. for (i = 0; i < (long_run ? 2000 : 3); i++) {
  266. copy_tag_check();
  267. printf("%d ", i);
  268. fflush(stdout);
  269. }
  270. printf("after copy_tag_check: %d allocated\n", nr_allocated);
  271. }
  272. int main(int argc, char **argv)
  273. {
  274. bool long_run = false;
  275. int opt;
  276. while ((opt = getopt(argc, argv, "l")) != -1) {
  277. if (opt == 'l')
  278. long_run = true;
  279. }
  280. rcu_register_thread();
  281. radix_tree_init();
  282. regression1_test();
  283. regression2_test();
  284. regression3_test();
  285. iteration_test();
  286. single_thread_tests(long_run);
  287. sleep(1);
  288. printf("after sleep(1): %d allocated\n", nr_allocated);
  289. rcu_unregister_thread();
  290. exit(0);
  291. }