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