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