1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Each loop does 3 things:
5 * - truncate file to zero (not via ftruncate though, to test O_APPEND)
6 * - append a "chunk" of data (should be at file offset 0 after truncate)
7 * - on each of two threads either append or truncate-up the file
9 * If the truncate happened first, we should have a hole in the file.
10 * If the append happened first, we should have truncated the file down.
12 * We pick the CHUNK_SIZE_MAX and APPEND_SIZE_MAX so that we cross a stripe.
14 * compile: mpicc -g -Wall -o write_append_truncate write_append_truncate.c
15 * run: mpirun -np 2 -machlist <hostlist file> write_append_truncate <file>
16 * or: pdsh -w <two hosts> write_append_truncate <file>
17 * or: prun -n 2 [-N 2] write_append_truncate <file>
22 #include <sys/types.h>
30 #define DEFAULT_ITER 50000
32 #define CHUNK_SIZE_MAX 123456
33 #define CHUNK_CHAR 'C'
35 #define APPEND_SIZE_MAX 123456
36 #define APPEND_CHAR 'A'
38 #define TRUNC_SIZE_MAX (CHUNK_SIZE_MAX+APPEND_SIZE_MAX)
40 #define HOSTNAME_SIZE 50
42 void usage(char *prog)
44 printf("usage: %s <filename> [nloops]\n", prog);
45 printf("%s must be run with at least 2 processes\n", prog);
51 /* Print process rank, loop count, message, and exit (i.e. a fatal error) */
52 int rprintf(int rank, int loop, const char *fmt, ...)
56 printf("rank %d, loop %d: ", rank, loop);
66 int main(int argc, char *argv[])
68 int n, nloops = 0, fd;
70 int chunk_size, append_size, trunc_offset;
71 char append_buf[APPEND_SIZE_MAX];
72 char chunk_buf[CHUNK_SIZE_MAX];
73 char read_buf[TRUNC_SIZE_MAX+APPEND_SIZE_MAX];
74 char trunc_buf[TRUNC_SIZE_MAX];
77 char hostname[HOSTNAME_SIZE];
80 error = MPI_Init(&argc, &argv);
81 if (error != MPI_SUCCESS)
82 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
84 prog = strrchr(argv[0], '/');
90 if (argc < 2 || argc > 3)
93 error = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
94 if (error != MPI_SUCCESS)
95 rprintf(-1, -1, "MPI_Comm_rank failed: %d\n", error);
97 error = MPI_Comm_size(MPI_COMM_WORLD, &size);
98 if (error != MPI_SUCCESS)
99 rprintf(rank, -1, "MPI_Comm_size failed: %d\n", error);
102 rprintf(rank, -1, "%s: must run with at least 2 processes\n",
105 memset(append_buf, APPEND_CHAR, APPEND_SIZE_MAX);
106 memset(chunk_buf, CHUNK_CHAR, CHUNK_SIZE_MAX);
107 memset(trunc_buf, 0, TRUNC_SIZE_MAX);
109 if (gethostname(hostname, HOSTNAME_SIZE) < 0)
110 rprintf(rank, -1, "gethostname failed: %s\n", strerror(errno));
115 nloops = strtoul(argv[2], NULL, 0);
117 nloops = DEFAULT_ITER;
120 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
122 rprintf(0, -1, "create %s failed: %s\n", fname,
124 printf("using %s\n", fname);
126 error = MPI_Barrier(MPI_COMM_WORLD);
127 if (error != MPI_SUCCESS)
128 rprintf(rank, -1, "prep MPI_Barrier failed: %d\n", error);
130 fd = open(fname, O_RDWR | O_APPEND);
132 rprintf(rank, -1, "open %s failed: %s\n",fname,strerror(errno));
134 for (n = 0; n < nloops; n++) {
135 /* reset the environment */
136 chunk_size = (rand()%(CHUNK_SIZE_MAX-1))+1;
137 append_size = (rand()%(APPEND_SIZE_MAX-1))+1;
138 trunc_offset = chunk_size + rand()%append_size;
141 printf("loop %5d: chunk %6d/%#07x, "
142 "append %6d/%#07x, trunc @ %6d/%#07x\n",
143 n, chunk_size, chunk_size, append_size,
144 append_size, trunc_offset, trunc_offset);
146 ret = truncate(fname, (off_t)0);
148 rprintf(0, n, "truncate @ 0: %s\n",
152 ret = write(fd, chunk_buf+done,chunk_size-done);
154 rprintf(0, n, "chunk @ %d: %s\n",
155 done, strerror(errno));
159 } while (done != chunk_size);
162 error = MPI_Barrier(MPI_COMM_WORLD);
163 if (error != MPI_SUCCESS)
164 rprintf(rank, n, "start MPI_Barrier: %d\n",error);
167 if (rank == n % size) {
171 ret = write(fd, append_buf + done,
175 "loop %d: append @ %u: %s\n",
176 done, strerror(errno));
180 } while (done != append_size);
181 } else if (rank == (n + 1) % size) {
182 ret = truncate(fname, (off_t)trunc_offset);
184 rprintf(rank, n, "truncate @ %u: %s\n",
185 trunc_offset, strerror(errno) );
188 error = MPI_Barrier(MPI_COMM_WORLD);
189 if (error != MPI_SUCCESS)
190 rprintf(rank, n, "end MPI_Barrier: %d\n", error);
194 /* Check the result */
197 if (stat(fname, &st) < 0)
198 rprintf(0, n, "loop %d: stat %s: %s\n",
199 fname, strerror(errno));
201 if (lseek(fd, (off_t)0, SEEK_SET) != 0)
202 rprintf(0, n, "lseek fname 0: %s\n", fname,
207 ret = read(fd, read_buf+done, st.st_size-done);
209 rprintf(0, n, "read @ %u: %s\n",
210 done, strerror(errno));
213 } while (done != st.st_size);
215 if (memcmp(read_buf, chunk_buf, chunk_size)) {
216 printf("loop %d: base chunk bad"
217 " [0-%d]/[0-%#x] != %c\n", n,
218 chunk_size - 1, chunk_size - 1,
223 if (st.st_size == trunc_offset) {
224 /* Check case 1: first append then truncate */
225 error = memcmp(read_buf+chunk_size, append_buf,
226 trunc_offset - chunk_size);
228 printf("loop %d: trunc-after-append bad"
229 " [%d-%d]/[%#x-%#x] != %c\n",
230 n, chunk_size, trunc_offset - 1,
231 chunk_size, trunc_offset - 1,
235 /* Check case 2: first truncate then append */
236 if (memcmp(read_buf+chunk_size, trunc_buf,
237 trunc_offset-chunk_size)) {
238 printf("loop %d: append-after-TRUNC bad"
239 " [%d-%d]/[%#x-%#x] != 0\n",
240 n, chunk_size, trunc_offset - 1,
241 chunk_size, trunc_offset - 1);
243 } else if (memcmp(read_buf+trunc_offset,
244 append_buf, append_size)) {
245 printf("loop %d: APPEND-after-trunc bad"
246 " [%d-%d]/[%#x-%#x] != %c\n",
247 n, trunc_offset, append_size - 1,
248 trunc_offset, append_size - 1,
254 ret = MPI_Bcast(&error, 1, MPI_INT, 0, MPI_COMM_WORLD);
255 if (ret != MPI_SUCCESS)
256 rprintf(rank, n, "MPI_Bcast: %d\n");
262 printf("loop %5d: chunk %6d/%#07x, "
263 "append %6d/%#07x, trunc @ %6d/%#07x\n",
264 n, chunk_size, chunk_size, append_size,
265 append_size, trunc_offset, trunc_offset);
267 sprintf(command, "od -Ax -a %s", fname);
270 rprintf(rank, n, "on machine %s with pid %d\n",
271 hostname, (int)getpid());
275 printf("rank %d, loop %d: finished\n", rank, n);
279 error = unlink(fname);
281 rprintf(0, n, "unlink %s failed: %s\n",
282 fname, strerror(errno));