quick-test.c 4.2 KB

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