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