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