2 * 2003, Copyright, Hewlett-Packard Development Compnay, LP.
4 * Developed under the sponsorship of the U.S. Government
5 * under Subcontract No. B514193
9 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
10 * Use is subject to license terms.
12 * Copyright (c) 2012, 2015, Intel Corporation.
18 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <sys/xattr.h>
36 #include <lustre/lustreapi.h> /* for O_LOV_DELAY_CREATE */
38 #define CHECK_COUNT 10000
39 #define DISPLAY_COUNT (CHECK_COUNT * 10)
40 #define DISPLAY_TIME 100
74 struct option longOpts[] = {
75 {"create", 0, NULL, CREATE },
76 {"lookup", 0, NULL, LOOKUP },
77 {"mknod", 0, NULL, MKNOD },
78 {"open", 0, NULL, OPEN },
79 {"stat", 0, NULL, STAT },
80 {"unlink", 0, NULL, UNLINK },
81 {"begin", 1, NULL, BEGIN },
82 {"iters", 1, NULL, ITERS },
83 {"time", 1, NULL, TIME }, /* seconds */
84 {"dirfmt", 1, NULL, DIRFMT },
85 {"ndirs", 1, NULL, NDIRS },
86 {"filefmt", 1, NULL, FILEFMT },
87 {"nfiles", 1, NULL, NFILES },
88 {"noexcl", 0, NULL, NOEXCL },
89 {"stripes", 1, NULL, STRIPES },
90 {"seed", 1, NULL, SEED },
91 {"seedfile", 1, NULL, SEEDFILE },
92 {"random_order", 0, NULL, RANDOM },
93 {"readdir_order", 0, NULL, READDIR },
94 {"recreate", 0, NULL, RECREATE },
95 {"setxattr", 0, NULL, SETXATTR },
96 {"smallwrite", 0, NULL, SMALLWRITE },
97 {"ignore", 0, NULL, IGNORE },
98 {"verbose", 0, NULL, VERBOSE },
99 {"debug", 0, NULL, DEBUG },
100 {"help", 0, NULL, HELP },
101 {"mdtcount", 1, NULL, MDTCOUNT },
102 {"mntcount", 1, NULL, MNTCOUNT },
103 {"mntfmt", 1, NULL, MNT },
113 char hostname[512] = "unknown";
116 int openflags = O_RDWR|O_CREAT|O_EXCL;
120 char mkdir_cmd[PATH_MAX+14];
124 struct dirent *dir_entry;
126 char filefmt[PATH_MAX];
127 char filename[PATH_MAX];
136 struct sigaction act;
145 char xattrname[] = "user.mdsrate";
147 /* max xattr name + value length is block size, use 4000 here to avoid ENOSPC */
154 #define dmesg if (debug) printf
156 #define DISPLAY_PROGRESS() { \
157 if (verbose && (nops % CHECK_COUNT == 0)) { \
158 curTime = MPI_Wtime(); \
159 interval = curTime - lastTime; \
160 if (interval > DISPLAY_TIME || nops % DISPLAY_COUNT == 0) { \
161 rate = (double)(nops - lastOps)/interval; \
162 printf("Rank %d: %.2f %ss/sec %.2f secs " \
163 "(total: %d %ss %.2f secs)\n", \
164 myrank, rate, cmd, interval, \
165 nops, cmd, curTime - startTime); \
167 lastTime = curTime; \
172 char *usage_msg = "usage: %s\n"
173 " { --create [ --noexcl | --setxattr | --smallwrite ] |\n"
174 " --lookup | --mknod [ --setxattr ] | --open |\n"
175 " --stat | --unlink [ --recreate ] [ --ignore ] |\n"
177 " [ --help ] [ --verbose ] [ --debug ]\n"
178 " { [ --begin <num> ] --nfiles <num> }\n"
179 " [ --iters <num> ] [ --time <secs> ]\n"
180 " [ --dirfmt <str> ] [ --ndirs <num> ]\n"
181 " [ --filefmt <str> ] [ --stripes <num> ]\n"
182 " [ --random_order [--seed <num> | --seedfile <file>] ]\n"
183 " [ --readdir_order ] [ --mntfmt <str> ]\n"
184 " [ --mntcount <num> ] [ --mdtcount <num> ]\n"
185 " [ --setxattr ] }\n";
188 usage(FILE *stream, char *fmt, ...)
194 fprintf(stream, "%s: ", prog);
196 vfprintf(stderr, fmt, ap);
199 fprintf(stream, usage_msg, prog);
203 exit(stream == stderr);
206 /* Print process myrank and message, and exit (i.e. a fatal error) */
208 fatal(int rank, const char *fmt, ...)
210 if (rank == myrank) {
213 fprintf(stderr, "rank %d: ", rank);
215 vfprintf(stderr, fmt, ap);
219 MPI_Abort(MPI_COMM_WORLD, 1);
224 sigalrm_handler(int signum)
229 /* HAVE_LLAPI_FILE_LOOKUP is defined by liblustreapi.h if this function is
230 * defined therein. Otherwise we can do the equivalent operation via ioctl
231 * if we have access to a complete lustre build tree to get the various
232 * definitions - then compile with USE_MDC_LOOKUP defined. */
233 #if defined(HAVE_LLAPI_FILE_LOOKUP)
234 #define HAVE_MDC_LOOKUP
235 #elif defined(USE_MDC_LOOKUP)
237 #include <lustre_ioctl.h>
239 int llapi_file_lookup(int dirfd, const char *name)
241 struct obd_ioctl_data data = { 0 };
246 if (dirfd < 0 || name == NULL)
249 data.ioc_version = OBD_IOCTL_VERSION;
250 data.ioc_len = sizeof(data);
251 data.ioc_inlbuf1 = name;
252 data.ioc_inllen1 = strlen(name) + 1;
254 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
256 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
260 return ioctl(fd, IOC_MDC_LOOKUP, buf);
262 #define HAVE_MDC_LOOKUP
266 process_args(int argc, char *argv[])
269 int i, index, offset, tmpend, rc;
276 prog = basename(argv[0]);
277 strcpy(filefmt, "f%d");
278 gethostname(hostname, sizeof(hostname));
280 /* auto create shortOpts rather than maintaining a static string. */
281 for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
287 while ((rc = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
290 openflags &= ~(O_CREAT|O_EXCL);
292 #ifdef HAVE_MDC_LOOKUP
299 fatal(0, "Invalid - more than one operation "
301 longOpts[index].name);
304 cmd = (char *)longOpts[index].name;
307 if (mode != CREATE && mode != MKNOD) {
308 usage(stderr, "--noexcl only applies to "
309 "--create or --mknod.\n");
311 openflags &= ~O_EXCL;
314 if (mode != UNLINK) {
315 usage(stderr, "--recreate only makes sense"
323 cmd = (char *)longOpts[index].name;
324 } else if (mode == CREATE || mode == MKNOD) {
327 usage(stderr, "--setxattr only makes sense "
328 "with --create, --mknod or alone.\n");
333 usage(stderr, "--smallwrite only applies to "
338 begin = strtol(optarg, &endptr, 0);
339 if ((*endptr != 0) || (begin < 0)) {
340 fatal(0, "Invalid --start value.\n");
344 iters = strtol(optarg, &endptr, 0);
345 if ((*endptr != 0) || (iters <= 0)) {
346 fatal(0, "Invalid --iters value.\n");
348 if (mode != LOOKUP && mode != OPEN) {
349 usage(stderr, "--iters only makes sense with "
350 "--lookup or --open.\n");
354 seconds = strtol(optarg, &endptr, 0);
355 if ((*endptr != 0) || (seconds <= 0)) {
356 fatal(0, "Invalid --time value.\n");
360 if (strlen(optarg) > (PATH_MAX - 16)) {
361 fatal(0, "--dirfmt too long\n");
366 ndirs = strtol(optarg, &endptr, 0);
367 if ((*endptr != 0) || (ndirs <= 0)) {
368 fatal(0, "Invalid --ndirs value.\n");
370 if ((ndirs > nthreads) &&
371 ((mode == CREATE) || (mode == MKNOD))) {
372 fatal(0, "--ndirs=%d must be less than or "
373 "equal to the number of threads (%d).\n",
378 if (strlen(optarg) > 4080) {
379 fatal(0, "--filefmt too long\n");
382 /* Use %%d where you want the file # in the name. */
383 sprintf(filefmt, optarg, myrank);
386 nfiles = strtol(optarg, &endptr, 0);
387 if ((*endptr != 0) || (nfiles <= 0)) {
388 fatal(0, "Invalid --nfiles value.\n");
392 stripes = strtol(optarg, &endptr, 0);
393 if ((*endptr != 0) || (stripes < 0)) {
394 fatal(0, "Invalid --stripes value.\n");
398 openflags |= O_LOV_DELAY_CREATE;
400 fatal(0, "non-zero --stripes value "
401 "not yet supported.\n");
406 seed = strtoul(optarg, &endptr, 0);
408 fatal(0, "bad --seed option %s\n", optarg);
412 seed_file = fopen(optarg, "r");
414 fatal(myrank, "fopen(%s) error: %s\n",
415 optarg, strerror(errno));
418 for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
424 rc = sscanf(tmp, "%d", &seed);
425 if ((rc != 1) || (seed < 0)) {
426 fatal(myrank, "Invalid seed value '%s' "
427 "at line %d in %s.\n",
431 fatal(myrank, "File '%s' too short. Does not "
432 "contain a seed for thread %d.\n",
440 if (mode != LOOKUP && mode != OPEN) {
441 fatal(0, "--%s can only be specified with "
442 "--lookup, or --open.\n",
443 (char *)longOpts[index].name);
459 if (strlen(optarg) > (PATH_MAX - 16))
460 fatal(0, "--mnt too long\n");
464 mnt_count = strtol(optarg, &endptr, 0);
465 if ((*endptr != 0) || (mnt_count <= 0)) {
466 fatal(0, "Invalid --mnt_count value %s.\n",
471 mdt_count = strtol(optarg, &endptr, 0);
472 if ((*endptr != 0) || (mdt_count <= 0)) {
473 fatal(0, "Invalid --mdt_count value %s.\n",
478 usage(stderr, "unrecognized option: '%c'.\n", optopt);
483 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
486 if ((mnt_count != -1 && mntfmt == NULL) ||
487 (mnt_count == -1 && mntfmt != NULL)) {
488 usage(stderr, "mnt_count and mntfmt must be specified at the "
492 if (mode == CREATE || mode == MKNOD || mode == UNLINK ||
493 mode == STAT || mode == SETXATTR) {
497 } else if (nfiles == 0) {
498 usage(stderr, "--nfiles or --time must be specified "
501 } else if (mode == LOOKUP || mode == OPEN) {
505 } else if (iters == 0) {
506 usage(stderr, "--iters or --time must be specifed "
511 usage(stderr, "--nfiles must be specifed with --%s.\n",
516 int fd = open("/dev/urandom", O_RDONLY);
519 if (read(fd, &seed, sizeof(seed)) <
530 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
531 (order == RANDOM) ? "random_order" : "readdir_order");
533 usage(stderr, "one --create, --mknod, --open, --stat,"
534 #ifdef HAVE_MDC_LOOKUP
537 " --unlink or --setxattr must be specifed.");
540 /* support for multiple threads in a dir, set begin/end appropriately.*/
541 dirnum = myrank % ndirs;
542 dirthreads = nthreads / ndirs;
543 if (nthreads > (ndirs * dirthreads + dirnum))
546 offset = myrank / ndirs;
548 tmpend = begin + nfiles - 1;
552 end = begin + (nfiles / dirthreads) * dirthreads + offset;
553 if ((end > tmpend) || (end <= 0))
556 /* make sure mnt_count <= nthreads, otherwise it might div 0 in
557 * the following test */
558 if (mnt_count > nthreads)
559 mnt_count = nthreads;
567 dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
568 "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
570 if (dirfmt == NULL) {
575 if (mntfmt != NULL) {
576 sprintf(dir, mntfmt, (myrank / (nthreads/mnt_count)));
578 dir_len = strlen(dir);
580 sprintf(dir + dir_len, dirfmt, dirnum);
584 if (stat(dir, &sb) == 0) {
585 if (!S_ISDIR(sb.st_mode))
586 fatal(myrank, "'%s' is not dir\n", dir);
587 } else if (errno == ENOENT) {
588 sprintf(mkdir_cmd, "lfs mkdir -i %d %s",
589 myrank % mdt_count, dir);
591 fatal(myrank, "'%s' stat failed\n", dir);
594 sprintf(mkdir_cmd, "mkdir -p %s", dir);
597 dmesg("%d: %s\n", myrank, mkdir_cmd);
598 #ifdef _LIGHTWEIGHT_KERNEL
599 printf("NOTICE: not running system(%s)\n", mkdir_cmd);
601 rc = system(mkdir_cmd);
603 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
608 fatal(myrank, "unable to chdir to '%s'.\n", dir);
613 static inline char *next_file()
615 if (order == RANDOM) {
616 sprintf(filename, filefmt, random() % nfiles);
622 dir_entry = readdir(directory);
623 if (dir_entry == NULL) {
624 rewinddir(directory);
625 while ((dir_entry = readdir(directory)) != NULL) {
626 if (dir_entry->d_name[0] != '.')
627 return(dir_entry->d_name);
630 fatal(myrank, "unable to read directory %s (%s).\n",
631 dir, strerror(errno));
634 return(dir_entry->d_name);
638 main(int argc, char *argv[])
640 int i, j, fd, rc, nops, lastOps;
642 double ag_interval = 0;
644 double rate, avg_rate, effective_rate;
645 double startTime, curTime, lastTime, interval;
649 rc = MPI_Init(&argc, &argv);
650 if (rc != MPI_SUCCESS)
651 fatal(myrank, "MPI_Init failed: %d\n", rc);
653 rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
654 if (rc != MPI_SUCCESS)
655 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
657 rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
658 if (rc != MPI_SUCCESS)
659 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
661 process_args(argc, argv);
664 if ((myrank == 0) || debug) {
665 printf("%d: %s starting at %s",
666 myrank, hostname, ctime(×tamp));
669 /* if we're not measuring creation rates then precreate
670 * the files we're operating on. */
671 if ((mode != CREATE) && (mode != MKNOD) && !ignore &&
672 (mode != UNLINK || recreate)) {
673 /* create the files in reverse order. When we encounter
674 * a file that already exists, assume the remainder of
675 * the files exist to save time. The timed performance
676 * test scripts make use of this behavior. */
677 for (i = end, j = 0; i >= begin; i -= dirthreads) {
678 sprintf(filename, filefmt, i);
679 fd = open(filename, openflags, 0644);
684 fatal(myrank, "precreate open(%s) error: %s\n",
685 filename, strerror(rc));
690 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
692 rc = MPI_Barrier(MPI_COMM_WORLD);
693 if (rc != MPI_SUCCESS)
694 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
697 if (order == READDIR) {
698 directory = opendir(dir);
699 if (directory == NULL) {
701 fatal(myrank, "opendir(%s) error: %s\n",
706 j = random() % nfiles;
707 dmesg("%d: %s initializing dir offset %u: %s",
708 myrank, hostname, j, ctime(×tamp));
710 for (i = 0; i <= j; i++) {
711 if ((dir_entry = readdir(directory)) == NULL) {
712 fatal(myrank, "could not read entry number %d "
713 "in directory %s.\n", i, dir);
718 dmesg("%d: index %d, filename %s, offset %ld: "
719 "%s initialization complete: %s",
720 myrank, i, dir_entry->d_name, telldir(directory),
721 hostname, ctime(×tamp));
725 act.sa_handler = sigalrm_handler;
726 (void)sigemptyset(&act.sa_mask);
728 sigaction(SIGALRM, &act, NULL);
732 rc = MPI_Barrier(MPI_COMM_WORLD);
733 if (rc != MPI_SUCCESS)
734 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
736 startTime = lastTime = MPI_Wtime();
741 for (; begin <= end && !alarm_caught; begin += dirthreads) {
742 snprintf(filename, sizeof(filename), filefmt, begin);
743 fd = open(filename, openflags, 0644);
746 if (rc == EINTR && alarm_caught)
748 fatal(myrank, "open(%s) error: %s\n",
749 filename, strerror(rc));
753 rc = fsetxattr(fd, xattrname, xattrbuf,
754 xattrlen, XATTR_CREATE);
757 if (rc == EINTR && alarm_caught)
760 "setxattr(%s) error: %s\n",
761 filename, strerror(rc));
765 rc = write(fd, xattrbuf, xattrlen);
768 if (rc == EINTR && alarm_caught)
771 "write(%s) error: %s\n",
772 filename, strerror(rc));
781 dmesg("%d: created %d files, last file '%s'.\n",
782 myrank, nops, filename);
784 #ifdef HAVE_MDC_LOOKUP
786 fd = open(dir, O_RDONLY);
788 fatal(myrank, "open(dir == '%s') error: %s\n",
789 dir, strerror(errno));
792 for (; nops < iters && !alarm_caught;) {
793 char *filename = next_file();
794 rc = llapi_file_lookup(fd, filename);
796 if (((rc = errno) == EINTR) && alarm_caught)
798 fatal(myrank, "llapi_file_lookup(%s) "
799 "error: %s\n", filename, strerror(rc));
808 for (; begin <= end && !alarm_caught; begin += dirthreads) {
809 snprintf(filename, sizeof(filename), filefmt, begin);
810 rc = mknod(filename, S_IFREG | 0644, 0);
813 if (rc == EINTR && alarm_caught)
815 fatal(myrank, "mknod(%s) error: %s\n",
816 filename, strerror(rc));
820 rc = setxattr(filename, xattrname, xattrbuf,
821 xattrlen, XATTR_CREATE);
824 if (rc == EINTR && alarm_caught)
827 "setxattr(%s) error: %s\n",
828 filename, strerror(rc));
837 for (; nops < iters && !alarm_caught;) {
839 if ((fd = open(file, openflags, 0644)) < 0) {
840 if (((rc = errno) == EINTR) && alarm_caught)
842 fatal(myrank, "open(%s) error: %s\n",
853 for (; begin <= end && !alarm_caught; begin += dirthreads) {
854 sprintf(filename, filefmt, begin);
855 rc = stat(filename, &statbuf);
857 if (((rc = errno) == EINTR) && alarm_caught)
859 if (((rc = errno) == ENOENT) && ignore)
861 fatal(myrank, "stat(%s) error: %s\n",
862 filename, strerror(rc));
870 for (; begin <= end && !alarm_caught; begin += dirthreads) {
871 sprintf(filename, filefmt, begin);
872 rc = unlink(filename);
874 if (((rc = errno) == EINTR) && alarm_caught)
876 if ((rc = errno) == ENOENT) {
879 /* no more files to unlink */
882 fatal(myrank, "unlink(%s) error: %s\n",
883 filename, strerror(rc));
891 for (; begin <= end && !alarm_caught; begin += dirthreads) {
892 snprintf(filename, sizeof(filename), filefmt, begin);
893 rc = setxattr(filename, xattrname, xattrbuf, xattrlen,
897 if (rc == EINTR && alarm_caught)
899 if (rc == ENOENT && ignore)
901 fatal(myrank, "setxattr(%s) error: %s\n",
902 filename, strerror(rc));
911 rc = MPI_Barrier(MPI_COMM_WORLD);
912 if (rc != MPI_SUCCESS)
913 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
914 curTime = MPI_Wtime();
915 interval = curTime - startTime;
916 rate = (double) (nops) / interval;
918 rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
920 if (rc != MPI_SUCCESS) {
921 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
924 rc = MPI_Reduce(&interval, &ag_interval, 1, MPI_DOUBLE, MPI_SUM, 0,
926 if (rc != MPI_SUCCESS) {
927 fatal(myrank, "Failure in MPI_Reduce of total interval.\n");
930 rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_DOUBLE, MPI_SUM, 0,
932 if (rc != MPI_SUCCESS) {
933 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
937 curTime = MPI_Wtime();
938 interval = curTime - startTime;
939 effective_rate = (double) ag_ops / interval;
940 avg_rate = (double) ag_ops / ag_interval;
942 printf("Rate: %.2f eff %.2f aggr %.2f avg client %ss/sec "
943 "(total: %d threads %d %ss %d dirs %d threads/dir %.2f secs)\n",
944 effective_rate, ag_rate, avg_rate, cmd, nthreads, ag_ops,
945 cmd, ndirs, dirthreads, interval);
946 if (mode == UNLINK && !recreate && !ignore && ag_ops != nfiles)
947 printf("Warning: only unlinked %d files instead of %d"
948 "\n", ag_ops, nfiles);
952 for (begin = beginsave; begin <= end; begin += dirthreads) {
953 sprintf(filename, filefmt, begin);
954 if ((fd = open(filename, openflags, 0644)) < 0) {
958 fatal(myrank, "recreate open(%s) error: %s\n",
959 filename, strerror(rc));
967 if ((myrank == 0) || debug) {
968 printf("%d: %s finished at %s",
969 myrank, hostname, ctime(×tamp));