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