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