Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / tests / createmany.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
11 void usage(char *prog)
12 {
13         printf("usage: %s {-o|-m} filenamefmt count\n", prog);
14         printf("       %s {-o|-m} filenamefmt -seconds\n", prog);
15         printf("       %s {-o|-m} filenamefmt start count\n", prog);
16 }
17
18 int main(int argc, char ** argv)
19 {
20         int i, rc = 0, do_open;
21         char format[4096], *fmt;
22         char filename[4096];
23         long start, last, end;
24         long begin = 0, count;
25
26         if (argc < 4 || argc > 5) {
27                 usage(argv[0]);
28                 return 1;
29         }
30
31         if (strcmp(argv[1], "-o") == 0) {
32                 do_open = 1;
33         } else if (strcmp(argv[1], "-m") == 0) {
34                 do_open = 0;
35         } else {
36                 usage(argv[0]);
37                 return 1;
38         }
39
40         if (strlen(argv[2]) > 4080) {
41                 printf("name too long\n");
42                 return 1;
43         }
44
45         start = last = time(0);
46
47         if (argc == 4) {
48                 end = strtol(argv[3], NULL, 0);
49                 if (end > 0) {
50                         count = end;
51                         end = -1UL >> 1;
52                 } else {
53                         end = start - end;
54                         count = -1UL >> 1;
55                 }
56         } else {
57                 end = -1UL >> 1;
58                 begin = strtol(argv[3], NULL, 0);
59                 count = strtol(argv[4], NULL, 0);
60         }
61
62         if (strchr(argv[2], '%'))
63                 fmt = argv[2];
64         else {
65                 sprintf(format, "%s%%d", argv[2]);
66                 fmt = format;
67         }
68         for (i = 0; i < count && time(0) < end; i++, begin++) {
69                 sprintf(filename, fmt, begin);
70                 if (do_open) {
71                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
72                         if (fd < 0) {
73                                 printf("open(%s) error: %s\n", filename,
74                                        strerror(errno));
75                                 rc = errno;
76                                 break;
77                         }
78                         close(fd);
79                 } else {
80                         rc = mknod(filename, S_IFREG| 0444, 0);
81                         if (rc) {
82                                 printf("mknod(%s) error: %s\n",
83                                        filename, strerror(errno));
84                                 rc = errno;
85                                 break;
86                         }
87                 }
88                 if ((i % 10000) == 0) {
89                         printf(" - created %d (time %ld ; total %ld ; last %ld)\n",
90                                i, time(0), time(0) - start, time(0) - last);
91                         last = time(0);
92                 }
93         }
94         printf("total: %d creates in %ld seconds: %f creates/second\n", i,
95                time(0) - start, ((float)i / (time(0) - start)));
96
97         return rc;
98 }