Whamcloud - gitweb
Oops.
[fs/lustre-release.git] / lustre / liblustre / test_lock_cancel.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light user test program
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define _BSD_SOURCE
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <sys/queue.h>
36
37 #include <sysio.h>
38 #include <mount.h>
39
40 #include <test_common.h>
41
42 #include <mpi.h>
43
44 /******************************************************************************/
45 /*
46  * MPI_CHECK will display a custom error message as well as an error string
47  * from the MPI_STATUS and then exit the program
48  */
49
50 #define MPI_CHECK(MPI_STATUS, MSG) do {                                  \
51     char resultString[MPI_MAX_ERROR_STRING];                             \
52     int resultLength;                                                    \
53                                                                          \
54     if (MPI_STATUS != MPI_SUCCESS) {                                     \
55         fprintf(stdout, "** error **\n");                                \
56         fprintf(stdout, "ERROR in %s (line %d): %s.\n",                  \
57                 __FILE__, __LINE__, MSG);                                \
58         MPI_Error_string(MPI_STATUS, resultString, &resultLength);       \
59         fprintf(stdout, "MPI %s\n", resultString);                       \
60         fprintf(stdout, "** exiting **\n");                              \
61         MPI_Abort(MPI_COMM_WORLD, 1);                                    \
62     }                                                                    \
63 } while(0)
64
65 int             numTasks     = 0,       /* MPI variables */
66                 rank         = 0,
67                 tasksPerNode = 0;       /* tasks per node */
68
69
70
71
72 static char *test_file_name = "/mnt/lustre/test_lock_cancel";
73
74 extern void __liblustre_setup_(void);
75 extern void __liblustre_cleanup_(void);
76
77 void usage(char *cmd)
78 {
79         printf("Usage: \t%s --target mdsnid:/mdsname/profile\n", cmd);
80         printf("       \t%s --dumpfile dumpfile\n", cmd);
81         exit(-1);
82 }
83
84 int main(int argc, char *argv[])
85 {
86         int opt_index, c;
87         static struct option long_opts[] = {
88                 {"target", 1, 0, 0},
89                 {"dumpfile", 1, 0, 0},
90                 {0, 0, 0, 0}
91         };
92         int fd;
93         long time1, time2;
94         struct stat statbuf;
95
96         if (argc < 3)
97                 usage(argv[0]);
98
99         while ((c = getopt_long(argc, argv, "", long_opts, &opt_index)) != -1) {
100                 switch (c) {
101                 case 0: {
102                         if (!optarg[0])
103                                 usage(argv[0]);
104
105                         if (!strcmp(long_opts[opt_index].name, "target")) {
106                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
107                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
108                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
109                         } else
110                                 usage(argv[0]);
111                         break;
112                 }
113                 default:
114                         usage(argv[0]);
115                 }
116         }
117
118         if (optind != argc)
119                 usage(argv[0]);
120
121         __liblustre_setup_();
122
123         MPI_CHECK(MPI_Init(&argc, &argv), "MPI_Init()");
124         MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &numTasks), "MPI_Comm_size");
125         MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &rank), "MPI_Comm_rank");
126
127         if (numTasks < 2) {
128                 printf("this demo can't run on single node!\n");
129                 goto cleanup;
130         }
131
132         if (rank == 0) {
133                 unlink(test_file_name);
134         }
135
136         MPI_Barrier(MPI_COMM_WORLD);
137         if (rank == 1) {
138                 printf("Node 1: creating file %s ...\n", test_file_name);
139                 fflush(stdout);
140
141                 fd = open(test_file_name, O_CREAT|O_RDWR, 0755);
142                 if (fd < 0) {
143                         printf("Node %d: creat file err: %d", rank, fd);
144                         fflush(stdout);
145                         goto cleanup;
146                 }
147                 close(fd);
148                 printf("Node 1: done creation. perform stat on file %s ...\n", test_file_name);
149                 fflush(stdout);
150
151                 if (stat(test_file_name, &statbuf)) {
152                         printf("Node %d: stat file err: %d", rank, fd);
153                         fflush(stdout);
154                         goto cleanup;
155                 }
156
157                 printf("Node %d: done stat on file\n", rank);
158                 fflush(stdout);
159         } else {
160                 printf("Node %d: waiting node 1 create & stat file\n", rank);
161                 fflush(stdout);
162         }
163
164         MPI_Barrier(MPI_COMM_WORLD);
165         
166         if (rank == 1) {
167                 printf("Node 1: file has been create+stat, abort excution here!!!!!!!\n");
168                 fflush(stdout);
169                 exit(0);
170         }
171         
172         sleep(1);
173         printf("Node %d: synced with Node 1. sleep 5 seconds...\n", rank);
174         fflush(stdout);
175         sleep(5);
176         printf("Node %d: wakeup from sleep. perform unlink()...\n", rank);
177         fflush(stdout);
178
179         time1 = time(NULL);
180         if (unlink(test_file_name)) {
181                 printf("Node %d: error unlink file: %d\n", rank, fd);
182                 fflush(stdout);
183                 goto cleanup;
184         }
185         time2 = time(NULL);
186         printf("Node %d: successfully unlink file, cost %ld seconds.\n",
187                 rank, time2 - time1);
188         fflush(stdout);
189
190 cleanup:
191         __liblustre_cleanup_();
192         printf("Node %d: end sucessfully.\n", rank);
193         return 0;
194 }