main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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, NULL, start, end, ITEMS, 0, 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, NULL, start, end, tmp, 0, 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 __locate_check(struct radix_tree_root *tree, unsigned long index,
  205. unsigned order)
  206. {
  207. struct item *item;
  208. unsigned long index2;
  209. item_insert_order(tree, index, order);
  210. item = item_lookup(tree, index);
  211. index2 = find_item(tree, item);
  212. if (index != index2) {
  213. printv(2, "index %ld order %d inserted; found %ld\n",
  214. index, order, index2);
  215. abort();
  216. }
  217. }
  218. static void __order_0_locate_check(void)
  219. {
  220. RADIX_TREE(tree, GFP_KERNEL);
  221. int i;
  222. for (i = 0; i < 50; i++)
  223. __locate_check(&tree, rand() % INT_MAX, 0);
  224. item_kill_tree(&tree);
  225. }
  226. static void locate_check(void)
  227. {
  228. RADIX_TREE(tree, GFP_KERNEL);
  229. unsigned order;
  230. unsigned long offset, index;
  231. __order_0_locate_check();
  232. for (order = 0; order < 20; order++) {
  233. for (offset = 0; offset < (1 << (order + 3));
  234. offset += (1UL << order)) {
  235. for (index = 0; index < (1UL << (order + 5));
  236. index += (1UL << order)) {
  237. __locate_check(&tree, index + offset, order);
  238. }
  239. if (find_item(&tree, &tree) != -1)
  240. abort();
  241. item_kill_tree(&tree);
  242. }
  243. }
  244. if (find_item(&tree, &tree) != -1)
  245. abort();
  246. __locate_check(&tree, -1, 0);
  247. if (find_item(&tree, &tree) != -1)
  248. abort();
  249. item_kill_tree(&tree);
  250. }
  251. static void single_thread_tests(bool long_run)
  252. {
  253. int i;
  254. printv(1, "starting single_thread_tests: %d allocated, preempt %d\n",
  255. nr_allocated, preempt_count);
  256. multiorder_checks();
  257. rcu_barrier();
  258. printv(2, "after multiorder_check: %d allocated, preempt %d\n",
  259. nr_allocated, preempt_count);
  260. locate_check();
  261. rcu_barrier();
  262. printv(2, "after locate_check: %d allocated, preempt %d\n",
  263. nr_allocated, preempt_count);
  264. tag_check();
  265. rcu_barrier();
  266. printv(2, "after tag_check: %d allocated, preempt %d\n",
  267. nr_allocated, preempt_count);
  268. gang_check();
  269. rcu_barrier();
  270. printv(2, "after gang_check: %d allocated, preempt %d\n",
  271. nr_allocated, preempt_count);
  272. add_and_check();
  273. rcu_barrier();
  274. printv(2, "after add_and_check: %d allocated, preempt %d\n",
  275. nr_allocated, preempt_count);
  276. dynamic_height_check();
  277. rcu_barrier();
  278. printv(2, "after dynamic_height_check: %d allocated, preempt %d\n",
  279. nr_allocated, preempt_count);
  280. idr_checks();
  281. ida_tests();
  282. rcu_barrier();
  283. printv(2, "after idr_checks: %d allocated, preempt %d\n",
  284. nr_allocated, preempt_count);
  285. big_gang_check(long_run);
  286. rcu_barrier();
  287. printv(2, "after big_gang_check: %d allocated, preempt %d\n",
  288. nr_allocated, preempt_count);
  289. for (i = 0; i < (long_run ? 2000 : 3); i++) {
  290. copy_tag_check();
  291. printv(2, "%d ", i);
  292. fflush(stdout);
  293. }
  294. rcu_barrier();
  295. printv(2, "after copy_tag_check: %d allocated, preempt %d\n",
  296. nr_allocated, preempt_count);
  297. }
  298. int main(int argc, char **argv)
  299. {
  300. bool long_run = false;
  301. int opt;
  302. unsigned int seed = time(NULL);
  303. while ((opt = getopt(argc, argv, "ls:v")) != -1) {
  304. if (opt == 'l')
  305. long_run = true;
  306. else if (opt == 's')
  307. seed = strtoul(optarg, NULL, 0);
  308. else if (opt == 'v')
  309. test_verbose++;
  310. }
  311. printf("random seed %u\n", seed);
  312. srand(seed);
  313. printf("running tests\n");
  314. rcu_register_thread();
  315. radix_tree_init();
  316. regression1_test();
  317. regression2_test();
  318. regression3_test();
  319. iteration_test(0, 10 + 90 * long_run);
  320. iteration_test(7, 10 + 90 * long_run);
  321. single_thread_tests(long_run);
  322. /* Free any remaining preallocated nodes */
  323. radix_tree_cpu_dead(0);
  324. benchmark();
  325. rcu_barrier();
  326. printv(2, "after rcu_barrier: %d allocated, preempt %d\n",
  327. nr_allocated, preempt_count);
  328. rcu_unregister_thread();
  329. printf("tests completed\n");
  330. exit(0);
  331. }