add GPLv2
[platform/upstream/btrfs-progs.git] / quick-test.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26 #include "transaction.h"
27
28 /* for testing only */
29 int next_key(int i, int max_key) {
30         return rand() % max_key;
31         // return i;
32 }
33
34 int main(int ac, char **av) {
35         struct btrfs_key ins;
36         struct btrfs_key last = { (u64)-1, 0, 0};
37         char *buf;
38         int i;
39         int num;
40         int ret;
41         int run_size = 100000;
42         int max_key =  100000000;
43         int tree_size = 0;
44         struct btrfs_path path;
45         struct btrfs_super_block super;
46         struct btrfs_root *root;
47         struct btrfs_trans_handle *trans;
48
49         buf = malloc(512);
50         memset(buf, 0, 512);
51
52         radix_tree_init();
53
54         root = open_ctree(av[1], &super);
55         trans = btrfs_start_transaction(root, 1);
56         srand(55);
57         ins.flags = 0;
58         btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
59         for (i = 0; i < run_size; i++) {
60                 num = next_key(i, max_key);
61                 // num = i;
62                 sprintf(buf, "string-%d", num);
63                 if (i % 10000 == 0)
64                         fprintf(stderr, "insert %d:%d\n", num, i);
65                 ins.objectid = num;
66                 ins.offset = 0;
67                 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
68                 if (!ret)
69                         tree_size++;
70                 if (i == run_size - 5) {
71                         btrfs_commit_transaction(trans, root, &super);
72                 }
73         }
74         close_ctree(root, &super);
75         root = open_ctree(av[1], &super);
76         printf("starting search\n");
77         srand(55);
78         for (i = 0; i < run_size; i++) {
79                 num = next_key(i, max_key);
80                 ins.objectid = num;
81                 btrfs_init_path(&path);
82                 if (i % 10000 == 0)
83                         fprintf(stderr, "search %d:%d\n", num, i);
84                 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
85                 if (ret) {
86                         btrfs_print_tree(root, root->node);
87                         printf("unable to find %d\n", num);
88                         exit(1);
89                 }
90                 btrfs_release_path(root, &path);
91         }
92         close_ctree(root, &super);
93         root = open_ctree(av[1], &super);
94         printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
95                 btrfs_header_level(&root->node->node.header),
96                 btrfs_header_nritems(&root->node->node.header),
97                 BTRFS_NODEPTRS_PER_BLOCK(root) -
98                 btrfs_header_nritems(&root->node->node.header));
99         printf("all searches good, deleting some items\n");
100         i = 0;
101         srand(55);
102         for (i = 0 ; i < run_size/4; i++) {
103                 num = next_key(i, max_key);
104                 ins.objectid = num;
105                 btrfs_init_path(&path);
106                 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
107                 if (!ret) {
108                         if (i % 10000 == 0)
109                                 fprintf(stderr, "del %d:%d\n", num, i);
110                         ret = btrfs_del_item(trans, root, &path);
111                         if (ret != 0)
112                                 BUG();
113                         tree_size--;
114                 }
115                 btrfs_release_path(root, &path);
116         }
117         close_ctree(root, &super);
118         root = open_ctree(av[1], &super);
119         srand(128);
120         for (i = 0; i < run_size; i++) {
121                 num = next_key(i, max_key);
122                 sprintf(buf, "string-%d", num);
123                 ins.objectid = num;
124                 if (i % 10000 == 0)
125                         fprintf(stderr, "insert %d:%d\n", num, i);
126                 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
127                 if (!ret)
128                         tree_size++;
129         }
130         close_ctree(root, &super);
131         root = open_ctree(av[1], &super);
132         srand(128);
133         printf("starting search2\n");
134         for (i = 0; i < run_size; i++) {
135                 num = next_key(i, max_key);
136                 ins.objectid = num;
137                 btrfs_init_path(&path);
138                 if (i % 10000 == 0)
139                         fprintf(stderr, "search %d:%d\n", num, i);
140                 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
141                 if (ret) {
142                         btrfs_print_tree(root, root->node);
143                         printf("unable to find %d\n", num);
144                         exit(1);
145                 }
146                 btrfs_release_path(root, &path);
147         }
148         printf("starting big long delete run\n");
149         while(root->node &&
150               btrfs_header_nritems(&root->node->node.header) > 0) {
151                 struct btrfs_leaf *leaf;
152                 int slot;
153                 ins.objectid = (u64)-1;
154                 btrfs_init_path(&path);
155                 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
156                 if (ret == 0)
157                         BUG();
158
159                 leaf = &path.nodes[0]->leaf;
160                 slot = path.slots[0];
161                 if (slot != btrfs_header_nritems(&leaf->header))
162                         BUG();
163                 while(path.slots[0] > 0) {
164                         path.slots[0] -= 1;
165                         slot = path.slots[0];
166                         leaf = &path.nodes[0]->leaf;
167
168                         btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
169                         if (tree_size % 10000 == 0)
170                                 printf("big del %d:%d\n", tree_size, i);
171                         ret = btrfs_del_item(trans, root, &path);
172                         if (ret != 0) {
173                                 printf("del_item returned %d\n", ret);
174                                 BUG();
175                         }
176                         tree_size--;
177                 }
178                 btrfs_release_path(root, &path);
179         }
180         /*
181         printf("previous tree:\n");
182         btrfs_print_tree(root, root->commit_root);
183         printf("map before commit\n");
184         btrfs_print_tree(root->extent_root, root->extent_root->node);
185         */
186         btrfs_commit_transaction(trans, root, &super);
187         printf("tree size is now %d\n", tree_size);
188         printf("root %p commit root %p\n", root->node, root->commit_root);
189         printf("map tree\n");
190         btrfs_print_tree(root->fs_info->extent_root,
191                          root->fs_info->extent_root->node);
192         close_ctree(root, &super);
193         return 0;
194 }