Whamcloud - gitweb
f739266dffb8740c7e383ee9abbf69eb2ee52104
[fs/lustre-release.git] / lustre / tests / mpi / write_append_truncate.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) 2003, 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  * lustre/tests/write_append_truncate.c
35  *
36  * Each loop does 3 things:
37  *   - truncate file to zero (not via ftruncate though, to test O_APPEND)
38  *   - write a "chunk" of data (should be at file offset 0 after truncate)
39  *   - on each of two threads either append or truncate-up the file
40  *
41  * If the truncate happened first, we should have a hole in the file.
42  * If the append happened first, we should have truncated the file down.
43  *
44  * WRITE_SIZE_MAX and APPEND_SIZE_MAX are large enough to cross a stripe.
45  *
46  * compile: mpicc -g -Wall -o write_append_truncate write_append_truncate.c
47  * run:     mpirun -np 2 -machlist <hostlist file> write_append_truncate <file>
48  *  or:     pdsh -w <two hosts> write_append_truncate <file>
49  *  or:     prun -n 2 [-N 2] write_append_truncate <file>
50  */
51
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <stdarg.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <time.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <getopt.h>
63 #include "mpi.h"
64
65 #define DEFAULT_ITER    10000
66
67 #define WRITE_SIZE_MAX  1234567
68 #define APPEND_SIZE_MAX 1234567
69 #define TRUNC_SIZE_MAX  1234567
70
71 #define STATUS_FMT "WR %c %7d/%#08x, AP %c %7d/%#08x, TR@ %7d/%#08x"
72
73 #define HOSTNAME_SIZE 50
74 char hostname[HOSTNAME_SIZE];
75 #define FNAMES_MAX 256
76
77 void usage(char *prog)
78 {
79         printf("usage: %s [-a append_max] [-C] [-n nloops] [-s seed]\n"
80                "\t\t[-t trunc_max] [-T] [-v] [-w write_max] <filename> ...\n", prog);
81         printf("\t-a append_max: maximum size of append, default %u bytes\n",
82                APPEND_SIZE_MAX);
83         printf("\t-C: 'classic' checks (on file 0)\n");
84         printf("\t-n nloops: count of loops to run, default %u\n",DEFAULT_ITER);
85         printf("\t-s seed: random seed to use, default {current time}\n");
86         printf("\t-t trunc_max: maximum size of truncate, default %u bytes\n",
87                TRUNC_SIZE_MAX);
88         printf("\t-T: 'classic' truncates (on file 0)\n");
89         printf("\t-w write_max: maximum size of write, default %u bytes\n",
90                WRITE_SIZE_MAX);
91         printf("\t-W: 'classic' writes (on rank 0, file 0)\n");
92         printf("\t-v: run in verbose mode (repeat for more verbosity)\n");
93         printf("\tfilename for each mountpoint of same filesystem on a node\n");
94         printf("\b%s must be run with at least 2 processes\n", prog);
95
96         MPI_Finalize();
97         exit(1);
98 }
99
100 /* Print process rank, loop count, message, and exit (i.e. a fatal error) */
101 void rprintf(int rank, int loop, int error, const char *fmt, ...)
102 __attribute__ ((format (printf, 4, 5)));
103
104 void rprintf(int rank, int loop, int error, const char *fmt, ...)
105 {
106         va_list       ap;
107
108         printf("r=%2u", rank);
109         if (loop >= 0)
110                 printf(" l=%04u", loop);
111         if (error != 0)
112                 printf(" %s", hostname);
113         printf(": ");
114
115         va_start(ap, fmt);
116
117         vprintf(fmt, ap);
118
119         if (error != 0)
120                 MPI_Abort(MPI_COMM_WORLD, error);
121 }
122
123 int main(int argc, char *argv[])
124 {
125         int n, nloops = DEFAULT_ITER;
126         int nfnames = 0, ifnames, fd;
127         int rank = -1, nproc, ret;
128         unsigned write_max = WRITE_SIZE_MAX;
129         unsigned append_max = APPEND_SIZE_MAX;
130         unsigned write_size = 0, append_size = 0, trunc_size = 0;
131         unsigned trunc_max = 0, trunc_offset = 0;
132         char *append_buf;
133         char *write_buf;
134         char *read_buf = NULL;
135         char *trunc_buf = NULL;
136         int seed = time(0);
137         int done;
138         int error;
139         int verbose = 0;
140         int classic_check = 0, classic_trunc = 0, classic_write = 0;
141         char write_char = 'A', append_char = 'a';
142         char *fnames[FNAMES_MAX], *end;
143         char *prog = "write_append_truncate";
144         int c;
145
146         error = MPI_Init(&argc, &argv);
147         if (error != MPI_SUCCESS)
148                 printf("%s: MPI_Init failed: %d\n", prog, error);
149         else if (verbose > 2)
150                 printf("%s: MPI_Init succeeded\n", prog);
151
152         prog = strrchr(argv[0], '/');
153         if (prog == NULL)
154                 prog = argv[0];
155         else
156                 prog++;
157
158         while ((c = getopt(argc, argv, "a:cCn:s:t:Tvw:W")) != -1) {
159                 switch(c) {
160                 case 'a':
161                         append_max = strtoul(optarg, &end, 0);
162                         if (append_max == 0 || *end) {
163                                 fprintf(stderr, "%s: bad append option '%s'\n",
164                                         prog, optarg);
165                                 usage(prog);
166                         }
167                         break;
168                 case 'C':
169                         classic_check++;
170                         break;
171                 case 'n':
172                         nloops = strtoul(optarg, &end, 0);
173                         if (nloops == 0 || *end) {
174                                 fprintf(stderr, "%s: bad nloops option '%s'\n",
175                                         prog, optarg);
176                                 usage(prog);
177                         }
178                         break;
179                 case 's':
180                         seed = strtoul(optarg, &end, 0);
181                         if (*end) {
182                                 fprintf(stderr, "%s: bad seed option '%s'\n",
183                                         prog, optarg);
184                                 usage(prog);
185                         }
186                         break;
187                 case 't':
188                         trunc_max = strtoul(optarg, &end, 0);
189                         if (*end) {
190                                 fprintf(stderr,"%s: bad truncate option '%s'\n",
191                                         prog, optarg);
192                                 usage(prog);
193                         }
194                         break;
195                 case 'T':
196                         classic_trunc++;
197                         break;
198                 case 'v':
199                         verbose++;
200                         break;
201                 case 'w':
202                         write_max = strtoul(optarg, &end, 0);
203                         if (write_max == 0 || *end) {
204                                 fprintf(stderr, "%s: bad write option '%s'\n",
205                                         prog, optarg);
206                                 usage(prog);
207                         }
208                         break;
209                 case 'W':
210                         classic_write++;
211                         break;
212                 default:
213                         fprintf(stderr, "%s: unknown option '%c'\n", prog, c);
214                         usage(prog);
215                 }
216         }
217
218         srand(seed);
219
220         if (argc == optind) {
221                 fprintf(stderr, "%s: missing filename argument\n", prog);
222                 usage(prog);
223         }
224
225         if (argc > optind + FNAMES_MAX) {
226                 fprintf(stderr, "%s: too many extra options\n", prog);
227                 usage(prog);
228         }
229
230         while (optind < argc)
231                 fnames[nfnames++] = argv[optind++];
232
233         error = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
234         if (verbose > 2 || error != MPI_SUCCESS)
235                 rprintf(rank, -1, error != MPI_SUCCESS, "MPI_Comm_rank: %d\n",
236                         error);
237
238         error = MPI_Comm_size(MPI_COMM_WORLD, &nproc);
239         if (verbose > 2 || error != MPI_SUCCESS)
240                 rprintf(rank, -1, error != MPI_SUCCESS, "MPI_Comm_size: %d\n",
241                         error);
242
243         if (nproc < 2)
244                 rprintf(rank, -1, 1, "%s: must run with at least 2 processes\n",
245                         prog);
246
247         append_buf = malloc(append_max);
248         if (append_buf == NULL)
249                 rprintf(rank, -1, 1,"%s: error allocating append_buf %u\n",
250                         prog, append_max);
251
252         write_buf = malloc(write_max);
253         if (write_buf == NULL)
254                 rprintf(rank, -1, 1, "%s: error allocating write_buf %u\n",
255                         prog, write_max);
256
257         if (gethostname(hostname, HOSTNAME_SIZE) < 0)
258                 rprintf(rank, -1, 1, "%s: gethostname failed: %s\n",
259                         prog, strerror(errno));
260
261         if (rank == 0) {
262                 int max_size = write_max + (trunc_max ?: append_max)+append_max;
263
264                 fd = open(fnames[0], O_WRONLY|O_CREAT|O_TRUNC, 0666);
265                 rprintf(rank,-1, fd<0, "create %s, max size: %u, seed %u: %s\n",
266                         fnames[0], max_size, seed, strerror(errno));
267                 close(fd);
268
269                 trunc_buf = calloc(1, trunc_max ?: append_max);
270                 if (trunc_buf == NULL)
271                         rprintf(rank,-1,1,"%s: error allocating trunc_buf %u\n",
272                                 prog, trunc_max ?: append_max);
273
274                 /* initial write + truncate up + append */
275                 read_buf = malloc(max_size);
276                 if (read_buf == NULL)
277                         rprintf(rank,-1,1,"%s: error allocating read_buf %u\n",
278                                 prog, max_size);
279         }
280
281         error = MPI_Barrier(MPI_COMM_WORLD);
282         if (verbose > 2 || error != MPI_SUCCESS)
283                 rprintf(rank, -1, error != MPI_SUCCESS,
284                         "prep MPI_Barrier: %d\n", error);
285
286         ifnames = rank % nfnames;
287         fd = open(fnames[ifnames], O_RDWR | O_APPEND);
288         if (verbose || fd < 0)
289                 rprintf(rank, -1, errno, "open '%s' (%u): %s\n",
290                         fnames[ifnames], ifnames, strerror(errno));
291
292         for (n = 0; n < nloops; n++) {
293                 /* Initialized only to quiet stupid GCC warnings */
294                 unsigned write_rank = 0, append_rank = n, trunc_rank = n + 1;
295                 unsigned mpi_shared_vars[6];
296
297                 /* reset the environment */
298                 write_char = 'A' + (n % 26);
299                 append_char = 'a' + (n % 26);
300
301                 if (rank == 0) {
302                         write_size = (rand() % (write_max - 1)) + 1;
303                         append_size = (rand() % (append_max - 1)) + 1;
304                         trunc_size = (rand() % ((trunc_max?: append_size)-1))+1;
305                         trunc_offset = write_size + trunc_size;
306
307                         if (verbose || n % 1000 == 0)
308                                 rprintf(rank, n, 0, STATUS_FMT"\n",
309                                         write_char, write_size, write_size,
310                                         append_char, append_size, append_size,
311                                         trunc_offset, trunc_offset);
312
313                         write_rank = (classic_write ? 0 : rand()) % nproc;
314                         do {
315                                 append_rank = (classic_write ? n : rand()) %
316                                                                 nproc;
317                                 /* We can't allow the append rank be the same
318                                  * as the classic_trunc trunc_rank, or we will
319                                  * spin here forever. */
320                         } while (append_rank == (n + 1) % nproc);
321                         do {
322                                 trunc_rank = (classic_trunc? (n + 1) : rand()) %
323                                                                 nproc;
324                         } while (trunc_rank == append_rank);
325
326                         mpi_shared_vars[0] = write_size;
327                         mpi_shared_vars[1] = append_size;
328                         mpi_shared_vars[2] = trunc_size;
329                         mpi_shared_vars[3] = write_rank;
330                         mpi_shared_vars[4] = append_rank;
331                         mpi_shared_vars[5] = trunc_rank;
332                 }
333
334                 error = MPI_Bcast(&mpi_shared_vars, 6,
335                                   MPI_INT, 0, MPI_COMM_WORLD);
336                 if (verbose > 2 || error != MPI_SUCCESS)
337                         rprintf(rank, n, error != MPI_SUCCESS,
338                                 "MPI_Bcast mpi_shared_vars"
339                                 "[%u, %u, %u, %u, %u, %u]: %d\n",
340                                 mpi_shared_vars[0], mpi_shared_vars[1],
341                                 mpi_shared_vars[2], mpi_shared_vars[3],
342                                 mpi_shared_vars[4], mpi_shared_vars[5], error);
343
344                 if (rank != 0) {
345                         write_size  = mpi_shared_vars[0];
346                         append_size = mpi_shared_vars[1];
347                         trunc_size  = mpi_shared_vars[2];
348                         write_rank  = mpi_shared_vars[3];
349                         append_rank = mpi_shared_vars[4];
350                         trunc_rank  = mpi_shared_vars[5];
351
352                         trunc_offset = write_size + trunc_size;
353                 }
354
355                 if (rank == write_rank || rank == 0)
356                         memset(write_buf, write_char, write_max);
357
358                 if (rank == write_rank) {
359                         ifnames = (classic_write ? 0 : rand()) % nfnames;
360                         ret = truncate(fnames[ifnames], 0);
361                         if (verbose > 1 || ret != 0)
362                                 rprintf(rank, n, ret,
363                                         "initial truncate %s (%u) @ 0: %s\n",
364                                         fnames[ifnames], ifnames,
365                                         strerror(errno));
366
367                         done = 0;
368                         do {
369                                 ret = write(fd, write_buf+done,write_size-done);
370                                 if (verbose > 1 || ret < 0) {
371                                         rprintf(rank, n,
372                                                 ret < 0 && errno != EINTR,
373                                                 "write %d/%d @ %d: %s\n",
374                                                 ret + done, write_size, done,
375                                                 strerror(errno));
376                                         if (ret < 0 && errno != EINTR)
377                                                 break;
378                                 }
379                                 if (ret > 0)
380                                         done += ret;
381                         } while (done != write_size);
382                 }
383
384                 if (rank == append_rank || rank == 0)
385                         memset(append_buf, append_char, append_size);
386
387                 error = MPI_Barrier(MPI_COMM_WORLD);
388                 if (verbose > 2 || error != MPI_SUCCESS)
389                         rprintf(rank, n, error != MPI_SUCCESS,
390                                 "start MPI_Barrier: %d\n", error);
391
392                 /* Do the race */
393                 if (rank == append_rank) {
394                         done = 0;
395                         do {
396                                 ret = write(fd, append_buf + done,
397                                             append_size - done);
398                                 if (ret < 0) {
399                                         rprintf(rank, n, errno != EINTR,
400                                                 "append %u/%u: %s\n",
401                                                 ret + done, append_size,
402                                                 strerror(errno));
403                                         if (errno != EINTR)
404                                                 break;
405                                 } else if (verbose > 1 || ret != append_size) {
406                                         rprintf(rank, n, ret != append_size,
407                                                 "append %u/%u\n",
408                                                 ret + done, append_size);
409                                 }
410                                 if (ret > 0)
411                                         done += ret;
412                         } while (done != append_size);
413                 } else if (rank == trunc_rank) {
414                         /* XXX: truncating the same file descriptor as the
415                          *      append on a single node causes this test
416                          *      to fail currently (2009-02-01). */
417                         ifnames = (classic_trunc ? rank : rand()) % nfnames;
418                         ret = truncate(fnames[ifnames], trunc_offset);
419                         if (verbose > 1 || ret != 0)
420                                 rprintf(rank, n, ret,
421                                         "truncate %s (%u) @ %u: %s\n",
422                                         fnames[ifnames], ifnames,
423                                         trunc_offset, strerror(errno));
424                 }
425
426                 error = MPI_Barrier(MPI_COMM_WORLD);
427                 if (verbose > 2 || error != MPI_SUCCESS)
428                         rprintf(rank, n, error != MPI_SUCCESS,
429                                 "end MPI_Barrier: %d\n", error);
430
431                 error = 0;
432
433                 /* Check the result */
434                 if (rank == 0) {
435                         char *tmp_buf;
436                         struct stat st = { 0 };
437
438                         ifnames = classic_check ? 0 : (rand() % nfnames);
439                         ret = stat(fnames[ifnames], &st);
440                         if (verbose > 1 || ret != 0)
441                                 rprintf(rank, n, ret,
442                                         "stat %s (%u) size %llu: %s\n",
443                                         fnames[ifnames], ifnames,
444                                         (long long)st.st_size, strerror(errno));
445
446                         ret = lseek(fd, 0, SEEK_SET);
447                         if (ret != 0)
448                                 rprintf(rank, n, ret, "lseek 0: %s\n",
449                                         strerror(errno));
450
451                         done = 0;
452                         do {
453                                 ret = read(fd, read_buf+done, st.st_size-done);
454                                 if (verbose > 1 || ret <= 0) {
455                                         rprintf(rank, n, ret <= 0,
456                                                 "read %d/%llu @ %u: %s\n",
457                                                 ret, (long long)st.st_size-done,
458                                                 done, ret != 0 ?
459                                                 strerror(errno) : "short read");
460                                 }
461                                 done += ret;
462                         } while (done != st.st_size);
463
464                         if (memcmp(read_buf, write_buf, write_size)) {
465                                 rprintf(rank, n, 0, "WRITE bad "
466                                         "[0-%d]/[0-%#x] != %c\n",
467                                         write_size - 1, write_size - 1,
468                                         write_char);
469                                 error = 1;
470                         }
471
472                         tmp_buf = read_buf + write_size;
473
474                         if (st.st_size == trunc_offset) {
475                                 /* Check case 1: first append then truncate */
476                                 int tmp_size, tmp_offset;
477
478                                 tmp_size = trunc_size < append_size ?
479                                                 trunc_size : append_size;
480                                 tmp_offset = write_size + tmp_size;
481
482                                 if (memcmp(tmp_buf, append_buf, tmp_size)) {
483                                         rprintf(rank, n, 0,"trunc-after-APPEND "
484                                                 "bad [%d-%d]/[%#x-%#x] != %c\n",
485                                                 write_size, tmp_offset - 1,
486                                                 write_size, tmp_offset - 1,
487                                                 append_char);
488                                         error = 1;
489                                 } else if (trunc_size > append_size &&
490                                            memcmp(tmp_buf+append_size,trunc_buf,
491                                                   trunc_size - append_size)) {
492                                         rprintf(rank, n, 0,"TRUNC-after-append "
493                                                 "bad [%d-%d]/[%#x-%#x] != 0\n",
494                                                 tmp_offset, trunc_offset - 1,
495                                                 tmp_offset, trunc_offset - 1);
496                                         error = 1;
497                                 }
498                         } else {
499                                 int expected_size = trunc_offset + append_size;
500                                 /* Check case 2: first truncate then append */
501                                 if (st.st_size != expected_size) {
502                                         rprintf(rank, n, 0,"APPEND-after-trunc "
503                                                 "bad file size %llu != %u\n",
504                                                 (long long)st.st_size,
505                                                 expected_size);
506                                         error = 1;
507                                 }
508
509                                 if (memcmp(tmp_buf, trunc_buf, trunc_size)) {
510                                         rprintf(rank, n, 0,"append-after-TRUNC "
511                                                 "bad [%d-%d]/[%#x-%#x] != 0\n",
512                                                 write_size, trunc_offset - 1,
513                                                 write_size, trunc_offset - 1);
514                                         error = 1;
515                                 } else if (memcmp(read_buf + trunc_offset,
516                                                   append_buf, append_size)) {
517                                         rprintf(rank, n, 0,"APPEND-after-trunc "
518                                                 "bad [%d-%d]/[%#x-%#x] != %c\n",
519                                                 trunc_offset, expected_size - 1,
520                                                 trunc_offset, expected_size - 1,
521                                                 append_char);
522                                         error = 1;
523                                 }
524                         }
525
526                         if (error == 1) {
527                                 char command[4096];
528                                 int rc;
529
530                                 rprintf(rank, n, 0, STATUS_FMT"\n",
531                                         write_char, write_size, write_size,
532                                         append_char, append_size, append_size,
533                                         trunc_offset, trunc_offset);
534
535                                 sprintf(command, "od -Ax -a %s", fnames[0]);
536                                 rc = system(command);
537                                 MPI_Abort(MPI_COMM_WORLD, 1);
538                         }
539                 }
540         }
541
542         if (rank == 0 || verbose)
543                 printf("r=%2u n=%4u: "STATUS_FMT"\nPASS\n", rank, n - 1,
544                        write_char, write_size, write_size,
545                        append_char, append_size, append_size,
546                        trunc_offset, trunc_offset);
547
548         close(fd);
549
550         if (rank == 0) {
551                 ifnames = rand() % nfnames;
552                 ret = unlink(fnames[ifnames]);
553                 if (ret != 0)
554                         printf("%s: unlink %s failed: %s\n",
555                                prog, fnames[ifnames], strerror(errno));
556         }
557
558         MPI_Finalize();
559         return 0;
560 }