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);
62 MPI_Abort(MPI_COMM_WORLD, 1);
65 int main(int argc, char *argv[])
67 int n, nloops = 0, fd;
69 int chunk_size, append_size, trunc_offset;
70 char append_buf[APPEND_SIZE_MAX];
71 char chunk_buf[CHUNK_SIZE_MAX];
72 char read_buf[TRUNC_SIZE_MAX+APPEND_SIZE_MAX];
73 char trunc_buf[TRUNC_SIZE_MAX];
76 char hostname[HOSTNAME_SIZE];
79 error = MPI_Init(&argc, &argv);
80 if (error != MPI_SUCCESS)
81 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
83 prog = strrchr(argv[0], '/');
89 if (argc < 2 || argc > 3)
92 error = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
93 if (error != MPI_SUCCESS)
94 rprintf(-1, -1, "MPI_Comm_rank failed: %d\n", error);
96 error = MPI_Comm_size(MPI_COMM_WORLD, &size);
97 if (error != MPI_SUCCESS)
98 rprintf(rank, -1, "MPI_Comm_size failed: %d\n", error);
101 rprintf(rank, -1, "%s: must run with at least 2 processes\n",
104 memset(append_buf, APPEND_CHAR, APPEND_SIZE_MAX);
105 memset(chunk_buf, CHUNK_CHAR, CHUNK_SIZE_MAX);
106 memset(trunc_buf, 0, TRUNC_SIZE_MAX);
108 if (gethostname(hostname, HOSTNAME_SIZE) < 0)
109 rprintf(rank, -1, "gethostname failed: %s\n", strerror(errno));
114 nloops = strtoul(argv[2], NULL, 0);
116 nloops = DEFAULT_ITER;
119 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
121 rprintf(0, -1, "create %s failed: %s\n", fname,
123 printf("using %s\n", fname);
125 error = MPI_Barrier(MPI_COMM_WORLD);
126 if (error != MPI_SUCCESS)
127 rprintf(rank, -1, "prep MPI_Barrier failed: %d\n", error);
129 fd = open(fname, O_RDWR | O_APPEND);
131 rprintf(rank, -1, "open %s failed: %s\n",fname,strerror(errno));
133 for (n = 0; n < nloops; n++) {
134 /* reset the environment */
135 chunk_size = (rand()%(CHUNK_SIZE_MAX-1))+1;
136 append_size = (rand()%(APPEND_SIZE_MAX-1))+1;
137 trunc_offset = chunk_size + rand()%append_size;
140 printf("loop %5d: chunk %6d/%#07x, "
141 "append %6d/%#07x, trunc @ %6d/%#07x\n",
142 n, chunk_size, chunk_size, append_size,
143 append_size, trunc_offset, trunc_offset);
145 ret = truncate(fname, (off_t)0);
147 rprintf(0, n, "truncate @ 0: %s\n",
151 ret = write(fd, chunk_buf+done,chunk_size-done);
153 rprintf(0, n, "chunk @ %d: %s\n",
154 done, strerror(errno));
158 } while (done != chunk_size);
161 error = MPI_Barrier(MPI_COMM_WORLD);
162 if (error != MPI_SUCCESS)
163 rprintf(rank, n, "start MPI_Barrier: %d\n",error);
166 if (rank == n % size) {
170 ret = write(fd, append_buf + done,
174 "loop %d: append @ %u: %s\n",
175 done, strerror(errno));
179 } while (done != append_size);
180 } else if (rank == (n + 1) % size) {
181 ret = truncate(fname, (off_t)trunc_offset);
183 rprintf(rank, n, "truncate @ %u: %s\n",
184 trunc_offset, strerror(errno) );
187 error = MPI_Barrier(MPI_COMM_WORLD);
188 if (error != MPI_SUCCESS)
189 rprintf(rank, n, "end MPI_Barrier: %d\n", error);
193 /* Check the result */
196 if (stat(fname, &st) < 0)
197 rprintf(0, n, "loop %d: stat %s: %s\n",
198 fname, strerror(errno));
200 if (lseek(fd, (off_t)0, SEEK_SET) != 0)
201 rprintf(0, n, "lseek fname 0: %s\n", fname,
206 ret = read(fd, read_buf+done, st.st_size-done);
208 rprintf(0, n, "read @ %u: %s\n",
209 done, strerror(errno));
212 } while (done != st.st_size);
214 if (memcmp(read_buf, chunk_buf, chunk_size)) {
215 printf("loop %d: base chunk bad"
216 " [0-%d]/[0-%#x] != %c\n", n,
217 chunk_size - 1, chunk_size - 1,
222 if (st.st_size == trunc_offset) {
223 /* Check case 1: first append then truncate */
224 error = memcmp(read_buf+chunk_size, append_buf,
225 trunc_offset - chunk_size);
227 printf("loop %d: trunc-after-append bad"
228 " [%d-%d]/[%#x-%#x] != %c\n",
229 n, chunk_size, trunc_offset - 1,
230 chunk_size, trunc_offset - 1,
234 /* Check case 2: first truncate then append */
235 if (memcmp(read_buf+chunk_size, trunc_buf,
236 trunc_offset-chunk_size)) {
237 printf("loop %d: append-after-TRUNC bad"
238 " [%d-%d]/[%#x-%#x] != 0\n",
239 n, chunk_size, trunc_offset - 1,
240 chunk_size, trunc_offset - 1);
242 } else if (memcmp(read_buf+trunc_offset,
243 append_buf, append_size)) {
244 printf("loop %d: APPEND-after-trunc bad"
245 " [%d-%d]/[%#x-%#x] != %c\n",
246 n, trunc_offset, append_size - 1,
247 trunc_offset, append_size - 1,
253 ret = MPI_Bcast(&error, 1, MPI_INT, 0, MPI_COMM_WORLD);
254 if (ret != MPI_SUCCESS)
255 rprintf(rank, n, "MPI_Bcast: %d\n");
261 printf("loop %5d: chunk %6d/%#07x, "
262 "append %6d/%#07x, trunc @ %6d/%#07x\n",
263 n, chunk_size, chunk_size, append_size,
264 append_size, trunc_offset, trunc_offset);
266 sprintf(command, "od -Ax -a %s", fname);
269 rprintf(rank, n, "on machine %s with pid %d\n",
270 hostname, (int)getpid());
274 printf("rank %d, loop %d: finished\n", rank, n);
278 error = unlink(fname);
280 rprintf(0, n, "unlink %s failed: %s\n",
281 fname, strerror(errno));