printf cleanups
[platform/upstream/btrfs-progs.git] / random-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include "kerncompat.h"
5 #include "radix-tree.h"
6 #include "ctree.h"
7 #include "disk-io.h"
8 #include "print-tree.h"
9 #include "transaction.h"
10
11 int keep_running = 1;
12 struct btrfs_super_block super;
13
14 static int setup_key(struct radix_tree_root *root, struct btrfs_key *key,
15                      int exists)
16 {
17         int num = rand();
18         unsigned long res[2];
19         int ret;
20
21         key->flags = 0;
22         btrfs_set_key_type(key, BTRFS_STRING_ITEM_KEY);
23         key->offset = 0;
24 again:
25         ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
26         if (exists) {
27                 if (ret == 0)
28                         return -1;
29                 num = res[0];
30         } else if (ret != 0 && num == res[0]) {
31                 num++;
32                 if (ret > 1 && num == res[1]) {
33                         num++;
34                         goto again;
35                 }
36         }
37         key->objectid = num;
38         return 0;
39 }
40
41 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
42                    struct radix_tree_root *radix)
43 {
44         struct btrfs_path path;
45         struct btrfs_key key;
46         int ret;
47         char buf[128];
48         unsigned long oid;
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));
53         if (ret)
54                 goto error;
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();
59         if (ret)
60                 goto error;
61         return ret;
62 error:
63         printf("failed to insert %llu\n", (unsigned long long)key.objectid);
64         return -1;
65 }
66
67 static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
68                       *root, struct radix_tree_root *radix)
69 {
70         struct btrfs_path path;
71         struct btrfs_key key;
72         int ret;
73         char buf[128];
74         btrfs_init_path(&path);
75         ret = setup_key(radix, &key, 1);
76         if (ret < 0)
77                 return 0;
78         sprintf(buf, "str-%llu\n", (unsigned long long)key.objectid);
79         ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
80         if (ret != -EEXIST) {
81                 printf("insert on %llu gave us %d\n",
82                        (unsigned long long)key.objectid, ret);
83                 return 1;
84         }
85         return 0;
86 }
87
88 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
89                    struct radix_tree_root *radix)
90 {
91         struct btrfs_path path;
92         struct btrfs_key key;
93         int ret;
94         unsigned long *ptr;
95         btrfs_init_path(&path);
96         ret = setup_key(radix, &key, 1);
97         if (ret < 0)
98                 return 0;
99         ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
100         if (ret)
101                 goto error;
102         ret = btrfs_del_item(trans, root, &path);
103         btrfs_release_path(root, &path);
104         if (ret != 0)
105                 goto error;
106         ptr = radix_tree_delete(radix, key.objectid);
107         if (!ptr)
108                 goto error;
109         return 0;
110 error:
111         printf("failed to delete %llu\n", (unsigned long long)key.objectid);
112         return -1;
113 }
114
115 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
116                        *root, struct radix_tree_root *radix)
117 {
118         struct btrfs_path path;
119         struct btrfs_key key;
120         int ret;
121         btrfs_init_path(&path);
122         ret = setup_key(radix, &key, 1);
123         if (ret < 0)
124                 return 0;
125         ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
126         btrfs_release_path(root, &path);
127         if (ret)
128                 goto error;
129         return 0;
130 error:
131         printf("unable to find key %llu\n", (unsigned long long)key.objectid);
132         return -1;
133 }
134
135 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
136                          *root, struct radix_tree_root *radix)
137 {
138         struct btrfs_path path;
139         struct btrfs_key key;
140         int ret;
141         btrfs_init_path(&path);
142         ret = setup_key(radix, &key, 0);
143         if (ret < 0)
144                 return ret;
145         ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
146         btrfs_release_path(root, &path);
147         if (ret <= 0)
148                 goto error;
149         return 0;
150 error:
151         printf("able to find key that should not exist %llu\n",
152                (unsigned long long)key.objectid);
153         return -1;
154 }
155
156 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
157                       *root, struct radix_tree_root *radix, int nr)
158 {
159         struct btrfs_path path;
160         struct btrfs_key key;
161         unsigned long found = 0;
162         int ret;
163         int slot;
164         int *ptr;
165         int count = 0;
166
167         key.offset = 0;
168         key.flags = 0;
169         btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
170         key.objectid = (unsigned long)-1;
171         while(nr-- >= 0) {
172                 btrfs_init_path(&path);
173                 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
174                 if (ret < 0) {
175                         btrfs_release_path(root, &path);
176                         return ret;
177                 }
178                 if (ret != 0) {
179                         if (path.slots[0] == 0) {
180                                 btrfs_release_path(root, &path);
181                                 break;
182                         }
183                         path.slots[0] -= 1;
184                 }
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);
189                 count++;
190                 if (ret) {
191                         fprintf(stderr,
192                                 "failed to remove %lu from tree\n",
193                                 found);
194                         return -1;
195                 }
196                 btrfs_release_path(root, &path);
197                 ptr = radix_tree_delete(radix, found);
198                 if (!ptr)
199                         goto error;
200                 if (!keep_running)
201                         break;
202         }
203         return 0;
204 error:
205         fprintf(stderr, "failed to delete from the radix %lu\n", found);
206         return -1;
207 }
208
209 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
210                      struct radix_tree_root *radix, int count)
211 {
212         int i;
213         int ret = 0;
214         for (i = 0; i < count; i++) {
215                 ret = ins_one(trans, root, radix);
216                 if (ret) {
217                         fprintf(stderr, "fill failed\n");
218                         goto out;
219                 }
220                 if (i % 1000 == 0) {
221                         ret = btrfs_commit_transaction(trans, root, &super);
222                         if (ret) {
223                                 fprintf(stderr, "fill commit failed\n");
224                                 return ret;
225                         }
226                 }
227                 if (i && i % 10000 == 0) {
228                         printf("bigfill %d\n", i);
229                 }
230                 if (!keep_running)
231                         break;
232         }
233 out:
234         return ret;
235 }
236
237 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
238                    struct radix_tree_root *radix)
239 {
240         int ret;
241         int nr = rand() % 5000;
242         static int run_nr = 0;
243
244         /* do the bulk op much less frequently */
245         if (run_nr++ % 100)
246                 return 0;
247         ret = empty_tree(trans, root, radix, nr);
248         if (ret)
249                 return ret;
250         ret = fill_tree(trans, root, radix, nr);
251         if (ret)
252                 return ret;
253         return 0;
254 }
255
256
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 };
261
262 static int fill_radix(struct btrfs_root *root, struct radix_tree_root *radix)
263 {
264         struct btrfs_path path;
265         struct btrfs_key key;
266         unsigned long found = 0;
267         int ret;
268         int slot;
269         int i;
270
271         key.offset = 0;
272         key.flags = 0;
273         btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
274         key.objectid = (unsigned long)-1;
275         while(1) {
276                 btrfs_init_path(&path);
277                 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
278                 if (ret < 0) {
279                         btrfs_release_path(root, &path);
280                         return ret;
281                 }
282                 slot = path.slots[0];
283                 if (ret != 0) {
284                         if (slot == 0) {
285                                 btrfs_release_path(root, &path);
286                                 break;
287                         }
288                         slot -= 1;
289                 }
290                 for (i = slot; i >= 0; i--) {
291                         found = btrfs_disk_key_objectid(&path.nodes[0]->
292                                                         leaf.items[i].key);
293                         radix_tree_preload(GFP_KERNEL);
294                         ret = radix_tree_insert(radix, found, (void *)found);
295                         if (ret) {
296                                 fprintf(stderr,
297                                         "failed to insert %lu into radix\n",
298                                         found);
299                                 exit(1);
300                         }
301
302                         radix_tree_preload_end();
303                 }
304                 btrfs_release_path(root, &path);
305                 key.objectid = found - 1;
306                 if (key.objectid > found)
307                         break;
308         }
309         return 0;
310 }
311 void sigstopper(int ignored)
312 {
313         keep_running = 0;
314         fprintf(stderr, "caught exit signal, stopping\n");
315 }
316
317 int print_usage(void)
318 {
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");
324         exit(1);
325 }
326 int main(int ac, char **av)
327 {
328         RADIX_TREE(radix, GFP_KERNEL);
329         struct btrfs_root *root;
330         int i;
331         int ret;
332         int count;
333         int op;
334         int iterations = 20000;
335         int init_fill_count = 800000;
336         int err = 0;
337         int initial_only = 0;
338         struct btrfs_trans_handle *trans;
339         radix_tree_init();
340         root = open_ctree("dbfile", &super);
341         fill_radix(root, &radix);
342
343         signal(SIGTERM, sigstopper);
344         signal(SIGINT, sigstopper);
345
346         for (i = 1 ; i < ac ; i++) {
347                 if (strcmp(av[i], "-i") == 0) {
348                         initial_only = 1;
349                 } else if (strcmp(av[i], "-c") == 0) {
350                         iterations = atoi(av[i+1]);
351                         i++;
352                 } else if (strcmp(av[i], "-f") == 0) {
353                         init_fill_count = atoi(av[i+1]);
354                         i++;
355                 } else {
356                         print_usage();
357                 }
358         }
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");
363         if (ret) {
364                 err = ret;
365                 goto out;
366         }
367         if (initial_only == 1) {
368                 goto out;
369         }
370         for (i = 0; i < iterations; i++) {
371                 op = rand() % ARRAY_SIZE(ops);
372                 count = rand() % 128;
373                 if (i % 2000 == 0) {
374                         printf("%d\n", i);
375                         fflush(stdout);
376                 }
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);
383                 }
384                 while(count--) {
385                         ret = ops[op](trans, root, &radix);
386                         if (ret) {
387                                 fprintf(stderr, "op %d failed %d:%d\n",
388                                         op, i, iterations);
389                                 btrfs_print_tree(root, root->node);
390                                 fprintf(stderr, "op %d failed %d:%d\n",
391                                         op, i, iterations);
392                                 err = ret;
393                                 goto out;
394                         }
395                         if (ops[op] == bulk_op)
396                                 break;
397                         if (keep_running == 0) {
398                                 err = 0;
399                                 goto out;
400                         }
401                 }
402         }
403 out:
404         close_ctree(root, &super);
405         return err;
406 }
407