Whamcloud - gitweb
LU-12795 tests: Prefer FAILF in mpi tests
[fs/lustre-release.git] / lustre / tests / mpi / cascading_rw.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) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  *
30  * lustre/tests/mpi/cascading_rw.c
31  *
32  * Author: You Feng <youfeng@clusterfs.com>
33  */
34
35 #include <config.h>
36 #include <mpi.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <asm/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/ioctl.h>
46 #include <getopt.h>
47 #include <errno.h>
48
49 #include <lustre/lustreapi.h>
50 #include "lp_utils.h"
51 #ifndef _IOWR
52 # include <ioctl.h>
53 #endif
54
55
56 int rank = 0;
57 int size = 0;
58
59 char *testdir = NULL;
60
61 void rw_file(char *name, long stride, unsigned int seed)
62 {
63         char filename[MAX_FILENAME_LEN];
64         char *buf, *o_buf;
65         struct lov_user_md lum = {0};
66         int fd, rc, i, bad = 0, root = 0;
67         long off;
68         long page_size = sysconf(_SC_PAGESIZE);
69
70         sprintf(filename, "%s/%s", testdir, name);
71
72         if (rank == 0) {
73                 remove_file_or_dir(filename);
74
75                 lum.lmm_magic = LOV_USER_MAGIC;
76                 lum.lmm_stripe_size = 0;
77                 lum.lmm_stripe_count = 0;
78                 lum.lmm_stripe_offset = -1;
79
80                 fd = open(filename, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE,
81                         FILEMODE);
82                 if (fd == -1)
83                         FAILF("open of file %s", filename);
84
85                 rc = ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum);
86                 if (rc == -1)
87                         FAILF("ioctl SETSTRIPE of file %s", filename);
88
89                 if (close(fd) == -1)
90                         FAILF("close of file %s", filename);
91         }
92
93         MPI_Barrier(MPI_COMM_WORLD);
94
95         if (stride < 0) {
96                 if (rank == 0) {
97                         srandom(seed);
98                         while (stride < page_size/2) {
99                                 stride = random();
100                                 stride -= stride % 16;
101                                 if (stride < 0)
102                                         stride = -stride;
103                                 stride %= 2 * lum.lmm_stripe_size;
104                         }
105                 }
106
107                 MPI_Barrier(MPI_COMM_WORLD);
108
109                 MPI_Bcast(&stride, 1, MPI_LONG, root, MPI_COMM_WORLD);
110         }
111
112         MPI_Barrier(MPI_COMM_WORLD);
113
114         buf = (char *)malloc(stride);
115         if (!buf)
116                 FAILF("malloc of buf with size %ld", stride);
117
118         if (rank == 0) {
119                 fd = open(filename, O_RDWR);
120                 if (fd == -1)
121                         FAILF("open of file %s", filename);
122
123                 off = 0;
124                 fill_stride(buf, stride, 0, off);
125                 rc = write(fd, buf, stride);
126                 if (rc != stride)
127                         FAILF("write of file %s return %d", filename, rc);
128
129                 off += stride;
130                 while (off < size * stride) {
131                         fill_stride(buf, stride, 0x8080808080808080ULL, off);
132                         rc = write(fd, buf, stride);
133                         if (rc != stride)
134                                 FAILF("write of file %s return %d",
135                                       filename, rc);
136                         off += stride;
137                 }
138
139                 if (close(fd) == -1)
140                         FAILF("close of file %s", filename);
141         }
142
143         MPI_Barrier(MPI_COMM_WORLD);
144
145         o_buf = (char *)malloc(stride);
146         if (!o_buf)
147                 FAILF("malloc of o_buf with size %ld", stride);
148
149         fd = open(filename, O_RDWR);
150         if (fd == -1)
151                 FAILF("open of file %s", filename);
152
153         off = 0;
154         for (i = 1; i < size; ++i) {
155                 if (rank == i) {
156                         rc = lseek(fd, off, SEEK_SET);
157                         if (rc != off)
158                                 FAILF("lseek of file %s return %d",
159                                       filename, rc);
160
161                         rc = read(fd, buf, stride);
162                         if (rc != stride) {
163                                 if (rc > 0) {
164                                         fill_stride(o_buf, rc, i - 1, off);
165                                         dump_diff(o_buf, buf, rc, off);
166                                 }
167                                 FAILF("read of file %s return %d",
168                                       filename, rc);
169                         }
170
171                         fill_stride(o_buf, stride, i - 1, off);
172                         if (memcmp(o_buf, buf, stride) != 0) {
173                                 dump_diff(o_buf, buf, stride, off);
174                                 errno = 0;
175                                 FAILF("Error: diff data read from %s",
176                                       filename);
177                         }
178                 }
179
180                 off += stride;
181
182                 if (rank == i) {
183                         fill_stride(buf, stride, i, off);
184                         rc = write(fd, buf, stride);
185                         if (rc != stride)
186                                 FAILF("write of file %s return %d",
187                                       filename, rc);
188                 }
189
190                 MPI_Barrier(MPI_COMM_WORLD);
191         }
192
193         if (close(fd) == -1)
194                 FAILF("close of file %s", filename);
195
196         MPI_Barrier(MPI_COMM_WORLD);
197
198         if (rank == 0) {
199                 fd = open(filename, O_RDONLY);
200                 if (fd == -1)
201                         FAILF("open of file %s", filename);
202
203                 off = 0;
204                 for (i = 0; i < size; ++i) {
205                         rc = read(fd, buf, stride);
206                         if (rc != stride) {
207                                 if (rc > 0) {
208                                         fill_stride(o_buf, rc, i, off);
209                                         dump_diff(o_buf, buf, rc, off);
210                                 }
211                                 FAILF("read of file %s", filename);
212                         }
213
214                         fill_stride(o_buf, stride, i, off);
215                         if (memcmp(o_buf, buf, stride) != 0) {
216                                 bad = 1;
217                                 dump_diff(o_buf, buf, stride, off);
218                         }
219                         off += stride;
220                 }
221                 if (bad == 1) {
222                         errno = 0;
223                         FAILF("Error: diff data read from %s", filename);
224                 }
225         }
226
227         MPI_Barrier(MPI_COMM_WORLD);
228         fprintf(stderr, "passed barrier 5\n");
229
230         free(buf);
231         free(o_buf);
232 }
233
234 void cascading_rw(long stride, unsigned int seed)
235 {
236         begin("setup");
237         end("setup");
238
239         begin("test");
240         rw_file("cascading_rw", stride, seed);
241         end("test");
242
243         begin("cleanup");
244         remove_file("cascading_rw");
245         end("cleanup");
246 }
247
248 void usage(char *proc)
249 {
250         int i;
251
252         if (rank == 0) {
253                 printf("Usage: %s [-h] -d <testdir> [-s \"1024\"]\n", proc);
254                 printf("           [-n \"13\"] [-e \"12345\"]\n");
255                 printf("           [-v] [-V #] [-g]\n");
256                 printf("\t-h: prints this help message\n");
257                 printf("\t-d: the directory in which the tests will run\n");
258                 printf("\t-s: process stride size\n");
259                 printf("\t-n: repeat test # times\n");
260                 printf("\t-n: random seed, used to re-create previous runs\n");
261                 printf("\t-v: increase the verbositly level by 1\n");
262                 printf("\t-V: select a specific verbosity level\n");
263                 printf("\t-g: debug mode\n");
264         }
265
266         MPI_Initialized(&i);
267         if (i)
268                 MPI_Finalize();
269         exit(0);
270 }
271
272 int main(int argc, char *argv[])
273 {
274         int i, iterations = 16, c;
275         long stride = -1;
276         unsigned int seed = 0;
277
278         /*
279          * Check for -h parameter before MPI_Init so the binary can be
280          * called directly, without, for instance, mpirun
281          */
282         for (i = 1; i < argc; ++i) {
283                 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
284                         usage(argv[0]);
285         }
286
287         MPI_Init(&argc, &argv);
288         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
289         MPI_Comm_size(MPI_COMM_WORLD, &size);
290
291         /* Parse command line options */
292         while (1) {
293                 c = getopt(argc, argv, "d:e:ghn:s:vV:");
294                 if (c == -1)
295                         break;
296
297                 switch (c) {
298                 case 'd':
299                         testdir = optarg;
300                         break;
301                 case 'e':
302                         seed = (unsigned int)atoi(optarg);
303                         break;
304                 case 'g':
305                         debug = 1;
306                         break;
307                 case 'h':
308                         usage(argv[0]);
309                         break;
310                 case 'n':
311                         iterations = atoi(optarg);
312                         break;
313                 case 's':
314                         stride = atol(optarg);
315                         break;
316                 case 'v':
317                         verbose += 1;
318                         break;
319                 case 'V':
320                         verbose = atoi(optarg);
321                         break;
322                 }
323         }
324
325         if (rank == 0)
326                 printf("%s is running with %d process(es) %s\n",
327                        argv[0], size, debug ? "in DEBUG mode" : "\b\b");
328
329         if (size < 2) {
330                 fprintf(stderr,
331                         "There should be at least 3 process to run the test!\n");
332                 MPI_Abort(MPI_COMM_WORLD, 2);
333         }
334
335         if (testdir == NULL && rank == 0) {
336                 fprintf(stderr,
337                         "Please specify a test directory! (\"%s -h\" for help)\n",
338                         argv[0]);
339                 MPI_Abort(MPI_COMM_WORLD, 2);
340         }
341
342         lp_gethostname();
343
344         for (i = 0; i < iterations; ++i) {
345                 if (rank == 0)
346                         printf("%s: Running test #%s(iter %d)\n",
347                                timestamp(), argv[0], i);
348
349                 cascading_rw(stride, seed);
350                 MPI_Barrier(MPI_COMM_WORLD);
351         }
352
353         if (rank == 0)
354                 printf("%s: All tests passed!\n", timestamp());
355         MPI_Finalize();
356         return 0;
357 }