quick-test.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"

/* for testing only */
int next_key(int i, int max_key) {
	return rand() % max_key;
C
Chris Mason 已提交
12
	// return i;
13 14 15
}

int main(int ac, char **av) {
C
Chris Mason 已提交
16 17
	struct btrfs_key ins;
	struct btrfs_key last = { (u64)-1, 0, 0};
18 19 20 21
	char *buf;
	int i;
	int num;
	int ret;
22
	int run_size = 100000;
23 24
	int max_key =  100000000;
	int tree_size = 0;
C
Chris Mason 已提交
25 26 27
	struct btrfs_path path;
	struct btrfs_super_block super;
	struct btrfs_root *root;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

	radix_tree_init();

	root = open_ctree("dbfile", &super);
	srand(55);
	for (i = 0; i < run_size; i++) {
		buf = malloc(64);
		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;
		ins.flags = 0;
C
Chris Mason 已提交
43
		ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
44 45 46
		if (!ret)
			tree_size++;
		free(buf);
C
Chris Mason 已提交
47
		if (i == run_size - 5) {
C
Chris Mason 已提交
48
			btrfs_commit_transaction(root, &super);
C
Chris Mason 已提交
49
		}
50

51
	}
52
	close_ctree(root, &super);
53 54 55 56 57 58 59

	root = open_ctree("dbfile", &super);
	printf("starting search\n");
	srand(55);
	for (i = 0; i < run_size; i++) {
		num = next_key(i, max_key);
		ins.objectid = num;
C
Chris Mason 已提交
60
		btrfs_init_path(&path);
61 62
		if (i % 10000 == 0)
			fprintf(stderr, "search %d:%d\n", num, i);
C
Chris Mason 已提交
63
		ret = btrfs_search_slot(root, &ins, &path, 0, 0);
64
		if (ret) {
C
Chris Mason 已提交
65
			btrfs_print_tree(root, root->node);
66 67 68
			printf("unable to find %d\n", num);
			exit(1);
		}
C
Chris Mason 已提交
69
		btrfs_release_path(root, &path);
70
	}
71
	close_ctree(root, &super);
72 73
	root = open_ctree("dbfile", &super);
	printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
74 75 76 77
	        btrfs_header_level(&root->node->node.header),
		btrfs_header_nritems(&root->node->node.header),
		NODEPTRS_PER_BLOCK -
		btrfs_header_nritems(&root->node->node.header));
78 79 80 81 82 83
	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;
C
Chris Mason 已提交
84 85
		btrfs_init_path(&path);
		ret = btrfs_search_slot(root, &ins, &path, -1, 1);
86 87 88
		if (!ret) {
			if (i % 10000 == 0)
				fprintf(stderr, "del %d:%d\n", num, i);
C
Chris Mason 已提交
89
			ret = btrfs_del_item(root, &path);
90 91 92 93
			if (ret != 0)
				BUG();
			tree_size--;
		}
C
Chris Mason 已提交
94
		btrfs_release_path(root, &path);
95
	}
96
	close_ctree(root, &super);
97 98 99 100 101 102 103 104 105
	root = open_ctree("dbfile", &super);
	srand(128);
	for (i = 0; i < run_size; i++) {
		buf = malloc(64);
		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);
C
Chris Mason 已提交
106
		ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
107 108 109 110
		if (!ret)
			tree_size++;
		free(buf);
	}
111
	close_ctree(root, &super);
112 113 114 115 116 117
	root = open_ctree("dbfile", &super);
	srand(128);
	printf("starting search2\n");
	for (i = 0; i < run_size; i++) {
		num = next_key(i, max_key);
		ins.objectid = num;
C
Chris Mason 已提交
118
		btrfs_init_path(&path);
119 120
		if (i % 10000 == 0)
			fprintf(stderr, "search %d:%d\n", num, i);
C
Chris Mason 已提交
121
		ret = btrfs_search_slot(root, &ins, &path, 0, 0);
122
		if (ret) {
C
Chris Mason 已提交
123
			btrfs_print_tree(root, root->node);
124 125 126
			printf("unable to find %d\n", num);
			exit(1);
		}
C
Chris Mason 已提交
127
		btrfs_release_path(root, &path);
128 129
	}
	printf("starting big long delete run\n");
130 131
	while(root->node &&
	      btrfs_header_nritems(&root->node->node.header) > 0) {
C
Chris Mason 已提交
132
		struct btrfs_leaf *leaf;
133 134
		int slot;
		ins.objectid = (u64)-1;
C
Chris Mason 已提交
135 136
		btrfs_init_path(&path);
		ret = btrfs_search_slot(root, &ins, &path, -1, 1);
137 138 139 140 141
		if (ret == 0)
			BUG();

		leaf = &path.nodes[0]->leaf;
		slot = path.slots[0];
142
		if (slot != btrfs_header_nritems(&leaf->header))
143 144 145 146 147 148
			BUG();
		while(path.slots[0] > 0) {
			path.slots[0] -= 1;
			slot = path.slots[0];
			leaf = &path.nodes[0]->leaf;

C
Chris Mason 已提交
149
			btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
150 151
			if (tree_size % 10000 == 0)
				printf("big del %d:%d\n", tree_size, i);
C
Chris Mason 已提交
152
			ret = btrfs_del_item(root, &path);
153 154 155 156 157 158
			if (ret != 0) {
				printf("del_item returned %d\n", ret);
				BUG();
			}
			tree_size--;
		}
C
Chris Mason 已提交
159
		btrfs_release_path(root, &path);
160
	}
161 162
	/*
	printf("previous tree:\n");
C
Chris Mason 已提交
163
	btrfs_print_tree(root, root->commit_root);
164
	printf("map before commit\n");
C
Chris Mason 已提交
165
	btrfs_print_tree(root->extent_root, root->extent_root->node);
166
	*/
C
Chris Mason 已提交
167
	btrfs_commit_transaction(root, &super);
168
	printf("tree size is now %d\n", tree_size);
169
	printf("root %p commit root %p\n", root->node, root->commit_root);
170
	printf("map tree\n");
C
Chris Mason 已提交
171
	btrfs_print_tree(root->extent_root, root->extent_root->node);
172
	close_ctree(root, &super);
173 174
	return 0;
}