Whamcloud - gitweb
LU-9611 lod: allow -1 for default stripe count/offset
[fs/lustre-release.git] / lustre / tests / mpi / write_disjoint.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/tests/write_disjoint.c
33  *
34  * Each loop does 3 things:
35  *   - rank 0 truncates to 0
36  *   - all ranks agree on a random chunk size
37  *   - all ranks race to write their pattern to their chunk of the file
38  *   - rank 0 makes sure that the resulting file size is ranks * chunk size
39  *   - rank 0 makes sure that everyone's patterns went to the right place
40  *
41  * compile: mpicc -g -Wall -o write_disjoint write_disjoint.c
42  * run:     mpirun -np N -machlist <hostlist file> write_disjoint
43  *  or:     pdsh -w <N hosts> write_disjoint
44  *  or:     prun -n N [-N M] write_disjoint
45  */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdarg.h>
56 #include "mpi.h"
57
58 #define CHUNK_MAX_SIZE 123456
59
60 void rprintf(int rank, int loop, const char *fmt, ...)
61 {
62         va_list       ap;
63
64         printf("rank %d, loop %d: ", rank, loop);
65
66         va_start(ap, fmt);
67
68         vprintf(fmt, ap);
69
70         MPI_Abort(MPI_COMM_WORLD, -1); /* This will exit() according to man */
71 }
72
73 #define CHUNK_SIZE(n) chunk_size[(n) % 2]
74
75 int main (int argc, char *argv[]) {
76         int i, n, fd, c;
77         unsigned long chunk_size[2];
78         int rank, noProcessors, done;
79         int error;
80         off_t offset;
81         char **chunk_buf;
82         char *read_buf;
83         struct stat stat_buf;
84         ssize_t ret;
85         char *filename = "/mnt/lustre/write_disjoint";
86         int numloops = 1000;
87         int random = 0;
88
89         error = MPI_Init(&argc, &argv);
90         if (error != MPI_SUCCESS)
91                 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
92         /* Parse command line options */
93         while ((c = getopt(argc, argv, "f:n:")) != EOF) {
94                 switch (c) {
95                 case 'f':
96                         filename = optarg;
97                         break;
98                 case 'n':
99                         numloops = strtoul(optarg, NULL, 0);
100                         break;
101                 }
102         }
103
104         MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
105         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
106
107         chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
108         for (i=0; i < noProcessors; i++) {
109                 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
110                 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
111         }
112         read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
113
114         if (rank == 0) {
115                 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
116                 if (fd < 0)
117                         rprintf(rank, -1, "open() returned %s\n",
118                                 strerror(errno));
119         }
120         MPI_Barrier(MPI_COMM_WORLD);
121
122         fd = open(filename, O_RDWR);
123         if (fd < 0)
124                 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
125
126         for (n = 0; n < numloops; n++) {
127                 /* reset the environment */
128                 if (rank == 0) {
129                         ret = truncate(filename, 0);
130                         if (ret != 0)
131                                 rprintf(rank, n, "truncate() returned %s\n",
132                                         strerror(errno) );
133                         random = rand();
134                 }
135                 MPI_Bcast(&random, 1, MPI_INT, 0, MPI_COMM_WORLD);
136                 CHUNK_SIZE(n) = random % CHUNK_MAX_SIZE;
137
138                 if (n % 1000 == 0 && rank == 0)
139                         printf("loop %d: chunk_size %lu\n", n, CHUNK_SIZE(n));
140
141                 if (stat(filename, &stat_buf) < 0)
142                         rprintf(rank, n, "error stating %s: %s\n",
143                                 filename, strerror(errno));
144
145                 if (stat_buf.st_size != 0)
146                         rprintf(rank, n, "filesize = %lu. "
147                                 "Should be zero after truncate\n",
148                                 stat_buf.st_size);
149
150                 MPI_Barrier(MPI_COMM_WORLD);
151
152                 /* Do the race */
153                 offset = rank * CHUNK_SIZE(n);
154                 lseek(fd, offset, SEEK_SET);
155
156                 done = 0;
157                 do {
158                         ret = write(fd, chunk_buf[rank] + done,
159                                     CHUNK_SIZE(n) - done);
160                         if (ret < 0 && errno != EINTR)
161                                 rprintf(rank, n, "write() returned %s\n",
162                                         strerror(errno));
163                         if (ret > 0)
164                                 done += ret;
165                 } while (done != CHUNK_SIZE(n));
166
167                 MPI_Barrier(MPI_COMM_WORLD);
168
169                 /* Check the result */
170                 if (stat(filename, &stat_buf) < 0)
171                         rprintf(rank, n, "error stating %s: %s\n",
172                                 filename, strerror(errno));
173
174                 if (stat_buf.st_size != CHUNK_SIZE(n) * noProcessors) {
175                         if (n > 0)
176                                 printf("loop %d: chunk_size %lu, "
177                                        "file size was %lu\n",
178                                        n - 1, CHUNK_SIZE(n - 1),
179                                        CHUNK_SIZE(n - 1) *noProcessors);
180                         rprintf(rank, n, "invalid file size %lu"
181                                 " instead of %lu = %lu * %u\n",
182                                 (unsigned long)stat_buf.st_size,
183                                 CHUNK_SIZE(n) * noProcessors,
184                                 CHUNK_SIZE(n), noProcessors);
185                 }
186
187                 if (rank == 0) {
188                         if (lseek(fd, 0, SEEK_SET) < 0)
189                                 rprintf(rank, n, "error seeking to 0: %s\n",
190                                         strerror(errno));
191
192                         done = 0;
193                         do {
194                                 ret = read(fd, read_buf + done,
195                                            CHUNK_SIZE(n) * noProcessors - done);
196                                 if (ret < 0)
197                                         rprintf(rank, n, "read returned %s\n",
198                                                 strerror(errno));
199
200                                 done += ret;
201                         } while (done != CHUNK_SIZE(n) * noProcessors);
202
203                         for (i = 0; i < noProcessors; i++) {
204                                 char command[4096];
205                                 int j;
206                                 
207                                 if (!memcmp(read_buf + (i * CHUNK_SIZE(n)),
208                                             chunk_buf[i], CHUNK_SIZE(n)))
209                                         continue;
210
211                                 /* print out previous chunk sizes */
212                                 if (n > 0)
213                                         printf("loop %d: chunk_size %lu\n",
214                                                n - 1, CHUNK_SIZE(n - 1));
215
216                                 printf("loop %d: chunk %d corrupted "
217                                        "with chunk_size %lu, page_size %d\n",
218                                        n, i, CHUNK_SIZE(n), getpagesize());
219                                 printf("ranks:\tpage boundry\tchunk boundry\t"
220                                        "page boundry\n");
221                                 for (j = 1 ; j < noProcessors; j++) {
222                                         int b = j * CHUNK_SIZE(n);
223                                         printf("%c -> %c:\t%d\t%d\t%d\n",
224                                                'A' + j - 1, 'A' + j,
225                                                b & ~(getpagesize()-1), b,
226                                                (b + getpagesize()) &
227                                                ~(getpagesize()-1));
228                                 }
229
230                                 sprintf(command, "od -Ad -a %s", filename);
231                                 ret = system(command);
232                                 rprintf(0, n, "data check error - exiting\n");
233                         }
234                 }
235                 MPI_Barrier(MPI_COMM_WORLD);
236         }
237
238         printf("Finished after %d loops\n", n);
239         MPI_Finalize();
240         return 0;
241 }