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