Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[fs/lustre-release.git] / lustre / tests / rename_many.c
1 #define PATH_LENGTH 35
2 #include <math.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12
13 struct names {
14         char from[PATH_LENGTH];
15         char to[PATH_LENGTH];
16 } *names;
17
18 unsigned int loop_count = 500;
19 int file_count = 1000;
20 int seed;
21 int loops;
22 int stop;
23 long start;
24
25 int opt_exit_on_err;
26 int opt_verbose;
27 int opt_create_only;
28 int opt_rename_only;
29 int creat_errors;
30 int rename_errors;
31 int unlink_errors;
32
33 void usage(const char *progname)
34 {
35         fprintf(stderr, "usage: %s [-n numfiles] [-s seed] [-v] [-x] [dir]\n"
36                 "\t-c: only do the create step of first loop\n"
37                 "\t-f: number of files to create/rename/unlink per loop\n"
38                 "\t-n: number of test loops (0 to run forever)\n"
39                 "\t-r: only do the rename step of first loop\n"
40                 "\t-s: starting seed (equals loop number by default)\n"
41                 "\t-v: verbose\n"
42                 "\t-x: don't exit on error\n", progname);
43 }
44
45 void handler(int sig) {
46         static long last_time;
47         long now = time(0);
48
49         signal(SIGINT, handler);
50         signal(SIGALRM, handler);
51         printf("%6lds %8d iterations %d/%d/%d errors",
52                now - start, loops, creat_errors, rename_errors, unlink_errors);
53         if (sig != 0)
54                 printf(" - use SIGQUIT (^\\) or ^C^C to kill\n");
55         else
56                 printf("\n");
57
58         if (sig == SIGQUIT)
59                 stop = 1;
60         else if (sig == SIGINT) {
61                 if (now - last_time < 2)
62                         stop = 1;
63                 last_time = now;
64         }
65         alarm(60);
66 }
67
68 extern char *optarg;
69 extern int optind;
70
71 int main(int argc, char *argv[])
72 {
73         unsigned long n;
74         char msg[100], c, *end = NULL;
75         int h1, h2;
76         int i;
77
78         while ((c = getopt(argc, argv, "cf:n:rs:vx")) != EOF) {
79                 switch(c) {
80                 case 'c':
81                         ++opt_create_only;
82                         break;
83                 case 'f':
84                         i = strtoul(optarg, &end, 0);
85                         if (i && end != NULL && *end == '\0') {
86                                 file_count = i;
87                         } else {
88                                 fprintf(stderr, "bad file count '%s'\n",optarg);
89                                 usage(argv[0]);
90                                 return 1;
91                         }
92                         break;
93                 case 'n':
94                         i = strtoul(optarg, &end, 0);
95                         if (i && end != NULL && *end == '\0') {
96                                 loop_count = i;
97                         } else {
98                                 fprintf(stderr, "bad loop count '%s'\n",optarg);
99                                 usage(argv[0]);
100                                 return 1;
101                         }
102                         break;
103                 case 'r':
104                         ++opt_rename_only;
105                         break;
106                 case 's':
107                         i = strtoul(optarg, &end, 0);
108                         if (end && *end == '\0') {
109                                 seed = i;
110                         } else {
111                                 seed = random();
112                                 fprintf(stderr, "using random seed %u\n", seed);
113                         }
114                         break;
115                 case 'v':
116                         ++opt_verbose;
117                         break;
118                 case 'x':
119                         ++opt_exit_on_err;
120                         break;
121                 default:
122                         usage(argv[0]);
123                         return 1;
124                 }
125         }
126
127         names = malloc(sizeof(struct names) * file_count);
128         if (names == NULL) {
129                 perror("calloc");
130                 return(1);
131         }
132
133         h2 = sprintf(msg, "%x", file_count); /* just to figure length */
134         h1 = (PATH_LENGTH - h2 - 2) / 4;
135
136         n = (1ULL << h1 * 4) - 1;
137
138         //printf("h1 = %d, h2 = %d n = %lu\n", h1, h2, n);
139
140         start = time(0);
141
142         signal(SIGQUIT, handler);
143         signal(SIGINT, handler);
144         signal(SIGALRM, handler);
145         signal(SIGUSR1, handler);
146         alarm(60);
147
148         if (argc > optind + 1) {
149                 fprintf(stderr, "too many extra args %d\n", argc - optind);
150                 usage(argv[0]);
151                 return 1;
152         } else if (argv[optind] != NULL) {
153                 if (chdir(argv[optind]) < 0) {
154                         sprintf(msg, "chdir '%s'\n", argv[optind]);
155                         perror(msg);
156                         return 2;
157                 }
158         }
159
160         while (!stop && loop_count != 0 && loops < loop_count) {
161                 int j,k,l,m;
162
163                 srand(seed + loops);
164                 if (mkdir("tmp", S_IRWXU) == -1) {
165                         perror("mkdir tmp");
166                         return(1);
167                 }
168                 if (chdir("tmp") == -1) {
169                         perror("chdir tmp");
170                         return(1);
171                 }
172
173                 for (i = 0; i < file_count ; i++) {
174                         j = random() & n;
175                         k = random() & n;
176                         l = random() & n;
177                         m = random() & n;
178                         sprintf(names[i].from, "%0*x%0*x%0*x%0*x0%0*x",
179                                 h1, j, h1, k, h1, l, h1, m, h2, i);
180                         sprintf(names[i].to, "%0*x%0*x%0*x%0*x1%0*x",
181                                 h1, j, h1, k, h1, l, h1, m, h2, i);
182
183                 }
184
185                 for (i = 0; i < file_count; i++) {
186                         if (mknod(names[i].from, S_IFREG | S_IRWXU, 0) == -1) {
187                                 sprintf(msg, "loop %d.%d: creat %s",
188                                         loops, i, names[i].from);
189                                 perror(msg);
190                                 creat_errors++;
191                                 if (!opt_exit_on_err)
192                                         return 4;
193                         }
194                 }
195
196                 if (opt_create_only)
197                         return 0;
198
199                 for (i = 0; i < file_count; i++) {
200                         if (rename(names[i].from, names[i].to) == -1) {
201                                 sprintf(msg, "loop %d.%d: rename %s to %s",
202                                         loops, i, names[i].from, names[i].to);
203                                 perror(msg);
204                                 rename_errors++;
205                                 if (!opt_exit_on_err)
206                                         return 4;
207                         }
208                 }
209
210                 if (opt_rename_only)
211                         return 0;
212
213                 for (i = 0; i < file_count; i++) {
214                         if (unlink(names[i].to) == -1) {
215                                 sprintf(msg, "loop %d.%d: unlink %s",
216                                         loops, i, names[i].to);
217                                 perror(msg);
218                                 unlink_errors++;
219                                 if (!opt_exit_on_err)
220                                         return 4;
221                         }
222                 }
223
224                 if (chdir("..") == -1) {
225                         perror("chdir ..");
226                         return(1);
227                 }
228
229                 if (rmdir("tmp") == -1) {
230                         if (chdir("tmp") == -1) {
231                                 perror("chdir tmp 2");
232                                 return(1);
233                         }
234                         for (i = 0; i < file_count; i++) {
235                                 if (unlink(names[i].from) != -1) {
236                                         fprintf(stderr, "loop %d.%d: "
237                                                 "unexpected file %s\n",
238                                                 loops, i, names[i].to);
239                                         unlink_errors++;
240                                         if (!opt_exit_on_err)
241                                                 return 4;
242                                 }
243                         }
244                         if (chdir("..") == -1) {
245                                 perror("chdir .. 2");
246                                 return(1);
247                         }
248                         if (rmdir("tmp") == -1) {
249                                 perror("rmdir tmp");
250                                 return(1);
251                         }
252                 }
253
254                 loops++;
255                 if (opt_verbose)
256                         handler(0);
257         }
258
259         if (!opt_verbose)
260                 handler(0);
261         return(0);
262 }