btrfs-progs: check: enhanced progress indicator
[btrfs-progs-unstable/devel.git] / quick-test.c
blob1712adc4c9ec3c797786a99ca06dd9e3dd583889
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 <fcntl.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.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;
32 // return i;
35 int main(int ac, char **av) {
36 struct btrfs_key ins;
37 struct btrfs_key last = { (u64)-1, 0, 0};
38 char *buf;
39 int i;
40 int num;
41 int ret;
42 int run_size = 300000;
43 int max_key = 100000000;
44 int tree_size = 2;
45 struct btrfs_path path;
46 struct btrfs_root *root;
47 struct btrfs_trans_handle *trans;
49 buf = calloc(1, 512);
51 radix_tree_init();
53 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
54 if (!root) {
55 fprintf(stderr, "Open ctree failed\n");
56 exit(1);
58 trans = btrfs_start_transaction(root, 1);
59 BUG_ON(IS_ERR(trans));
60 srand(55);
61 ins.type = BTRFS_STRING_ITEM_KEY;
62 for (i = 0; i < run_size; i++) {
63 num = next_key(i, max_key);
64 // num = i;
65 sprintf(buf, "string-%d", num);
66 if (i % 10000 == 0)
67 fprintf(stderr, "insert %d:%d\n", num, i);
68 ins.objectid = num;
69 ins.offset = 0;
70 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
71 if (!ret)
72 tree_size++;
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);
80 close_ctree(root);
81 exit(1);
82 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
83 if (!root) {
84 fprintf(stderr, "Open ctree failed\n");
85 exit(1);
87 printf("starting search\n");
88 srand(55);
89 for (i = 0; i < run_size; i++) {
90 num = next_key(i, max_key);
91 ins.objectid = num;
92 btrfs_init_path(&path);
93 if (i % 10000 == 0)
94 fprintf(stderr, "search %d:%d\n", num, i);
95 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
96 if (ret) {
97 btrfs_print_tree(root->node, 1);
98 printf("unable to find %d\n", num);
99 exit(1);
101 btrfs_release_path(&path);
103 close_ctree(root);
105 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
106 if (!root) {
107 fprintf(stderr, "Open ctree failed\n");
108 exit(1);
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->fs_info) -
114 btrfs_header_nritems(root->node));
115 printf("all searches good, deleting some items\n");
116 i = 0;
117 srand(55);
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);
122 ins.objectid = num;
123 btrfs_init_path(&path);
124 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
125 if (!ret) {
126 if (i % 10000 == 0)
127 fprintf(stderr, "del %d:%d\n", num, i);
128 ret = btrfs_del_item(trans, root, &path);
129 if (ret != 0)
130 BUG();
131 tree_size--;
133 btrfs_release_path(&path);
135 btrfs_commit_transaction(trans, root);
136 close_ctree(root);
138 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
139 if (!root) {
140 fprintf(stderr, "Open ctree failed\n");
141 exit(1);
143 trans = btrfs_start_transaction(root, 1);
144 BUG_ON(IS_ERR(trans));
145 srand(128);
146 for (i = 0; i < run_size; i++) {
147 num = next_key(i, max_key);
148 sprintf(buf, "string-%d", num);
149 ins.objectid = num;
150 if (i % 10000 == 0)
151 fprintf(stderr, "insert %d:%d\n", num, i);
152 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
153 if (!ret)
154 tree_size++;
156 btrfs_commit_transaction(trans, root);
157 close_ctree(root);
159 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
160 if (!root) {
161 fprintf(stderr, "Open ctree failed\n");
162 exit(1);
164 srand(128);
165 printf("starting search2\n");
166 for (i = 0; i < run_size; i++) {
167 num = next_key(i, max_key);
168 ins.objectid = num;
169 btrfs_init_path(&path);
170 if (i % 10000 == 0)
171 fprintf(stderr, "search %d:%d\n", num, i);
172 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
173 if (ret) {
174 btrfs_print_tree(root->node, 1);
175 printf("unable to find %d\n", num);
176 exit(1);
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;
185 int slot;
186 ins.objectid = (u64)-1;
187 btrfs_init_path(&path);
188 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
189 if (ret == 0)
190 BUG();
192 leaf = path.nodes[0];
193 slot = path.slots[0];
194 if (slot != btrfs_header_nritems(leaf))
195 BUG();
196 while(path.slots[0] > 0) {
197 path.slots[0] -= 1;
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);
206 if (ret != 0) {
207 printf("del_item returned %d\n", ret);
208 BUG();
210 tree_size--;
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->node, 1);
224 close_ctree(root);
225 return 0;