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.
16 #include <sys/types.h>
27 #include <sys/ioctl.h>
33 #include <liblustre.h>
34 #include <lustre/liblustreapi.h> /* for O_LOV_DELAY_CREATE */
36 #define CHECK_COUNT 10000
37 #define DISPLAY_COUNT (CHECK_COUNT * 10)
38 #define DISPLAY_TIME 100
67 struct option longOpts[] = {
68 {"create", 0, NULL, CREATE },
69 {"lookup", 0, NULL, LOOKUP },
70 {"mknod", 0, NULL, MKNOD },
71 {"open", 0, NULL, OPEN },
72 {"stat", 0, NULL, STAT },
73 {"unlink", 0, NULL, UNLINK },
74 {"begin", 1, NULL, BEGIN },
75 {"iters", 1, NULL, ITERS },
76 {"time", 1, NULL, TIME }, /* seconds */
77 {"dirfmt", 1, NULL, DIRFMT },
78 {"ndirs", 1, NULL, NDIRS },
79 {"filefmt", 1, NULL, FILEFMT },
80 {"nfiles", 1, NULL, NFILES },
81 {"noexcl", 0, NULL, NOEXCL },
82 {"stripes", 1, NULL, STRIPES },
83 {"seed", 1, NULL, SEED },
84 {"seedfile", 1, NULL, SEEDFILE },
85 {"random_order", 0, NULL, RANDOM },
86 {"readdir_order", 0, NULL, READDIR },
87 {"recreate", 0, NULL, RECREATE },
88 {"ignore", 0, NULL, IGNORE },
89 {"verbose", 0, NULL, VERBOSE },
90 {"debug", 0, NULL, DEBUG },
91 {"help", 0, NULL, HELP },
101 char hostname[512] = "unknown";
104 int openflags = O_RDWR|O_CREAT|O_EXCL;
108 char mkdir_cmd[PATH_MAX+14];
112 struct dirent *dir_entry;
114 char filefmt[PATH_MAX];
115 char filename[PATH_MAX];
123 struct sigaction act;
132 #define dmesg if (debug) printf
134 #define DISPLAY_PROGRESS() { \
135 if (verbose && (nops % CHECK_COUNT == 0)) { \
136 curTime = MPI_Wtime(); \
137 interval = curTime - lastTime; \
138 if (interval > DISPLAY_TIME || nops % DISPLAY_COUNT == 0) { \
139 rate = (double)(nops - lastOps)/interval; \
140 printf("Rank %d: %.2f %ss/sec %.2f secs " \
141 "(total: %d %ss %.2f secs)\n", \
142 myrank, rate, cmd, interval, \
143 nops, cmd, curTime - startTime); \
145 lastTime = curTime; \
150 char *usage_msg = "usage: %s\n"
151 " { --create [ --noexcl ] | --lookup | --mknod |\n"
152 " --open | --stat | --unlink [ --recreate ] [ --ignore ] }\n"
153 " [ --help ] [ --verbose ] [ --debug ]\n"
154 " { [ --begin <num> ] --nfiles <num> }\n"
155 " [ --iters <num> ] [ --time <secs> ]\n"
156 " [ --dirfmt <str> ] [ --ndirs <num> ]\n"
157 " [ --filefmt <str> ] [ --stripes <num> ]\n"
158 " [ --random_order [--seed <num> | --seedfile <file>] ]\n"
159 " [ --readdir_order ]\n";
162 usage(FILE *stream, char *fmt, ...)
168 fprintf(stream, "%s: ", prog);
170 vfprintf(stderr, fmt, ap);
173 fprintf(stream, usage_msg, prog);
177 exit(stream == stderr);
180 /* Print process myrank and message, and exit (i.e. a fatal error) */
182 fatal(int rank, const char *fmt, ...)
184 if (rank == myrank) {
187 fprintf(stderr, "rank %d: ", rank);
189 vfprintf(stderr, fmt, ap);
193 MPI_Abort(MPI_COMM_WORLD, 1);
198 sigalrm_handler(int signum)
203 /* HAVE_LLAPI_FILE_LOOKUP is defined by liblustreapi.h if this function is
204 * defined therein. Otherwise we can do the equivalent operation via ioctl
205 * if we have access to a complete lustre build tree to get the various
206 * definitions - then compile with USE_MDC_LOOKUP defined. */
207 #if defined(HAVE_LLAPI_FILE_LOOKUP)
208 #define HAVE_MDC_LOOKUP
209 #elif defined(USE_MDC_LOOKUP)
211 #include <liblustre.h>
212 #include <linux/lustre_lib.h>
214 int llapi_file_lookup(int dirfd, const char *name)
216 struct obd_ioctl_data data = { 0 };
221 if (dirfd < 0 || name == NULL)
224 data.ioc_version = OBD_IOCTL_VERSION;
225 data.ioc_len = sizeof(data);
226 data.ioc_inlbuf1 = name;
227 data.ioc_inllen1 = strlen(name) + 1;
229 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
231 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
235 return ioctl(fd, IOC_MDC_LOOKUP, buf);
237 #define HAVE_MDC_LOOKUP
241 process_args(int argc, char *argv[])
244 int i, index, offset, tmpend, rc;
251 prog = basename(argv[0]);
252 strcpy(filefmt, "f%d");
253 gethostname(hostname, sizeof(hostname));
255 /* auto create shortOpts rather than maintaining a static string. */
256 for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
262 while ((rc = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
265 openflags &= ~(O_CREAT|O_EXCL);
267 #ifdef HAVE_MDC_LOOKUP
274 fatal(0, "Invalid - more than one operation "
276 longOpts[index].name);
279 cmd = (char *)longOpts[index].name;
282 if (mode != CREATE && mode != MKNOD) {
283 usage(stderr, "--noexcl only applies to "
284 "--create or --mknod.\n");
286 openflags &= ~O_EXCL;
289 if (mode != UNLINK) {
290 usage(stderr, "--recreate only makes sense"
296 begin = strtol(optarg, &endptr, 0);
297 if ((*endptr != 0) || (begin < 0)) {
298 fatal(0, "Invalid --start value.\n");
302 iters = strtol(optarg, &endptr, 0);
303 if ((*endptr != 0) || (iters <= 0)) {
304 fatal(0, "Invalid --iters value.\n");
306 if (mode != LOOKUP && mode != OPEN) {
307 usage(stderr, "--iters only makes sense with "
308 "--lookup or --open.\n");
312 seconds = strtol(optarg, &endptr, 0);
313 if ((*endptr != 0) || (seconds <= 0)) {
314 fatal(0, "Invalid --time value.\n");
318 if (strlen(optarg) > (PATH_MAX - 16)) {
319 fatal(0, "--dirfmt too long\n");
324 ndirs = strtol(optarg, &endptr, 0);
325 if ((*endptr != 0) || (ndirs <= 0)) {
326 fatal(0, "Invalid --ndirs value.\n");
328 if ((ndirs > nthreads) &&
329 ((mode == CREATE) || (mode == MKNOD))) {
330 fatal(0, "--ndirs=%d must be less than or "
331 "equal to the number of threads (%d).\n",
336 if (strlen(optarg) > 4080) {
337 fatal(0, "--filefmt too long\n");
340 /* Use %%d where you want the file # in the name. */
341 sprintf(filefmt, optarg, myrank);
344 nfiles = strtol(optarg, &endptr, 0);
345 if ((*endptr != 0) || (nfiles <= 0)) {
346 fatal(0, "Invalid --nfiles value.\n");
350 stripes = strtol(optarg, &endptr, 0);
351 if ((*endptr != 0) || (stripes < 0)) {
352 fatal(0, "Invalid --stripes value.\n");
356 openflags |= O_LOV_DELAY_CREATE;
358 fatal(0, "non-zero --stripes value "
359 "not yet supported.\n");
364 seed = strtoul(optarg, &endptr, 0);
366 fatal(0, "bad --seed option %s\n", optarg);
370 seed_file = fopen(optarg, "r");
372 fatal(myrank, "fopen(%s) error: %s\n",
373 optarg, strerror(errno));
376 for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
382 rc = sscanf(tmp, "%d", &seed);
383 if ((rc != 1) || (seed < 0)) {
384 fatal(myrank, "Invalid seed value '%s' "
385 "at line %d in %s.\n",
389 fatal(myrank, "File '%s' too short. Does not "
390 "contain a seed for thread %d.\n",
398 if (mode != LOOKUP && mode != OPEN) {
399 fatal(0, "--%s can only be specified with "
400 "--lookup, or --open.\n",
401 (char *)longOpts[index].name);
416 usage(stderr, "unrecognized option: '%c'.\n", optopt);
421 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
424 if (mode == CREATE || mode == MKNOD || mode == UNLINK || mode == STAT) {
428 } else if (nfiles == 0) {
429 usage(stderr, "--nfiles or --time must be specified "
432 } else if (mode == LOOKUP || mode == OPEN) {
436 } else if (iters == 0) {
437 usage(stderr, "--iters or --time must be specifed "
442 usage(stderr, "--nfiles must be specifed with --%s.\n",
447 int fd = open("/dev/urandom", O_RDONLY);
450 if (read(fd, &seed, sizeof(seed)) <
461 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
462 (order == RANDOM) ? "random_order" : "readdir_order");
464 usage(stderr, "one --create, --mknod, --open, --stat,"
465 #ifdef HAVE_MDC_LOOKUP
468 " or --unlink must be specifed.");
471 /* support for multiple threads in a dir, set begin/end appropriately.*/
472 dirnum = myrank % ndirs;
473 dirthreads = nthreads / ndirs;
474 if (nthreads > (ndirs * dirthreads + dirnum))
477 offset = myrank / ndirs;
479 tmpend = begin + nfiles - 1;
483 end = begin + (nfiles / dirthreads) * dirthreads + offset;
484 if ((end > tmpend) || (end <= 0))
493 dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
494 "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
496 if (dirfmt == NULL) {
499 sprintf(dir, dirfmt, dirnum);
501 sprintf(mkdir_cmd, "/bin/mkdir -p %s", dir);
502 #ifdef _LIGHTWEIGHT_KERNEL
503 printf("NOTICE: not running system(%s)\n", mkdir_cmd);
505 rc = system(mkdir_cmd);
507 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
513 fatal(myrank, "unable to chdir to '%s'.\n", dir);
518 static inline char *next_file()
520 if (order == RANDOM) {
521 sprintf(filename, filefmt, random() % nfiles);
527 dir_entry = readdir(directory);
528 if (dir_entry == NULL) {
529 rewinddir(directory);
530 while ((dir_entry = readdir(directory)) != NULL) {
531 if (dir_entry->d_name[0] != '.')
532 return(dir_entry->d_name);
535 fatal(myrank, "unable to read directory %s (%s).\n",
536 dir, strerror(errno));
539 return(dir_entry->d_name);
543 main(int argc, char *argv[])
545 int i, j, fd, rc, nops, lastOps;
547 double ag_interval = 0;
549 double rate, avg_rate, effective_rate;
550 double startTime, curTime, lastTime, interval;
554 rc = MPI_Init(&argc, &argv);
555 if (rc != MPI_SUCCESS)
556 fatal(myrank, "MPI_Init failed: %d\n", rc);
558 rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
559 if (rc != MPI_SUCCESS)
560 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
562 rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
563 if (rc != MPI_SUCCESS)
564 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
566 process_args(argc, argv);
569 if ((myrank == 0) || debug) {
570 printf("%d: %s starting at %s",
571 myrank, hostname, ctime(×tamp));
574 /* if we're not measuring creation rates then precreate
575 * the files we're operating on. */
576 if ((mode != CREATE) && (mode != MKNOD) && !ignore &&
577 (mode != UNLINK || recreate)) {
578 /* create the files in reverse order. When we encounter
579 * a file that already exists, assume the remainder of
580 * the files exist to save time. The timed performance
581 * test scripts make use of this behavior. */
582 for (i = end, j = 0; i >= begin; i -= dirthreads) {
583 sprintf(filename, filefmt, i);
584 fd = open(filename, openflags, 0644);
589 fatal(myrank, "precreate open(%s) error: %s\n",
590 filename, strerror(rc));
595 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
597 rc = MPI_Barrier(MPI_COMM_WORLD);
598 if (rc != MPI_SUCCESS)
599 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
602 if (order == READDIR) {
603 directory = opendir(dir);
604 if (directory == NULL) {
606 fatal(myrank, "opendir(%s) error: %s\n",
611 j = random() % nfiles;
612 dmesg("%d: %s initializing dir offset %u: %s",
613 myrank, hostname, j, ctime(×tamp));
615 for (i = 0; i <= j; i++) {
616 if ((dir_entry = readdir(directory)) == NULL) {
617 fatal(myrank, "could not read entry number %d "
618 "in directory %s.\n", i, dir);
623 dmesg("%d: index %d, filename %s, offset %ld: "
624 "%s initialization complete: %s",
625 myrank, i, dir_entry->d_name, telldir(directory),
626 hostname, ctime(×tamp));
630 act.sa_handler = sigalrm_handler;
631 (void)sigemptyset(&act.sa_mask);
633 sigaction(SIGALRM, &act, NULL);
637 rc = MPI_Barrier(MPI_COMM_WORLD);
638 if (rc != MPI_SUCCESS)
639 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
641 startTime = lastTime = MPI_Wtime();
646 for (; begin <= end && !alarm_caught; begin += dirthreads) {
647 sprintf(filename, filefmt, begin);
648 if ((fd = open(filename, openflags, 0644)) < 0) {
649 if (((rc = errno) == EINTR) && alarm_caught)
651 fatal(myrank, "open(%s) error: %s\n",
652 filename, strerror(rc));
660 dmesg("%d: created %d files, last file '%s'.\n",
661 myrank, nops, filename);
663 #ifdef HAVE_MDC_LOOKUP
665 fd = open(dir, O_RDONLY);
667 fatal(myrank, "open(dir == '%s') error: %s\n",
668 dir, strerror(errno));
671 for (; nops < iters && !alarm_caught;) {
672 char *filename = next_file();
673 rc = llapi_file_lookup(fd, filename);
675 if (((rc = errno) == EINTR) && alarm_caught)
677 fatal(myrank, "llapi_file_lookup(%s) "
678 "error: %s\n", filename, strerror(rc));
687 for (; begin <= end && !alarm_caught; begin += dirthreads) {
688 sprintf(filename, filefmt, begin);
689 rc = mknod(filename, S_IFREG| 0644, 0);
691 if (((rc = errno) == EINTR) && alarm_caught)
693 fatal(myrank, "mknod(%s) error: %s\n",
694 filename, strerror(rc));
702 for (; nops < iters && !alarm_caught;) {
704 if ((fd = open(file, openflags, 0644)) < 0) {
705 if (((rc = errno) == EINTR) && alarm_caught)
707 fatal(myrank, "open(%s) error: %s\n",
718 for (; begin <= end && !alarm_caught; begin += dirthreads) {
719 sprintf(filename, filefmt, begin);
720 rc = stat(filename, &statbuf);
722 if (((rc = errno) == EINTR) && alarm_caught)
724 if (((rc = errno) == ENOENT) && ignore)
726 fatal(myrank, "stat(%s) error: %s\n",
727 filename, strerror(rc));
735 for (; begin <= end && !alarm_caught; begin += dirthreads) {
736 sprintf(filename, filefmt, begin);
737 rc = unlink(filename);
739 if (((rc = errno) == EINTR) && alarm_caught)
741 if ((rc = errno) == ENOENT) {
744 /* no more files to unlink */
747 fatal(myrank, "unlink(%s) error: %s\n",
748 filename, strerror(rc));
757 rc = MPI_Barrier(MPI_COMM_WORLD);
758 if (rc != MPI_SUCCESS)
759 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
760 curTime = MPI_Wtime();
761 interval = curTime - startTime;
762 rate = (double) (nops) / interval;
764 rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
766 if (rc != MPI_SUCCESS) {
767 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
770 rc = MPI_Reduce(&interval, &ag_interval, 1, MPI_DOUBLE, MPI_SUM, 0,
772 if (rc != MPI_SUCCESS) {
773 fatal(myrank, "Failure in MPI_Reduce of total interval.\n");
776 rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_DOUBLE, MPI_SUM, 0,
778 if (rc != MPI_SUCCESS) {
779 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
784 curTime = MPI_Wtime();
785 interval = curTime - startTime;
786 effective_rate = (double) ag_ops / interval;
787 avg_rate = (double) ag_ops / ag_interval;
789 printf("Rate: %.2f eff %.2f aggr %.2f avg client %ss/sec "
790 "(total: %d threads %d %ss %d dirs %d threads/dir %.2f secs)\n",
791 effective_rate, ag_rate, avg_rate, cmd, nthreads, ag_ops,
792 cmd, ndirs, dirthreads, interval);
793 if (mode == UNLINK && !recreate && !ignore && ag_ops != nfiles)
794 printf("Warning: only unlinked %d files instead of %d"
795 "\n", ag_ops, nfiles);
799 for (begin = beginsave; begin <= end; begin += dirthreads) {
800 sprintf(filename, filefmt, begin);
801 if ((fd = open(filename, openflags, 0644)) < 0) {
805 fatal(myrank, "recreate open(%s) error: %s\n",
806 filename, strerror(rc));
814 if ((myrank == 0) || debug) {
815 printf("%d: %s finished at %s",
816 myrank, hostname, ctime(×tamp));