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