main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include <limits.h>
  7. #include <linux/slab.h>
  8. #include <linux/radix-tree.h>
  9. #include "test.h"
  10. #include "regression.h"
  11. void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)
  12. {
  13. long idx;
  14. RADIX_TREE(tree, GFP_KERNEL);
  15. middle = 1 << 30;
  16. for (idx = -down; idx < up; idx++)
  17. item_insert(&tree, middle + idx);
  18. item_check_absent(&tree, middle - down - 1);
  19. for (idx = -down; idx < up; idx++)
  20. item_check_present(&tree, middle + idx);
  21. item_check_absent(&tree, middle + up);
  22. item_gang_check_present(&tree, middle - down,
  23. up + down, chunk, hop);
  24. item_full_scan(&tree, middle - down, down + up, chunk);
  25. item_kill_tree(&tree);
  26. }
  27. void gang_check(void)
  28. {
  29. __gang_check(1 << 30, 128, 128, 35, 2);
  30. __gang_check(1 << 31, 128, 128, 32, 32);
  31. __gang_check(1 << 31, 128, 128, 32, 100);
  32. __gang_check(1 << 31, 128, 128, 17, 7);
  33. __gang_check(0xffff0000, 0, 65536, 17, 7);
  34. __gang_check(0xfffffffe, 1, 1, 17, 7);
  35. }
  36. void __big_gang_check(void)
  37. {
  38. unsigned long start;
  39. int wrapped = 0;
  40. start = 0;
  41. do {
  42. unsigned long old_start;
  43. // printf("0x%08lx\n", start);
  44. __gang_check(start, rand() % 113 + 1, rand() % 71,
  45. rand() % 157, rand() % 91 + 1);
  46. old_start = start;
  47. start += rand() % 1000000;
  48. start %= 1ULL << 33;
  49. if (start < old_start)
  50. wrapped = 1;
  51. } while (!wrapped);
  52. }
  53. void big_gang_check(bool long_run)
  54. {
  55. int i;
  56. for (i = 0; i < (long_run ? 1000 : 3); i++) {
  57. __big_gang_check();
  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. tagged = tag_tagged_items(&tree, NULL, start, end, ITEMS, 0, 1);
  179. // printf("checking copied tags\n");
  180. assert(tagged == count);
  181. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
  182. /* Copy tags in several rounds */
  183. // printf("\ncopying tags...\n");
  184. tmp = rand() % (count / 10 + 2);
  185. tagged = tag_tagged_items(&tree, NULL, start, end, tmp, 0, 2);
  186. assert(tagged == count);
  187. // printf("%lu %lu %lu\n", tagged, tmp, count);
  188. // printf("checking copied tags\n");
  189. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
  190. verify_tag_consistency(&tree, 0);
  191. verify_tag_consistency(&tree, 1);
  192. verify_tag_consistency(&tree, 2);
  193. // printf("\n");
  194. item_kill_tree(&tree);
  195. }
  196. static void __locate_check(struct radix_tree_root *tree, unsigned long index,
  197. unsigned order)
  198. {
  199. struct item *item;
  200. unsigned long index2;
  201. item_insert_order(tree, index, order);
  202. item = item_lookup(tree, index);
  203. index2 = find_item(tree, item);
  204. if (index != index2) {
  205. printf("index %ld order %d inserted; found %ld\n",
  206. index, order, index2);
  207. abort();
  208. }
  209. }
  210. static void __order_0_locate_check(void)
  211. {
  212. RADIX_TREE(tree, GFP_KERNEL);
  213. int i;
  214. for (i = 0; i < 50; i++)
  215. __locate_check(&tree, rand() % INT_MAX, 0);
  216. item_kill_tree(&tree);
  217. }
  218. static void locate_check(void)
  219. {
  220. RADIX_TREE(tree, GFP_KERNEL);
  221. unsigned order;
  222. unsigned long offset, index;
  223. __order_0_locate_check();
  224. for (order = 0; order < 20; order++) {
  225. for (offset = 0; offset < (1 << (order + 3));
  226. offset += (1UL << order)) {
  227. for (index = 0; index < (1UL << (order + 5));
  228. index += (1UL << order)) {
  229. __locate_check(&tree, index + offset, order);
  230. }
  231. if (find_item(&tree, &tree) != -1)
  232. abort();
  233. item_kill_tree(&tree);
  234. }
  235. }
  236. if (find_item(&tree, &tree) != -1)
  237. abort();
  238. __locate_check(&tree, -1, 0);
  239. if (find_item(&tree, &tree) != -1)
  240. abort();
  241. item_kill_tree(&tree);
  242. }
  243. static void single_thread_tests(bool long_run)
  244. {
  245. int i;
  246. printf("starting single_thread_tests: %d allocated, preempt %d\n",
  247. nr_allocated, preempt_count);
  248. multiorder_checks();
  249. rcu_barrier();
  250. printf("after multiorder_check: %d allocated, preempt %d\n",
  251. nr_allocated, preempt_count);
  252. locate_check();
  253. rcu_barrier();
  254. printf("after locate_check: %d allocated, preempt %d\n",
  255. nr_allocated, preempt_count);
  256. tag_check();
  257. rcu_barrier();
  258. printf("after tag_check: %d allocated, preempt %d\n",
  259. nr_allocated, preempt_count);
  260. gang_check();
  261. rcu_barrier();
  262. printf("after gang_check: %d allocated, preempt %d\n",
  263. nr_allocated, preempt_count);
  264. add_and_check();
  265. rcu_barrier();
  266. printf("after add_and_check: %d allocated, preempt %d\n",
  267. nr_allocated, preempt_count);
  268. dynamic_height_check();
  269. rcu_barrier();
  270. printf("after dynamic_height_check: %d allocated, preempt %d\n",
  271. nr_allocated, preempt_count);
  272. idr_checks();
  273. ida_checks();
  274. rcu_barrier();
  275. printf("after idr_checks: %d allocated, preempt %d\n",
  276. nr_allocated, preempt_count);
  277. big_gang_check(long_run);
  278. rcu_barrier();
  279. printf("after big_gang_check: %d allocated, preempt %d\n",
  280. nr_allocated, preempt_count);
  281. for (i = 0; i < (long_run ? 2000 : 3); i++) {
  282. copy_tag_check();
  283. printf("%d ", i);
  284. fflush(stdout);
  285. }
  286. rcu_barrier();
  287. printf("after copy_tag_check: %d allocated, preempt %d\n",
  288. nr_allocated, preempt_count);
  289. }
  290. int main(int argc, char **argv)
  291. {
  292. bool long_run = false;
  293. int opt;
  294. unsigned int seed = time(NULL);
  295. while ((opt = getopt(argc, argv, "ls:")) != -1) {
  296. if (opt == 'l')
  297. long_run = true;
  298. else if (opt == 's')
  299. seed = strtoul(optarg, NULL, 0);
  300. }
  301. printf("random seed %u\n", seed);
  302. srand(seed);
  303. rcu_register_thread();
  304. radix_tree_init();
  305. regression1_test();
  306. regression2_test();
  307. regression3_test();
  308. iteration_test(0, 10);
  309. iteration_test(7, 20);
  310. single_thread_tests(long_run);
  311. /* Free any remaining preallocated nodes */
  312. radix_tree_cpu_dead(0);
  313. benchmark();
  314. rcu_barrier();
  315. printf("after rcu_barrier: %d allocated, preempt %d\n",
  316. nr_allocated, preempt_count);
  317. rcu_unregister_thread();
  318. exit(0);
  319. }