random-test.c 8.9 KB

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