random-test.c 7.9 KB

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