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