Whamcloud - gitweb
b=15967
[fs/lustre-release.git] / lustre / tests / createmany-mpi.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <time.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15
16 #include "mpi.h"
17
18 void usage(char *prog)
19 {
20         printf("usage: %s {-o|-m|-l<tgt>} filenamefmt count\n", prog);
21         printf("       %s {-o|-m|-l<tgt>} filenamefmt -seconds\n", prog);
22         printf("       %s {-o|-m|-l<tgt>} filenamefmt start count\n", prog);
23 }
24
25 /* Print process rank, loop count, message, and exit (i.e. a fatal error) */
26 int rprintf(int rank, int loop, const char *fmt, ...)
27 {
28         va_list       ap;
29
30         printf("rank %d, loop %d: ", rank, loop);
31
32         va_start(ap, fmt);
33
34         printf(fmt, ap);
35
36         MPI_Finalize();
37         exit(1);
38 }
39
40 int main(int argc, char ** argv)
41 {
42         int i, rc = 0, do_open = 0, do_link = 0, rank;
43         char format[4096], *fmt, *tgt;
44         char filename[4096];
45         long start, last, end;
46         long begin = 0, count;
47
48         rc = MPI_Init(&argc, &argv);
49         if (rc != MPI_SUCCESS)
50                 rprintf(-1, -1, "MPI_Init failed: %d\n", rc);
51
52         if (argc < 4 || argc > 5) {
53                 usage(argv[0]);
54                 return 1;
55         }
56
57         if (strcmp(argv[1], "-o") == 0) {
58                 tgt = NULL;
59                 do_open = 1;
60         } else if (strncmp(argv[1], "-l", 2) == 0 && argv[1][2]) {
61                 tgt = argv[1] + 2;
62                 do_link = 1;
63         } else if (strcmp(argv[1], "-m") != 0) {
64                 usage(argv[0]);
65                 return 1;
66         }
67
68         if (strlen(argv[2]) > 4080) {
69                 printf("name too long\n");
70                 return 1;
71         }
72
73         rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
74         if (rc != MPI_SUCCESS)
75                 rprintf(-1, -1, "MPI_Comm_rank failed: %d\n", rc);
76
77         rc = MPI_Barrier(MPI_COMM_WORLD);
78         if (rc != MPI_SUCCESS)
79                 rprintf(rank, -1, "prep MPI_Barrier failed: %d\n", rc);
80
81         start = last = time(0);
82
83         if (argc == 4) {
84                 end = strtol(argv[3], NULL, 0);
85         } else {
86                 begin = strtol(argv[3], NULL, 0);
87                 end = strtol(argv[4], NULL, 0);
88         }
89         if (end > 0) {
90                 count = end;
91                 end = -1UL >> 1;
92         } else {
93                 end = start - end;
94                 count = -1UL >> 1;
95         }
96
97         if (strchr(argv[2], '%'))
98                 fmt = argv[2];
99         else {
100                 sprintf(format, "%s%%d", argv[2]);
101                 fmt = format;
102         }
103         printf("starting at %s", ctime(&start));
104         for (i = 0; i < count && time(0) < end; i++, begin++) {
105                 sprintf(filename, fmt, begin);
106                 if (do_open) {
107                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
108                         if (fd < 0) {
109                                 printf("open(%s) error: %s\n", filename,
110                                        strerror(errno));
111                                 rc = errno;
112                                 break;
113                         }
114                         close(fd);
115                 } else if (do_link) {
116                         rc = link(tgt, filename);
117                         if (rc) {
118                                 printf("link(%s, %s) error: %s\n",
119                                        tgt, filename, strerror(errno));
120                                 rc = errno;
121                                 break;
122                         }
123                 } else {
124                         rc = mknod(filename, S_IFREG| 0444, 0);
125                         if (rc) {
126                                 printf("mknod(%s) error: %s\n",
127                                        filename, strerror(errno));
128                                 rc = errno;
129                                 break;
130                         }
131                 }
132                 if ((i % 10000) == 0) {
133                         printf(" - created %d (time %ld total %ld last %ld)\n",
134                                i, time(0), time(0) - start, time(0) - last);
135                         last = time(0);
136                 }
137         }
138         printf("total: %d creates in %ld seconds: %f creates/second\n", i,
139                time(0) - start, ((float)i / (time(0) - start)));
140         start = time(0);
141         printf("finish at %s", ctime(&start));
142
143         return rc;
144 }