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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
39 #include <sys/types.h>
45 static void usage(const char *prog)
47 printf("usage: %s {-o [-k]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] "
48 "[-t seconds] filenamefmt [[start] count]\n", prog);
49 printf("\t-l\tlink files to existing <tgt> file\n"
50 "\t-m\tmknod regular files (don't create OST objects)\n"
51 "\t-o\topen+create files with path and printf format\n"
52 "\t-k\t keep files open until all files are opened\n"
53 "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
54 printf("\t-d\tuse directories instead of regular files\n"
55 "\t-t\tstop creating files after <seconds> have elapsed\n");
60 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
62 static char filename[4096];
65 bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
66 snprintf(filename, 4095, "%s%ld", fmt, n);
68 printf("file name too long\n");
77 gettimeofday(&tv, NULL);
78 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
81 int main(int argc, char ** argv)
83 bool do_open = false, do_keep = false, do_link = false;
84 bool do_unlink = false, do_mknod = false, do_mkdir = false;
85 bool do_rmdir = false;
86 char *filename, *progname;
87 char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
89 double start, last_t, end;
90 long begin = 0, count = ~0UL >> 1;
91 int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
92 long i, total, last_i = 0;
93 int c, last_fd = -1, stderr_fd;
96 /* Handle the deprecated positional last argument "-seconds" */
97 if (argc > 1 && argv[argc - 1][0] == '-' &&
98 (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
99 fprintf(stderr, "warning: '-runtime' deprecated, "
100 "use '-t runtime' instead\n");
103 /* Not '-number', let regular argument parsing handle it. */
107 if ((endp = strrchr(argv[0], '/')) != NULL)
112 while ((c = getopt(argc, argv, "dl:kmor::t:u::")) != -1) {
131 end = strtol(optarg, &endp, 0);
132 if (end <= 0.0 || *endp != '\0')
141 fprintf(stderr, "Unknown option '%c'\n", optopt);
146 if (do_open + do_mkdir + do_link + do_mknod > 1 ||
147 do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
148 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
151 if (do_mkdir && do_unlink)
154 if (!do_open && do_keep) {
155 fprintf(stderr, "error: can only use -k with -o\n");
159 switch (argc - optind) {
161 begin = strtol(argv[argc - 2], NULL, 0);
163 count = strtol(argv[argc - 1], NULL, 0);
171 has_fmt_spec = strchr(fmt, '%') != NULL;
172 if (fmt_unlink != NULL)
173 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
175 for (i = 0, start = last_t = now(), end += start;
176 i < count && now() < end; i++, begin++) {
179 filename = get_file_name(fmt, begin, has_fmt_spec);
181 int fd = open(filename, O_CREAT|O_RDWR, 0644);
183 printf("open(%s) error: %s\n", filename,
190 else if (fd > last_fd)
192 } else if (do_link) {
193 rc = link(tgt, filename);
195 printf("link(%s, %s) error: %s\n",
196 tgt, filename, strerror(errno));
200 } else if (do_mkdir) {
201 rc = mkdir(filename, 0755);
203 printf("mkdir(%s) error: %s\n",
204 filename, strerror(errno));
208 } else if (do_mknod) {
209 rc = mknod(filename, S_IFREG | 0444, 0);
211 printf("mknod(%s) error: %s\n",
212 filename, strerror(errno));
218 if (fmt_unlink != NULL)
219 filename = get_file_name(fmt_unlink, begin,
220 unlink_has_fmt_spec);
222 rc = do_rmdir ? rmdir(filename) : unlink(filename);
223 /* use rmdir if this is a directory */
224 if (!do_rmdir && rc && errno == EISDIR) {
226 rc = rmdir(filename);
229 printf("unlink(%s) error: %s\n",
230 filename, strerror(errno));
237 if (tmp - last_t >= 10.0 ||
238 (tmp - last_t > 2.0 && (i % 10000) == 0)) {
239 printf(" - %s%s %ld (time %.2f total %.2f last %.2f)"
241 do_open ? do_keep ? "open/keep" : "open/close" :
242 do_mkdir ? "mkdir" : do_link ? "link" :
243 do_mknod ? "create" : "",
244 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
246 (i - last_i) / (tmp - last_t));
253 printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
254 do_open ? do_keep ? "open/keep" : "open/close" :
255 do_mkdir ? "mkdir" : do_link ? "link" :
256 do_mknod ? "create" : "",
257 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
258 last_t - start, ((double)total / (last_t - start)));
263 stderr_fd = fileno(stderr);
265 /* Assume fd is allocated in order, doing extra closes is not harmful */
266 for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
269 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
272 printf(" - closed %ld (time %.2f total %.2f last %.2f)"
273 "\n", i, tmp, tmp - start,
274 (i - last_i) / (tmp - last_t));
281 printf("total: %ld close in %.2f seconds: %.2f close/second\n",
282 total, last_t - start, ((double)total / (last_t - start)));