Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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, 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 (optind + 2 + (length == LONG_MAX) != argc) {
92                 fprintf(stderr, "missing filenamebase, total_files, or iterations\n");
93                 usage(prog, stderr);
94         }
95
96         base = argv[optind];
97         if (strlen(base) > 4080) {
98                 fprintf(stderr, "filenamebase too long\n");
99                 exit(1);
100         }
101
102         if (seed == 0) {
103                 int f = open("/dev/urandom", O_RDONLY);
104
105                 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
106                         seed = time(0);
107                 if (f > 0)
108                         close(f);
109         }
110
111         printf("using seed %u\n", seed);
112         srand(seed);
113
114         count = strtoul(argv[optind + 1], NULL, 0);
115         if (length == LONG_MAX) {
116                 iter = strtoul(argv[optind + 2], NULL, 0);
117                 printf("running for %lu iterations\n", iter);
118         } else
119                 printf("running for %lu seconds\n", length);
120
121         start = last = time(0);
122
123         t = strrchr(base, '/');
124         if (t == NULL) {
125                 strcpy(parent, ".");
126                 offset = -1;
127         } else {
128                 strncpy(parent, base, t - base);
129                 offset = t - base + 1;
130         }
131
132         if (mode == 'l') {
133                 fd = open(parent, O_RDONLY);
134                 if (fd < 0) {
135                         printf("open(%s) error: %s\n", parent,
136                                strerror(errno));
137                         exit(errno);
138                 }
139         }
140
141         for (i = 0; i < iter && time(0) - start < length; i++) {
142                 char filename[4096];
143                 int tmp;
144
145                 tmp = random() % count;
146                 sprintf(filename, "%s%d", base, tmp);
147
148                 if (mode == 'e') {
149 #if 0
150                         fd = open(filename, O_RDWR|O_LARGEFILE);
151                         if (fd < 0) {
152                                 printf("open(%s) error: %s\n", filename,
153                                        strerror(errno));
154                                 break;
155                         }
156                         rc = ioctl(fd, EXTN_IOC_GETEA, NULL);
157                         if (rc < 0) {
158                                 printf("ioctl(%s) error: %s\n", filename,
159                                        strerror(errno));
160                                 break;
161                         }
162                         close(fd);
163                         break;
164 #endif
165                 } else if (mode == 's') {
166                         struct stat buf;
167
168                         rc = stat(filename, &buf);
169                         if (rc < 0) {
170                                 printf("stat(%s) error: %s\n", filename,
171                                        strerror(errno));
172                                 break;
173                         }
174                 } else if (mode == 'l') {
175                         struct obd_ioctl_data data;
176                         char rawbuf[8192];
177                         char *buf = rawbuf;
178                         int max = sizeof(rawbuf);
179
180                         memset(&data, 0, sizeof(data));
181                         data.ioc_version = OBD_IOCTL_VERSION;
182                         data.ioc_len = sizeof(data);
183                         if (offset >= 0)
184                                 data.ioc_inlbuf1 = filename + offset;
185                         else
186                                 data.ioc_inlbuf1 = filename;
187                         data.ioc_inllen1 = strlen(data.ioc_inlbuf1) + 1;
188
189                         if (obd_ioctl_pack(&data, &buf, max)) {
190                                 printf("ioctl_pack failed.\n");
191                                 break;
192                         }
193
194                         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
195                         if (rc < 0) {
196                                 printf("ioctl(%s) error: %s\n", filename,
197                                        strerror(errno));
198                                 break;
199                         }
200                 }
201                 if ((i % 10000) == 0) {
202                         printf(" - stat %lu (time %ld ; total %ld ; last %ld)\n",
203                                i, time(0), time(0) - start, time(0) - last);
204                         last = time(0);
205                 }
206         }
207
208         if (mode == 'l')
209                 close(fd);
210
211         printf("total: %lu stats in %ld seconds: %f stats/second\n", i,
212                time(0) - start, ((float)i / (time(0) - start)));
213
214         exit(rc);
215 }