dir-test.c 8.5 KB

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