2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.h"
27 #include "transaction.h"
29 /* for testing only */
30 static int next_key(int i, int max_key) {
31 return rand() % max_key;
35 int main(int ac, char **av) {
37 struct btrfs_key last = { (u64)-1, 0, 0};
42 int run_size = 300000;
43 int max_key = 100000000;
45 struct btrfs_path path;
46 struct btrfs_root *root;
47 struct btrfs_trans_handle *trans;
53 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
55 fprintf(stderr, "Open ctree failed\n");
58 trans = btrfs_start_transaction(root, 1);
59 BUG_ON(IS_ERR(trans));
61 ins.type = BTRFS_STRING_ITEM_KEY;
62 for (i = 0; i < run_size; i++) {
63 num = next_key(i, max_key);
65 sprintf(buf, "string-%d", num);
67 fprintf(stderr, "insert %d:%d\n", num, i);
70 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
73 if (i == run_size - 5) {
74 btrfs_commit_transaction(trans, root);
75 trans = btrfs_start_transaction(root, 1);
76 BUG_ON(IS_ERR(trans));
79 btrfs_commit_transaction(trans, root);
82 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
84 fprintf(stderr, "Open ctree failed\n");
87 printf("starting search\n");
89 for (i = 0; i < run_size; i++) {
90 num = next_key(i, max_key);
92 btrfs_init_path(&path);
94 fprintf(stderr, "search %d:%d\n", num, i);
95 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
97 btrfs_print_tree(root, root->node, 1);
98 printf("unable to find %d\n", num);
101 btrfs_release_path(&path);
105 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
107 fprintf(stderr, "Open ctree failed\n");
110 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
111 btrfs_header_level(root->node),
112 btrfs_header_nritems(root->node),
113 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) -
114 btrfs_header_nritems(root->node));
115 printf("all searches good, deleting some items\n");
118 trans = btrfs_start_transaction(root, 1);
119 BUG_ON(IS_ERR(trans));
120 for (i = 0 ; i < run_size/4; i++) {
121 num = next_key(i, max_key);
123 btrfs_init_path(&path);
124 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
127 fprintf(stderr, "del %d:%d\n", num, i);
128 ret = btrfs_del_item(trans, root, &path);
133 btrfs_release_path(&path);
135 btrfs_commit_transaction(trans, root);
138 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
140 fprintf(stderr, "Open ctree failed\n");
143 trans = btrfs_start_transaction(root, 1);
144 BUG_ON(IS_ERR(trans));
146 for (i = 0; i < run_size; i++) {
147 num = next_key(i, max_key);
148 sprintf(buf, "string-%d", num);
151 fprintf(stderr, "insert %d:%d\n", num, i);
152 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
156 btrfs_commit_transaction(trans, root);
159 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
161 fprintf(stderr, "Open ctree failed\n");
165 printf("starting search2\n");
166 for (i = 0; i < run_size; i++) {
167 num = next_key(i, max_key);
169 btrfs_init_path(&path);
171 fprintf(stderr, "search %d:%d\n", num, i);
172 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
174 btrfs_print_tree(root, root->node, 1);
175 printf("unable to find %d\n", num);
178 btrfs_release_path(&path);
180 printf("starting big long delete run\n");
181 trans = btrfs_start_transaction(root, 1);
182 BUG_ON(IS_ERR(trans));
183 while(root->node && btrfs_header_nritems(root->node) > 0) {
184 struct extent_buffer *leaf;
186 ins.objectid = (u64)-1;
187 btrfs_init_path(&path);
188 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
192 leaf = path.nodes[0];
193 slot = path.slots[0];
194 if (slot != btrfs_header_nritems(leaf))
196 while(path.slots[0] > 0) {
198 slot = path.slots[0];
199 leaf = path.nodes[0];
201 btrfs_item_key_to_cpu(leaf, &last, slot);
203 if (tree_size % 10000 == 0)
204 printf("big del %d:%d\n", tree_size, i);
205 ret = btrfs_del_item(trans, root, &path);
207 printf("del_item returned %d\n", ret);
212 btrfs_release_path(&path);
215 printf("previous tree:\n");
216 btrfs_print_tree(root, root->commit_root);
217 printf("map before commit\n");
218 btrfs_print_tree(root->extent_root, root->extent_root->node);
220 btrfs_commit_transaction(trans, root);
221 printf("tree size is now %d\n", tree_size);
222 printf("root %p commit root %p\n", root->node, root->commit_root);
223 btrfs_print_tree(root, root->node, 1);