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