Whamcloud - gitweb
LU-6142 tests: Fix style issues for statmany.c
[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                 "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,
79                                  NULL)) != -1) {
80                 char *e;
81
82                 switch (rc) {
83                 case 'r':
84                         seed = strtoul(optarg, &e, 0);
85                         if (*e) {
86                                 fprintf(stderr, "bad -r option %s\n", optarg);
87                                 usage(prog, stderr);
88                         }
89                         break;
90                 case 'l':
91                 case 's':
92                         mode = rc;
93                         break;
94                 case '0':
95                 case '1':
96                 case '2':
97                 case '3':
98                 case '4':
99                 case '5':
100                 case '6':
101                 case '7':
102                 case '8':
103                 case '9':
104                         if (length == LONG_MAX)
105                                 length = rc - '0';
106                         else
107                                 length = length * 10 + (rc - '0');
108                         break;
109                 case 'h':
110                         usage(prog, stdout);
111                 case '?':
112                         usage(prog, stderr);
113                 }
114         }
115
116         if (optind + 2 + (length == LONG_MAX) != argc) {
117                 fprintf(stderr,
118                         "missing filenamebase, total_files, or iterations\n");
119                 usage(prog, stderr);
120         }
121
122         base = argv[optind];
123         if (strlen(base) > 4080) {
124                 fprintf(stderr, "filenamebase too long\n");
125                 exit(1);
126         }
127
128         if (seed == 0) {
129                 int f = open("/dev/urandom", O_RDONLY);
130
131                 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
132                         seed = time(0);
133                 if (f > 0)
134                         close(f);
135         }
136
137         printf("using seed %u\n", seed);
138         srand(seed);
139
140         count = strtoul(argv[optind + 1], NULL, 0);
141         if (length == LONG_MAX) {
142                 iter = strtoul(argv[optind + 2], NULL, 0);
143                 printf("running for %lu iterations\n", iter);
144         } else {
145                 printf("running for %lu seconds\n", length);
146         }
147
148         start = time(0);
149         last = start;
150
151         t = strrchr(base, '/');
152         if (!t) {
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                         char *name = filename;
187
188                         if (offset >= 0)
189                                 name += offset;
190
191                         rc = llapi_file_lookup(fd, name);
192                         if (rc < 0) {
193                                 printf("llapi_file_lookup for (%s) error: %s\n",
194                                        filename, strerror(errno));
195                                 break;
196                         }
197                 }
198                 if ((i % 10000) == 0) {
199                         printf(" - stat %lu (time %ld ; total %ld ; last %ld)\n",
200                                i, time(0), time(0) - start, time(0) - last);
201                         last = time(0);
202                 }
203         }
204
205         if (mode == 'l')
206                 close(fd);
207
208         printf("total: %lu stats in %ld seconds: %f stats/second\n", i,
209                time(0) - start, ((float)i / (time(0) - start)));
210
211         exit(rc);
212 }