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