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