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