Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41
42 #include "mpi.h"
43
44 void usage(char *prog)
45 {
46         printf("usage: %s {-o|-m|-l<tgt>} filenamefmt count\n", prog);
47         printf("       %s {-o|-m|-l<tgt>} filenamefmt -seconds\n", prog);
48         printf("       %s {-o|-m|-l<tgt>} filenamefmt start count\n", prog);
49 }
50
51 /* Print process rank, loop count, message, and exit (i.e. a fatal error) */
52 int rprintf(int rank, int loop, const char *fmt, ...)
53 {
54         va_list       ap;
55
56         printf("rank %d, loop %d: ", rank, loop);
57
58         va_start(ap, fmt);
59
60         printf(fmt, ap);
61
62         MPI_Finalize();
63         exit(1);
64 }
65
66 int main(int argc, char ** argv)
67 {
68         int i, rc = 0, do_open = 0, do_link = 0, rank;
69         char format[4096], *fmt, *tgt = NULL;
70         char filename[4096];
71         long start, last, end;
72         long begin = 0, count;
73
74         rc = MPI_Init(&argc, &argv);
75         if (rc != MPI_SUCCESS)
76                 rprintf(-1, -1, "MPI_Init failed: %d\n", rc);
77
78         if (argc < 4 || argc > 5) {
79                 usage(argv[0]);
80                 return 1;
81         }
82
83         if (strcmp(argv[1], "-o") == 0) {
84                 do_open = 1;
85                 tgt = NULL;
86         } else if (strncmp(argv[1], "-l", 2) == 0 && argv[1][2]) {
87                 tgt = argv[1] + 2;
88                 do_link = 1;
89         } else if (strcmp(argv[1], "-m") != 0) {
90                 usage(argv[0]);
91                 return 1;
92         }
93
94         if (strlen(argv[2]) > 4080) {
95                 printf("name too long\n");
96                 return 1;
97         }
98
99         rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
100         if (rc != MPI_SUCCESS)
101                 rprintf(-1, -1, "MPI_Comm_rank failed: %d\n", rc);
102
103         rc = MPI_Barrier(MPI_COMM_WORLD);
104         if (rc != MPI_SUCCESS)
105                 rprintf(rank, -1, "prep MPI_Barrier failed: %d\n", rc);
106
107         start = last = time(0);
108
109         if (argc == 4) {
110                 end = strtol(argv[3], NULL, 0);
111         } else {
112                 begin = strtol(argv[3], NULL, 0);
113                 end = strtol(argv[4], NULL, 0);
114         }
115         if (end > 0) {
116                 count = end;
117                 end = -1UL >> 1;
118         } else {
119                 end = start - end;
120                 count = -1UL >> 1;
121         }
122
123         if (strchr(argv[2], '%'))
124                 fmt = argv[2];
125         else {
126                 sprintf(format, "%s%%d", argv[2]);
127                 fmt = format;
128         }
129         printf("starting at %s", ctime(&start));
130         for (i = 0; i < count && time(0) < end; i++, begin++) {
131                 sprintf(filename, fmt, begin);
132                 if (do_open) {
133                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
134                         if (fd < 0) {
135                                 printf("open(%s) error: %s\n", filename,
136                                        strerror(errno));
137                                 rc = errno;
138                                 break;
139                         }
140                         close(fd);
141                 } else if (do_link) {
142                         rc = link(tgt, filename);
143                         if (rc) {
144                                 printf("link(%s, %s) error: %s\n",
145                                        tgt, filename, strerror(errno));
146                                 rc = errno;
147                                 break;
148                         }
149                 } else {
150                         rc = mknod(filename, S_IFREG| 0444, 0);
151                         if (rc) {
152                                 printf("mknod(%s) error: %s\n",
153                                        filename, strerror(errno));
154                                 rc = errno;
155                                 break;
156                         }
157                 }
158                 if ((i % 10000) == 0) {
159                         printf(" - created %d (time %ld total %ld last %ld)\n",
160                                i, time(0), time(0) - start, time(0) - last);
161                         last = time(0);
162                 }
163         }
164         printf("total: %d creates in %ld seconds: %f creates/second\n", i,
165                time(0) - start, ((float)i / (time(0) - start)));
166         start = time(0);
167         printf("finish at %s", ctime(&start));
168
169         return rc;
170 }