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