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, 2013, Intel Corporation.
18 #include <sys/types.h>
29 #include <sys/ioctl.h>
35 #include <lustre/lustreapi.h> /* for O_LOV_DELAY_CREATE */
37 #define CHECK_COUNT 10000
38 #define DISPLAY_COUNT (CHECK_COUNT * 10)
39 #define DISPLAY_TIME 100
71 struct option longOpts[] = {
72 {"create", 0, NULL, CREATE },
73 {"lookup", 0, NULL, LOOKUP },
74 {"mknod", 0, NULL, MKNOD },
75 {"open", 0, NULL, OPEN },
76 {"stat", 0, NULL, STAT },
77 {"unlink", 0, NULL, UNLINK },
78 {"begin", 1, NULL, BEGIN },
79 {"iters", 1, NULL, ITERS },
80 {"time", 1, NULL, TIME }, /* seconds */
81 {"dirfmt", 1, NULL, DIRFMT },
82 {"ndirs", 1, NULL, NDIRS },
83 {"filefmt", 1, NULL, FILEFMT },
84 {"nfiles", 1, NULL, NFILES },
85 {"noexcl", 0, NULL, NOEXCL },
86 {"stripes", 1, NULL, STRIPES },
87 {"seed", 1, NULL, SEED },
88 {"seedfile", 1, NULL, SEEDFILE },
89 {"random_order", 0, NULL, RANDOM },
90 {"readdir_order", 0, NULL, READDIR },
91 {"recreate", 0, NULL, RECREATE },
92 {"ignore", 0, NULL, IGNORE },
93 {"verbose", 0, NULL, VERBOSE },
94 {"debug", 0, NULL, DEBUG },
95 {"help", 0, NULL, HELP },
96 {"mdtcount", 1, NULL, MDTCOUNT },
97 {"mntcount", 1, NULL, MNTCOUNT },
98 {"mntfmt", 1, NULL, MNT },
108 char hostname[512] = "unknown";
111 int openflags = O_RDWR|O_CREAT|O_EXCL;
115 char mkdir_cmd[PATH_MAX+14];
119 struct dirent *dir_entry;
121 char filefmt[PATH_MAX];
122 char filename[PATH_MAX];
131 struct sigaction act;
143 #define dmesg if (debug) printf
145 #define DISPLAY_PROGRESS() { \
146 if (verbose && (nops % CHECK_COUNT == 0)) { \
147 curTime = MPI_Wtime(); \
148 interval = curTime - lastTime; \
149 if (interval > DISPLAY_TIME || nops % DISPLAY_COUNT == 0) { \
150 rate = (double)(nops - lastOps)/interval; \
151 printf("Rank %d: %.2f %ss/sec %.2f secs " \
152 "(total: %d %ss %.2f secs)\n", \
153 myrank, rate, cmd, interval, \
154 nops, cmd, curTime - startTime); \
156 lastTime = curTime; \
161 char *usage_msg = "usage: %s\n"
162 " { --create [ --noexcl ] | --lookup | --mknod |\n"
163 " --open | --stat | --unlink [ --recreate ] [ --ignore ] }\n"
164 " [ --help ] [ --verbose ] [ --debug ]\n"
165 " { [ --begin <num> ] --nfiles <num> }\n"
166 " [ --iters <num> ] [ --time <secs> ]\n"
167 " [ --dirfmt <str> ] [ --ndirs <num> ]\n"
168 " [ --filefmt <str> ] [ --stripes <num> ]\n"
169 " [ --random_order [--seed <num> | --seedfile <file>] ]\n"
170 " [ --readdir_order ] [ --mntfmt <str> ]\n"
171 " [ --mntcount <num> ] [ --mdtcount <num> ]\n";
174 usage(FILE *stream, char *fmt, ...)
180 fprintf(stream, "%s: ", prog);
182 vfprintf(stderr, fmt, ap);
185 fprintf(stream, usage_msg, prog);
189 exit(stream == stderr);
192 /* Print process myrank and message, and exit (i.e. a fatal error) */
194 fatal(int rank, const char *fmt, ...)
196 if (rank == myrank) {
199 fprintf(stderr, "rank %d: ", rank);
201 vfprintf(stderr, fmt, ap);
205 MPI_Abort(MPI_COMM_WORLD, 1);
210 sigalrm_handler(int signum)
215 /* HAVE_LLAPI_FILE_LOOKUP is defined by liblustreapi.h if this function is
216 * defined therein. Otherwise we can do the equivalent operation via ioctl
217 * if we have access to a complete lustre build tree to get the various
218 * definitions - then compile with USE_MDC_LOOKUP defined. */
219 #if defined(HAVE_LLAPI_FILE_LOOKUP)
220 #define HAVE_MDC_LOOKUP
221 #elif defined(USE_MDC_LOOKUP)
223 #include <libcfs/libcfs.h>
224 #include <lustre_ioctl.h>
226 int llapi_file_lookup(int dirfd, const char *name)
228 struct obd_ioctl_data data = { 0 };
233 if (dirfd < 0 || name == NULL)
236 data.ioc_version = OBD_IOCTL_VERSION;
237 data.ioc_len = sizeof(data);
238 data.ioc_inlbuf1 = name;
239 data.ioc_inllen1 = strlen(name) + 1;
241 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
243 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
247 return ioctl(fd, IOC_MDC_LOOKUP, buf);
249 #define HAVE_MDC_LOOKUP
253 process_args(int argc, char *argv[])
256 int i, index, offset, tmpend, rc;
263 prog = basename(argv[0]);
264 strcpy(filefmt, "f%d");
265 gethostname(hostname, sizeof(hostname));
267 /* auto create shortOpts rather than maintaining a static string. */
268 for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
274 while ((rc = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
277 openflags &= ~(O_CREAT|O_EXCL);
279 #ifdef HAVE_MDC_LOOKUP
286 fatal(0, "Invalid - more than one operation "
288 longOpts[index].name);
291 cmd = (char *)longOpts[index].name;
294 if (mode != CREATE && mode != MKNOD) {
295 usage(stderr, "--noexcl only applies to "
296 "--create or --mknod.\n");
298 openflags &= ~O_EXCL;
301 if (mode != UNLINK) {
302 usage(stderr, "--recreate only makes sense"
308 begin = strtol(optarg, &endptr, 0);
309 if ((*endptr != 0) || (begin < 0)) {
310 fatal(0, "Invalid --start value.\n");
314 iters = strtol(optarg, &endptr, 0);
315 if ((*endptr != 0) || (iters <= 0)) {
316 fatal(0, "Invalid --iters value.\n");
318 if (mode != LOOKUP && mode != OPEN) {
319 usage(stderr, "--iters only makes sense with "
320 "--lookup or --open.\n");
324 seconds = strtol(optarg, &endptr, 0);
325 if ((*endptr != 0) || (seconds <= 0)) {
326 fatal(0, "Invalid --time value.\n");
330 if (strlen(optarg) > (PATH_MAX - 16)) {
331 fatal(0, "--dirfmt too long\n");
336 ndirs = strtol(optarg, &endptr, 0);
337 if ((*endptr != 0) || (ndirs <= 0)) {
338 fatal(0, "Invalid --ndirs value.\n");
340 if ((ndirs > nthreads) &&
341 ((mode == CREATE) || (mode == MKNOD))) {
342 fatal(0, "--ndirs=%d must be less than or "
343 "equal to the number of threads (%d).\n",
348 if (strlen(optarg) > 4080) {
349 fatal(0, "--filefmt too long\n");
352 /* Use %%d where you want the file # in the name. */
353 sprintf(filefmt, optarg, myrank);
356 nfiles = strtol(optarg, &endptr, 0);
357 if ((*endptr != 0) || (nfiles <= 0)) {
358 fatal(0, "Invalid --nfiles value.\n");
362 stripes = strtol(optarg, &endptr, 0);
363 if ((*endptr != 0) || (stripes < 0)) {
364 fatal(0, "Invalid --stripes value.\n");
368 openflags |= O_LOV_DELAY_CREATE;
370 fatal(0, "non-zero --stripes value "
371 "not yet supported.\n");
376 seed = strtoul(optarg, &endptr, 0);
378 fatal(0, "bad --seed option %s\n", optarg);
382 seed_file = fopen(optarg, "r");
384 fatal(myrank, "fopen(%s) error: %s\n",
385 optarg, strerror(errno));
388 for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
394 rc = sscanf(tmp, "%d", &seed);
395 if ((rc != 1) || (seed < 0)) {
396 fatal(myrank, "Invalid seed value '%s' "
397 "at line %d in %s.\n",
401 fatal(myrank, "File '%s' too short. Does not "
402 "contain a seed for thread %d.\n",
410 if (mode != LOOKUP && mode != OPEN) {
411 fatal(0, "--%s can only be specified with "
412 "--lookup, or --open.\n",
413 (char *)longOpts[index].name);
429 if (strlen(optarg) > (PATH_MAX - 16))
430 fatal(0, "--mnt too long\n");
434 mnt_count = strtol(optarg, &endptr, 0);
435 if ((*endptr != 0) || (mnt_count <= 0)) {
436 fatal(0, "Invalid --mnt_count value %s.\n",
441 mdt_count = strtol(optarg, &endptr, 0);
442 if ((*endptr != 0) || (mdt_count <= 0)) {
443 fatal(0, "Invalid --mdt_count value %s.\n",
448 usage(stderr, "unrecognized option: '%c'.\n", optopt);
453 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
456 if ((mnt_count != -1 && mntfmt == NULL) ||
457 (mnt_count == -1 && mntfmt != NULL)) {
458 usage(stderr, "mnt_count and mntfmt must be specified at the "
462 if (mode == CREATE || mode == MKNOD || mode == UNLINK || mode == STAT) {
466 } else if (nfiles == 0) {
467 usage(stderr, "--nfiles or --time must be specified "
470 } else if (mode == LOOKUP || mode == OPEN) {
474 } else if (iters == 0) {
475 usage(stderr, "--iters or --time must be specifed "
480 usage(stderr, "--nfiles must be specifed with --%s.\n",
485 int fd = open("/dev/urandom", O_RDONLY);
488 if (read(fd, &seed, sizeof(seed)) <
499 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
500 (order == RANDOM) ? "random_order" : "readdir_order");
502 usage(stderr, "one --create, --mknod, --open, --stat,"
503 #ifdef HAVE_MDC_LOOKUP
506 " or --unlink must be specifed.");
509 /* support for multiple threads in a dir, set begin/end appropriately.*/
510 dirnum = myrank % ndirs;
511 dirthreads = nthreads / ndirs;
512 if (nthreads > (ndirs * dirthreads + dirnum))
515 offset = myrank / ndirs;
517 tmpend = begin + nfiles - 1;
521 end = begin + (nfiles / dirthreads) * dirthreads + offset;
522 if ((end > tmpend) || (end <= 0))
525 /* make sure mnt_count <= nthreads, otherwise it might div 0 in
526 * the following test */
527 if (mnt_count > nthreads)
528 mnt_count = nthreads;
536 dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
537 "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
539 if (dirfmt == NULL) {
544 if (mntfmt != NULL) {
545 sprintf(dir, mntfmt, (myrank / (nthreads/mnt_count)));
547 dir_len = strlen(dir);
549 sprintf(dir + dir_len, dirfmt, dirnum);
553 if (stat(dir, &sb) == 0) {
554 if (!S_ISDIR(sb.st_mode))
555 fatal(myrank, "'%s' is not dir\n", dir);
556 } else if (errno == ENOENT) {
557 sprintf(mkdir_cmd, "lfs mkdir -i %d %s",
558 myrank % mdt_count, dir);
560 fatal(myrank, "'%s' stat failed\n", dir);
563 sprintf(mkdir_cmd, "mkdir -p %s", dir);
566 dmesg("%d: %s\n", myrank, mkdir_cmd);
567 #ifdef _LIGHTWEIGHT_KERNEL
568 printf("NOTICE: not running system(%s)\n", mkdir_cmd);
570 rc = system(mkdir_cmd);
572 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
577 fatal(myrank, "unable to chdir to '%s'.\n", dir);
582 static inline char *next_file()
584 if (order == RANDOM) {
585 sprintf(filename, filefmt, random() % nfiles);
591 dir_entry = readdir(directory);
592 if (dir_entry == NULL) {
593 rewinddir(directory);
594 while ((dir_entry = readdir(directory)) != NULL) {
595 if (dir_entry->d_name[0] != '.')
596 return(dir_entry->d_name);
599 fatal(myrank, "unable to read directory %s (%s).\n",
600 dir, strerror(errno));
603 return(dir_entry->d_name);
607 main(int argc, char *argv[])
609 int i, j, fd, rc, nops, lastOps;
611 double ag_interval = 0;
613 double rate, avg_rate, effective_rate;
614 double startTime, curTime, lastTime, interval;
618 rc = MPI_Init(&argc, &argv);
619 if (rc != MPI_SUCCESS)
620 fatal(myrank, "MPI_Init failed: %d\n", rc);
622 rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
623 if (rc != MPI_SUCCESS)
624 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
626 rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
627 if (rc != MPI_SUCCESS)
628 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
630 process_args(argc, argv);
633 if ((myrank == 0) || debug) {
634 printf("%d: %s starting at %s",
635 myrank, hostname, ctime(×tamp));
638 /* if we're not measuring creation rates then precreate
639 * the files we're operating on. */
640 if ((mode != CREATE) && (mode != MKNOD) && !ignore &&
641 (mode != UNLINK || recreate)) {
642 /* create the files in reverse order. When we encounter
643 * a file that already exists, assume the remainder of
644 * the files exist to save time. The timed performance
645 * test scripts make use of this behavior. */
646 for (i = end, j = 0; i >= begin; i -= dirthreads) {
647 sprintf(filename, filefmt, i);
648 fd = open(filename, openflags, 0644);
653 fatal(myrank, "precreate open(%s) error: %s\n",
654 filename, strerror(rc));
659 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
661 rc = MPI_Barrier(MPI_COMM_WORLD);
662 if (rc != MPI_SUCCESS)
663 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
666 if (order == READDIR) {
667 directory = opendir(dir);
668 if (directory == NULL) {
670 fatal(myrank, "opendir(%s) error: %s\n",
675 j = random() % nfiles;
676 dmesg("%d: %s initializing dir offset %u: %s",
677 myrank, hostname, j, ctime(×tamp));
679 for (i = 0; i <= j; i++) {
680 if ((dir_entry = readdir(directory)) == NULL) {
681 fatal(myrank, "could not read entry number %d "
682 "in directory %s.\n", i, dir);
687 dmesg("%d: index %d, filename %s, offset %ld: "
688 "%s initialization complete: %s",
689 myrank, i, dir_entry->d_name, telldir(directory),
690 hostname, ctime(×tamp));
694 act.sa_handler = sigalrm_handler;
695 (void)sigemptyset(&act.sa_mask);
697 sigaction(SIGALRM, &act, NULL);
701 rc = MPI_Barrier(MPI_COMM_WORLD);
702 if (rc != MPI_SUCCESS)
703 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
705 startTime = lastTime = MPI_Wtime();
710 for (; begin <= end && !alarm_caught; begin += dirthreads) {
711 sprintf(filename, filefmt, begin);
712 if ((fd = open(filename, openflags, 0644)) < 0) {
713 if (((rc = errno) == EINTR) && alarm_caught)
715 fatal(myrank, "open(%s) error: %s\n",
716 filename, strerror(rc));
724 dmesg("%d: created %d files, last file '%s'.\n",
725 myrank, nops, filename);
727 #ifdef HAVE_MDC_LOOKUP
729 fd = open(dir, O_RDONLY);
731 fatal(myrank, "open(dir == '%s') error: %s\n",
732 dir, strerror(errno));
735 for (; nops < iters && !alarm_caught;) {
736 char *filename = next_file();
737 rc = llapi_file_lookup(fd, filename);
739 if (((rc = errno) == EINTR) && alarm_caught)
741 fatal(myrank, "llapi_file_lookup(%s) "
742 "error: %s\n", filename, strerror(rc));
751 for (; begin <= end && !alarm_caught; begin += dirthreads) {
752 sprintf(filename, filefmt, begin);
753 rc = mknod(filename, S_IFREG| 0644, 0);
755 if (((rc = errno) == EINTR) && alarm_caught)
757 fatal(myrank, "mknod(%s) error: %s\n",
758 filename, strerror(rc));
766 for (; nops < iters && !alarm_caught;) {
768 if ((fd = open(file, openflags, 0644)) < 0) {
769 if (((rc = errno) == EINTR) && alarm_caught)
771 fatal(myrank, "open(%s) error: %s\n",
782 for (; begin <= end && !alarm_caught; begin += dirthreads) {
783 sprintf(filename, filefmt, begin);
784 rc = stat(filename, &statbuf);
786 if (((rc = errno) == EINTR) && alarm_caught)
788 if (((rc = errno) == ENOENT) && ignore)
790 fatal(myrank, "stat(%s) error: %s\n",
791 filename, strerror(rc));
799 for (; begin <= end && !alarm_caught; begin += dirthreads) {
800 sprintf(filename, filefmt, begin);
801 rc = unlink(filename);
803 if (((rc = errno) == EINTR) && alarm_caught)
805 if ((rc = errno) == ENOENT) {
808 /* no more files to unlink */
811 fatal(myrank, "unlink(%s) error: %s\n",
812 filename, strerror(rc));
821 rc = MPI_Barrier(MPI_COMM_WORLD);
822 if (rc != MPI_SUCCESS)
823 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
824 curTime = MPI_Wtime();
825 interval = curTime - startTime;
826 rate = (double) (nops) / interval;
828 rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
830 if (rc != MPI_SUCCESS) {
831 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
834 rc = MPI_Reduce(&interval, &ag_interval, 1, MPI_DOUBLE, MPI_SUM, 0,
836 if (rc != MPI_SUCCESS) {
837 fatal(myrank, "Failure in MPI_Reduce of total interval.\n");
840 rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_DOUBLE, MPI_SUM, 0,
842 if (rc != MPI_SUCCESS) {
843 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
848 curTime = MPI_Wtime();
849 interval = curTime - startTime;
850 effective_rate = (double) ag_ops / interval;
851 avg_rate = (double) ag_ops / ag_interval;
853 printf("Rate: %.2f eff %.2f aggr %.2f avg client %ss/sec "
854 "(total: %d threads %d %ss %d dirs %d threads/dir %.2f secs)\n",
855 effective_rate, ag_rate, avg_rate, cmd, nthreads, ag_ops,
856 cmd, ndirs, dirthreads, interval);
857 if (mode == UNLINK && !recreate && !ignore && ag_ops != nfiles)
858 printf("Warning: only unlinked %d files instead of %d"
859 "\n", ag_ops, nfiles);
863 for (begin = beginsave; begin <= end; begin += dirthreads) {
864 sprintf(filename, filefmt, begin);
865 if ((fd = open(filename, openflags, 0644)) < 0) {
869 fatal(myrank, "recreate open(%s) error: %s\n",
870 filename, strerror(rc));
878 if ((myrank == 0) || debug) {
879 printf("%d: %s finished at %s",
880 myrank, hostname, ctime(×tamp));