random-test.c 7.9 KB

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