5 #include "kerncompat.h"
6 #include "radix-tree.h"
9 #include "print-tree.h"
11 #include "transaction.h"
14 struct btrfs_super_block super
;
15 static u64 dir_oid
= 44556;
16 static u64 file_oid
= 33778;
18 static int find_num(struct radix_tree_root
*root
, unsigned long *num_ret
,
21 unsigned long num
= rand();
26 ret
= radix_tree_gang_lookup(root
, (void **)res
, num
, 2);
31 } else if (ret
!= 0 && num
== res
[0]) {
33 if (ret
> 1 && num
== res
[1]) {
42 static int ins_one(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
43 struct radix_tree_root
*radix
)
48 struct btrfs_path path
;
50 find_num(radix
, &oid
, 0);
51 sprintf(buf
, "str-%lu", oid
);
53 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
58 radix_tree_preload(GFP_KERNEL
);
59 ret
= radix_tree_insert(radix
, oid
, (void *)oid
);
60 radix_tree_preload_end();
69 * if we got an EEXIST, it may be due to hash collision, double
72 btrfs_init_path(&path
);
73 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
77 if (!btrfs_match_dir_item_name(root
, &path
, buf
, strlen(buf
))) {
78 struct btrfs_dir_item
*di
;
84 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
85 struct btrfs_dir_item
);
86 found
= (char *)(di
+ 1);
87 found_len
= btrfs_dir_name_len(di
);
88 btrfs_name_hash(buf
, strlen(buf
), &myhash
);
89 btrfs_name_hash(found
, found_len
, &foundhash
);
90 if (myhash
!= foundhash
)
92 btrfs_release_path(root
, &path
);
96 btrfs_release_path(root
, &path
);
98 printf("failed to insert %lu ret %d\n", oid
, ret
);
102 static int insert_dup(struct btrfs_trans_handle
*trans
, struct btrfs_root
103 *root
, struct radix_tree_root
*radix
)
109 ret
= find_num(radix
, &oid
, 1);
112 sprintf(buf
, "str-%lu", oid
);
114 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
116 if (ret
!= -EEXIST
) {
117 printf("insert on %s gave us %d\n", buf
, ret
);
123 static int del_one(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
124 struct radix_tree_root
*radix
)
129 struct btrfs_path path
;
132 ret
= find_num(radix
, &oid
, 1);
135 sprintf(buf
, "str-%lu", oid
);
136 btrfs_init_path(&path
);
137 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
141 ret
= btrfs_del_item(trans
, root
, &path
);
144 btrfs_release_path(root
, &path
);
145 ptr
= radix_tree_delete(radix
, oid
);
152 btrfs_release_path(root
, &path
);
154 printf("failed to delete %lu %d\n", oid
, ret
);
158 static int lookup_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
159 *root
, struct radix_tree_root
*radix
)
161 struct btrfs_path path
;
166 ret
= find_num(radix
, &oid
, 1);
169 sprintf(buf
, "str-%lu", oid
);
170 btrfs_init_path(&path
);
171 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
173 btrfs_release_path(root
, &path
);
175 printf("unable to find key %lu\n", oid
);
181 static int lookup_enoent(struct btrfs_trans_handle
*trans
, struct btrfs_root
182 *root
, struct radix_tree_root
*radix
)
184 struct btrfs_path path
;
189 ret
= find_num(radix
, &oid
, 0);
192 sprintf(buf
, "str-%lu", oid
);
193 btrfs_init_path(&path
);
194 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
196 btrfs_release_path(root
, &path
);
198 printf("able to find key that should not exist %lu\n", oid
);
204 static int empty_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
205 *root
, struct radix_tree_root
*radix
, int nr
)
207 struct btrfs_path path
;
208 struct btrfs_key key
;
209 unsigned long found
= 0;
216 struct btrfs_dir_item
*di
;
218 key
.offset
= (u64
)-1;
220 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
221 key
.objectid
= dir_oid
;
223 btrfs_init_path(&path
);
224 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, -1, 1);
226 btrfs_release_path(root
, &path
);
230 if (path
.slots
[0] == 0) {
231 btrfs_release_path(root
, &path
);
236 slot
= path
.slots
[0];
237 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, slot
,
238 struct btrfs_dir_item
);
239 found_len
= btrfs_dir_name_len(di
);
240 memcpy(buf
, (char *)(di
+ 1), found_len
);
241 BUG_ON(found_len
> 128);
242 buf
[found_len
] = '\0';
243 found
= atoi(buf
+ 4);
244 ret
= btrfs_del_item(trans
, root
, &path
);
248 "failed to remove %lu from tree\n",
252 btrfs_release_path(root
, &path
);
253 ptr
= radix_tree_delete(radix
, found
);
261 fprintf(stderr
, "failed to delete from the radix %lu\n", found
);
265 static int fill_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
266 struct radix_tree_root
*radix
, int count
)
270 for (i
= 0; i
< count
; i
++) {
271 ret
= ins_one(trans
, root
, radix
);
273 fprintf(stderr
, "fill failed\n");
277 ret
= btrfs_commit_transaction(trans
, root
, &super
);
279 fprintf(stderr
, "fill commit failed\n");
283 if (i
&& i
% 10000 == 0) {
284 printf("bigfill %d\n", i
);
293 static int bulk_op(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
294 struct radix_tree_root
*radix
)
297 int nr
= rand() % 5000;
298 static int run_nr
= 0;
300 /* do the bulk op much less frequently */
303 ret
= empty_tree(trans
, root
, radix
, nr
);
306 ret
= fill_tree(trans
, root
, radix
, nr
);
313 int (*ops
[])(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
, struct
314 radix_tree_root
*radix
) =
315 { ins_one
, insert_dup
, del_one
, lookup_item
,
316 lookup_enoent
, bulk_op
};
318 void sigstopper(int ignored
)
321 fprintf(stderr
, "caught exit signal, stopping\n");
324 int print_usage(void)
326 printf("usage: tester [-ih] [-c count] [-f count]\n");
327 printf("\t -c count -- iteration count after filling\n");
328 printf("\t -f count -- run this many random inserts before starting\n");
329 printf("\t -i -- only do initial fill\n");
330 printf("\t -h -- this help text\n");
333 int main(int ac
, char **av
)
335 RADIX_TREE(radix
, GFP_KERNEL
);
336 struct btrfs_root
*root
;
341 int iterations
= 20000;
342 int init_fill_count
= 800000;
344 int initial_only
= 0;
345 struct btrfs_trans_handle
*trans
;
348 printf("removing old tree\n");
350 root
= open_ctree("dbfile", &super
);
351 trans
= btrfs_start_transaction(root
, 1);
353 signal(SIGTERM
, sigstopper
);
354 signal(SIGINT
, sigstopper
);
356 for (i
= 1 ; i
< ac
; i
++) {
357 if (strcmp(av
[i
], "-i") == 0) {
359 } else if (strcmp(av
[i
], "-c") == 0) {
360 iterations
= atoi(av
[i
+1]);
362 } else if (strcmp(av
[i
], "-f") == 0) {
363 init_fill_count
= atoi(av
[i
+1]);
369 printf("initial fill\n");
370 ret
= fill_tree(trans
, root
, &radix
, init_fill_count
);
371 printf("starting run\n");
376 if (initial_only
== 1) {
379 for (i
= 0; i
< iterations
; i
++) {
380 op
= rand() % ARRAY_SIZE(ops
);
381 count
= rand() % 128;
386 if (i
&& i
% 5000 == 0) {
387 printf("open & close, root level %d nritems %d\n",
388 btrfs_header_level(&root
->node
->node
.header
),
389 btrfs_header_nritems(&root
->node
->node
.header
));
390 close_ctree(root
, &super
);
391 root
= open_ctree("dbfile", &super
);
394 ret
= ops
[op
](trans
, root
, &radix
);
396 fprintf(stderr
, "op %d failed %d:%d\n",
398 btrfs_print_tree(root
, root
->node
);
399 fprintf(stderr
, "op %d failed %d:%d\n",
404 if (ops
[op
] == bulk_op
)
406 if (keep_running
== 0) {
413 close_ctree(root
, &super
);