main.c 8.6 KB

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