Whamcloud - gitweb
LU-5560 llite: basic support of SELinux in CLIO
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <sys/wait.h>
45 #include <time.h>
46 #include <sys/time.h>
47
48
49 #define difftime(a, b)                                          \
50         ((double)(a)->tv_sec - (b)->tv_sec +                    \
51          ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
52
53
54
55 char cmdname[512];
56 int o_abort = 0;
57 int o_quiet = 0;
58
59 void usage(char *name)
60 {
61         fprintf(stderr, "usage: %s [opts] <dirname> <seconds> <threads>\n",
62                 name);
63         fprintf(stderr, "  -q quiet\n");
64         fprintf(stderr, "  -a abort other children on first err\n");
65         exit(1);
66 }
67
68
69 struct kid_list_t {
70         pid_t kid;
71         struct kid_list_t *next;
72 };
73
74 struct kid_list_t *head = NULL;
75
76 int push_kid(pid_t kid)
77 {
78         struct kid_list_t *new;
79         new = (struct kid_list_t *)malloc(sizeof(struct kid_list_t));
80         if (new == NULL)
81                 return 1;
82
83         new->kid = kid;
84         new->next = head;
85         head = new;
86         return 0;
87 }
88
89 void kill_kids(void)
90 {
91         while(head) {
92                 kill(head->kid, SIGTERM);
93                 head = head->next;
94         }
95 }
96
97 static int usr1_received;
98 void usr1_handler(int unused)
99 {
100         usr1_received = 1;
101         kill_kids();
102 }
103
104 int wait_for_threads(int live_threads)
105 {
106         int rc = 0;
107
108         while (live_threads > 0) {
109                 int status;
110                 pid_t ret;
111
112                 ret = waitpid(0, &status, 0);
113                 if (ret == 0) {
114                         continue;
115                 }
116
117                 if (ret < 0) {
118                         fprintf(stderr, "%s: error: wait - %s\n",
119                                 cmdname, strerror(errno));
120                         if (!rc)
121                                 rc = errno;
122                 } else {
123                         /*
124                          * This is a hack.  We _should_ be able to use
125                          * WIFEXITED(status) to see if there was an
126                          * error, but it appears to be broken and it
127                          * always returns 1 (OK).  See wait(2).
128                          */
129                         int err = WEXITSTATUS(status);
130                         if (err)
131                                 fprintf(stderr,
132                                         "%s: error: PID %d had rc=%d\n",
133                                         cmdname, ret, err);
134                         /* Record first error */
135                         if (!rc)
136                                 rc = err;
137
138                         /* Give up on first error */
139                         if (rc && o_abort) {
140                                 kill_kids();
141                                 break;
142                         }
143
144                         live_threads--;
145                 }
146         }
147         if (!o_quiet)
148                 printf("%s done, rc = %d\n", cmdname, rc);
149         return rc;
150 }
151
152 void print_err(char *op, char *filename, struct timeval *time, int err)
153 {
154         fprintf(stderr, "%s: %d.%.06d error: %s(%s): %s\n",
155                 cmdname, (int)(time->tv_sec), (int)(time->tv_usec), op,
156                 filename, strerror(errno));
157 }
158
159 int run_one_child(char *file, int thread, int seconds)
160 {
161         struct timeval start, cur;
162         double diff;
163         char filename[1024];
164         char buf[1024];
165         int fd, rc = 0, rand, maxrand, len;
166         long nfiles = 0, nbytes = 0;
167
168         if (!o_quiet)
169                 printf("%s: running thread #%d\n", cmdname, thread);
170
171         srandom(thread);
172         /* Higher thread numbers will produce bigger random files.
173            Thread 1 will produce only 0-len files. */
174         maxrand = 1; rand = thread;
175         while (--rand)
176                 maxrand *= 10;
177
178         gettimeofday(&start, NULL);
179         cur = start;
180
181         while(!rc) {
182                 if (usr1_received)
183                         break;
184
185                 gettimeofday(&cur, NULL);
186                 if (seconds) {
187                         if (cur.tv_sec > (start.tv_sec + seconds))
188                                 break;
189                 }
190                 
191                 snprintf(filename, sizeof(filename), "%s-%d-%ld",
192                          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                        diff == 0 ? (double)0 : (double)nfiles / diff,
237                        diff == 0 ? (double)0 : (double)nbytes/1024/1024 / diff,
238                        rc);
239
240         return rc;
241 }
242
243 int main(int argc, char *argv[])
244 {
245         unsigned long duration;
246         int threads = 0;
247         char *end;
248         char *directory;
249         int i = 1, rc = 0;
250
251         snprintf(cmdname, sizeof(cmdname), "%s", argv[0]);
252
253         while((i < argc) && (argv[i][0] == '-')) {
254                 switch (argv[i][1]) {
255                 case 'q':
256                         o_quiet++;
257                         break;
258                 case 'a':
259                         o_abort++;
260                         break;
261                 }
262                 i++;
263         }
264
265         if ((argc - i) < 3)
266                 usage(argv[0]);
267
268         directory = argv[i];
269         duration = strtoul(argv[++i], &end, 0);
270         if (*end) {
271                 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
272                         cmdname, argv[i]);
273                 exit(2);
274         }
275
276         threads = strtoul(argv[++i], &end, 0);
277         if (*end) {
278                 fprintf(stderr, "%s: error: bad thread count '%s'\n",
279                         cmdname, argv[i]);
280                 exit(2);
281         }
282
283         signal(SIGUSR1, usr1_handler);
284
285         for (i = 1; i <= threads; i++) {
286                 rc = fork();
287                 if (rc < 0) {
288                         if (!o_quiet)
289                                 fprintf(stderr, "%s: error: #%d - %s\n",
290                                         cmdname, i, strerror(rc = errno));
291                         return (rc);
292                 }
293                 if (rc == 0) {
294                         /* children */
295                         snprintf(cmdname, sizeof(cmdname), "%s-%d", argv[0], i);
296                         return (run_one_child(directory, i, duration));
297                 } else {
298                         /* parent */
299                         rc = push_kid(rc);
300                         if (rc != 0) {
301                                 kill_kids();
302                                 exit(3);
303                         }
304                 }
305         }
306         /* parent process */
307         if (!o_quiet)
308                 printf("%s will run for %ld minutes\n", cmdname, duration/60);
309         return (wait_for_threads(threads));
310 }