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