main.c 8.1 KB

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