btrfs-progs/quick-test.c

177 lines
4.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
2007-03-16 20:20:31 +00:00
#include "transaction.h"
/* for testing only */
int next_key(int i, int max_key) {
return rand() % max_key;
2007-03-10 11:35:47 +00:00
// return i;
}
int main(int ac, char **av) {
2007-03-12 20:22:34 +00:00
struct btrfs_key ins;
struct btrfs_key last = { (u64)-1, 0, 0};
char *buf;
int i;
int num;
int ret;
2007-03-07 01:08:01 +00:00
int run_size = 100000;
int max_key = 100000000;
int tree_size = 0;
2007-03-13 14:46:10 +00:00
struct btrfs_path path;
struct btrfs_super_block super;
struct btrfs_root *root;
2007-03-16 20:20:31 +00:00
struct btrfs_trans_handle *trans;
2007-04-04 17:27:22 +00:00
buf = malloc(512);
memset(buf, 0, 512);
radix_tree_init();
2007-04-02 18:18:17 +00:00
root = open_ctree(av[1], &super);
2007-03-16 20:20:31 +00:00
trans = btrfs_start_transaction(root, 1);
srand(55);
ins.flags = 0;
btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
// num = i;
sprintf(buf, "string-%d", num);
if (i % 10000 == 0)
fprintf(stderr, "insert %d:%d\n", num, i);
ins.objectid = num;
ins.offset = 0;
2007-04-04 17:27:22 +00:00
ret = btrfs_insert_item(trans, root, &ins, buf, 512);
if (!ret)
tree_size++;
2007-03-10 11:35:47 +00:00
if (i == run_size - 5) {
2007-03-16 20:20:31 +00:00
btrfs_commit_transaction(trans, root, &super);
2007-03-10 11:35:47 +00:00
}
}
2007-03-07 01:08:01 +00:00
close_ctree(root, &super);
2007-04-04 17:27:22 +00:00
root = open_ctree(av[1], &super);
printf("starting search\n");
srand(55);
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
ins.objectid = num;
2007-03-13 14:46:10 +00:00
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
2007-03-16 20:20:31 +00:00
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
2007-03-13 14:46:10 +00:00
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
exit(1);
}
2007-03-13 14:46:10 +00:00
btrfs_release_path(root, &path);
}
2007-03-07 01:08:01 +00:00
close_ctree(root, &super);
2007-04-04 17:27:22 +00:00
root = open_ctree(av[1], &super);
printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
2007-03-12 16:01:18 +00:00
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header),
2007-03-14 18:14:43 +00:00
BTRFS_NODEPTRS_PER_BLOCK(root) -
2007-03-12 16:01:18 +00:00
btrfs_header_nritems(&root->node->node.header));
printf("all searches good, deleting some items\n");
i = 0;
srand(55);
for (i = 0 ; i < run_size/4; i++) {
num = next_key(i, max_key);
ins.objectid = num;
2007-03-13 14:46:10 +00:00
btrfs_init_path(&path);
2007-03-16 20:20:31 +00:00
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (!ret) {
if (i % 10000 == 0)
fprintf(stderr, "del %d:%d\n", num, i);
2007-03-16 20:20:31 +00:00
ret = btrfs_del_item(trans, root, &path);
if (ret != 0)
BUG();
tree_size--;
}
2007-03-13 14:46:10 +00:00
btrfs_release_path(root, &path);
}
2007-03-07 01:08:01 +00:00
close_ctree(root, &super);
2007-04-04 17:27:22 +00:00
root = open_ctree(av[1], &super);
srand(128);
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
sprintf(buf, "string-%d", num);
ins.objectid = num;
if (i % 10000 == 0)
fprintf(stderr, "insert %d:%d\n", num, i);
2007-04-04 17:27:22 +00:00
ret = btrfs_insert_item(trans, root, &ins, buf, 512);
if (!ret)
tree_size++;
}
2007-03-07 01:08:01 +00:00
close_ctree(root, &super);
2007-04-04 17:27:22 +00:00
root = open_ctree(av[1], &super);
srand(128);
printf("starting search2\n");
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
ins.objectid = num;
2007-03-13 14:46:10 +00:00
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
2007-03-16 20:20:31 +00:00
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
2007-03-13 14:46:10 +00:00
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
exit(1);
}
2007-03-13 14:46:10 +00:00
btrfs_release_path(root, &path);
}
printf("starting big long delete run\n");
2007-03-12 16:01:18 +00:00
while(root->node &&
btrfs_header_nritems(&root->node->node.header) > 0) {
2007-03-13 14:46:10 +00:00
struct btrfs_leaf *leaf;
int slot;
ins.objectid = (u64)-1;
2007-03-13 14:46:10 +00:00
btrfs_init_path(&path);
2007-03-16 20:20:31 +00:00
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (ret == 0)
BUG();
leaf = &path.nodes[0]->leaf;
slot = path.slots[0];
2007-03-12 16:01:18 +00:00
if (slot != btrfs_header_nritems(&leaf->header))
BUG();
while(path.slots[0] > 0) {
path.slots[0] -= 1;
slot = path.slots[0];
leaf = &path.nodes[0]->leaf;
2007-03-12 20:22:34 +00:00
btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
if (tree_size % 10000 == 0)
printf("big del %d:%d\n", tree_size, i);
2007-03-16 20:20:31 +00:00
ret = btrfs_del_item(trans, root, &path);
if (ret != 0) {
printf("del_item returned %d\n", ret);
BUG();
}
tree_size--;
}
2007-03-13 14:46:10 +00:00
btrfs_release_path(root, &path);
}
2007-03-07 01:08:01 +00:00
/*
printf("previous tree:\n");
2007-03-13 14:46:10 +00:00
btrfs_print_tree(root, root->commit_root);
2007-03-07 01:08:01 +00:00
printf("map before commit\n");
2007-03-13 14:46:10 +00:00
btrfs_print_tree(root->extent_root, root->extent_root->node);
2007-03-07 01:08:01 +00:00
*/
2007-03-16 20:20:31 +00:00
btrfs_commit_transaction(trans, root, &super);
printf("tree size is now %d\n", tree_size);
2007-03-07 01:08:01 +00:00
printf("root %p commit root %p\n", root->node, root->commit_root);
printf("map tree\n");
btrfs_print_tree(root->fs_info->extent_root,
root->fs_info->extent_root->node);
2007-03-07 01:08:01 +00:00
close_ctree(root, &super);
return 0;
}