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