• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Metadata stress testing program for file system
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; version
7  * 2.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should find a copy of v2 of the GNU General Public License somewhere
15  * on your Linux system; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This program helps you to generate a k-tree in recusive.
19  *
20  * A k-tree is a tranformation of binary tree, A binary has 2
21  * children at most in each node, but a k-tree has k sub-nodes.
22  * We test both file and directory entries, So we do some changes.
23  * We create k sub directories and k text file in each parent.
24  *
25  * Copyright (C) 2009, Intel Corp.
26  * Author: Shaohui Zheng <shaohui.zheng@intel.com>
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 
37 #define MAX_PATH 8192
38 
39 /*
40  * k tree generator.
41  *
42  * parameters:
43  * lvl: tree level number from button to top
44  * node_nr: the maximun nodes number
45  * return val: if it is leaf, return 0, or return 1
46  */
47 
k_tree_gen(int lvl,int node_nr)48 int k_tree_gen(int lvl, int node_nr)
49 {
50 	int cnt;
51 	char dir[MAX_PATH], cwd[MAX_PATH], leaf[MAX_PATH];
52 	if (lvl <= 0)
53 		return 0;
54 
55 	for (cnt = 0; cnt < node_nr; cnt++) {
56 		int fd = 0, fd2;
57 		// generate dir name or file name
58 		sprintf(dir, "%d-d", cnt);
59 		sprintf(leaf, "%d-f", cnt);
60 
61 		// create an empty file
62 		// API: open,close,dup,read,write,lseek
63 		fd = open(leaf, O_CREAT | O_RDWR);
64 		fd2 = dup(fd);
65 		close(fd);
66 		fd = fd2;
67 		write(fd, leaf, 3);
68 		lseek(fd, SEEK_SET, 0);
69 		read(fd, leaf, 3);
70 		close(fd);
71 
72 		// create directory entry
73 		mkdir(dir, 0777);
74 		getcwd(cwd, sizeof(cwd));
75 		chdir(dir);
76 		k_tree_gen(lvl - 1, node_nr);
77 		chdir(cwd);
78 	}
79 
80 	return 1;
81 }
82 
main(int argc,char ** argv)83 int main(int argc, char **argv)
84 {
85 	if (argc < 2) {
86 		printf("Usage: k-tree tree_depth tree_width\n");
87 		return 1;
88 	}
89 
90 	printf("Generate k tree (depth: %s, width: %s) ...\n", argv[1],
91 	       argv[2]);
92 	k_tree_gen(atoi(argv[1]), atoi(argv[2]));
93 	printf("Generate k tree (depth: %s, width: %s), done\n", argv[1],
94 	       argv[2]);
95 	return 0;
96 }
97