quick-test.c 4.4 KB

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