random-test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include "kerncompat.h"
  5. #include "radix-tree.h"
  6. #include "ctree.h"
  7. #include "disk-io.h"
  8. #include "print-tree.h"
  9. int keep_running = 1;
  10. static int setup_key(struct radix_tree_root *root, struct key *key, int exists)
  11. {
  12. int num = rand();
  13. unsigned long res[2];
  14. int ret;
  15. key->flags = 0;
  16. key->offset = 0;
  17. again:
  18. ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
  19. if (exists) {
  20. if (ret == 0)
  21. return -1;
  22. num = res[0];
  23. } else if (ret != 0 && num == res[0]) {
  24. num++;
  25. if (ret > 1 && num == res[1]) {
  26. num++;
  27. goto again;
  28. }
  29. }
  30. key->objectid = num;
  31. return 0;
  32. }
  33. static int ins_one(struct ctree_root *root, struct radix_tree_root *radix)
  34. {
  35. struct ctree_path path;
  36. struct key key;
  37. int ret;
  38. char buf[128];
  39. init_path(&path);
  40. ret = setup_key(radix, &key, 0);
  41. sprintf(buf, "str-%lu\n", key.objectid);
  42. ret = insert_item(root, &key, buf, strlen(buf));
  43. if (ret)
  44. goto error;
  45. radix_tree_preload(GFP_KERNEL);
  46. ret = radix_tree_insert(radix, key.objectid,
  47. (void *)key.objectid);
  48. radix_tree_preload_end();
  49. if (ret)
  50. goto error;
  51. return ret;
  52. error:
  53. printf("failed to insert %lu\n", key.objectid);
  54. return -1;
  55. }
  56. static int insert_dup(struct ctree_root *root, struct radix_tree_root *radix)
  57. {
  58. struct ctree_path path;
  59. struct key key;
  60. int ret;
  61. char buf[128];
  62. init_path(&path);
  63. ret = setup_key(radix, &key, 1);
  64. if (ret < 0)
  65. return 0;
  66. sprintf(buf, "str-%lu\n", key.objectid);
  67. ret = insert_item(root, &key, buf, strlen(buf));
  68. if (ret != -EEXIST) {
  69. printf("insert on %lu gave us %d\n", key.objectid, ret);
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
  75. {
  76. struct ctree_path path;
  77. struct key key;
  78. int ret;
  79. unsigned long *ptr;
  80. init_path(&path);
  81. ret = setup_key(radix, &key, 1);
  82. if (ret < 0)
  83. return 0;
  84. ret = search_slot(root, &key, &path, -1);
  85. if (ret)
  86. goto error;
  87. ret = del_item(root, &path);
  88. release_path(root, &path);
  89. if (ret != 0)
  90. goto error;
  91. ptr = radix_tree_delete(radix, key.objectid);
  92. if (!ptr)
  93. goto error;
  94. return 0;
  95. error:
  96. printf("failed to delete %lu\n", key.objectid);
  97. return -1;
  98. }
  99. static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
  100. {
  101. struct ctree_path path;
  102. struct key key;
  103. int ret;
  104. init_path(&path);
  105. ret = setup_key(radix, &key, 1);
  106. if (ret < 0)
  107. return 0;
  108. ret = search_slot(root, &key, &path, 0);
  109. release_path(root, &path);
  110. if (ret)
  111. goto error;
  112. return 0;
  113. error:
  114. printf("unable to find key %lu\n", key.objectid);
  115. return -1;
  116. }
  117. static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
  118. {
  119. struct ctree_path path;
  120. struct key key;
  121. int ret;
  122. init_path(&path);
  123. ret = setup_key(radix, &key, 0);
  124. if (ret < 0)
  125. return ret;
  126. ret = search_slot(root, &key, &path, 0);
  127. release_path(root, &path);
  128. if (ret == 0)
  129. goto error;
  130. return 0;
  131. error:
  132. printf("able to find key that should not exist %lu\n", key.objectid);
  133. return -1;
  134. }
  135. int (*ops[])(struct ctree_root *root, struct radix_tree_root *radix) =
  136. { ins_one, insert_dup, del_one, lookup_item, lookup_enoent };
  137. static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
  138. {
  139. struct ctree_path path;
  140. struct key key;
  141. u64 found;
  142. int ret;
  143. int slot;
  144. int i;
  145. key.offset = 0;
  146. key.flags = 0;
  147. key.objectid = (unsigned long)-1;
  148. while(1) {
  149. init_path(&path);
  150. ret = search_slot(root, &key, &path, 0);
  151. slot = path.slots[0];
  152. if (ret != 0) {
  153. if (slot == 0) {
  154. release_path(root, &path);
  155. break;
  156. }
  157. slot -= 1;
  158. }
  159. for (i = slot; i >= 0; i--) {
  160. found = path.nodes[0]->leaf.items[i].key.objectid;
  161. radix_tree_preload(GFP_KERNEL);
  162. ret = radix_tree_insert(radix, found, (void *)found);
  163. if (ret) {
  164. fprintf(stderr,
  165. "failed to insert %lu into radix\n",
  166. found);
  167. exit(1);
  168. }
  169. radix_tree_preload_end();
  170. }
  171. release_path(root, &path);
  172. key.objectid = found - 1;
  173. if (key.objectid > found)
  174. break;
  175. }
  176. return 0;
  177. }
  178. void sigstopper(int ignored)
  179. {
  180. keep_running = 0;
  181. fprintf(stderr, "caught exit signal, stopping\n");
  182. }
  183. int print_usage(void)
  184. {
  185. printf("usage: tester [-ih] [-c count] [-f count]\n");
  186. printf("\t -c count -- iteration count after filling\n");
  187. printf("\t -f count -- run this many random inserts before starting\n");
  188. printf("\t -i -- only do initial fill\n");
  189. printf("\t -h -- this help text\n");
  190. exit(1);
  191. }
  192. int main(int ac, char **av)
  193. {
  194. RADIX_TREE(radix, GFP_KERNEL);
  195. struct ctree_super_block super;
  196. struct ctree_root *root;
  197. int i;
  198. int ret;
  199. int count;
  200. int op;
  201. int iterations = 20000;
  202. int init_fill_count = 800000;
  203. int err = 0;
  204. int initial_only = 0;
  205. radix_tree_init();
  206. root = open_ctree("dbfile", &super);
  207. fill_radix(root, &radix);
  208. signal(SIGTERM, sigstopper);
  209. signal(SIGINT, sigstopper);
  210. for (i = 1 ; i < ac ; i++) {
  211. if (strcmp(av[i], "-i") == 0) {
  212. initial_only = 1;
  213. } else if (strcmp(av[i], "-c") == 0) {
  214. iterations = atoi(av[i+1]);
  215. i++;
  216. } else if (strcmp(av[i], "-f") == 0) {
  217. init_fill_count = atoi(av[i+1]);
  218. i++;
  219. } else {
  220. print_usage();
  221. }
  222. }
  223. for (i = 0; i < init_fill_count; i++) {
  224. ret = ins_one(root, &radix);
  225. if (ret) {
  226. printf("initial fill failed\n");
  227. err = ret;
  228. goto out;
  229. }
  230. if (i % 10000 == 0) {
  231. printf("initial fill %d level %d count %d\n", i,
  232. node_level(root->node->node.header.flags),
  233. root->node->node.header.nritems);
  234. }
  235. if (keep_running == 0) {
  236. err = 0;
  237. goto out;
  238. }
  239. }
  240. if (initial_only == 1) {
  241. goto out;
  242. }
  243. for (i = 0; i < iterations; i++) {
  244. op = rand() % ARRAY_SIZE(ops);
  245. count = rand() % 128;
  246. if (i % 2000 == 0) {
  247. printf("%d\n", i);
  248. fflush(stdout);
  249. }
  250. if (i && i % 5000 == 0) {
  251. printf("open & close, root level %d nritems %d\n",
  252. node_level(root->node->node.header.flags),
  253. root->node->node.header.nritems);
  254. write_ctree_super(root, &super);
  255. close_ctree(root);
  256. root = open_ctree("dbfile", &super);
  257. }
  258. while(count--) {
  259. ret = ops[op](root, &radix);
  260. if (ret) {
  261. fprintf(stderr, "op %d failed %d:%d\n",
  262. op, i, iterations);
  263. print_tree(root, root->node);
  264. fprintf(stderr, "op %d failed %d:%d\n",
  265. op, i, iterations);
  266. err = ret;
  267. goto out;
  268. }
  269. if (keep_running == 0) {
  270. err = 0;
  271. goto out;
  272. }
  273. }
  274. }
  275. out:
  276. write_ctree_super(root, &super);
  277. close_ctree(root);
  278. return err;
  279. }