random-test.c 8.0 KB

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