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>
42 #include <sys/ioctl.h>
43 #include <sys/xattr.h>
46 #include <lustre/lustreapi.h>
48 #include <linux/lustre/lustre_user.h>
49 #include <lustre/lustreapi.h>
51 static void usage(const char *prog)
54 "usage: %s {-o [-k] [-x <size>]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] [-i mdt_index] [-t seconds] filenamefmt [[start] count]\n",
56 printf("\t-i\tMDT to create the directories on\n"
57 "\t-l\tlink files to existing <tgt> file\n"
58 "\t-m\tmknod regular files (don't create OST objects)\n"
59 "\t-o\topen+create files with path and printf format\n"
60 "\t-k\t keep files open until all files are opened\n"
61 "\t-x\t set an xattr with <size> length on the files\n"
62 "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
63 printf("\t-d\tuse directories instead of regular files\n"
64 "\t-t\tstop creating files after <seconds> have elapsed\n");
65 printf("\t-S\tthe file size\n"
66 "\t-U\tthe start User ID of the file\n"
67 "\t-G\tthe start Group ID of the file\n"
68 "\t-P\tthe start Project ID of the file\n");
73 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
75 static char filename[4096];
78 bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
79 snprintf(filename, 4095, "%s%ld", fmt, n);
81 printf("file name too long\n");
87 static double now(void)
91 gettimeofday(&tv, NULL);
92 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
95 int main(int argc, char **argv)
97 bool do_open = false, do_keep = false, do_link = false;
98 bool do_unlink = false, do_mknod = false, do_mkdir = false;
99 bool do_setsize = false, do_chuid = false, do_chgid = false;
100 bool do_chprj = false;
101 bool do_rmdir = false;
102 bool do_xattr = false;
103 int stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
104 int stripe_offset = -1, stripe_count = 1;
105 size_t xattr_size = 0;
106 char *xattr_buf = NULL;
107 char *filename, *progname;
108 char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
110 double start, last_t, end;
111 long begin = 0, count = ~0UL >> 1;
112 int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
113 long i, total, last_i = 0;
114 int c, last_fd = -1, stderr_fd;
115 unsigned int uid = 0, gid = 0, pid = 0;
119 /* Handle the deprecated positional last argument "-seconds" */
120 if (argc > 1 && argv[argc - 1][0] == '-' &&
121 (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
123 "warning: '-runtime' deprecated, use '-t runtime' instead\n");
126 /* Not '-number', let regular argument parsing handle it. */
130 if ((endp = strrchr(argv[0], '/')) != NULL)
135 while ((c = getopt(argc, argv, "i:dG:l:kmor::S:t:u::U:x:")) != -1) {
142 gid = strtoul(optarg, NULL, 0);
145 stripe_offset = strtoul(optarg, &endp, 0);
147 fprintf(stderr, "invalid MDT index '%s'\n",
167 pid = strtoul(optarg, NULL, 0);
174 end = strtol(optarg, &endp, 0);
175 if (end <= 0.0 || *endp != '\0')
185 uid = strtoul(optarg, NULL, 0);
189 xattr_size = strtoul(optarg, &endp, 0);
191 fprintf(stderr, "invalid xattr size '%s'\n",
197 fprintf(stderr, "Unknown option '%c'\n", optopt);
202 if (!do_open && (do_setsize || do_chuid || do_chgid || do_chprj)) {
203 fprintf(stderr, "error: -S, -U, -G, -P works only with -o\n");
207 if (do_open + do_mkdir + do_link + do_mknod > 1 ||
208 do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
209 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
212 if (do_mkdir && do_unlink)
215 if (!do_open && (do_keep || do_xattr)) {
216 fprintf(stderr, "error: can only use -k|-x with -o\n");
220 switch (argc - optind) {
222 begin = strtol(argv[argc - 2], NULL, 0);
224 count = strtol(argv[argc - 1], NULL, 0);
232 has_fmt_spec = strchr(fmt, '%') != NULL;
233 if (fmt_unlink != NULL)
234 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
237 xattr_buf = malloc(xattr_size);
239 printf("malloc xattr buf error: %s\n", strerror(errno));
244 for (i = 0, start = last_t = now(), end += start;
245 i < count && now() < end; i++, begin++) {
248 filename = get_file_name(fmt, begin, has_fmt_spec);
252 fd = open(filename, O_CREAT|O_RDWR, 0644);
254 printf("open(%s) error: %s\n", filename,
260 rc = lseek(fd, (size - 6) < 0 ? 0 : size - 6,
263 printf("lseek(%s, %d) error: %s\n",
264 filename, size, strerror(errno));
267 rc = write(fd, "Lustre", 6);
269 printf("write(%s, %d) error: %s\n",
270 filename, 6, strerror(errno));
275 if (do_chuid || do_chgid) {
276 rc = fchown(fd, do_chuid ? uid + i : -1,
277 do_chgid ? gid + i : -1);
279 printf("fchown(%s, %u, %u) error: %s\n",
280 filename, do_chuid ? uid : -1,
290 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
292 printf("ioctl(%s) error: %s\n",
298 fsx.fsx_projid = pid + i;
299 rc = ioctl(fd, FS_IOC_FSSETXATTR, &fsx);
301 printf("ioctl(%s, %d) error: %s\n",
302 "FS_IOC_SETXATTR", pid,
309 strncpy(xattr_buf, filename, xattr_size);
310 rc = fsetxattr(fd, "user.createmany", xattr_buf,
313 printf("fsetxattr(%s) error: %s\n",
314 filename, strerror(errno));
322 else if (fd > last_fd)
324 } else if (do_link) {
325 rc = link(tgt, filename);
327 printf("link(%s, %s) error: %s\n",
328 tgt, filename, strerror(errno));
332 } else if (do_mkdir) {
333 if (stripe_offset != -1) {
334 rc = llapi_dir_create_pool(filename, 0755,
340 printf("llapi_dir_create_pool(%s) error: %s\n",
341 filename, strerror(-rc));
346 rc = mkdir(filename, 0755);
348 printf("mkdir(%s) error: %s\n",
349 filename, strerror(errno));
354 } else if (do_mknod) {
355 rc = mknod(filename, S_IFREG | 0444, 0);
357 printf("mknod(%s) error: %s\n",
358 filename, strerror(errno));
364 if (fmt_unlink != NULL)
365 filename = get_file_name(fmt_unlink, begin,
366 unlink_has_fmt_spec);
368 rc = do_rmdir ? rmdir(filename) : unlink(filename);
369 /* use rmdir if this is a directory */
370 if (!do_rmdir && rc && errno == EISDIR) {
372 rc = rmdir(filename);
375 printf("unlink(%s) error: %s\n",
376 filename, strerror(errno));
383 if (tmp - last_t >= 10.0 ||
384 (tmp - last_t > 2.0 && (i % 10000) == 0)) {
385 printf(" - %s%s %ld (time %.2f total %.2f last %.2f)\n",
386 do_open ? do_keep ? "open/keep" : "open/close" :
387 do_mkdir ? "mkdir" : do_link ? "link" :
388 do_mknod ? "create" : "",
389 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
391 (i - last_i) / (tmp - last_t));
398 printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
399 do_open ? do_keep ? "open/keep" : "open/close" :
400 do_mkdir ? "mkdir" : do_link ? "link" :
401 do_mknod ? "create" : "",
402 do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
403 last_t - start, ((double)total / (last_t - start)));
411 stderr_fd = fileno(stderr);
413 /* Assume fd is allocated in order, doing extra closes is not harmful */
414 for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
417 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
420 printf(" - closed %ld (time %.2f total %.2f last %.2f)\n",
422 (i - last_i) / (tmp - last_t));
429 printf("total: %ld close in %.2f seconds: %.2f close/second\n",
430 total, last_t - start, ((double)total / (last_t - start)));