random-test.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. unsigned long oid;
  40. init_path(&path);
  41. ret = setup_key(radix, &key, 0);
  42. sprintf(buf, "str-%Lu\n", key.objectid);
  43. ret = insert_item(root, &key, buf, strlen(buf));
  44. if (ret)
  45. goto error;
  46. oid = (unsigned long)key.objectid;
  47. radix_tree_preload(GFP_KERNEL);
  48. ret = radix_tree_insert(radix, oid, (void *)oid);
  49. radix_tree_preload_end();
  50. if (ret)
  51. goto error;
  52. return ret;
  53. error:
  54. printf("failed to insert %Lu\n", key.objectid);
  55. return -1;
  56. }
  57. static int insert_dup(struct ctree_root *root, struct radix_tree_root *radix)
  58. {
  59. struct ctree_path path;
  60. struct key key;
  61. int ret;
  62. char buf[128];
  63. init_path(&path);
  64. ret = setup_key(radix, &key, 1);
  65. if (ret < 0)
  66. return 0;
  67. sprintf(buf, "str-%Lu\n", key.objectid);
  68. ret = insert_item(root, &key, buf, strlen(buf));
  69. if (ret != -EEXIST) {
  70. printf("insert on %Lu gave us %d\n", key.objectid, ret);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
  76. {
  77. struct ctree_path path;
  78. struct key key;
  79. int ret;
  80. unsigned long *ptr;
  81. init_path(&path);
  82. ret = setup_key(radix, &key, 1);
  83. if (ret < 0)
  84. return 0;
  85. ret = search_slot(root, &key, &path, -1);
  86. if (ret)
  87. goto error;
  88. ret = del_item(root, &path);
  89. release_path(root, &path);
  90. if (ret != 0)
  91. goto error;
  92. ptr = radix_tree_delete(radix, key.objectid);
  93. if (!ptr)
  94. goto error;
  95. return 0;
  96. error:
  97. printf("failed to delete %Lu\n", key.objectid);
  98. return -1;
  99. }
  100. static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
  101. {
  102. struct ctree_path path;
  103. struct key key;
  104. int ret;
  105. init_path(&path);
  106. ret = setup_key(radix, &key, 1);
  107. if (ret < 0)
  108. return 0;
  109. ret = search_slot(root, &key, &path, 0);
  110. release_path(root, &path);
  111. if (ret)
  112. goto error;
  113. return 0;
  114. error:
  115. printf("unable to find key %Lu\n", key.objectid);
  116. return -1;
  117. }
  118. static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
  119. {
  120. struct ctree_path path;
  121. struct key key;
  122. int ret;
  123. init_path(&path);
  124. ret = setup_key(radix, &key, 0);
  125. if (ret < 0)
  126. return ret;
  127. ret = search_slot(root, &key, &path, 0);
  128. release_path(root, &path);
  129. if (ret <= 0)
  130. goto error;
  131. return 0;
  132. error:
  133. printf("able to find key that should not exist %Lu\n", key.objectid);
  134. return -1;
  135. }
  136. static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
  137. int nr)
  138. {
  139. struct ctree_path path;
  140. struct key key;
  141. unsigned long found = 0;
  142. int ret;
  143. int slot;
  144. int *ptr;
  145. int count = 0;
  146. key.offset = 0;
  147. key.flags = 0;
  148. key.objectid = (unsigned long)-1;
  149. while(nr-- >= 0) {
  150. init_path(&path);
  151. ret = search_slot(root, &key, &path, -1);
  152. if (ret < 0) {
  153. release_path(root, &path);
  154. return ret;
  155. }
  156. if (ret != 0) {
  157. if (path.slots[0] == 0) {
  158. release_path(root, &path);
  159. break;
  160. }
  161. path.slots[0] -= 1;
  162. }
  163. slot = path.slots[0];
  164. found = path.nodes[0]->leaf.items[slot].key.objectid;
  165. ret = del_item(root, &path);
  166. count++;
  167. if (ret) {
  168. fprintf(stderr,
  169. "failed to remove %lu from tree\n",
  170. found);
  171. return -1;
  172. }
  173. release_path(root, &path);
  174. ptr = radix_tree_delete(radix, found);
  175. if (!ptr)
  176. goto error;
  177. if (!keep_running)
  178. break;
  179. }
  180. return 0;
  181. error:
  182. fprintf(stderr, "failed to delete from the radix %lu\n", found);
  183. return -1;
  184. }
  185. static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix,
  186. int count)
  187. {
  188. int i;
  189. int err;
  190. int ret = 0;
  191. for (i = 0; i < count; i++) {
  192. ret = ins_one(root, radix);
  193. if (ret) {
  194. printf("fill failed\n");
  195. err = ret;
  196. goto out;
  197. }
  198. if (!keep_running)
  199. break;
  200. }
  201. out:
  202. return ret;
  203. }
  204. static int bulk_op(struct ctree_root *root, struct radix_tree_root *radix)
  205. {
  206. int ret;
  207. int nr = rand() % 20000;
  208. static int run_nr = 0;
  209. /* do the bulk op much less frequently */
  210. if (run_nr++ % 100)
  211. return 0;
  212. ret = empty_tree(root, radix, nr);
  213. if (ret)
  214. return ret;
  215. ret = fill_tree(root, radix, nr);
  216. if (ret)
  217. return ret;
  218. return 0;
  219. }
  220. int (*ops[])(struct ctree_root *root, struct radix_tree_root *radix) =
  221. { ins_one, insert_dup, del_one, lookup_item, lookup_enoent, bulk_op };
  222. static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
  223. {
  224. struct ctree_path path;
  225. struct key key;
  226. unsigned long found;
  227. int ret;
  228. int slot;
  229. int i;
  230. key.offset = 0;
  231. key.flags = 0;
  232. key.objectid = (unsigned long)-1;
  233. while(1) {
  234. init_path(&path);
  235. ret = search_slot(root, &key, &path, 0);
  236. if (ret < 0) {
  237. release_path(root, &path);
  238. return ret;
  239. }
  240. slot = path.slots[0];
  241. if (ret != 0) {
  242. if (slot == 0) {
  243. release_path(root, &path);
  244. break;
  245. }
  246. slot -= 1;
  247. }
  248. for (i = slot; i >= 0; i--) {
  249. found = path.nodes[0]->leaf.items[i].key.objectid;
  250. radix_tree_preload(GFP_KERNEL);
  251. ret = radix_tree_insert(radix, found, (void *)found);
  252. if (ret) {
  253. fprintf(stderr,
  254. "failed to insert %lu into radix\n",
  255. found);
  256. exit(1);
  257. }
  258. radix_tree_preload_end();
  259. }
  260. release_path(root, &path);
  261. key.objectid = found - 1;
  262. if (key.objectid > found)
  263. break;
  264. }
  265. return 0;
  266. }
  267. void sigstopper(int ignored)
  268. {
  269. keep_running = 0;
  270. fprintf(stderr, "caught exit signal, stopping\n");
  271. }
  272. int print_usage(void)
  273. {
  274. printf("usage: tester [-ih] [-c count] [-f count]\n");
  275. printf("\t -c count -- iteration count after filling\n");
  276. printf("\t -f count -- run this many random inserts before starting\n");
  277. printf("\t -i -- only do initial fill\n");
  278. printf("\t -h -- this help text\n");
  279. exit(1);
  280. }
  281. int main(int ac, char **av)
  282. {
  283. RADIX_TREE(radix, GFP_KERNEL);
  284. struct ctree_super_block super;
  285. struct ctree_root *root;
  286. int i;
  287. int ret;
  288. int count;
  289. int op;
  290. int iterations = 20000;
  291. int init_fill_count = 800000;
  292. int err = 0;
  293. int initial_only = 0;
  294. radix_tree_init();
  295. root = open_ctree("dbfile", &super);
  296. fill_radix(root, &radix);
  297. signal(SIGTERM, sigstopper);
  298. signal(SIGINT, sigstopper);
  299. for (i = 1 ; i < ac ; i++) {
  300. if (strcmp(av[i], "-i") == 0) {
  301. initial_only = 1;
  302. } else if (strcmp(av[i], "-c") == 0) {
  303. iterations = atoi(av[i+1]);
  304. i++;
  305. } else if (strcmp(av[i], "-f") == 0) {
  306. init_fill_count = atoi(av[i+1]);
  307. i++;
  308. } else {
  309. print_usage();
  310. }
  311. }
  312. printf("initial fill\n");
  313. ret = fill_tree(root, &radix, init_fill_count);
  314. printf("starting run\n");
  315. if (ret) {
  316. err = ret;
  317. goto out;
  318. }
  319. if (initial_only == 1) {
  320. goto out;
  321. }
  322. for (i = 0; i < iterations; i++) {
  323. op = rand() % ARRAY_SIZE(ops);
  324. count = rand() % 128;
  325. if (i % 2000 == 0) {
  326. printf("%d\n", i);
  327. fflush(stdout);
  328. }
  329. if (i && i % 5000 == 0) {
  330. printf("open & close, root level %d nritems %d\n",
  331. node_level(root->node->node.header.flags),
  332. root->node->node.header.nritems);
  333. write_ctree_super(root, &super);
  334. close_ctree(root);
  335. root = open_ctree("dbfile", &super);
  336. }
  337. while(count--) {
  338. ret = ops[op](root, &radix);
  339. if (ret) {
  340. fprintf(stderr, "op %d failed %d:%d\n",
  341. op, i, iterations);
  342. print_tree(root, root->node);
  343. fprintf(stderr, "op %d failed %d:%d\n",
  344. op, i, iterations);
  345. err = ret;
  346. goto out;
  347. }
  348. if (ops[op] == bulk_op)
  349. break;
  350. if (keep_running == 0) {
  351. err = 0;
  352. goto out;
  353. }
  354. }
  355. }
  356. out:
  357. write_ctree_super(root, &super);
  358. close_ctree(root);
  359. return err;
  360. }