4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/tests/write_append_truncate.c
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
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.
42 * WRITE_SIZE_MAX and APPEND_SIZE_MAX are large enough to cross a stripe.
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>
53 #include <sys/types.h>
63 #define DEFAULT_ITER 10000
65 #define WRITE_SIZE_MAX 1234567
66 #define APPEND_SIZE_MAX 1234567
67 #define TRUNC_SIZE_MAX 1234567
69 #define STATUS_FMT "WR %c %7d/%#08x, AP %c %7d/%#08x, TR@ %7d/%#08x"
71 #define HOSTNAME_SIZE 50
72 char hostname[HOSTNAME_SIZE];
73 #define FNAMES_MAX 256
75 void usage(char *prog)
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",
79 printf("\t-a append_max: maximum size of append, default %u bytes\n",
81 printf("\t-C: 'classic' checks (on file 0)\n");
82 printf("\t-n nloops: count of loops to run, default %u\n",
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",
87 printf("\t-T: 'classic' truncates (on file 0)\n");
88 printf("\t-w write_max: maximum size of write, default %u bytes\n",
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);
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)));
103 void rprintf(int rank, int loop, int error, const char *fmt, ...)
107 printf("r=%2u", rank);
109 printf(" l=%04u", loop);
111 printf(" %s", hostname);
119 MPI_Abort(MPI_COMM_WORLD, error);
122 int main(int argc, char *argv[])
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;
133 char *read_buf = NULL;
134 char *trunc_buf = NULL;
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";
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);
151 prog = strrchr(argv[0], '/');
157 while ((c = getopt(argc, argv, "a:cCn:s:t:Tvw:W")) != -1) {
160 append_max = strtoul(optarg, &end, 0);
161 if (append_max < 2 || *end) {
162 fprintf(stderr, "%s: bad append option '%s'\n",
171 nloops = strtoul(optarg, &end, 0);
172 if (nloops == 0 || *end) {
173 fprintf(stderr, "%s: bad nloops option '%s'\n",
179 seed = strtoul(optarg, &end, 0);
181 fprintf(stderr, "%s: bad seed option '%s'\n",
187 trunc_max = strtoul(optarg, &end, 0);
190 "%s: bad truncate option '%s'\n", prog,
202 write_max = strtoul(optarg, &end, 0);
203 if (write_max < 2 || *end) {
204 fprintf(stderr, "%s: bad write option '%s'\n",
213 fprintf(stderr, "%s: unknown option '%c'\n", prog, c);
220 if (argc == optind) {
221 fprintf(stderr, "%s: missing filename argument\n", prog);
225 if (argc > optind + FNAMES_MAX) {
226 fprintf(stderr, "%s: too many extra options\n", prog);
230 while (optind < argc)
231 fnames[nfnames++] = argv[optind++];
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",
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",
244 rprintf(rank, -1, 1, "%s: must run with at least 2 processes\n",
247 append_buf = malloc(append_max);
249 rprintf(rank, -1, 1, "%s: error allocating append_buf %u\n",
252 write_buf = malloc(write_max);
254 rprintf(rank, -1, 1, "%s: error allocating write_buf %u\n",
257 if (gethostname(hostname, HOSTNAME_SIZE) < 0)
258 rprintf(rank, -1, 1, "%s: gethostname failed: %s\n",
259 prog, strerror(errno));
262 int max_size = write_max + (trunc_max ?: append_max) +
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));
271 trunc_buf = calloc(1, trunc_max ?: append_max);
274 "%s: error allocating trunc_buf %u\n",
275 prog, trunc_max ?: append_max);
277 /* initial write + truncate up + append */
278 read_buf = malloc(max_size);
281 "%s: error allocating read_buf %u\n",
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);
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));
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];
302 /* reset the environment */
303 write_char = 'A' + (n % 26);
304 append_char = 'a' + (n % 26);
307 write_size = (rand() % (write_max - 1)) + 1;
308 append_size = (rand() % (append_max - 1)) + 1;
309 trunc_size = (append_size == 1) ? 1 :
311 ((trunc_max ?: append_size) - 1)) + 1;
312 trunc_offset = write_size + trunc_size;
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);
320 write_rank = (classic_write ? 0 : rand()) % nproc;
322 append_rank = (classic_write ? n : rand()) %
325 * We can't allow the append rank be the same
326 * as the classic_trunc trunc_rank, or we will
329 } while (append_rank == (n + 1) % nproc);
331 trunc_rank = (classic_trunc ? (n + 1) :
333 } while (trunc_rank == append_rank);
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;
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);
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];
360 trunc_offset = write_size + trunc_size;
363 if (rank == write_rank || rank == 0)
364 memset(write_buf, write_char, write_max);
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,
377 ret = write(fd, write_buf + done,
379 if (verbose > 1 || ret < 0) {
381 ret < 0 && errno != EINTR,
382 "write %d/%d @ %d: %s\n",
383 ret + done, write_size, done,
385 if (ret < 0 && errno != EINTR)
390 } while (done != write_size);
393 if (rank == append_rank || rank == 0)
394 memset(append_buf, append_char, append_size);
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);
402 if (rank == append_rank) {
405 ret = write(fd, append_buf + done,
408 rprintf(rank, n, errno != EINTR,
409 "append %u/%u: %s\n",
410 ret + done, append_size,
414 } else if (verbose > 1 || ret != append_size) {
415 rprintf(rank, n, ret != append_size,
417 ret + done, append_size);
421 } while (done != append_size);
422 } else if (rank == trunc_rank) {
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).
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));
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);
444 /* Check the result */
447 struct stat st = { 0 };
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));
457 ret = lseek(fd, 0, SEEK_SET);
459 rprintf(rank, n, ret, "lseek 0: %s\n",
464 ret = read(fd, read_buf + done,
466 if (verbose > 1 || ret <= 0) {
467 rprintf(rank, n, ret <= 0,
468 "read %d/%llu @ %u: %s\n",
470 (long long)st.st_size - done,
472 strerror(errno) : "short read");
475 } while (done != st.st_size);
477 if (memcmp(read_buf, write_buf, write_size)) {
479 "WRITE bad [0-%d]/[0-%#x] != %c\n",
480 write_size - 1, write_size - 1,
485 tmp_buf = read_buf + write_size;
487 if (st.st_size == trunc_offset) {
488 /* Check case 1: first append then truncate */
489 int tmp_size, tmp_offset;
491 tmp_size = trunc_size < append_size ?
492 trunc_size : append_size;
493 tmp_offset = write_size + tmp_size;
495 if (memcmp(tmp_buf, append_buf, tmp_size)) {
497 "trunc-after-APPEND bad [%d-%d]/[%#x-%#x] != %c\n",
498 write_size, tmp_offset - 1,
499 write_size, tmp_offset - 1,
502 } else if (trunc_size > append_size &&
503 memcmp(tmp_buf + append_size,
505 trunc_size - append_size)) {
507 "TRUNC-after-append bad [%d-%d]/[%#x-%#x] != 0\n",
508 tmp_offset, trunc_offset - 1,
509 tmp_offset, trunc_offset - 1);
513 int expected_size = trunc_offset + append_size;
514 /* Check case 2: first truncate then append */
515 if (st.st_size != expected_size) {
517 "APPEND-after-trunc bad file size %llu != %u\n",
518 (long long)st.st_size,
523 if (memcmp(tmp_buf, trunc_buf, trunc_size)) {
525 "append-after-TRUNC bad [%d-%d]/[%#x-%#x] != 0\n",
526 write_size, trunc_offset - 1,
527 write_size, trunc_offset - 1);
529 } else if (memcmp(read_buf + trunc_offset,
530 append_buf, append_size)) {
532 "APPEND-after-trunc bad [%d-%d]/[%#x-%#x] != %c\n",
533 trunc_offset, expected_size - 1,
534 trunc_offset, expected_size - 1,
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);
548 sprintf(command, "od -Ax -a %s", fnames[0]);
549 ret = system(command);
550 MPI_Abort(MPI_COMM_WORLD, 1);
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);
563 ifnames = rand() % nfnames;
564 ret = unlink(fnames[ifnames]);
566 printf("%s: unlink %s failed: %s\n",
567 prog, fnames[ifnames], strerror(errno));