4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #include <sys/types.h>
44 #include <lustre/lustreapi.h>
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' },
53 char *shortopts = "hlr:s0123456789";
55 static int usage(char *prog, FILE *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",
69 #define LONG_MAX (1 << ((8 * sizeof(long)) - 1))
72 int main(int argc, char **argv)
74 long i, count, iter = 1, mode = 0, offset;
75 long int start, duration = LONG_MAX, last, now;
76 char parent[4096], *t;
81 prog = strrchr(argv[0], '/');
87 while ((rc = getopt_long(argc, argv, shortopts, longopts,
93 seed = strtoul(optarg, &e, 0);
95 fprintf(stderr, "bad -r option %s\n", optarg);
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".
117 if (duration == LONG_MAX)
120 duration = duration * 10 + (rc - '0');
129 if (argc < optind + 2 || argc > optind + 3) {
131 "missing filenamebase, total_files, or iterations\n");
136 if (strlen(base) > 4080) {
137 fprintf(stderr, "filenamebase too long\n");
142 int f = open("/dev/urandom", O_RDONLY);
144 if (f < 0 || read(f, &seed, sizeof(seed)) < sizeof(seed))
150 printf("using seed %u\n", seed);
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);
159 printf("running for %lu iterations\n", iter);
162 printf("running for %lu seconds\n", duration);
168 t = strrchr(base, '/');
173 strncpy(parent, base, t - base);
174 offset = t - base + 1;
178 fd = open(parent, O_RDONLY);
180 printf("open(%s) error: %s\n", parent,
186 for (i = 0, now = start; i < iter && now - start < duration; i++) {
187 char filename[4096] = "";
190 tmp = random() % count;
191 sprintf(filename, "%s%d", base, tmp);
196 rc = stat(filename, &buf);
198 printf("stat(%s) error: %s\n", filename,
202 } else if (mode == 'l') {
203 char *name = filename;
208 rc = llapi_file_lookup(fd, name);
210 printf("llapi_file_lookup for (%s) error: %s\n",
211 filename, strerror(errno));
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);
226 printf("total: %lu stats in %ld seconds: %f stats/second\n",
227 i, now - start, ((float)i / (now - start)));