4 #include "kerncompat.h"
5 #include "radix-tree.h"
8 #include "print-tree.h"
11 struct ctree_super_block super;
13 static int setup_key(struct radix_tree_root *root, struct btrfs_key *key,
23 ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
28 } else if (ret != 0 && num == res[0]) {
30 if (ret > 1 && num == res[1]) {
39 static int ins_one(struct ctree_root *root, struct radix_tree_root *radix)
41 struct ctree_path path;
47 ret = setup_key(radix, &key, 0);
48 sprintf(buf, "str-%Lu\n", key.objectid);
49 ret = insert_item(root, &key, buf, strlen(buf));
52 oid = (unsigned long)key.objectid;
53 radix_tree_preload(GFP_KERNEL);
54 ret = radix_tree_insert(radix, oid, (void *)oid);
55 radix_tree_preload_end();
60 printf("failed to insert %Lu\n", key.objectid);
64 static int insert_dup(struct ctree_root *root, struct radix_tree_root *radix)
66 struct ctree_path path;
71 ret = setup_key(radix, &key, 1);
74 sprintf(buf, "str-%Lu\n", key.objectid);
75 ret = insert_item(root, &key, buf, strlen(buf));
77 printf("insert on %Lu gave us %d\n", key.objectid, ret);
83 static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
85 struct ctree_path path;
90 ret = setup_key(radix, &key, 1);
93 ret = search_slot(root, &key, &path, -1, 1);
96 ret = del_item(root, &path);
97 release_path(root, &path);
100 ptr = radix_tree_delete(radix, key.objectid);
105 printf("failed to delete %Lu\n", key.objectid);
109 static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
111 struct ctree_path path;
112 struct btrfs_key key;
115 ret = setup_key(radix, &key, 1);
118 ret = search_slot(root, &key, &path, 0, 1);
119 release_path(root, &path);
124 printf("unable to find key %Lu\n", key.objectid);
128 static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
130 struct ctree_path path;
131 struct btrfs_key key;
134 ret = setup_key(radix, &key, 0);
137 ret = search_slot(root, &key, &path, 0, 0);
138 release_path(root, &path);
143 printf("able to find key that should not exist %Lu\n", key.objectid);
147 static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
150 struct ctree_path path;
151 struct btrfs_key key;
152 unsigned long found = 0;
160 key.objectid = (unsigned long)-1;
163 ret = search_slot(root, &key, &path, -1, 1);
165 release_path(root, &path);
169 if (path.slots[0] == 0) {
170 release_path(root, &path);
175 slot = path.slots[0];
176 found = path.nodes[0]->leaf.items[slot].key.objectid;
177 ret = del_item(root, &path);
181 "failed to remove %lu from tree\n",
185 release_path(root, &path);
186 ptr = radix_tree_delete(radix, found);
194 fprintf(stderr, "failed to delete from the radix %lu\n", found);
198 static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix,
203 for (i = 0; i < count; i++) {
204 ret = ins_one(root, radix);
206 fprintf(stderr, "fill failed\n");
210 ret = commit_transaction(root, &super);
212 fprintf(stderr, "fill commit failed\n");
216 if (i && i % 10000 == 0) {
217 printf("bigfill %d\n", i);
226 static int bulk_op(struct ctree_root *root, struct radix_tree_root *radix)
229 int nr = rand() % 5000;
230 static int run_nr = 0;
232 /* do the bulk op much less frequently */
235 ret = empty_tree(root, radix, nr);
238 ret = fill_tree(root, radix, nr);
245 int (*ops[])(struct ctree_root *root, struct radix_tree_root *radix) =
246 { ins_one, insert_dup, del_one, lookup_item,
247 lookup_enoent, bulk_op };
249 static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
251 struct ctree_path path;
252 struct btrfs_key key;
260 key.objectid = (unsigned long)-1;
263 ret = search_slot(root, &key, &path, 0, 0);
265 release_path(root, &path);
268 slot = path.slots[0];
271 release_path(root, &path);
276 for (i = slot; i >= 0; i--) {
277 found = path.nodes[0]->leaf.items[i].key.objectid;
278 radix_tree_preload(GFP_KERNEL);
279 ret = radix_tree_insert(radix, found, (void *)found);
282 "failed to insert %lu into radix\n",
287 radix_tree_preload_end();
289 release_path(root, &path);
290 key.objectid = found - 1;
291 if (key.objectid > found)
296 void sigstopper(int ignored)
299 fprintf(stderr, "caught exit signal, stopping\n");
302 int print_usage(void)
304 printf("usage: tester [-ih] [-c count] [-f count]\n");
305 printf("\t -c count -- iteration count after filling\n");
306 printf("\t -f count -- run this many random inserts before starting\n");
307 printf("\t -i -- only do initial fill\n");
308 printf("\t -h -- this help text\n");
311 int main(int ac, char **av)
313 RADIX_TREE(radix, GFP_KERNEL);
314 struct ctree_root *root;
319 int iterations = 20000;
320 int init_fill_count = 800000;
322 int initial_only = 0;
324 root = open_ctree("dbfile", &super);
325 fill_radix(root, &radix);
327 signal(SIGTERM, sigstopper);
328 signal(SIGINT, sigstopper);
330 for (i = 1 ; i < ac ; i++) {
331 if (strcmp(av[i], "-i") == 0) {
333 } else if (strcmp(av[i], "-c") == 0) {
334 iterations = atoi(av[i+1]);
336 } else if (strcmp(av[i], "-f") == 0) {
337 init_fill_count = atoi(av[i+1]);
343 printf("initial fill\n");
344 ret = fill_tree(root, &radix, init_fill_count);
345 printf("starting run\n");
350 if (initial_only == 1) {
353 for (i = 0; i < iterations; i++) {
354 op = rand() % ARRAY_SIZE(ops);
355 count = rand() % 128;
360 if (i && i % 5000 == 0) {
361 printf("open & close, root level %d nritems %d\n",
362 btrfs_header_level(&root->node->node.header),
363 btrfs_header_nritems(&root->node->node.header));
364 close_ctree(root, &super);
365 root = open_ctree("dbfile", &super);
368 ret = ops[op](root, &radix);
370 fprintf(stderr, "op %d failed %d:%d\n",
372 print_tree(root, root->node);
373 fprintf(stderr, "op %d failed %d:%d\n",
378 if (ops[op] == bulk_op)
380 if (keep_running == 0) {
387 close_ctree(root, &super);