Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / tests / statmany.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <limits.h>
12 #include <sys/ioctl.h>
13
14 #if 0
15 #include <linux/extN_fs.h>
16 #endif
17 #include <linux/lustre_lib.h>
18 #include <linux/obd.h>
19
20 struct option longopts[] = {
21         {"ea", 0, 0, 'e'},
22         {"lookup", 0, 0, 'l'},
23         {"random", 0, 0, 'r'},
24         {"stat", 0, 0, 's'},
25         {NULL, 0, 0, 0},
26 };
27 char *shortopts = "ehlr:s0123456789";
28
29 static int usage(char *prog, FILE *out)
30 {
31         fprintf(out,
32                 "Usage: %s [-r rand_seed] {-s|-e|-l} filenamebase total_files iterations\n"
33                "-r : random seed\n"
34                "-s : regular stat() calls\n"
35                "-e : open then GET_EA ioctl\n"
36                "-l : lookup ioctl only\n", prog);
37         exit(out == stderr);
38 }
39
40 #ifndef LONG_MAX
41 #define LONG_MAX (1 << ((8 * sizeof(long)) - 1))
42 #endif
43
44 int main(int argc, char ** argv)
45 {
46         long i, count, iter = LONG_MAX, mode, offset;
47         long int start, length = LONG_MAX, last, rc = 0;
48         char parent[4096], *t;
49         char c, *prog = argv[0], *base;
50         int seed = 0;
51         int fd = -1;
52
53         while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
54                 char *e;
55                 switch (c) {
56                 case 'r':
57                         seed = strtoul(optarg, &e, 0);
58                         if (*e) {
59                                 fprintf(stderr, "bad -r option %s\n", optarg);
60                                 usage(prog, stderr);
61                         }
62                         break;
63                 case 'e':
64                 case 'l':
65                 case 's':
66                         mode = c;
67                         break;
68                 case '0':
69                 case '1':
70                 case '2':
71                 case '3':
72                 case '4':
73                 case '5':
74                 case '6':
75                 case '7':
76                 case '8':
77                 case '9':
78                         if (length == LONG_MAX)
79                                 length = c - '0';
80                         else
81                                 length = length * 10 + (c - '0');
82                         break;
83                 case 'h':
84                         usage(prog, stdout);
85                 case '?':
86                         usage(prog, stderr);
87                 }
88         }
89
90         if (optind + 2 + (length == LONG_MAX) != argc) {
91                 fprintf(stderr, "missing filenamebase, total_files, or iterations\n");
92                 usage(prog, stderr);
93         }
94
95         base = argv[optind];
96         if (strlen(base) > 4080) {
97                 fprintf(stderr, "filenamebase too long\n");
98                 exit(1);
99         }
100
101         if (seed == 0) {
102                 int f = open("/dev/urandom", O_RDONLY);
103
104                 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
105                         seed = time(0);
106                 if (f > 0)
107                         close(f);
108         }
109
110         printf("using seed %u\n", seed);
111         srand(seed);
112
113         count = strtoul(argv[optind + 1], NULL, 0);
114         if (length == LONG_MAX) {
115                 iter = strtoul(argv[optind + 2], NULL, 0);
116                 printf("running for %lu iterations\n", iter);
117         } else
118                 printf("running for %lu seconds\n", length);
119
120         start = last = time(0);
121
122         t = strrchr(base, '/');
123         if (t == NULL) {
124                 strcpy(parent, ".");
125                 offset = -1;
126         } else {
127                 strncpy(parent, base, t - base);
128                 offset = t - base + 1;
129         }
130
131         if (mode == 'l') {
132                 fd = open(parent, O_RDONLY);
133                 if (fd < 0) {
134                         printf("open(%s) error: %s\n", parent,
135                                strerror(errno));
136                         exit(errno);
137                 }
138         }
139
140         for (i = 0; i < iter && time(0) - start < length; i++) {
141                 char filename[4096];
142                 int tmp;
143
144                 tmp = random() % count;
145                 sprintf(filename, "%s%d", base, tmp);
146
147                 if (mode == 'e') {
148 #if 0
149                         fd = open(filename, O_RDWR|O_LARGEFILE);
150                         if (fd < 0) {
151                                 printf("open(%s) error: %s\n", filename,
152                                        strerror(errno));
153                                 break;
154                         }
155                         rc = ioctl(fd, EXTN_IOC_GETEA, NULL);
156                         if (rc < 0) {
157                                 printf("ioctl(%s) error: %s\n", filename,
158                                        strerror(errno));
159                                 break;
160                         }
161                         close(fd);
162                         break;
163 #endif
164                 } else if (mode == 's') {
165                         struct stat buf;
166
167                         rc = stat(filename, &buf);
168                         if (rc < 0) {
169                                 printf("stat(%s) error: %s\n", filename,
170                                        strerror(errno));
171                                 break;
172                         }
173                 } else if (mode == 'l') {
174                         struct obd_ioctl_data data;
175                         char rawbuf[8192];
176                         char *buf = rawbuf;
177                         int max = sizeof(rawbuf);
178
179                         memset(&data, 0, sizeof(data));
180                         data.ioc_version = OBD_IOCTL_VERSION;
181                         data.ioc_len = sizeof(data);
182                         if (offset >= 0)
183                                 data.ioc_inlbuf1 = filename + offset;
184                         else
185                                 data.ioc_inlbuf1 = filename;
186                         data.ioc_inllen1 = strlen(data.ioc_inlbuf1) + 1;
187
188                         if (obd_ioctl_pack(&data, &buf, max)) {
189                                 printf("ioctl_pack failed.\n");
190                                 break;
191                         }
192
193                         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
194                         if (rc < 0) {
195                                 printf("ioctl(%s) error: %s\n", filename,
196                                        strerror(errno));
197                                 break;
198                         }
199                 }
200                 if ((i % 10000) == 0) {
201                         printf(" - stat %lu (time %ld ; total %ld ; last %ld)\n",
202                                i, time(0), time(0) - start, time(0) - last);
203                         last = time(0);
204                 }
205         }
206
207         if (mode == 'l')
208                 close(fd);
209
210         printf("total: %lu stats in %ld seconds: %f stats/second\n", i,
211                time(0) - start, ((float)i / (time(0) - start)));
212
213         exit(rc);
214 }