Whamcloud - gitweb
b=19016
[fs/lustre-release.git] / lustre / tests / rename_many.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define PATH_LENGTH 35
38 #include <math.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <stdio.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <getopt.h>
48
49 struct names {
50         char from[PATH_LENGTH];
51         char to[PATH_LENGTH];
52 } *names;
53
54 unsigned int loop_count = 500;
55 int file_count = 1000;
56 int seed;
57 int loops;
58 int stop;
59 long start;
60
61 int opt_exit_on_err;
62 int opt_verbose;
63 int opt_create_only;
64 int opt_rename_only;
65 int creat_errors;
66 int rename_errors;
67 int unlink_errors;
68
69 void usage(const char *progname)
70 {
71         fprintf(stderr, "usage: %s [-n numfiles] [-s seed] [-v] [-x] [dir]\n"
72                 "\t-c: only do the create step of first loop\n"
73                 "\t-f: number of files to create/rename/unlink per loop\n"
74                 "\t-n: number of test loops (0 to run forever)\n"
75                 "\t-r: only do the rename step of first loop\n"
76                 "\t-s: starting seed (equals loop number by default)\n"
77                 "\t-v: verbose\n"
78                 "\t-x: don't exit on error\n", progname);
79 }
80
81 void handler(int sig) {
82         static long last_time;
83         long now = time(0);
84
85         signal(SIGINT, handler);
86         signal(SIGALRM, handler);
87         printf("%6lds %8d iterations %d/%d/%d errors",
88                now - start, loops, creat_errors, rename_errors, unlink_errors);
89         if (sig != 0)
90                 printf(" - use SIGQUIT (^\\) or ^C^C to kill\n");
91         else
92                 printf("\n");
93
94         if (sig == SIGQUIT)
95                 stop = 1;
96         else if (sig == SIGINT) {
97                 if (now - last_time < 2)
98                         stop = 1;
99                 last_time = now;
100         }
101         alarm(60);
102 }
103
104 extern char *optarg;
105 extern int optind;
106
107 int main(int argc, char *argv[])
108 {
109         unsigned long n;
110         char msg[100], *end = NULL;
111         int h1, h2;
112         int i, c;
113
114         while ((c = getopt(argc, argv, "cf:n:rs:vx")) != EOF) {
115                 switch(c) {
116                 case 'c':
117                         ++opt_create_only;
118                         break;
119                 case 'f':
120                         i = strtoul(optarg, &end, 0);
121                         if (i && end != NULL && *end == '\0') {
122                                 file_count = i;
123                         } else {
124                                 fprintf(stderr, "bad file count '%s'\n",optarg);
125                                 usage(argv[0]);
126                                 return 1;
127                         }
128                         break;
129                 case 'n':
130                         i = strtoul(optarg, &end, 0);
131                         if (i && end != NULL && *end == '\0') {
132                                 loop_count = i;
133                         } else {
134                                 fprintf(stderr, "bad loop count '%s'\n",optarg);
135                                 usage(argv[0]);
136                                 return 1;
137                         }
138                         break;
139                 case 'r':
140                         ++opt_rename_only;
141                         break;
142                 case 's':
143                         i = strtoul(optarg, &end, 0);
144                         if (end && *end == '\0') {
145                                 seed = i;
146                         } else {
147                                 seed = random();
148                                 fprintf(stderr, "using random seed %u\n", seed);
149                         }
150                         break;
151                 case 'v':
152                         ++opt_verbose;
153                         break;
154                 case 'x':
155                         ++opt_exit_on_err;
156                         break;
157                 default:
158                         usage(argv[0]);
159                         return 1;
160                 }
161         }
162
163         names = malloc(sizeof(struct names) * file_count);
164         if (names == NULL) {
165                 perror("calloc");
166                 return(1);
167         }
168
169         h2 = sprintf(msg, "%x", file_count); /* just to figure length */
170         h1 = (PATH_LENGTH - h2 - 2) / 4;
171
172         n = (1ULL << h1 * 4) - 1;
173
174         //printf("h1 = %d, h2 = %d n = %lu\n", h1, h2, n);
175
176         start = time(0);
177
178         signal(SIGQUIT, handler);
179         signal(SIGINT, handler);
180         signal(SIGALRM, handler);
181         signal(SIGUSR1, handler);
182         alarm(60);
183
184         if (argc > optind + 1) {
185                 fprintf(stderr, "too many extra args %d\n", argc - optind);
186                 usage(argv[0]);
187                 return 1;
188         } else if (argv[optind] != NULL) {
189                 if (chdir(argv[optind]) < 0) {
190                         sprintf(msg, "chdir '%s'\n", argv[optind]);
191                         perror(msg);
192                         return 2;
193                 }
194         }
195
196         while (!stop && loop_count != 0 && loops < loop_count) {
197                 int j,k,l,m;
198
199                 srand(seed + loops);
200                 if (mkdir("tmp", S_IRWXU) == -1) {
201                         perror("mkdir tmp");
202                         return(1);
203                 }
204                 if (chdir("tmp") == -1) {
205                         perror("chdir tmp");
206                         return(1);
207                 }
208
209                 for (i = 0; i < file_count ; i++) {
210                         j = random() & n;
211                         k = random() & n;
212                         l = random() & n;
213                         m = random() & n;
214                         sprintf(names[i].from, "%0*x%0*x%0*x%0*x0%0*x",
215                                 h1, j, h1, k, h1, l, h1, m, h2, i);
216                         sprintf(names[i].to, "%0*x%0*x%0*x%0*x1%0*x",
217                                 h1, j, h1, k, h1, l, h1, m, h2, i);
218
219                 }
220
221                 for (i = 0; i < file_count; i++) {
222                         if (mknod(names[i].from, S_IFREG | S_IRWXU, 0) == -1) {
223                                 sprintf(msg, "loop %d.%d: creat %s",
224                                         loops, i, names[i].from);
225                                 perror(msg);
226                                 creat_errors++;
227                                 if (!opt_exit_on_err)
228                                         return 4;
229                         }
230                 }
231
232                 if (opt_create_only)
233                         return 0;
234
235                 for (i = 0; i < file_count; i++) {
236                         if (rename(names[i].from, names[i].to) == -1) {
237                                 sprintf(msg, "loop %d.%d: rename %s to %s",
238                                         loops, i, names[i].from, names[i].to);
239                                 perror(msg);
240                                 rename_errors++;
241                                 if (!opt_exit_on_err)
242                                         return 4;
243                         }
244                 }
245
246                 if (opt_rename_only)
247                         return 0;
248
249                 for (i = 0; i < file_count; i++) {
250                         if (unlink(names[i].to) == -1) {
251                                 sprintf(msg, "loop %d.%d: unlink %s",
252                                         loops, i, names[i].to);
253                                 perror(msg);
254                                 unlink_errors++;
255                                 if (!opt_exit_on_err)
256                                         return 4;
257                         }
258                 }
259
260                 if (chdir("..") == -1) {
261                         perror("chdir ..");
262                         return(1);
263                 }
264
265                 if (rmdir("tmp") == -1) {
266                         if (chdir("tmp") == -1) {
267                                 perror("chdir tmp 2");
268                                 return(1);
269                         }
270                         for (i = 0; i < file_count; i++) {
271                                 if (unlink(names[i].from) != -1) {
272                                         fprintf(stderr, "loop %d.%d: "
273                                                 "unexpected file %s\n",
274                                                 loops, i, names[i].to);
275                                         unlink_errors++;
276                                         if (!opt_exit_on_err)
277                                                 return 4;
278                                 }
279                         }
280                         if (chdir("..") == -1) {
281                                 perror("chdir .. 2");
282                                 return(1);
283                         }
284                         if (rmdir("tmp") == -1) {
285                                 perror("rmdir tmp");
286                                 return(1);
287                         }
288                 }
289
290                 loops++;
291                 if (opt_verbose)
292                         handler(0);
293         }
294
295         if (!opt_verbose)
296                 handler(0);
297         return(0);
298 }