blob: d9287cad35c9fd48f2bbacb0ca4dc30112799c5b [file] [log] [blame]
Chris Mason79f95c82007-03-01 15:16:26 -05001#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
9/* for testing only */
10int next_key(int i, int max_key) {
11 return rand() % max_key;
Chris Mason20524f02007-03-10 06:35:47 -050012 // return i;
Chris Mason79f95c82007-03-01 15:16:26 -050013}
14
15int main(int ac, char **av) {
Chris Masone2fa7222007-03-12 16:22:34 -040016 struct btrfs_key ins;
17 struct btrfs_key last = { (u64)-1, 0, 0};
Chris Mason79f95c82007-03-01 15:16:26 -050018 char *buf;
19 int i;
20 int num;
21 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050022 int run_size = 100000;
Chris Mason79f95c82007-03-01 15:16:26 -050023 int max_key = 100000000;
24 int tree_size = 0;
Chris Mason234b63a2007-03-13 10:46:10 -040025 struct btrfs_path path;
26 struct btrfs_super_block super;
27 struct btrfs_root *root;
Chris Mason79f95c82007-03-01 15:16:26 -050028
29 radix_tree_init();
30
31 root = open_ctree("dbfile", &super);
32 srand(55);
33 for (i = 0; i < run_size; i++) {
34 buf = malloc(64);
35 num = next_key(i, max_key);
36 // num = i;
37 sprintf(buf, "string-%d", num);
38 if (i % 10000 == 0)
39 fprintf(stderr, "insert %d:%d\n", num, i);
40 ins.objectid = num;
41 ins.offset = 0;
42 ins.flags = 0;
Chris Mason234b63a2007-03-13 10:46:10 -040043 ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
Chris Mason79f95c82007-03-01 15:16:26 -050044 if (!ret)
45 tree_size++;
46 free(buf);
Chris Mason20524f02007-03-10 06:35:47 -050047 if (i == run_size - 5) {
Chris Mason234b63a2007-03-13 10:46:10 -040048 btrfs_commit_transaction(root, &super);
Chris Mason20524f02007-03-10 06:35:47 -050049 }
Chris Masona28ec192007-03-06 20:08:01 -050050
Chris Mason79f95c82007-03-01 15:16:26 -050051 }
Chris Masona28ec192007-03-06 20:08:01 -050052 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050053
54 root = open_ctree("dbfile", &super);
55 printf("starting search\n");
56 srand(55);
57 for (i = 0; i < run_size; i++) {
58 num = next_key(i, max_key);
59 ins.objectid = num;
Chris Mason234b63a2007-03-13 10:46:10 -040060 btrfs_init_path(&path);
Chris Mason79f95c82007-03-01 15:16:26 -050061 if (i % 10000 == 0)
62 fprintf(stderr, "search %d:%d\n", num, i);
Chris Mason234b63a2007-03-13 10:46:10 -040063 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
Chris Mason79f95c82007-03-01 15:16:26 -050064 if (ret) {
Chris Mason234b63a2007-03-13 10:46:10 -040065 btrfs_print_tree(root, root->node);
Chris Mason79f95c82007-03-01 15:16:26 -050066 printf("unable to find %d\n", num);
67 exit(1);
68 }
Chris Mason234b63a2007-03-13 10:46:10 -040069 btrfs_release_path(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -050070 }
Chris Masona28ec192007-03-06 20:08:01 -050071 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050072 root = open_ctree("dbfile", &super);
73 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
Chris Mason7518a232007-03-12 12:01:18 -040074 btrfs_header_level(&root->node->node.header),
75 btrfs_header_nritems(&root->node->node.header),
Chris Mason123abc82007-03-14 14:14:43 -040076 BTRFS_NODEPTRS_PER_BLOCK(root) -
Chris Mason7518a232007-03-12 12:01:18 -040077 btrfs_header_nritems(&root->node->node.header));
Chris Mason79f95c82007-03-01 15:16:26 -050078 printf("all searches good, deleting some items\n");
79 i = 0;
80 srand(55);
81 for (i = 0 ; i < run_size/4; i++) {
82 num = next_key(i, max_key);
83 ins.objectid = num;
Chris Mason234b63a2007-03-13 10:46:10 -040084 btrfs_init_path(&path);
85 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
Chris Mason79f95c82007-03-01 15:16:26 -050086 if (!ret) {
87 if (i % 10000 == 0)
88 fprintf(stderr, "del %d:%d\n", num, i);
Chris Mason234b63a2007-03-13 10:46:10 -040089 ret = btrfs_del_item(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -050090 if (ret != 0)
91 BUG();
92 tree_size--;
93 }
Chris Mason234b63a2007-03-13 10:46:10 -040094 btrfs_release_path(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -050095 }
Chris Masona28ec192007-03-06 20:08:01 -050096 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050097 root = open_ctree("dbfile", &super);
98 srand(128);
99 for (i = 0; i < run_size; i++) {
100 buf = malloc(64);
101 num = next_key(i, max_key);
102 sprintf(buf, "string-%d", num);
103 ins.objectid = num;
104 if (i % 10000 == 0)
105 fprintf(stderr, "insert %d:%d\n", num, i);
Chris Mason234b63a2007-03-13 10:46:10 -0400106 ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
Chris Mason79f95c82007-03-01 15:16:26 -0500107 if (!ret)
108 tree_size++;
109 free(buf);
110 }
Chris Masona28ec192007-03-06 20:08:01 -0500111 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -0500112 root = open_ctree("dbfile", &super);
113 srand(128);
114 printf("starting search2\n");
115 for (i = 0; i < run_size; i++) {
116 num = next_key(i, max_key);
117 ins.objectid = num;
Chris Mason234b63a2007-03-13 10:46:10 -0400118 btrfs_init_path(&path);
Chris Mason79f95c82007-03-01 15:16:26 -0500119 if (i % 10000 == 0)
120 fprintf(stderr, "search %d:%d\n", num, i);
Chris Mason234b63a2007-03-13 10:46:10 -0400121 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
Chris Mason79f95c82007-03-01 15:16:26 -0500122 if (ret) {
Chris Mason234b63a2007-03-13 10:46:10 -0400123 btrfs_print_tree(root, root->node);
Chris Mason79f95c82007-03-01 15:16:26 -0500124 printf("unable to find %d\n", num);
125 exit(1);
126 }
Chris Mason234b63a2007-03-13 10:46:10 -0400127 btrfs_release_path(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -0500128 }
129 printf("starting big long delete run\n");
Chris Mason7518a232007-03-12 12:01:18 -0400130 while(root->node &&
131 btrfs_header_nritems(&root->node->node.header) > 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400132 struct btrfs_leaf *leaf;
Chris Mason79f95c82007-03-01 15:16:26 -0500133 int slot;
134 ins.objectid = (u64)-1;
Chris Mason234b63a2007-03-13 10:46:10 -0400135 btrfs_init_path(&path);
136 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500137 if (ret == 0)
138 BUG();
139
140 leaf = &path.nodes[0]->leaf;
141 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400142 if (slot != btrfs_header_nritems(&leaf->header))
Chris Mason79f95c82007-03-01 15:16:26 -0500143 BUG();
144 while(path.slots[0] > 0) {
145 path.slots[0] -= 1;
146 slot = path.slots[0];
147 leaf = &path.nodes[0]->leaf;
148
Chris Masone2fa7222007-03-12 16:22:34 -0400149 btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
Chris Mason79f95c82007-03-01 15:16:26 -0500150 if (tree_size % 10000 == 0)
151 printf("big del %d:%d\n", tree_size, i);
Chris Mason234b63a2007-03-13 10:46:10 -0400152 ret = btrfs_del_item(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -0500153 if (ret != 0) {
154 printf("del_item returned %d\n", ret);
155 BUG();
156 }
157 tree_size--;
158 }
Chris Mason234b63a2007-03-13 10:46:10 -0400159 btrfs_release_path(root, &path);
Chris Mason79f95c82007-03-01 15:16:26 -0500160 }
Chris Masona28ec192007-03-06 20:08:01 -0500161 /*
162 printf("previous tree:\n");
Chris Mason234b63a2007-03-13 10:46:10 -0400163 btrfs_print_tree(root, root->commit_root);
Chris Masona28ec192007-03-06 20:08:01 -0500164 printf("map before commit\n");
Chris Mason234b63a2007-03-13 10:46:10 -0400165 btrfs_print_tree(root->extent_root, root->extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500166 */
Chris Mason234b63a2007-03-13 10:46:10 -0400167 btrfs_commit_transaction(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -0500168 printf("tree size is now %d\n", tree_size);
Chris Masona28ec192007-03-06 20:08:01 -0500169 printf("root %p commit root %p\n", root->node, root->commit_root);
Chris Mason79f95c82007-03-01 15:16:26 -0500170 printf("map tree\n");
Chris Mason234b63a2007-03-13 10:46:10 -0400171 btrfs_print_tree(root->extent_root, root->extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500172 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -0500173 return 0;
174}