main.c 7.4 KB

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