Btrfs image tool
[btrfs-progs-unstable.git] / quick-test.c
blob44c2318546574a16b785dc1c54dd6fd9c905738d
1 /*
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.
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"
28 /* for testing only */
29 int next_key(int i, int max_key) {
30 return rand() % max_key;
31 // return i;
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 = 300000;
42 int max_key = 100000000;
43 int tree_size = 2;
44 struct btrfs_path path;
45 struct btrfs_root *root;
46 struct btrfs_trans_handle *trans;
48 buf = malloc(512);
49 memset(buf, 0, 512);
51 radix_tree_init();
53 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
54 trans = btrfs_start_transaction(root, 1);
55 srand(55);
56 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
57 for (i = 0; i < run_size; i++) {
58 num = next_key(i, max_key);
59 // num = i;
60 sprintf(buf, "string-%d", num);
61 if (i % 10000 == 0)
62 fprintf(stderr, "insert %d:%d\n", num, i);
63 ins.objectid = num;
64 ins.offset = 0;
65 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
66 if (!ret)
67 tree_size++;
68 if (i == run_size - 5) {
69 btrfs_commit_transaction(trans, root);
70 trans = btrfs_start_transaction(root, 1);
73 btrfs_commit_transaction(trans, root);
74 close_ctree(root);
75 exit(1);
76 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
77 printf("starting search\n");
78 srand(55);
79 for (i = 0; i < run_size; i++) {
80 num = next_key(i, max_key);
81 ins.objectid = num;
82 btrfs_init_path(&path);
83 if (i % 10000 == 0)
84 fprintf(stderr, "search %d:%d\n", num, i);
85 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
86 if (ret) {
87 btrfs_print_tree(root, root->node);
88 printf("unable to find %d\n", num);
89 exit(1);
91 btrfs_release_path(root, &path);
93 close_ctree(root);
95 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
96 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
97 btrfs_header_level(root->node),
98 btrfs_header_nritems(root->node),
99 BTRFS_NODEPTRS_PER_BLOCK(root) -
100 btrfs_header_nritems(root->node));
101 printf("all searches good, deleting some items\n");
102 i = 0;
103 srand(55);
104 trans = btrfs_start_transaction(root, 1);
105 for (i = 0 ; i < run_size/4; i++) {
106 num = next_key(i, max_key);
107 ins.objectid = num;
108 btrfs_init_path(&path);
109 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
110 if (!ret) {
111 if (i % 10000 == 0)
112 fprintf(stderr, "del %d:%d\n", num, i);
113 ret = btrfs_del_item(trans, root, &path);
114 if (ret != 0)
115 BUG();
116 tree_size--;
118 btrfs_release_path(root, &path);
120 btrfs_commit_transaction(trans, root);
121 close_ctree(root);
123 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
124 trans = btrfs_start_transaction(root, 1);
125 srand(128);
126 for (i = 0; i < run_size; i++) {
127 num = next_key(i, max_key);
128 sprintf(buf, "string-%d", num);
129 ins.objectid = num;
130 if (i % 10000 == 0)
131 fprintf(stderr, "insert %d:%d\n", num, i);
132 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
133 if (!ret)
134 tree_size++;
136 btrfs_commit_transaction(trans, root);
137 close_ctree(root);
139 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
140 srand(128);
141 printf("starting search2\n");
142 for (i = 0; i < run_size; i++) {
143 num = next_key(i, max_key);
144 ins.objectid = num;
145 btrfs_init_path(&path);
146 if (i % 10000 == 0)
147 fprintf(stderr, "search %d:%d\n", num, i);
148 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
149 if (ret) {
150 btrfs_print_tree(root, root->node);
151 printf("unable to find %d\n", num);
152 exit(1);
154 btrfs_release_path(root, &path);
156 printf("starting big long delete run\n");
157 trans = btrfs_start_transaction(root, 1);
158 while(root->node && btrfs_header_nritems(root->node) > 0) {
159 struct extent_buffer *leaf;
160 int slot;
161 ins.objectid = (u64)-1;
162 btrfs_init_path(&path);
163 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
164 if (ret == 0)
165 BUG();
167 leaf = path.nodes[0];
168 slot = path.slots[0];
169 if (slot != btrfs_header_nritems(leaf))
170 BUG();
171 while(path.slots[0] > 0) {
172 path.slots[0] -= 1;
173 slot = path.slots[0];
174 leaf = path.nodes[0];
176 btrfs_item_key_to_cpu(leaf, &last, slot);
178 if (tree_size % 10000 == 0)
179 printf("big del %d:%d\n", tree_size, i);
180 ret = btrfs_del_item(trans, root, &path);
181 if (ret != 0) {
182 printf("del_item returned %d\n", ret);
183 BUG();
185 tree_size--;
187 btrfs_release_path(root, &path);
190 printf("previous tree:\n");
191 btrfs_print_tree(root, root->commit_root);
192 printf("map before commit\n");
193 btrfs_print_tree(root->extent_root, root->extent_root->node);
195 btrfs_commit_transaction(trans, root);
196 printf("tree size is now %d\n", tree_size);
197 printf("root %p commit root %p\n", root->node, root->commit_root);
198 btrfs_print_tree(root, root->node);
199 close_ctree(root);
200 return 0;