main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. printv(2, "%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. printv(2, "%lu-%lu: %lu, tags %d-%d\n", start,
  108. end, idx[i], item_tag_get(tree, idx[i],
  109. fromtag),
  110. item_tag_get(tree, idx[i], totag));
  111. }
  112. assert(!item_tag_get(tree, idx[i], totag));
  113. continue;
  114. }
  115. if (item_tag_get(tree, idx[i], fromtag) ^
  116. item_tag_get(tree, idx[i], totag)) {
  117. printv(2, "%lu-%lu: %lu, tags %d-%d\n", start, end,
  118. idx[i], item_tag_get(tree, idx[i], fromtag),
  119. item_tag_get(tree, idx[i], totag));
  120. }
  121. assert(!(item_tag_get(tree, idx[i], fromtag) ^
  122. item_tag_get(tree, idx[i], totag)));
  123. }
  124. }
  125. #define ITEMS 50000
  126. void copy_tag_check(void)
  127. {
  128. RADIX_TREE(tree, GFP_KERNEL);
  129. unsigned long idx[ITEMS];
  130. unsigned long start, end, count = 0, tagged, cur, tmp;
  131. int i;
  132. // printf("generating radix tree indices...\n");
  133. start = rand();
  134. end = rand();
  135. if (start > end && (rand() % 10)) {
  136. cur = start;
  137. start = end;
  138. end = cur;
  139. }
  140. /* Specifically create items around the start and the end of the range
  141. * with high probability to check for off by one errors */
  142. cur = rand();
  143. if (cur & 1) {
  144. item_insert(&tree, start);
  145. if (cur & 2) {
  146. if (start <= end)
  147. count++;
  148. item_tag_set(&tree, start, 0);
  149. }
  150. }
  151. if (cur & 4) {
  152. item_insert(&tree, start-1);
  153. if (cur & 8)
  154. item_tag_set(&tree, start-1, 0);
  155. }
  156. if (cur & 16) {
  157. item_insert(&tree, end);
  158. if (cur & 32) {
  159. if (start <= end)
  160. count++;
  161. item_tag_set(&tree, end, 0);
  162. }
  163. }
  164. if (cur & 64) {
  165. item_insert(&tree, end+1);
  166. if (cur & 128)
  167. item_tag_set(&tree, end+1, 0);
  168. }
  169. for (i = 0; i < ITEMS; i++) {
  170. do {
  171. idx[i] = rand();
  172. } while (item_lookup(&tree, idx[i]));
  173. item_insert(&tree, idx[i]);
  174. if (rand() & 1) {
  175. item_tag_set(&tree, idx[i], 0);
  176. if (idx[i] >= start && idx[i] <= end)
  177. count++;
  178. }
  179. /* if (i % 1000 == 0)
  180. putchar('.'); */
  181. }
  182. // printf("\ncopying tags...\n");
  183. tagged = tag_tagged_items(&tree, NULL, start, end, ITEMS, 0, 1);
  184. // printf("checking copied tags\n");
  185. assert(tagged == count);
  186. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
  187. /* Copy tags in several rounds */
  188. // printf("\ncopying tags...\n");
  189. tmp = rand() % (count / 10 + 2);
  190. tagged = tag_tagged_items(&tree, NULL, start, end, tmp, 0, 2);
  191. assert(tagged == count);
  192. // printf("%lu %lu %lu\n", tagged, tmp, count);
  193. // printf("checking copied tags\n");
  194. check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
  195. verify_tag_consistency(&tree, 0);
  196. verify_tag_consistency(&tree, 1);
  197. verify_tag_consistency(&tree, 2);
  198. // printf("\n");
  199. item_kill_tree(&tree);
  200. }
  201. static void __locate_check(struct radix_tree_root *tree, unsigned long index,
  202. unsigned order)
  203. {
  204. struct item *item;
  205. unsigned long index2;
  206. item_insert_order(tree, index, order);
  207. item = item_lookup(tree, index);
  208. index2 = find_item(tree, item);
  209. if (index != index2) {
  210. printv(2, "index %ld order %d inserted; found %ld\n",
  211. index, order, index2);
  212. abort();
  213. }
  214. }
  215. static void __order_0_locate_check(void)
  216. {
  217. RADIX_TREE(tree, GFP_KERNEL);
  218. int i;
  219. for (i = 0; i < 50; i++)
  220. __locate_check(&tree, rand() % INT_MAX, 0);
  221. item_kill_tree(&tree);
  222. }
  223. static void locate_check(void)
  224. {
  225. RADIX_TREE(tree, GFP_KERNEL);
  226. unsigned order;
  227. unsigned long offset, index;
  228. __order_0_locate_check();
  229. for (order = 0; order < 20; order++) {
  230. for (offset = 0; offset < (1 << (order + 3));
  231. offset += (1UL << order)) {
  232. for (index = 0; index < (1UL << (order + 5));
  233. index += (1UL << order)) {
  234. __locate_check(&tree, index + offset, order);
  235. }
  236. if (find_item(&tree, &tree) != -1)
  237. abort();
  238. item_kill_tree(&tree);
  239. }
  240. }
  241. if (find_item(&tree, &tree) != -1)
  242. abort();
  243. __locate_check(&tree, -1, 0);
  244. if (find_item(&tree, &tree) != -1)
  245. abort();
  246. item_kill_tree(&tree);
  247. }
  248. static void single_thread_tests(bool long_run)
  249. {
  250. int i;
  251. printv(1, "starting single_thread_tests: %d allocated, preempt %d\n",
  252. nr_allocated, preempt_count);
  253. multiorder_checks();
  254. rcu_barrier();
  255. printv(2, "after multiorder_check: %d allocated, preempt %d\n",
  256. nr_allocated, preempt_count);
  257. locate_check();
  258. rcu_barrier();
  259. printv(2, "after locate_check: %d allocated, preempt %d\n",
  260. nr_allocated, preempt_count);
  261. tag_check();
  262. rcu_barrier();
  263. printv(2, "after tag_check: %d allocated, preempt %d\n",
  264. nr_allocated, preempt_count);
  265. gang_check();
  266. rcu_barrier();
  267. printv(2, "after gang_check: %d allocated, preempt %d\n",
  268. nr_allocated, preempt_count);
  269. add_and_check();
  270. rcu_barrier();
  271. printv(2, "after add_and_check: %d allocated, preempt %d\n",
  272. nr_allocated, preempt_count);
  273. dynamic_height_check();
  274. rcu_barrier();
  275. printv(2, "after dynamic_height_check: %d allocated, preempt %d\n",
  276. nr_allocated, preempt_count);
  277. idr_checks();
  278. ida_checks();
  279. rcu_barrier();
  280. printv(2, "after idr_checks: %d allocated, preempt %d\n",
  281. nr_allocated, preempt_count);
  282. big_gang_check(long_run);
  283. rcu_barrier();
  284. printv(2, "after big_gang_check: %d allocated, preempt %d\n",
  285. nr_allocated, preempt_count);
  286. for (i = 0; i < (long_run ? 2000 : 3); i++) {
  287. copy_tag_check();
  288. printv(2, "%d ", i);
  289. fflush(stdout);
  290. }
  291. rcu_barrier();
  292. printv(2, "after copy_tag_check: %d allocated, preempt %d\n",
  293. nr_allocated, preempt_count);
  294. }
  295. int main(int argc, char **argv)
  296. {
  297. bool long_run = false;
  298. int opt;
  299. unsigned int seed = time(NULL);
  300. while ((opt = getopt(argc, argv, "ls:v")) != -1) {
  301. if (opt == 'l')
  302. long_run = true;
  303. else if (opt == 's')
  304. seed = strtoul(optarg, NULL, 0);
  305. else if (opt == 'v')
  306. test_verbose++;
  307. }
  308. printf("random seed %u\n", seed);
  309. srand(seed);
  310. printf("running tests\n");
  311. rcu_register_thread();
  312. radix_tree_init();
  313. regression1_test();
  314. regression2_test();
  315. regression3_test();
  316. iteration_test(0, 10 + 90 * long_run);
  317. iteration_test(7, 10 + 90 * long_run);
  318. single_thread_tests(long_run);
  319. ida_thread_tests();
  320. /* Free any remaining preallocated nodes */
  321. radix_tree_cpu_dead(0);
  322. benchmark();
  323. rcu_barrier();
  324. printv(2, "after rcu_barrier: %d allocated, preempt %d\n",
  325. nr_allocated, preempt_count);
  326. rcu_unregister_thread();
  327. printf("tests completed\n");
  328. exit(0);
  329. }