Whamcloud - gitweb
b=24410 exit with error if NFSCLIENT is set, but no nfs export found
[fs/lustre-release.git] / lustre / tests / statmany.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <getopt.h>
45 #include <unistd.h>
46 #include <time.h>
47 #include <limits.h>
48 #include <sys/ioctl.h>
49
50 #if 0
51 #include <linux/ldiskfs_fs.h>
52 #endif
53 #include <liblustre.h>
54 #include <lustre_lib.h>
55 #include <obd.h>
56
57 struct option longopts[] = {
58         {"ea", 0, 0, 'e'},
59         {"lookup", 0, 0, 'l'},
60         {"random", 0, 0, 'r'},
61         {"stat", 0, 0, 's'},
62         {NULL, 0, 0, 0},
63 };
64 char *shortopts = "ehlr:s0123456789";
65
66 static int usage(char *prog, FILE *out)
67 {
68         fprintf(out,
69                 "Usage: %s [-r rand_seed] {-s|-e|-l} filenamebase total_files iterations\n"
70                "-r : random seed\n"
71                "-s : regular stat() calls\n"
72                "-e : open then GET_EA ioctl\n"
73                "-l : lookup ioctl only\n", prog);
74         exit(out == stderr);
75 }
76
77 #ifndef LONG_MAX
78 #define LONG_MAX (1 << ((8 * sizeof(long)) - 1))
79 #endif
80
81 int main(int argc, char ** argv)
82 {
83         long i, c, count, iter = LONG_MAX, mode = 0, offset;
84         long int start, length = LONG_MAX, last, rc = 0;
85         char parent[4096], *t;
86         char *prog = argv[0], *base;
87         int seed = 0;
88         int fd = -1;
89
90         while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
91                 char *e;
92                 switch (c) {
93                 case 'r':
94                         seed = strtoul(optarg, &e, 0);
95                         if (*e) {
96                                 fprintf(stderr, "bad -r option %s\n", optarg);
97                                 usage(prog, stderr);
98                         }
99                         break;
100                 case 'e':
101                 case 'l':
102                 case 's':
103                         mode = c;
104                         break;
105                 case '0':
106                 case '1':
107                 case '2':
108                 case '3':
109                 case '4':
110                 case '5':
111                 case '6':
112                 case '7':
113                 case '8':
114                 case '9':
115                         if (length == LONG_MAX)
116                                 length = c - '0';
117                         else
118                                 length = length * 10 + (c - '0');
119                         break;
120                 case 'h':
121                         usage(prog, stdout);
122                 case '?':
123                         usage(prog, stderr);
124                 }
125         }
126
127         if (optind + 2 + (length == LONG_MAX) != argc) {
128                 fprintf(stderr, "missing filenamebase, total_files, or iterations\n");
129                 usage(prog, stderr);
130         }
131
132         base = argv[optind];
133         if (strlen(base) > 4080) {
134                 fprintf(stderr, "filenamebase too long\n");
135                 exit(1);
136         }
137
138         if (seed == 0) {
139                 int f = open("/dev/urandom", O_RDONLY);
140
141                 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
142                         seed = time(0);
143                 if (f > 0)
144                         close(f);
145         }
146
147         printf("using seed %u\n", seed);
148         srand(seed);
149
150         count = strtoul(argv[optind + 1], NULL, 0);
151         if (length == LONG_MAX) {
152                 iter = strtoul(argv[optind + 2], NULL, 0);
153                 printf("running for %lu iterations\n", iter);
154         } else
155                 printf("running for %lu seconds\n", length);
156
157         start = last = time(0);
158
159         t = strrchr(base, '/');
160         if (t == NULL) {
161                 strcpy(parent, ".");
162                 offset = -1;
163         } else {
164                 strncpy(parent, base, t - base);
165                 offset = t - base + 1;
166         }
167
168         if (mode == 'l') {
169                 fd = open(parent, O_RDONLY);
170                 if (fd < 0) {
171                         printf("open(%s) error: %s\n", parent,
172                                strerror(errno));
173                         exit(errno);
174                 }
175         }
176
177         for (i = 0; i < iter && time(0) - start < length; i++) {
178                 char filename[4096];
179                 int tmp;
180
181                 tmp = random() % count;
182                 sprintf(filename, "%s%d", base, tmp);
183
184                 if (mode == 'e') {
185 #if 0
186                         fd = open(filename, O_RDWR|O_LARGEFILE);
187                         if (fd < 0) {
188                                 printf("open(%s) error: %s\n", filename,
189                                        strerror(errno));
190                                 break;
191                         }
192                         rc = ioctl(fd, LDISKFS_IOC_GETEA, NULL);
193                         if (rc < 0) {
194                                 printf("ioctl(%s) error: %s\n", filename,
195                                        strerror(errno));
196                                 break;
197                         }
198                         close(fd);
199                         break;
200 #endif
201                 } else if (mode == 's') {
202                         struct stat buf;
203
204                         rc = stat(filename, &buf);
205                         if (rc < 0) {
206                                 printf("stat(%s) error: %s\n", filename,
207                                        strerror(errno));
208                                 break;
209                         }
210                 } else if (mode == 'l') {
211                         struct obd_ioctl_data data;
212                         char rawbuf[8192];
213                         char *buf = rawbuf;
214                         int max = sizeof(rawbuf);
215
216                         memset(&data, 0, sizeof(data));
217                         data.ioc_version = OBD_IOCTL_VERSION;
218                         data.ioc_len = sizeof(data);
219                         if (offset >= 0)
220                                 data.ioc_inlbuf1 = filename + offset;
221                         else
222                                 data.ioc_inlbuf1 = filename;
223                         data.ioc_inllen1 = strlen(data.ioc_inlbuf1) + 1;
224
225                         if (obd_ioctl_pack(&data, &buf, max)) {
226                                 printf("ioctl_pack failed.\n");
227                                 break;
228                         }
229
230                         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
231                         if (rc < 0) {
232                                 printf("ioctl(%s) error: %s\n", filename,
233                                        strerror(errno));
234                                 break;
235                         }
236                 }
237                 if ((i % 10000) == 0) {
238                         printf(" - stat %lu (time %ld ; total %ld ; last %ld)\n",
239                                i, time(0), time(0) - start, time(0) - last);
240                         last = time(0);
241                 }
242         }
243
244         if (mode == 'l')
245                 close(fd);
246
247         printf("total: %lu stats in %ld seconds: %f stats/second\n", i,
248                time(0) - start, ((float)i / (time(0) - start)));
249
250         exit(rc);
251 }