Whamcloud - gitweb
b=17605
[fs/lustre-release.git] / lustre / liblustre / tests / mpi / test_lock_cancel.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  2008 Sun Microsystems, Inc. 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/liblustre/tests/test_lock_cancel.c
37  *
38  * Lustre Light user test program
39  */
40
41 #define _BSD_SOURCE
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <time.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <sys/queue.h>
53
54 #include <sysio.h>
55 #include <mount.h>
56
57 #include <../test_common.h>
58
59 #include <mpi.h>
60
61 /******************************************************************************/
62 /*
63  * MPI_CHECK will display a custom error message as well as an error string
64  * from the MPI_STATUS and then exit the program
65  */
66
67 #define MPI_CHECK(MPI_STATUS, MSG) do {                                  \
68     char resultString[MPI_MAX_ERROR_STRING];                             \
69     int resultLength;                                                    \
70                                                                          \
71     if (MPI_STATUS != MPI_SUCCESS) {                                     \
72         fprintf(stdout, "** error **\n");                                \
73         fprintf(stdout, "ERROR in %s (line %d): %s.\n",                  \
74                 __FILE__, __LINE__, MSG);                                \
75         MPI_Error_string(MPI_STATUS, resultString, &resultLength);       \
76         fprintf(stdout, "MPI %s\n", resultString);                       \
77         fprintf(stdout, "** exiting **\n");                              \
78         MPI_Abort(MPI_COMM_WORLD, 1);                                    \
79     }                                                                    \
80 } while(0)
81
82 int             numTasks     = 0,       /* MPI variables */
83                 rank         = 0,
84                 tasksPerNode = 0;       /* tasks per node */
85
86
87
88
89 static char *test_file_name = "/mnt/lustre/test_lock_cancel";
90
91 extern void __liblustre_setup_(void);
92 extern void __liblustre_cleanup_(void);
93
94 void usage(char *cmd)
95 {
96         printf("Usage: \t%s --target mdsnid:/mdsname/profile\n", cmd);
97         printf("       \t%s --dumpfile dumpfile\n", cmd);
98         exit(-1);
99 }
100
101 int main(int argc, char *argv[])
102 {
103         int opt_index, c;
104         static struct option long_opts[] = {
105                 {"target", 1, 0, 0},
106                 {"dumpfile", 1, 0, 0},
107                 {0, 0, 0, 0}
108         };
109         int fd;
110         long time1, time2;
111         struct stat statbuf;
112
113         if (argc < 3)
114                 usage(argv[0]);
115
116         while ((c = getopt_long(argc, argv, "", long_opts, &opt_index)) != -1) {
117                 switch (c) {
118                 case 0: {
119                         if (!optarg[0])
120                                 usage(argv[0]);
121
122                         if (!strcmp(long_opts[opt_index].name, "target")) {
123                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
124                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
125                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
126                         } else
127                                 usage(argv[0]);
128                         break;
129                 }
130                 default:
131                         usage(argv[0]);
132                 }
133         }
134
135         if (optind != argc)
136                 usage(argv[0]);
137
138         __liblustre_setup_();
139
140         MPI_CHECK(MPI_Init(&argc, &argv), "MPI_Init()");
141         MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &numTasks), "MPI_Comm_size");
142         MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &rank), "MPI_Comm_rank");
143
144         if (numTasks < 2) {
145                 printf("this demo can't run on single node!\n");
146                 goto cleanup;
147         }
148
149         if (rank == 0) {
150                 unlink(test_file_name);
151         }
152
153         MPI_Barrier(MPI_COMM_WORLD);
154         if (rank == 1) {
155                 printf("Node 1: creating file %s ...\n", test_file_name);
156                 fflush(stdout);
157
158                 fd = open(test_file_name, O_CREAT|O_RDWR, 0755);
159                 if (fd < 0) {
160                         printf("Node %d: creat file err: %d", rank, fd);
161                         fflush(stdout);
162                         goto cleanup;
163                 }
164                 close(fd);
165                 printf("Node 1: done creation. perform stat on file %s ...\n", test_file_name);
166                 fflush(stdout);
167
168                 if (stat(test_file_name, &statbuf)) {
169                         printf("Node %d: stat file err: %d", rank, fd);
170                         fflush(stdout);
171                         goto cleanup;
172                 }
173
174                 printf("Node %d: done stat on file\n", rank);
175                 fflush(stdout);
176         } else {
177                 printf("Node %d: waiting node 1 create & stat file\n", rank);
178                 fflush(stdout);
179         }
180
181         MPI_Barrier(MPI_COMM_WORLD);
182         
183         if (rank == 1) {
184                 printf("Node 1: file has been create+stat, abort excution here!!!!!!!\n");
185                 fflush(stdout);
186                 exit(0);
187         }
188         
189         sleep(1);
190         printf("Node %d: synced with Node 1. sleep 5 seconds...\n", rank);
191         fflush(stdout);
192         sleep(5);
193         printf("Node %d: wakeup from sleep. perform unlink()...\n", rank);
194         fflush(stdout);
195
196         time1 = time(NULL);
197         if (unlink(test_file_name)) {
198                 printf("Node %d: error unlink file: %d\n", rank, fd);
199                 fflush(stdout);
200                 goto cleanup;
201         }
202         time2 = time(NULL);
203         printf("Node %d: successfully unlink file, cost %ld seconds.\n",
204                 rank, time2 - time1);
205         fflush(stdout);
206
207 cleanup:
208         __liblustre_cleanup_();
209         printf("Node %d: end sucessfully.\n", rank);
210         return 0;
211 }