quick-test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "print-tree.h"
  8. #include "transaction.h"
  9. /* for testing only */
  10. int next_key(int i, int max_key) {
  11. return rand() % max_key;
  12. // return i;
  13. }
  14. int main(int ac, char **av) {
  15. struct btrfs_key ins;
  16. struct btrfs_key last = { (u64)-1, 0, 0};
  17. char *buf;
  18. int i;
  19. int num;
  20. int ret;
  21. int run_size = 100000;
  22. int max_key = 100000000;
  23. int tree_size = 0;
  24. struct btrfs_path path;
  25. struct btrfs_super_block super;
  26. struct btrfs_root *root;
  27. struct btrfs_trans_handle *trans;
  28. radix_tree_init();
  29. root = open_ctree("dbfile", &super);
  30. trans = btrfs_start_transaction(root, 1);
  31. srand(55);
  32. ins.flags = 0;
  33. btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
  34. for (i = 0; i < run_size; i++) {
  35. buf = malloc(64);
  36. num = next_key(i, max_key);
  37. // num = i;
  38. sprintf(buf, "string-%d", num);
  39. if (i % 10000 == 0)
  40. fprintf(stderr, "insert %d:%d\n", num, i);
  41. ins.objectid = num;
  42. ins.offset = 0;
  43. ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
  44. if (!ret)
  45. tree_size++;
  46. free(buf);
  47. if (i == run_size - 5) {
  48. btrfs_commit_transaction(trans, root, &super);
  49. }
  50. }
  51. close_ctree(root, &super);
  52. root = open_ctree("dbfile", &super);
  53. printf("starting search\n");
  54. srand(55);
  55. for (i = 0; i < run_size; i++) {
  56. num = next_key(i, max_key);
  57. ins.objectid = num;
  58. btrfs_init_path(&path);
  59. if (i % 10000 == 0)
  60. fprintf(stderr, "search %d:%d\n", num, i);
  61. ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
  62. if (ret) {
  63. btrfs_print_tree(root, root->node);
  64. printf("unable to find %d\n", num);
  65. exit(1);
  66. }
  67. btrfs_release_path(root, &path);
  68. }
  69. close_ctree(root, &super);
  70. root = open_ctree("dbfile", &super);
  71. printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
  72. btrfs_header_level(&root->node->node.header),
  73. btrfs_header_nritems(&root->node->node.header),
  74. BTRFS_NODEPTRS_PER_BLOCK(root) -
  75. btrfs_header_nritems(&root->node->node.header));
  76. printf("all searches good, deleting some items\n");
  77. i = 0;
  78. srand(55);
  79. for (i = 0 ; i < run_size/4; i++) {
  80. num = next_key(i, max_key);
  81. ins.objectid = num;
  82. btrfs_init_path(&path);
  83. ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
  84. if (!ret) {
  85. if (i % 10000 == 0)
  86. fprintf(stderr, "del %d:%d\n", num, i);
  87. ret = btrfs_del_item(trans, root, &path);
  88. if (ret != 0)
  89. BUG();
  90. tree_size--;
  91. }
  92. btrfs_release_path(root, &path);
  93. }
  94. close_ctree(root, &super);
  95. root = open_ctree("dbfile", &super);
  96. srand(128);
  97. for (i = 0; i < run_size; i++) {
  98. buf = malloc(64);
  99. num = next_key(i, max_key);
  100. sprintf(buf, "string-%d", num);
  101. ins.objectid = num;
  102. if (i % 10000 == 0)
  103. fprintf(stderr, "insert %d:%d\n", num, i);
  104. ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
  105. if (!ret)
  106. tree_size++;
  107. free(buf);
  108. }
  109. close_ctree(root, &super);
  110. root = open_ctree("dbfile", &super);
  111. srand(128);
  112. printf("starting search2\n");
  113. for (i = 0; i < run_size; i++) {
  114. num = next_key(i, max_key);
  115. ins.objectid = num;
  116. btrfs_init_path(&path);
  117. if (i % 10000 == 0)
  118. fprintf(stderr, "search %d:%d\n", num, i);
  119. ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
  120. if (ret) {
  121. btrfs_print_tree(root, root->node);
  122. printf("unable to find %d\n", num);
  123. exit(1);
  124. }
  125. btrfs_release_path(root, &path);
  126. }
  127. printf("starting big long delete run\n");
  128. while(root->node &&
  129. btrfs_header_nritems(&root->node->node.header) > 0) {
  130. struct btrfs_leaf *leaf;
  131. int slot;
  132. ins.objectid = (u64)-1;
  133. btrfs_init_path(&path);
  134. ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
  135. if (ret == 0)
  136. BUG();
  137. leaf = &path.nodes[0]->leaf;
  138. slot = path.slots[0];
  139. if (slot != btrfs_header_nritems(&leaf->header))
  140. BUG();
  141. while(path.slots[0] > 0) {
  142. path.slots[0] -= 1;
  143. slot = path.slots[0];
  144. leaf = &path.nodes[0]->leaf;
  145. btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
  146. if (tree_size % 10000 == 0)
  147. printf("big del %d:%d\n", tree_size, i);
  148. ret = btrfs_del_item(trans, root, &path);
  149. if (ret != 0) {
  150. printf("del_item returned %d\n", ret);
  151. BUG();
  152. }
  153. tree_size--;
  154. }
  155. btrfs_release_path(root, &path);
  156. }
  157. /*
  158. printf("previous tree:\n");
  159. btrfs_print_tree(root, root->commit_root);
  160. printf("map before commit\n");
  161. btrfs_print_tree(root->extent_root, root->extent_root->node);
  162. */
  163. btrfs_commit_transaction(trans, root, &super);
  164. printf("tree size is now %d\n", tree_size);
  165. printf("root %p commit root %p\n", root->node, root->commit_root);
  166. printf("map tree\n");
  167. btrfs_print_tree(root->fs_info->extent_root,
  168. root->fs_info->extent_root->node);
  169. close_ctree(root, &super);
  170. return 0;
  171. }