dir-test.c 8.4 KB

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