Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <getopt.h>
40 #include <unistd.h>
41 #include <time.h>
42 #include <limits.h>
43
44 #include <lustre/lustreapi.h>
45
46 struct option longopts[] = {
47         { .name = "lookup", .has_arg = no_argument, .val = 'l' },
48         { .name = "random", .has_arg = no_argument, .val = 'r' },
49         { .name = "stat", .has_arg = no_argument, .val = 's' },
50         { .name = NULL }
51 };
52
53 char *shortopts = "hlr:s0123456789";
54
55 static int usage(char *prog, FILE *out)
56 {
57         fprintf(out,
58                 "random stat of files within a directory\n"
59                 "usage: %s [-r rand_seed] {-s|-l} filenamebase total_files [iterations]\n"
60                 "-r : random seed for repeatable sequence\n"
61                 "-s : regular stat() calls\n"
62                 "-l : llapi_file_lookup() ioctl only\n"
63                 "iterations: default = total_files, or negative for seconds\n",
64                 prog);
65         exit(out == stderr);
66 }
67
68 #ifndef LONG_MAX
69 #define LONG_MAX (1 << ((8 * sizeof(long)) - 1))
70 #endif
71
72 int main(int argc, char **argv)
73 {
74         long i, count, iter = 1, mode = 0, offset;
75         long int start, duration = LONG_MAX, last, now;
76         char parent[4096], *t;
77         char *prog, *base;
78         int seed = 0, rc;
79         int fd = -1;
80
81         prog = strrchr(argv[0], '/');
82         if (prog)
83                 prog++;
84         else
85                 prog = argv[0];
86
87         while ((rc = getopt_long(argc, argv, shortopts, longopts,
88                                  NULL)) != -1) {
89                 char *e;
90
91                 switch (rc) {
92                 case 'r':
93                         seed = strtoul(optarg, &e, 0);
94                         if (*e) {
95                                 fprintf(stderr, "bad -r option %s\n", optarg);
96                                 usage(prog, stderr);
97                         }
98                         break;
99                 case 'l':
100                 case 's':
101                         mode = rc;
102                         break;
103                 /* a negative "count" argument (test duration in seconds,
104                  * e.g. "-300") is treated as a command-line argument.
105                  * Parse all of the digits here back into "duration".
106                  */
107                 case '0':
108                 case '1':
109                 case '2':
110                 case '3':
111                 case '4':
112                 case '5':
113                 case '6':
114                 case '7':
115                 case '8':
116                 case '9':
117                         if (duration == LONG_MAX)
118                                 duration = rc - '0';
119                         else
120                                 duration = duration * 10 + (rc - '0');
121                         break;
122                 case 'h':
123                         usage(prog, stdout);
124                 case '?':
125                         usage(prog, stderr);
126                 }
127         }
128
129         if (argc < optind + 2 || argc > optind + 3) {
130                 fprintf(stderr,
131                         "missing filenamebase, total_files, or iterations\n");
132                 usage(prog, stderr);
133         }
134
135         base = argv[optind];
136         if (strlen(base) > 4080) {
137                 fprintf(stderr, "filenamebase too long\n");
138                 exit(1);
139         }
140
141         if (seed == 0) {
142                 int f = open("/dev/urandom", O_RDONLY);
143
144                 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
145                         seed = time(0);
146                 if (f > 0)
147                         close(f);
148         }
149
150         printf("using seed %u\n", seed);
151         srand(seed);
152
153         count = strtoul(argv[optind + 1], NULL, 0);
154         if (duration == LONG_MAX) {
155                 if (argc > optind + 2)
156                         iter = strtoul(argv[optind + 2], NULL, 0);
157                 else
158                         iter = count;
159                 printf("running for %lu iterations\n", iter);
160         } else {
161                 iter = LONG_MAX;
162                 printf("running for %lu seconds\n", duration);
163         }
164
165         start = time(0);
166         last = start;
167
168         t = strrchr(base, '/');
169         if (!t) {
170                 strcpy(parent, ".");
171                 offset = -1;
172         } else {
173                 strncpy(parent, base, t - base);
174                 offset = t - base + 1;
175         }
176
177         if (mode == 'l') {
178                 fd = open(parent, O_RDONLY);
179                 if (fd < 0) {
180                         printf("open(%s) error: %s\n", parent,
181                                strerror(errno));
182                         exit(errno);
183                 }
184         }
185
186         for (i = 0, now = start; i < iter && now - start < duration; i++) {
187                 char filename[4096] = "";
188                 int tmp;
189
190                 tmp = random() % count;
191                 sprintf(filename, "%s%d", base, tmp);
192
193                 if (mode == 's') {
194                         struct stat buf;
195
196                         rc = stat(filename, &buf);
197                         if (rc < 0) {
198                                 printf("stat(%s) error: %s\n", filename,
199                                        strerror(errno));
200                                 break;
201                         }
202                 } else if (mode == 'l') {
203                         char *name = filename;
204
205                         if (offset >= 0)
206                                 name += offset;
207
208                         rc = llapi_file_lookup(fd, name);
209                         if (rc < 0) {
210                                 printf("llapi_file_lookup for (%s) error: %s\n",
211                                        filename, strerror(errno));
212                                 break;
213                         }
214                 }
215                 now = time(0);
216                 if ((i > 0 && (i % 10000) == 0) || now - last > 10) {
217                         printf(" - stat %lu (time %ld ; total %ld ; last %ld)\n",
218                                i, now, now - start, now - last);
219                         last = now;
220                 }
221         }
222
223         if (mode == 'l')
224                 close(fd);
225
226         printf("total: %lu stats in %ld seconds: %f stats/second\n",
227                i, now - start, ((float)i / (now - start)));
228
229         return rc;
230 }