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