Whamcloud - gitweb
b=3643
[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                                 fprintf(stderr, "bad seed '%s'\n", optarg);
112                                 usage(argv[0]);
113                                 return 1;
114                         }
115                         break;
116                 case 'v':
117                         ++opt_verbose;
118                         break;
119                 case 'x':
120                         ++opt_exit_on_err;
121                         break;
122                 default:
123                         usage(argv[0]);
124                         return 1;
125                 }
126         }
127
128         names = malloc(sizeof(struct names) * file_count);
129         if (names == NULL) {
130                 perror("calloc");
131                 return(1);
132         }
133
134         h2 = sprintf(msg, "%x", file_count); /* just to figure length */
135         h1 = (PATH_LENGTH - h2 - 2) / 4;
136
137         n = (1ULL << h1 * 4) - 1;
138
139         //printf("h1 = %d, h2 = %d n = %lu\n", h1, h2, n);
140
141         start = time(0);
142
143         signal(SIGQUIT, handler);
144         signal(SIGINT, handler);
145         signal(SIGALRM, handler);
146         signal(SIGUSR1, handler);
147         alarm(60);
148
149         if (argc > optind + 1) {
150                 fprintf(stderr, "too many extra args %d\n", argc - optind);
151                 usage(argv[0]);
152                 return 1;
153         } else if (argv[optind] != NULL) {
154                 if (chdir(argv[optind]) < 0) {
155                         sprintf(msg, "chdir '%s'\n", argv[optind]);
156                         perror(msg);
157                         return 2;
158                 }
159         }
160
161         while (!stop && loop_count != 0 && loops < loop_count) {
162                 int j,k,l,m;
163
164                 srand(seed + loops);
165                 if (mkdir("tmp", S_IRWXU) == -1) {
166                         perror("mkdir tmp");
167                         return(1);
168                 }
169                 if (chdir("tmp") == -1) {
170                         perror("chdir tmp");
171                         return(1);
172                 }
173
174                 for (i = 0; i < file_count ; i++) {
175                         j = random() & n;
176                         k = random() & n;
177                         l = random() & n;
178                         m = random() & n;
179                         sprintf(names[i].from, "%0*x%0*x%0*x%0*x0%0*x",
180                                 h1, j, h1, k, h1, l, h1, m, h2, i);
181                         sprintf(names[i].to, "%0*x%0*x%0*x%0*x1%0*x",
182                                 h1, j, h1, k, h1, l, h1, m, h2, i);
183
184                 }
185
186                 for (i = 0; i < file_count; i++) {
187                         if (mknod(names[i].from, S_IFREG | S_IRWXU, 0) == -1) {
188                                 sprintf(msg, "loop %d.%d: creat %s",
189                                         loops, i, names[i].from);
190                                 perror(msg);
191                                 creat_errors++;
192                                 if (!opt_exit_on_err)
193                                         return 4;
194                         }
195                 }
196
197                 if (opt_create_only)
198                         return 0;
199
200                 for (i = 0; i < file_count; i++) {
201                         if (rename(names[i].from, names[i].to) == -1) {
202                                 sprintf(msg, "loop %d.%d: rename %s to %s",
203                                         loops, i, names[i].from, names[i].to);
204                                 perror(msg);
205                                 rename_errors++;
206                                 if (!opt_exit_on_err)
207                                         return 4;
208                         }
209                 }
210
211                 if (opt_rename_only)
212                         return 0;
213
214                 for (i = 0; i < file_count; i++) {
215                         if (unlink(names[i].to) == -1) {
216                                 sprintf(msg, "loop %d.%d: unlink %s",
217                                         loops, i, names[i].to);
218                                 perror(msg);
219                                 unlink_errors++;
220                                 if (!opt_exit_on_err)
221                                         return 4;
222                         }
223                 }
224
225                 if (chdir("..") == -1) {
226                         perror("chdir ..");
227                         return(1);
228                 }
229
230                 if (rmdir("tmp") == -1) {
231                         if (chdir("tmp") == -1) {
232                                 perror("chdir tmp 2");
233                                 return(1);
234                         }
235                         for (i = 0; i < file_count; i++) {
236                                 if (unlink(names[i].from) != -1) {
237                                         fprintf(stderr, "loop %d.%d: "
238                                                 "unexpected file %s\n",
239                                                 loops, i, names[i].to);
240                                         unlink_errors++;
241                                         if (!opt_exit_on_err)
242                                                 return 4;
243                                 }
244                         }
245                         if (chdir("..") == -1) {
246                                 perror("chdir .. 2");
247                                 return(1);
248                         }
249                         if (rmdir("tmp") == -1) {
250                                 perror("rmdir tmp");
251                                 return(1);
252                         }
253                 }
254
255                 loops++;
256                 if (opt_verbose)
257                         handler(0);
258         }
259
260         if (!opt_verbose)
261                 handler(0);
262         return(0);
263 }