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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/tests/write_append_truncate.c
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
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.
44 * WRITE_SIZE_MAX and APPEND_SIZE_MAX are large enough to cross a stripe.
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>
55 #include <sys/types.h>
65 #define DEFAULT_ITER 10000
67 #define WRITE_SIZE_MAX 1234567
68 #define APPEND_SIZE_MAX 1234567
69 #define TRUNC_SIZE_MAX 1234567
71 #define STATUS_FMT "WR %c %7d/%#08x, AP %c %7d/%#08x, TR@ %7d/%#08x"
73 #define HOSTNAME_SIZE 50
74 char hostname[HOSTNAME_SIZE];
75 #define FNAMES_MAX 256
77 void usage(char *prog)
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",
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",
88 printf("\t-T: 'classic' truncates (on file 0)\n");
89 printf("\t-w write_max: maximum size of write, default %u bytes\n",
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);
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)));
104 void rprintf(int rank, int loop, int error, const char *fmt, ...)
108 printf("r=%2u", rank);
110 printf(" l=%04u", loop);
112 printf(" %s", hostname);
120 MPI_Abort(MPI_COMM_WORLD, error);
123 int main(int argc, char *argv[])
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;
134 char *read_buf = NULL;
135 char *trunc_buf = NULL;
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";
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);
152 prog = strrchr(argv[0], '/');
158 while ((c = getopt(argc, argv, "a:cCn:s:t:Tvw:W")) != -1) {
161 append_max = strtoul(optarg, &end, 0);
162 if (append_max == 0 || *end) {
163 fprintf(stderr, "%s: bad append option '%s'\n",
172 nloops = strtoul(optarg, &end, 0);
173 if (nloops == 0 || *end) {
174 fprintf(stderr, "%s: bad nloops option '%s'\n",
180 seed = strtoul(optarg, &end, 0);
182 fprintf(stderr, "%s: bad seed option '%s'\n",
188 trunc_max = strtoul(optarg, &end, 0);
190 fprintf(stderr,"%s: bad truncate option '%s'\n",
202 write_max = strtoul(optarg, &end, 0);
203 if (write_max == 0 || *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);
248 if (append_buf == NULL)
249 rprintf(rank, -1, 1,"%s: error allocating append_buf %u\n",
252 write_buf = malloc(write_max);
253 if (write_buf == NULL)
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)+append_max;
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));
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);
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",
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);
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));
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];
297 /* reset the environment */
298 write_char = 'A' + (n % 26);
299 append_char = 'a' + (n % 26);
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;
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);
313 write_rank = (classic_write ? 0 : rand()) % nproc;
315 append_rank = (classic_write ? n : rand()) %
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);
322 trunc_rank = (classic_trunc? (n + 1) : rand()) %
324 } while (trunc_rank == append_rank);
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;
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);
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];
352 trunc_offset = write_size + trunc_size;
355 if (rank == write_rank || rank == 0)
356 memset(write_buf, write_char, write_max);
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,
369 ret = write(fd, write_buf+done,write_size-done);
370 if (verbose > 1 || ret < 0) {
372 ret < 0 && errno != EINTR,
373 "write %d/%d @ %d: %s\n",
374 ret + done, write_size, done,
376 if (ret < 0 && errno != EINTR)
381 } while (done != write_size);
384 if (rank == append_rank || rank == 0)
385 memset(append_buf, append_char, append_size);
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);
393 if (rank == append_rank) {
396 ret = write(fd, append_buf + done,
399 rprintf(rank, n, errno != EINTR,
400 "append %u/%u: %s\n",
401 ret + done, append_size,
405 } else if (verbose > 1 || ret != append_size) {
406 rprintf(rank, n, ret != append_size,
408 ret + done, append_size);
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));
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);
433 /* Check the result */
436 struct stat st = { 0 };
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));
446 ret = lseek(fd, 0, SEEK_SET);
448 rprintf(rank, n, ret, "lseek 0: %s\n",
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,
459 strerror(errno) : "short read");
462 } while (done != st.st_size);
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,
472 tmp_buf = read_buf + write_size;
474 if (st.st_size == trunc_offset) {
475 /* Check case 1: first append then truncate */
476 int tmp_size, tmp_offset;
478 tmp_size = trunc_size < append_size ?
479 trunc_size : append_size;
480 tmp_offset = write_size + tmp_size;
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,
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);
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,
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);
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,
529 rprintf(rank, n, 0, STATUS_FMT"\n",
530 write_char, write_size, write_size,
531 append_char, append_size, append_size,
532 trunc_offset, trunc_offset);
534 sprintf(command, "od -Ax -a %s", fnames[0]);
535 ret = system(command);
536 MPI_Abort(MPI_COMM_WORLD, 1);
541 if (rank == 0 || verbose)
542 printf("r=%2u n=%4u: "STATUS_FMT"\nPASS\n", rank, n - 1,
543 write_char, write_size, write_size,
544 append_char, append_size, append_size,
545 trunc_offset, trunc_offset);
550 ifnames = rand() % nfnames;
551 ret = unlink(fnames[ifnames]);
553 printf("%s: unlink %s failed: %s\n",
554 prog, fnames[ifnames], strerror(errno));