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