Whamcloud - gitweb
Update copyright messages.
[fs/lustre-release.git] / lustre / tests / mpi / write_disjoint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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
92         error = MPI_Init(&argc, &argv);
93         if (error != MPI_SUCCESS)
94                 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
95         /* Parse command line options */
96         while ((c = getopt(argc, argv, "f:n:")) != EOF) {
97                 switch (c) {
98                 case 'f':
99                         filename = optarg;
100                         break;
101                 case 'n':
102                         numloops = strtoul(optarg, NULL, 0);
103                         break;
104                 }
105         }
106
107         MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
108         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
109
110         chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
111         for (i=0; i < noProcessors; i++) {
112                 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
113                 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
114         }
115         read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
116
117         if (rank == 0) {
118                 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
119                 if (fd < 0)
120                         rprintf(rank, -1, "open() returned %s\n",
121                                 strerror(errno));
122         }
123         MPI_Barrier(MPI_COMM_WORLD);
124
125         fd = open(filename, O_RDWR);
126         if (fd < 0)
127                 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
128
129         for (n = 0; n < numloops; n++) {
130                 /* reset the environment */
131                 if (rank == 0) {
132                         ret = truncate(filename, 0);
133                         if (ret != 0)
134                                 rprintf(rank, n, "truncate() returned %s\n",
135                                         strerror(errno) );
136                 }
137                 CHUNK_SIZE(n) = rand() % CHUNK_MAX_SIZE;
138
139                 if (n % 1000 == 0 && rank == 0)
140                         printf("loop %d: chunk_size %lu\n", n, CHUNK_SIZE(n));
141
142                 MPI_Barrier(MPI_COMM_WORLD);
143
144                 /* Do the race */
145                 offset = rank * CHUNK_SIZE(n);
146                 lseek(fd, offset, SEEK_SET);
147
148                 done = 0;
149                 do {
150                         ret = write(fd, chunk_buf[rank] + done,
151                                     CHUNK_SIZE(n) - done);
152                         if (ret < 0 && errno != EINTR)
153                                 rprintf(rank, n, "write() returned %s\n",
154                                         strerror(errno));
155                         if (ret > 0)
156                                 done += ret;
157                 } while (done != CHUNK_SIZE(n));
158
159                 MPI_Barrier(MPI_COMM_WORLD);
160
161                 /* Check the result */
162                 if (stat(filename, &stat_buf) < 0)
163                         rprintf(rank, n, "error stating %s: %s\n",
164                                 filename, strerror(errno));
165
166                 if (stat_buf.st_size != CHUNK_SIZE(n) * noProcessors) {
167                         if (n > 0)
168                                 printf("loop %d: chunk_size %lu, "
169                                        "file size was %lu\n",
170                                        n - 1, CHUNK_SIZE(n - 1),
171                                        CHUNK_SIZE(n - 1) *noProcessors);
172                         rprintf(rank, n, "invalid file size %lu"
173                                 " instead of %lu = %lu * %u\n",
174                                 (unsigned long)stat_buf.st_size,
175                                 CHUNK_SIZE(n) * noProcessors,
176                                 CHUNK_SIZE(n), noProcessors);
177                 }
178
179                 if (rank == 0) {
180                         if (lseek(fd, 0, SEEK_SET) < 0)
181                                 rprintf(rank, n, "error seeking to 0: %s\n",
182                                         strerror(errno));
183
184                         done = 0;
185                         do {
186                                 ret = read(fd, read_buf + done,
187                                            CHUNK_SIZE(n) * noProcessors - done);
188                                 if (ret < 0)
189                                         rprintf(rank, n, "read returned %s\n",
190                                                 strerror(errno));
191
192                                 done += ret;
193                         } while (done != CHUNK_SIZE(n) * noProcessors);
194
195                         for (i = 0; i < noProcessors; i++) {
196                                 char command[4096];
197                                 int j, rc;
198                                 if (!memcmp(read_buf + (i * CHUNK_SIZE(n)),
199                                             chunk_buf[i], CHUNK_SIZE(n)))
200                                         continue;
201
202                                 /* print out previous chunk sizes */
203                                 if (n > 0)
204                                         printf("loop %d: chunk_size %lu\n",
205                                                n - 1, CHUNK_SIZE(n - 1));
206
207                                 printf("loop %d: chunk %d corrupted "
208                                        "with chunk_size %lu, page_size %d\n",
209                                        n, i, CHUNK_SIZE(n), getpagesize());
210                                 printf("ranks:\tpage boundry\tchunk boundry\t"
211                                        "page boundry\n");
212                                 for (j = 1 ; j < noProcessors; j++) {
213                                         int b = j * CHUNK_SIZE(n);
214                                         printf("%c -> %c:\t%d\t%d\t%d\n",
215                                                'A' + j - 1, 'A' + j,
216                                                b & ~(getpagesize()-1), b,
217                                                (b + getpagesize()) &
218                                                ~(getpagesize()-1));
219                                 }
220
221                                 sprintf(command, "od -Ad -a %s", filename);
222                                 rc = system(command);
223                                 rprintf(0, n, "data check error - exiting\n");
224                         }
225                 }
226                 MPI_Barrier(MPI_COMM_WORLD);
227         }
228
229         printf("Finished after %d loops\n", n);
230         MPI_Finalize();
231         return 0;
232 }