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 char *filename, *progname;
86 char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
88 double start, last_t, end;
89 long begin = 0, count = ~0UL >> 1;
90 int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
91 long i, total, last_i = 0;
92 int c, last_fd = -1, stderr_fd;
95 /* Handle the deprecated positional last argument "-seconds" */
96 if (argc > 1 && argv[argc - 1][0] == '-' &&
97 (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
98 fprintf(stderr, "warning: '-runtime' deprecated, "
99 "use '-t runtime' instead\n");
102 /* Not '-number', let regular argument parsing handle it. */
106 if ((endp = strrchr(argv[0], '/')) != NULL)
111 while ((c = getopt(argc, argv, "dl:kmor::t:u::")) != -1) {
130 end = strtol(optarg, &endp, 0);
131 if (end <= 0.0 || *endp != '\0')
140 fprintf(stderr, "Unknown option '%c'\n", optopt);
145 if (do_open + do_mkdir + do_link + do_mknod > 1 ||
146 do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
147 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
151 if (!do_open && do_keep) {
152 fprintf(stderr, "error: can only use -k with -o\n");
156 switch (argc - optind) {
158 begin = strtol(argv[argc - 2], NULL, 0);
160 count = strtol(argv[argc - 1], NULL, 0);
168 has_fmt_spec = strchr(fmt, '%') != NULL;
169 if (fmt_unlink != NULL)
170 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
172 for (i = 0, start = last_t = now(), end += start;
173 i < count && now() < end; i++, begin++) {
174 filename = get_file_name(fmt, begin, has_fmt_spec);
176 int fd = open(filename, O_CREAT|O_RDWR, 0644);
178 printf("open(%s) error: %s\n", filename,
185 else if (fd > last_fd)
187 } else if (do_link) {
188 rc = link(tgt, filename);
190 printf("link(%s, %s) error: %s\n",
191 tgt, filename, strerror(errno));
195 } else if (do_mkdir) {
196 rc = mkdir(filename, 0755);
198 printf("mkdir(%s) error: %s\n",
199 filename, strerror(errno));
203 } else if (do_mknod) {
204 rc = mknod(filename, S_IFREG | 0444, 0);
206 printf("mknod(%s) error: %s\n",
207 filename, strerror(errno));
213 if (fmt_unlink != NULL)
214 filename = get_file_name(fmt_unlink, begin,
215 unlink_has_fmt_spec);
217 rc = do_mkdir ? rmdir(filename) : unlink(filename);
219 printf("unlink(%s) error: %s\n",
220 filename, strerror(errno));
226 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
229 printf(" - %s%s %ld (time %.2f total %.2f last %.2f)"
231 do_open ? do_keep ? "open/keep" : "open/close" :
232 do_mkdir ? "mkdir" : do_link ? "link" :
233 do_mknod ? "create" : "",
234 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
236 (i - last_i) / (tmp - last_t));
243 printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
244 do_open ? do_keep ? "open/keep" : "open/close" :
245 do_mkdir ? "mkdir" : do_link ? "link" :
246 do_mknod ? "create" : "",
247 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
248 last_t - start, ((double)total / (last_t - start)));
253 stderr_fd = fileno(stderr);
255 /* Assume fd is allocated in order, doing extra closes is not harmful */
256 for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
259 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
262 printf(" - closed %ld (time %.2f total %.2f last %.2f)"
263 "\n", i, tmp, tmp - start,
264 (i - last_i) / (tmp - last_t));
271 printf("total: %ld close in %.2f seconds: %.2f close/second\n",
272 total, last_t - start, ((double)total / (last_t - start)));