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