1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/mpi/cascading_rw.c
38 * Author: You Feng <youfeng@clusterfs.com>
46 #include <sys/types.h>
47 #include <asm/types.h>
51 #include <sys/ioctl.h>
55 #include <libcfs/libcfs.h>
61 #include <lustre/liblustreapi.h>
68 void rw_file(char *name, long stride, unsigned int seed)
70 char filename[MAX_FILENAME_LEN];
71 char path[MAX_FILENAME_LEN];
72 char errmsg[MAX_FILENAME_LEN+20];
74 struct lov_user_md lum = {0};
75 int fd, rc, i, bad = 0, root = 0;
77 long page_size = sysconf(_SC_PAGESIZE);
79 sprintf(filename, "%s/%s", testdir, name);
82 remove_file_or_dir(filename);
84 lum.lmm_magic = LOV_USER_MAGIC;
85 lum.lmm_stripe_size = 0;
86 lum.lmm_stripe_count = 0;
87 lum.lmm_stripe_offset = -1;
89 fd = open(filename, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE,
92 sprintf(errmsg, "open of file %s", filename);
96 rc = ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum);
98 sprintf(errmsg, "ioctl SETSTRIPE of file %s", filename);
102 if (close(fd) == -1) {
103 sprintf(errmsg, "close of file %s", filename);
107 strncpy(path, filename, MAX_FILENAME_LEN);
108 rc = llapi_file_get_stripe(path, &lum);
110 sprintf(errmsg, "get stripe of file %s", filename);
115 MPI_Barrier(MPI_COMM_WORLD);
120 while (stride < page_size/2) {
122 stride -= stride % 16;
125 stride %= 2 * lum.lmm_stripe_size;
129 MPI_Barrier(MPI_COMM_WORLD);
131 MPI_Bcast(&stride, 1, MPI_LONG, root, MPI_COMM_WORLD);
134 MPI_Barrier(MPI_COMM_WORLD);
136 buf = (char *)malloc(stride);
138 sprintf(errmsg, "malloc of buf with size %ld", stride);
143 fd = open(filename, O_RDWR);
145 sprintf(errmsg, "open of file %s", filename);
150 fill_stride(buf, stride, 0, off);
151 rc = write(fd, buf, stride);
153 sprintf(errmsg, "write of file %s return %d",
159 while (off < size * stride) {
160 fill_stride(buf, stride, 0x8080808080808080ULL, off);
161 rc = write(fd, buf, stride);
163 sprintf(errmsg, "write of file %s return %d",
171 if (close(fd) == -1) {
172 sprintf(errmsg, "close of file %s", filename);
177 MPI_Barrier(MPI_COMM_WORLD);
179 o_buf = (char *)malloc(stride);
181 sprintf(errmsg, "malloc of o_buf with size %ld", stride);
185 fd = open(filename, O_RDWR);
187 sprintf(errmsg, "open of file %s", filename);
192 for (i = 1; i < size; ++i) {
194 rc = lseek(fd, off, SEEK_SET);
196 sprintf(errmsg, "lseek of file %s return %d",
201 rc = read(fd, buf, stride);
204 fill_stride(o_buf, rc, i - 1, off);
205 dump_diff(o_buf, buf, rc, off);
207 sprintf(errmsg, "read of file %s return %d",
212 fill_stride(o_buf, stride, i - 1, off);
213 if (memcmp(o_buf, buf, stride) != 0) {
214 dump_diff(o_buf, buf, stride, off);
216 sprintf(errmsg, "Error: diff data read from %s",
225 fill_stride(buf, stride, i, off);
226 rc = write(fd, buf, stride);
228 sprintf(errmsg, "write of file %s return %d",
234 MPI_Barrier(MPI_COMM_WORLD);
237 if (close(fd) == -1) {
238 sprintf(errmsg, "close of file %s", filename);
242 MPI_Barrier(MPI_COMM_WORLD);
245 fd = open(filename, O_RDONLY);
247 sprintf(errmsg, "open of file %s", filename);
252 for (i = 0; i < size; ++i) {
253 rc = read(fd, buf, stride);
256 fill_stride(o_buf, rc, i, off);
257 dump_diff(o_buf, buf, rc, off);
259 sprintf(errmsg, "read of file %s", filename);
263 fill_stride(o_buf, stride, i, off);
264 if (memcmp(o_buf, buf, stride) != 0) {
266 dump_diff(o_buf, buf, stride, off);
272 sprintf(errmsg, "Error: diff data read from %s", filename);
277 MPI_Barrier(MPI_COMM_WORLD);
278 fprintf(stderr, "passed barrier 5\n");
284 void cascading_rw(long stride, unsigned int seed)
290 rw_file("cascading_rw", stride, seed);
294 remove_file("cascading_rw");
298 void usage(char *proc)
303 printf("Usage: %s [-h] -d <testdir> [-s \"1024\"]\n", proc);
304 printf(" [-n \"13\"] [-e \"12345\"]\n");
305 printf(" [-v] [-V #] [-g]\n");
306 printf("\t-h: prints this help message\n");
307 printf("\t-d: the directory in which the tests will run\n");
308 printf("\t-s: process stride size\n");
309 printf("\t-n: repeat test # times\n");
310 printf("\t-n: random seed, used to re-create previous runs\n");
311 printf("\t-v: increase the verbositly level by 1\n");
312 printf("\t-V: select a specific verbosity level\n");
313 printf("\t-g: debug mode\n");
317 if (i) MPI_Finalize();
321 int main(int argc, char *argv[])
323 int i, iterations = 16, c;
325 unsigned int seed = 0;
327 /* Check for -h parameter before MPI_Init so the binary can be
328 called directly, without, for instance, mpirun */
329 for (i = 1; i < argc; ++i) {
330 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
334 MPI_Init(&argc, &argv);
335 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
336 MPI_Comm_size(MPI_COMM_WORLD, &size);
338 /* Parse command line options */
340 c = getopt(argc, argv, "d:e:ghn:s:vV:");
349 seed = (unsigned int)atoi(optarg);
358 iterations = atoi(optarg);
361 stride = atol(optarg);
367 verbose = atoi(optarg);
373 printf("%s is running with %d process(es) %s\n",
374 argv[0], size, debug ? "in DEBUG mode" : "\b\b");
378 "There should be at least 3 process to run the test!\n");
379 MPI_Abort(MPI_COMM_WORLD, 2);
382 if (testdir == NULL && rank == 0) {
383 fprintf(stderr, "Please specify a test directory! (\"%s -h\" for help)\n",
385 MPI_Abort(MPI_COMM_WORLD, 2);
390 for (i = 0; i < iterations; ++i) {
392 printf("%s: Running test #%s(iter %d)\n",
393 timestamp(), argv[0], i);
395 cascading_rw(stride, seed);
396 MPI_Barrier(MPI_COMM_WORLD);
400 printf("%s: All tests passed!\n", timestamp());