Whamcloud - gitweb
LU-1538 tests: keep /sbin/mount.lustre until cleanup
[fs/lustre-release.git] / lustre / tests / mpi / createmany-mpi.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
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <stdarg.h>
45
46 #include "mpi.h"
47
48 void usage(char *prog)
49 {
50         printf("usage: %s {-o|-m|-l<tgt>} filenamefmt count\n", prog);
51         printf("       %s {-o|-m|-l<tgt>} filenamefmt -seconds\n", prog);
52         printf("       %s {-o|-m|-l<tgt>} filenamefmt start count\n", prog);
53 }
54
55 /* Print process rank, loop count, message, and exit (i.e. a fatal error) */
56 int rprintf(int rank, int loop, const char *fmt, ...)
57 {
58         va_list       ap;
59
60         printf("rank %d, loop %d: ", rank, loop);
61
62         va_start(ap, fmt);
63
64         printf(fmt, ap);
65
66         MPI_Finalize();
67         exit(1);
68 }
69
70 int main(int argc, char ** argv)
71 {
72         int i, rc = 0, do_open = 0, do_link = 0, rank;
73         char format[4096], *fmt, *tgt = NULL;
74         char filename[4096];
75         long start, last, end;
76         long begin = 0, count;
77
78         rc = MPI_Init(&argc, &argv);
79         if (rc != MPI_SUCCESS)
80                 rprintf(-1, -1, "MPI_Init failed: %d\n", rc);
81
82         if (argc < 4 || argc > 5) {
83                 usage(argv[0]);
84                 return 1;
85         }
86
87         if (strcmp(argv[1], "-o") == 0) {
88                 do_open = 1;
89                 tgt = NULL;
90         } else if (strncmp(argv[1], "-l", 2) == 0 && argv[1][2]) {
91                 tgt = argv[1] + 2;
92                 do_link = 1;
93         } else if (strcmp(argv[1], "-m") != 0) {
94                 usage(argv[0]);
95                 return 1;
96         }
97
98         if (strlen(argv[2]) > 4080) {
99                 printf("name too long\n");
100                 return 1;
101         }
102
103         rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
104         if (rc != MPI_SUCCESS)
105                 rprintf(-1, -1, "MPI_Comm_rank failed: %d\n", rc);
106
107         rc = MPI_Barrier(MPI_COMM_WORLD);
108         if (rc != MPI_SUCCESS)
109                 rprintf(rank, -1, "prep MPI_Barrier failed: %d\n", rc);
110
111         start = last = time(0);
112
113         if (argc == 4) {
114                 end = strtol(argv[3], NULL, 0);
115         } else {
116                 begin = strtol(argv[3], NULL, 0);
117                 end = strtol(argv[4], NULL, 0);
118         }
119         if (end > 0) {
120                 count = end;
121                 end = -1UL >> 1;
122         } else {
123                 end = start - end;
124                 count = -1UL >> 1;
125         }
126
127         if (strchr(argv[2], '%'))
128                 fmt = argv[2];
129         else {
130                 sprintf(format, "%s%%d", argv[2]);
131                 fmt = format;
132         }
133         printf("starting at %s", ctime(&start));
134         for (i = 0; i < count && time(0) < end; i++, begin++) {
135                 sprintf(filename, fmt, begin);
136                 if (do_open) {
137                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
138                         if (fd < 0) {
139                                 printf("open(%s) error: %s\n", filename,
140                                        strerror(errno));
141                                 rc = errno;
142                                 break;
143                         }
144                         close(fd);
145                 } else if (do_link) {
146                         rc = link(tgt, filename);
147                         if (rc) {
148                                 printf("link(%s, %s) error: %s\n",
149                                        tgt, filename, strerror(errno));
150                                 rc = errno;
151                                 break;
152                         }
153                 } else {
154                         rc = mknod(filename, S_IFREG| 0444, 0);
155                         if (rc) {
156                                 printf("mknod(%s) error: %s\n",
157                                        filename, strerror(errno));
158                                 rc = errno;
159                                 break;
160                         }
161                 }
162                 if ((i % 10000) == 0) {
163                         printf(" - created %d (time %ld total %ld last %ld)\n",
164                                i, time(0), time(0) - start, time(0) - last);
165                         last = time(0);
166                 }
167         }
168         printf("total: %d creates in %ld seconds: %f creates/second\n", i,
169                time(0) - start, ((float)i / (time(0) - start)));
170         start = time(0);
171         printf("finish at %s", ctime(&start));
172
173         return rc;
174 }