4 #include "kerncompat.h"
5 #include "radix-tree.h"
8 #include "print-tree.h"
9 #include "transaction.h"
12 struct btrfs_super_block super;
14 static int setup_key(struct radix_tree_root *root, struct btrfs_key *key,
22 btrfs_set_key_type(key, BTRFS_STRING_ITEM_KEY);
25 ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
30 } else if (ret != 0 && num == res[0]) {
32 if (ret > 1 && num == res[1]) {
41 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
42 struct radix_tree_root *radix)
44 struct btrfs_path path;
49 btrfs_init_path(&path);
50 ret = setup_key(radix, &key, 0);
51 sprintf(buf, "str-%llu\n", (unsigned long long)key.objectid);
52 ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
55 oid = (unsigned long)key.objectid;
56 radix_tree_preload(GFP_KERNEL);
57 ret = radix_tree_insert(radix, oid, (void *)oid);
58 radix_tree_preload_end();
63 printf("failed to insert %llu\n", (unsigned long long)key.objectid);
67 static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
68 *root, struct radix_tree_root *radix)
70 struct btrfs_path path;
74 btrfs_init_path(&path);
75 ret = setup_key(radix, &key, 1);
78 sprintf(buf, "str-%llu\n", (unsigned long long)key.objectid);
79 ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
81 printf("insert on %llu gave us %d\n",
82 (unsigned long long)key.objectid, ret);
88 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
89 struct radix_tree_root *radix)
91 struct btrfs_path path;
95 btrfs_init_path(&path);
96 ret = setup_key(radix, &key, 1);
99 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
102 ret = btrfs_del_item(trans, root, &path);
103 btrfs_release_path(root, &path);
106 ptr = radix_tree_delete(radix, key.objectid);
111 printf("failed to delete %llu\n", (unsigned long long)key.objectid);
115 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
116 *root, struct radix_tree_root *radix)
118 struct btrfs_path path;
119 struct btrfs_key key;
121 btrfs_init_path(&path);
122 ret = setup_key(radix, &key, 1);
125 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
126 btrfs_release_path(root, &path);
131 printf("unable to find key %llu\n", (unsigned long long)key.objectid);
135 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
136 *root, struct radix_tree_root *radix)
138 struct btrfs_path path;
139 struct btrfs_key key;
141 btrfs_init_path(&path);
142 ret = setup_key(radix, &key, 0);
145 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
146 btrfs_release_path(root, &path);
151 printf("able to find key that should not exist %llu\n",
152 (unsigned long long)key.objectid);
156 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
157 *root, struct radix_tree_root *radix, int nr)
159 struct btrfs_path path;
160 struct btrfs_key key;
161 unsigned long found = 0;
169 btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
170 key.objectid = (unsigned long)-1;
172 btrfs_init_path(&path);
173 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
175 btrfs_release_path(root, &path);
179 if (path.slots[0] == 0) {
180 btrfs_release_path(root, &path);
185 slot = path.slots[0];
186 found = btrfs_disk_key_objectid(
187 &path.nodes[0]->leaf.items[slot].key);
188 ret = btrfs_del_item(trans, root, &path);
192 "failed to remove %lu from tree\n",
196 btrfs_release_path(root, &path);
197 ptr = radix_tree_delete(radix, found);
205 fprintf(stderr, "failed to delete from the radix %lu\n", found);
209 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
210 struct radix_tree_root *radix, int count)
214 for (i = 0; i < count; i++) {
215 ret = ins_one(trans, root, radix);
217 fprintf(stderr, "fill failed\n");
221 ret = btrfs_commit_transaction(trans, root, &super);
223 fprintf(stderr, "fill commit failed\n");
227 if (i && i % 10000 == 0) {
228 printf("bigfill %d\n", i);
237 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
238 struct radix_tree_root *radix)
241 int nr = rand() % 5000;
242 static int run_nr = 0;
244 /* do the bulk op much less frequently */
247 ret = empty_tree(trans, root, radix, nr);
250 ret = fill_tree(trans, root, radix, nr);
257 int (*ops[])(struct btrfs_trans_handle *,
258 struct btrfs_root *root, struct radix_tree_root *radix) =
259 { ins_one, insert_dup, del_one, lookup_item,
260 lookup_enoent, bulk_op };
262 static int fill_radix(struct btrfs_root *root, struct radix_tree_root *radix)
264 struct btrfs_path path;
265 struct btrfs_key key;
266 unsigned long found = 0;
273 btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
274 key.objectid = (unsigned long)-1;
276 btrfs_init_path(&path);
277 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
279 btrfs_release_path(root, &path);
282 slot = path.slots[0];
285 btrfs_release_path(root, &path);
290 for (i = slot; i >= 0; i--) {
291 found = btrfs_disk_key_objectid(&path.nodes[0]->
293 radix_tree_preload(GFP_KERNEL);
294 ret = radix_tree_insert(radix, found, (void *)found);
297 "failed to insert %lu into radix\n",
302 radix_tree_preload_end();
304 btrfs_release_path(root, &path);
305 key.objectid = found - 1;
306 if (key.objectid > found)
311 void sigstopper(int ignored)
314 fprintf(stderr, "caught exit signal, stopping\n");
317 int print_usage(void)
319 printf("usage: tester [-ih] [-c count] [-f count]\n");
320 printf("\t -c count -- iteration count after filling\n");
321 printf("\t -f count -- run this many random inserts before starting\n");
322 printf("\t -i -- only do initial fill\n");
323 printf("\t -h -- this help text\n");
326 int main(int ac, char **av)
328 RADIX_TREE(radix, GFP_KERNEL);
329 struct btrfs_root *root;
334 int iterations = 20000;
335 int init_fill_count = 800000;
337 int initial_only = 0;
338 struct btrfs_trans_handle *trans;
340 root = open_ctree("dbfile", &super);
341 fill_radix(root, &radix);
343 signal(SIGTERM, sigstopper);
344 signal(SIGINT, sigstopper);
346 for (i = 1 ; i < ac ; i++) {
347 if (strcmp(av[i], "-i") == 0) {
349 } else if (strcmp(av[i], "-c") == 0) {
350 iterations = atoi(av[i+1]);
352 } else if (strcmp(av[i], "-f") == 0) {
353 init_fill_count = atoi(av[i+1]);
359 printf("initial fill\n");
360 trans = btrfs_start_transaction(root, 1);
361 ret = fill_tree(trans, root, &radix, init_fill_count);
362 printf("starting run\n");
367 if (initial_only == 1) {
370 for (i = 0; i < iterations; i++) {
371 op = rand() % ARRAY_SIZE(ops);
372 count = rand() % 128;
377 if (i && i % 5000 == 0) {
378 printf("open & close, root level %d nritems %d\n",
379 btrfs_header_level(&root->node->node.header),
380 btrfs_header_nritems(&root->node->node.header));
381 close_ctree(root, &super);
382 root = open_ctree("dbfile", &super);
385 ret = ops[op](trans, root, &radix);
387 fprintf(stderr, "op %d failed %d:%d\n",
389 btrfs_print_tree(root, root->node);
390 fprintf(stderr, "op %d failed %d:%d\n",
395 if (ops[op] == bulk_op)
397 if (keep_running == 0) {
404 close_ctree(root, &super);