Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / writemany.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 #include <stdlib.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <signal.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <string.h>
46 #include <sys/wait.h>
47 #include <time.h>
48 #include <sys/time.h>
49
50
51 #define difftime(a, b)                                          \
52         ((double)(a)->tv_sec - (b)->tv_sec +                    \
53          ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
54
55
56
57 char cmdname[512];
58 int o_abort = 0;
59 int o_quiet = 0;
60
61 void usage(char *name)
62 {
63         fprintf(stderr, "usage: %s [opts] <dirname> <seconds> <threads>\n",
64                 name);
65         fprintf(stderr, "  -q quiet\n");
66         fprintf(stderr, "  -a abort other children on first err\n");
67         exit(1);
68 }
69
70
71 struct kid_list_t {
72         pid_t kid;
73         struct kid_list_t *next;
74 };
75
76 struct kid_list_t *head = NULL;
77
78 int push_kid(pid_t kid)
79 {
80         struct kid_list_t *new;
81         new = (struct kid_list_t *)malloc(sizeof(struct kid_list_t));
82         if (new == NULL)
83                 return 1;
84
85         new->kid = kid;
86         new->next = head;
87         head = new;
88         return 0;
89 }
90
91 void kill_kids(void)
92 {
93         while(head) {
94                 kill(head->kid, SIGTERM);
95                 head = head->next;
96         }
97 }
98
99 static int usr1_received;
100 void usr1_handler(int unused)
101 {
102         usr1_received = 1;
103         kill_kids();
104 }
105
106 int wait_for_threads(int live_threads)
107 {
108         int rc = 0;
109
110         while (live_threads > 0) {
111                 int status;
112                 pid_t ret;
113
114                 ret = waitpid(0, &status, 0);
115                 if (ret == 0) {
116                         continue;
117                 }
118
119                 if (ret < 0) {
120                         fprintf(stderr, "%s: error: wait - %s\n",
121                                 cmdname, strerror(errno));
122                         if (!rc)
123                                 rc = errno;
124                 } else {
125                         /*
126                          * This is a hack.  We _should_ be able to use
127                          * WIFEXITED(status) to see if there was an
128                          * error, but it appears to be broken and it
129                          * always returns 1 (OK).  See wait(2).
130                          */
131                         int err = WEXITSTATUS(status);
132                         if (err)
133                                 fprintf(stderr,
134                                         "%s: error: PID %d had rc=%d\n",
135                                         cmdname, ret, err);
136                         /* Record first error */
137                         if (!rc)
138                                 rc = err;
139
140                         /* Give up on first error */
141                         if (rc && o_abort) {
142                                 kill_kids();
143                                 break;
144                         }
145
146                         live_threads--;
147                 }
148         }
149         if (!o_quiet)
150                 printf("%s done, rc = %d\n", cmdname, rc);
151         return rc;
152 }
153
154 void print_err(char *op, char *filename, struct timeval *time, int err)
155 {
156         fprintf(stderr, "%s: %d.%.06d error: %s(%s): %s\n",
157                 cmdname, (int)(time->tv_sec), (int)(time->tv_usec), op,
158                 filename, strerror(errno));
159 }
160
161 int run_one_child(char *file, int thread, int seconds)
162 {
163         struct timeval start, cur;
164         double diff;
165         char filename[1024];
166         char buf[1024];
167         int fd, rc = 0, rand, maxrand, len;
168         long nfiles = 0, nbytes = 0;
169
170         if (!o_quiet)
171                 printf("%s: running thread #%d\n", cmdname, thread);
172
173         srandom(thread);
174         /* Higher thread numbers will produce bigger random files.
175            Thread 1 will produce only 0-len files. */
176         maxrand = 1; rand = thread;
177         while (--rand)
178                 maxrand *= 10;
179
180         gettimeofday(&start, NULL);
181
182         while(!rc) {
183                 if (usr1_received)
184                         break;
185
186                 gettimeofday(&cur, NULL);
187                 if (seconds) {
188                         if (cur.tv_sec > (start.tv_sec + seconds))
189                                 break;
190                 }
191                 
192                 sprintf(filename, "%s-%d-%ld", file, thread, nfiles);
193
194                 fd = open(filename, O_RDWR | O_CREAT, 0666);
195                 if (fd < 0) {
196                         print_err("open", filename, &cur, errno);
197                         rc = errno;
198                         break;
199                 }
200
201                 sprintf(buf, "%s %010ld %.19s.%012d\n", cmdname,
202                         nfiles++, ctime(&cur.tv_sec), (int)cur.tv_usec);
203                 len = strlen(buf);
204
205                 rand = random() % maxrand;
206                 while (rand-- > 0) {
207                         if (write(fd, buf, len) != len) {
208                                 print_err("write", filename, &cur, errno);
209                                 rc = errno;
210                                 break;
211                         }
212                         nbytes += len;
213                 }
214
215                 if (close(fd) < 0) {
216                         print_err("close", filename, &cur, errno);
217                         rc = errno;
218                         break;
219                 }
220                 if (unlink(filename) < 0) {
221                         print_err("unlink", filename, &cur, errno);
222                         if (errno == ENOENT) {
223                                 printf("Ignoring known bug 6082\n");
224                         } else {
225                                 rc = errno;
226                                 break;
227                         }
228                 }
229         }
230
231         diff = difftime(&cur, &start);
232         if (!o_quiet)
233                 printf("%s: %7ld files, %4ld MB in %.2fs (%7.2f files/s, "
234                        "%5.2f MB/s): rc = %d\n",
235                        cmdname, nfiles, nbytes >> 20, diff,
236                        (double)nfiles / diff, (double)nbytes/1024/1024 / diff,
237                        rc);
238
239         return rc;
240 }
241
242 int main(int argc, char *argv[])
243 {
244         unsigned long duration;
245         int threads = 0;
246         char *end;
247         char *directory;
248         int i = 1, rc = 0;
249
250         sprintf(cmdname, "%s", argv[0]);
251
252         while((i < argc) && (argv[i][0] == '-')) {
253                 switch (argv[i][1]) {
254                 case 'q':
255                         o_quiet++;
256                         break;
257                 case 'a':
258                         o_abort++;
259                         break;
260                 }
261                 i++;
262         }
263
264         if ((argc - i) < 3)
265                 usage(argv[0]);
266
267         directory = argv[i];
268         duration = strtoul(argv[++i], &end, 0);
269         if (*end) {
270                 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
271                         cmdname, argv[i]);
272                 exit(2);
273         }
274
275         threads = strtoul(argv[++i], &end, 0);
276         if (*end) {
277                 fprintf(stderr, "%s: error: bad thread count '%s'\n",
278                         cmdname, argv[i]);
279                 exit(2);
280         }
281
282         signal(SIGUSR1, usr1_handler);
283
284         for (i = 1; i <= threads; i++) {
285                 rc = fork();
286                 if (rc < 0) {
287                         if (!o_quiet)
288                                 fprintf(stderr, "%s: error: #%d - %s\n",
289                                         cmdname, i, strerror(rc = errno));
290                         return (rc);
291                 }
292                 if (rc == 0) {
293                         /* children */
294                         sprintf(cmdname, "%s-%d", argv[0], i);
295                         return (run_one_child(directory, i, duration));
296                 } else {
297                         /* parent */
298                         rc = push_kid(rc);
299                         if (rc != 0) {
300                                 kill_kids();
301                                 exit(3);
302                         }
303                 }
304         }
305         /* parent process */
306         if (!o_quiet)
307                 printf("%s will run for %ld minutes\n", cmdname, duration/60);
308         return (wait_for_threads(threads));
309 }