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