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 #include <linux/lustre/lustre_user.h>
46 #include <lustre/lustreapi.h>
48 static void usage(const char *prog)
50 printf("usage: %s {-o [-k]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] "
51 "[-i mdt_index] [-t seconds] filenamefmt [[start] count]\n", prog);
52 printf("\t-i\tMDT to create the directories on\n"
53 "\t-l\tlink files to existing <tgt> file\n"
54 "\t-m\tmknod regular files (don't create OST objects)\n"
55 "\t-o\topen+create files with path and printf format\n"
56 "\t-k\t keep files open until all files are opened\n"
57 "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
58 printf("\t-d\tuse directories instead of regular files\n"
59 "\t-t\tstop creating files after <seconds> have elapsed\n");
64 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
66 static char filename[4096];
69 bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
70 snprintf(filename, 4095, "%s%ld", fmt, n);
72 printf("file name too long\n");
81 gettimeofday(&tv, NULL);
82 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
85 int main(int argc, char ** argv)
87 bool do_open = false, do_keep = false, do_link = false;
88 bool do_unlink = false, do_mknod = false, do_mkdir = false;
89 bool do_rmdir = false;
90 int stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
91 int stripe_offset = -1, stripe_count = 1;
92 char *filename, *progname;
93 char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
95 double start, last_t, end;
96 long begin = 0, count = ~0UL >> 1;
97 int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
98 long i, total, last_i = 0;
99 int c, last_fd = -1, stderr_fd;
102 /* Handle the deprecated positional last argument "-seconds" */
103 if (argc > 1 && argv[argc - 1][0] == '-' &&
104 (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
105 fprintf(stderr, "warning: '-runtime' deprecated, "
106 "use '-t runtime' instead\n");
109 /* Not '-number', let regular argument parsing handle it. */
113 if ((endp = strrchr(argv[0], '/')) != NULL)
118 while ((c = getopt(argc, argv, "i:dl:kmor::t:u::")) != -1) {
124 stripe_offset = strtoul(optarg, &endp, 0);
126 fprintf(stderr, "invalid MDT index '%s'\n",
145 end = strtol(optarg, &endp, 0);
146 if (end <= 0.0 || *endp != '\0')
155 fprintf(stderr, "Unknown option '%c'\n", optopt);
160 if (do_open + do_mkdir + do_link + do_mknod > 1 ||
161 do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
162 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
165 if (do_mkdir && do_unlink)
168 if (!do_open && do_keep) {
169 fprintf(stderr, "error: can only use -k with -o\n");
173 switch (argc - optind) {
175 begin = strtol(argv[argc - 2], NULL, 0);
177 count = strtol(argv[argc - 1], NULL, 0);
185 has_fmt_spec = strchr(fmt, '%') != NULL;
186 if (fmt_unlink != NULL)
187 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
189 for (i = 0, start = last_t = now(), end += start;
190 i < count && now() < end; i++, begin++) {
193 filename = get_file_name(fmt, begin, has_fmt_spec);
195 int fd = open(filename, O_CREAT|O_RDWR, 0644);
197 printf("open(%s) error: %s\n", filename,
204 else if (fd > last_fd)
206 } else if (do_link) {
207 rc = link(tgt, filename);
209 printf("link(%s, %s) error: %s\n",
210 tgt, filename, strerror(errno));
214 } else if (do_mkdir) {
215 if (stripe_offset != -1) {
216 rc = llapi_dir_create_pool(filename, 0755,
217 stripe_offset, stripe_count,
218 stripe_pattern, NULL);
220 printf("llapi_dir_create_pool(%s) error: %s\n",
221 filename, strerror(-rc));
226 rc = mkdir(filename, 0755);
228 printf("mkdir(%s) error: %s\n",
229 filename, strerror(errno));
234 } else if (do_mknod) {
235 rc = mknod(filename, S_IFREG | 0444, 0);
237 printf("mknod(%s) error: %s\n",
238 filename, strerror(errno));
244 if (fmt_unlink != NULL)
245 filename = get_file_name(fmt_unlink, begin,
246 unlink_has_fmt_spec);
248 rc = do_rmdir ? rmdir(filename) : unlink(filename);
249 /* use rmdir if this is a directory */
250 if (!do_rmdir && rc && errno == EISDIR) {
252 rc = rmdir(filename);
255 printf("unlink(%s) error: %s\n",
256 filename, strerror(errno));
263 if (tmp - last_t >= 10.0 ||
264 (tmp - last_t > 2.0 && (i % 10000) == 0)) {
265 printf(" - %s%s %ld (time %.2f total %.2f last %.2f)"
267 do_open ? do_keep ? "open/keep" : "open/close" :
268 do_mkdir ? "mkdir" : do_link ? "link" :
269 do_mknod ? "create" : "",
270 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
272 (i - last_i) / (tmp - last_t));
279 printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
280 do_open ? do_keep ? "open/keep" : "open/close" :
281 do_mkdir ? "mkdir" : do_link ? "link" :
282 do_mknod ? "create" : "",
283 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
284 last_t - start, ((double)total / (last_t - start)));
289 stderr_fd = fileno(stderr);
291 /* Assume fd is allocated in order, doing extra closes is not harmful */
292 for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
295 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
298 printf(" - closed %ld (time %.2f total %.2f last %.2f)"
299 "\n", i, tmp, tmp - start,
300 (i - last_i) / (tmp - last_t));
307 printf("total: %ld close in %.2f seconds: %.2f close/second\n",
308 total, last_t - start, ((double)total / (last_t - start)));