Whamcloud - gitweb
b=23049 set path to truncate
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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         cur = start;
182
183         while(!rc) {
184                 if (usr1_received)
185                         break;
186
187                 gettimeofday(&cur, NULL);
188                 if (seconds) {
189                         if (cur.tv_sec > (start.tv_sec + seconds))
190                                 break;
191                 }
192                 
193                 sprintf(filename, "%s-%d-%ld", file, thread, nfiles);
194
195                 fd = open(filename, O_RDWR | O_CREAT, 0666);
196                 if (fd < 0) {
197                         print_err("open", filename, &cur, errno);
198                         rc = errno;
199                         break;
200                 }
201
202                 sprintf(buf, "%s %010ld %.19s.%012d\n", cmdname,
203                         nfiles++, ctime(&cur.tv_sec), (int)cur.tv_usec);
204                 len = strlen(buf);
205
206                 rand = random() % maxrand;
207                 while (rand-- > 0) {
208                         if (write(fd, buf, len) != len) {
209                                 print_err("write", filename, &cur, errno);
210                                 rc = errno;
211                                 break;
212                         }
213                         nbytes += len;
214                 }
215
216                 if (close(fd) < 0) {
217                         print_err("close", filename, &cur, errno);
218                         rc = errno;
219                         break;
220                 }
221                 if (unlink(filename) < 0) {
222                         print_err("unlink", filename, &cur, errno);
223                         if (errno == ENOENT) {
224                                 printf("Ignoring known bug 6082\n");
225                         } else {
226                                 rc = errno;
227                                 break;
228                         }
229                 }
230         }
231
232         diff = difftime(&cur, &start);
233         if (!o_quiet)
234                 printf("%s: %7ld files, %4ld MB in %.2fs (%7.2f files/s, "
235                        "%5.2f MB/s): rc = %d\n",
236                        cmdname, nfiles, nbytes >> 20, diff,
237                        diff == 0 ? (double)0 : (double)nfiles / diff,
238                        diff == 0 ? (double)0 : (double)nbytes/1024/1024 / diff,
239                        rc);
240
241         return rc;
242 }
243
244 int main(int argc, char *argv[])
245 {
246         unsigned long duration;
247         int threads = 0;
248         char *end;
249         char *directory;
250         int i = 1, rc = 0;
251
252         sprintf(cmdname, "%s", argv[0]);
253
254         while((i < argc) && (argv[i][0] == '-')) {
255                 switch (argv[i][1]) {
256                 case 'q':
257                         o_quiet++;
258                         break;
259                 case 'a':
260                         o_abort++;
261                         break;
262                 }
263                 i++;
264         }
265
266         if ((argc - i) < 3)
267                 usage(argv[0]);
268
269         directory = argv[i];
270         duration = strtoul(argv[++i], &end, 0);
271         if (*end) {
272                 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
273                         cmdname, argv[i]);
274                 exit(2);
275         }
276
277         threads = strtoul(argv[++i], &end, 0);
278         if (*end) {
279                 fprintf(stderr, "%s: error: bad thread count '%s'\n",
280                         cmdname, argv[i]);
281                 exit(2);
282         }
283
284         signal(SIGUSR1, usr1_handler);
285
286         for (i = 1; i <= threads; i++) {
287                 rc = fork();
288                 if (rc < 0) {
289                         if (!o_quiet)
290                                 fprintf(stderr, "%s: error: #%d - %s\n",
291                                         cmdname, i, strerror(rc = errno));
292                         return (rc);
293                 }
294                 if (rc == 0) {
295                         /* children */
296                         sprintf(cmdname, "%s-%d", argv[0], i);
297                         return (run_one_child(directory, i, duration));
298                 } else {
299                         /* parent */
300                         rc = push_kid(rc);
301                         if (rc != 0) {
302                                 kill_kids();
303                                 exit(3);
304                         }
305                 }
306         }
307         /* parent process */
308         if (!o_quiet)
309                 printf("%s will run for %ld minutes\n", cmdname, duration/60);
310         return (wait_for_threads(threads));
311 }