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"
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",
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",
86 printf("\t-T: 'classic' truncates (on file 0)\n");
87 printf("\t-w write_max: maximum size of write, default %u bytes\n",
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);
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)));
102 void rprintf(int rank, int loop, int error, const char *fmt, ...)
106 printf("r=%2u", rank);
108 printf(" l=%04u", loop);
110 printf(" %s", hostname);
118 MPI_Abort(MPI_COMM_WORLD, error);
121 int main(int argc, char *argv[])
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;
132 char *read_buf = NULL;
133 char *trunc_buf = NULL;
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";
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);
150 prog = strrchr(argv[0], '/');
156 while ((c = getopt(argc, argv, "a:cCn:s:t:Tvw:W")) != -1) {
159 append_max = strtoul(optarg, &end, 0);
160 if (append_max < 2 || *end) {
161 fprintf(stderr, "%s: bad append option '%s'\n",
170 nloops = strtoul(optarg, &end, 0);
171 if (nloops == 0 || *end) {
172 fprintf(stderr, "%s: bad nloops option '%s'\n",
178 seed = strtoul(optarg, &end, 0);
180 fprintf(stderr, "%s: bad seed option '%s'\n",
186 trunc_max = strtoul(optarg, &end, 0);
188 fprintf(stderr,"%s: bad truncate option '%s'\n",
200 write_max = strtoul(optarg, &end, 0);
201 if (write_max < 2 || *end) {
202 fprintf(stderr, "%s: bad write option '%s'\n",
211 fprintf(stderr, "%s: unknown option '%c'\n", prog, c);
218 if (argc == optind) {
219 fprintf(stderr, "%s: missing filename argument\n", prog);
223 if (argc > optind + FNAMES_MAX) {
224 fprintf(stderr, "%s: too many extra options\n", prog);
228 while (optind < argc)
229 fnames[nfnames++] = argv[optind++];
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",
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",
242 rprintf(rank, -1, 1, "%s: must run with at least 2 processes\n",
245 append_buf = malloc(append_max);
246 if (append_buf == NULL)
247 rprintf(rank, -1, 1,"%s: error allocating append_buf %u\n",
250 write_buf = malloc(write_max);
251 if (write_buf == NULL)
252 rprintf(rank, -1, 1, "%s: error allocating write_buf %u\n",
255 if (gethostname(hostname, HOSTNAME_SIZE) < 0)
256 rprintf(rank, -1, 1, "%s: gethostname failed: %s\n",
257 prog, strerror(errno));
260 int max_size = write_max + (trunc_max ?: append_max)+append_max;
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));
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);
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",
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);
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));
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];
295 /* reset the environment */
296 write_char = 'A' + (n % 26);
297 append_char = 'a' + (n % 26);
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;
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);
312 write_rank = (classic_write ? 0 : rand()) % nproc;
314 append_rank = (classic_write ? n : rand()) %
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);
321 trunc_rank = (classic_trunc? (n + 1) : rand()) %
323 } while (trunc_rank == append_rank);
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;
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);
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];
351 trunc_offset = write_size + trunc_size;
354 if (rank == write_rank || rank == 0)
355 memset(write_buf, write_char, write_max);
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,
368 ret = write(fd, write_buf+done,write_size-done);
369 if (verbose > 1 || ret < 0) {
371 ret < 0 && errno != EINTR,
372 "write %d/%d @ %d: %s\n",
373 ret + done, write_size, done,
375 if (ret < 0 && errno != EINTR)
380 } while (done != write_size);
383 if (rank == append_rank || rank == 0)
384 memset(append_buf, append_char, append_size);
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);
392 if (rank == append_rank) {
395 ret = write(fd, append_buf + done,
398 rprintf(rank, n, errno != EINTR,
399 "append %u/%u: %s\n",
400 ret + done, append_size,
404 } else if (verbose > 1 || ret != append_size) {
405 rprintf(rank, n, ret != append_size,
407 ret + done, append_size);
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));
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);
432 /* Check the result */
435 struct stat st = { 0 };
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));
445 ret = lseek(fd, 0, SEEK_SET);
447 rprintf(rank, n, ret, "lseek 0: %s\n",
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,
458 strerror(errno) : "short read");
461 } while (done != st.st_size);
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,
471 tmp_buf = read_buf + write_size;
473 if (st.st_size == trunc_offset) {
474 /* Check case 1: first append then truncate */
475 int tmp_size, tmp_offset;
477 tmp_size = trunc_size < append_size ?
478 trunc_size : append_size;
479 tmp_offset = write_size + tmp_size;
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,
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);
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,
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);
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,
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);
533 sprintf(command, "od -Ax -a %s", fnames[0]);
534 ret = system(command);
535 MPI_Abort(MPI_COMM_WORLD, 1);
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);
549 ifnames = rand() % nfnames;
550 ret = unlink(fnames[ifnames]);
552 printf("%s: unlink %s failed: %s\n",
553 prog, fnames[ifnames], strerror(errno));