1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * 2003, Copyright, Hewlett-Packard Development Compnay, LP.
6 * Developed under the sponsorship of the U.S. Government
7 * under Subcontract No. B514193
11 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
12 * Use is subject to license terms.
18 #include <sys/types.h>
29 #include <sys/ioctl.h>
35 #include <lustre/liblustreapi.h> /* for O_LOV_DELAY_CREATE */
37 #define CHECK_COUNT 10000
38 #define DISPLAY_COUNT (CHECK_COUNT * 10)
39 #define DISPLAY_TIME 100
68 struct option longOpts[] = {
69 {"create", 0, NULL, CREATE },
70 {"lookup", 0, NULL, LOOKUP },
71 {"mknod", 0, NULL, MKNOD },
72 {"open", 0, NULL, OPEN },
73 {"stat", 0, NULL, STAT },
74 {"unlink", 0, NULL, UNLINK },
75 {"begin", 1, NULL, BEGIN },
76 {"iters", 1, NULL, ITERS },
77 {"time", 1, NULL, TIME }, /* seconds */
78 {"dirfmt", 1, NULL, DIRFMT },
79 {"ndirs", 1, NULL, NDIRS },
80 {"filefmt", 1, NULL, FILEFMT },
81 {"nfiles", 1, NULL, NFILES },
82 {"noexcl", 0, NULL, NOEXCL },
83 {"stripes", 1, NULL, STRIPES },
84 {"seed", 1, NULL, SEED },
85 {"seedfile", 1, NULL, SEEDFILE },
86 {"random_order", 0, NULL, RANDOM },
87 {"readdir_order", 0, NULL, READDIR },
88 {"recreate", 0, NULL, RECREATE },
89 {"ignore", 0, NULL, IGNORE },
90 {"verbose", 0, NULL, VERBOSE },
91 {"debug", 0, NULL, DEBUG },
92 {"help", 0, NULL, HELP },
102 char hostname[512] = "unknown";
105 int openflags = O_RDWR|O_CREAT|O_EXCL;
109 char mkdir_cmd[PATH_MAX+14];
113 struct dirent *dir_entry;
115 char filefmt[PATH_MAX];
116 char filename[PATH_MAX];
124 struct sigaction act;
133 #define dmesg if (debug) printf
135 #define DISPLAY_PROGRESS() { \
136 if ((++nops % CHECK_COUNT) == 0 && verbose) { \
138 interval = curTime - lastTime; \
139 if (interval > DISPLAY_TIME || nops % DISPLAY_COUNT == 0) { \
140 rate = (float)(nops - lastOps); \
142 rate /= (float)interval; \
143 printf("Rank %d: %.2f %ss/sec %lu secs " \
144 "(total: %d %ss %lu secs)\n", \
145 myrank, rate, cmd, interval, \
146 nops, cmd, curTime - startTime); \
148 lastTime = curTime; \
153 char *usage_msg = "usage: %s\n"
154 " { --create [ --noexcl ] | --lookup | --mknod |\n"
155 " --open | --stat | --unlink [ --recreate ] [ --ignore ] }\n"
156 " [ --help ] [ --verbose ] [ --debug ]\n"
157 " { [ --begin <num> ] --nfiles <num> }\n"
158 " [ --iters <num> ] [ --time <secs> ]\n"
159 " [ --dirfmt <str> ] [ --ndirs <num> ]\n"
160 " [ --filefmt <str> ] [ --stripes <num> ]\n"
161 " [ --random_order [--seed <num> | --seedfile <file>] ]\n"
162 " [ --readdir_order ]\n";
165 usage(FILE *stream, char *fmt, ...)
171 fprintf(stream, "%s: ", prog);
173 vfprintf(stderr, fmt, ap);
176 fprintf(stream, usage_msg, prog);
180 exit(stream == stderr);
183 /* Print process myrank and message, and exit (i.e. a fatal error) */
185 fatal(int rank, const char *fmt, ...)
187 if (rank == myrank) {
190 fprintf(stderr, "rank %d: ", rank);
192 vfprintf(stderr, fmt, ap);
196 MPI_Abort(MPI_COMM_WORLD, 1);
201 sigalrm_handler(int signum)
206 /* HAVE_LLAPI_FILE_LOOKUP is defined by liblustreapi.h if this function is
207 * defined therein. Otherwise we can do the equivalent operation via ioctl
208 * if we have access to a complete lustre build tree to get the various
209 * definitions - then compile with USE_MDC_LOOKUP defined. */
210 #if defined(HAVE_LLAPI_FILE_LOOKUP)
211 #define HAVE_MDC_LOOKUP
212 #elif defined(USE_MDC_LOOKUP)
214 #include <liblustre.h>
215 #include <linux/lustre_lib.h>
217 int llapi_file_lookup(int dirfd, const char *name)
219 struct obd_ioctl_data data = { 0 };
224 if (dirfd < 0 || name == NULL)
227 data.ioc_version = OBD_IOCTL_VERSION;
228 data.ioc_len = sizeof(data);
229 data.ioc_inlbuf1 = name;
230 data.ioc_inllen1 = strlen(name) + 1;
232 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
234 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
238 return ioctl(fd, IOC_MDC_LOOKUP, buf);
240 #define HAVE_MDC_LOOKUP
244 process_args(int argc, char *argv[])
246 char c, *cp, *endptr;
247 int i, index, offset, tmpend, rc;
254 prog = basename(argv[0]);
255 strcpy(filefmt, "f%d");
256 gethostname(hostname, sizeof(hostname));
258 /* auto create shortOpts rather than maintaining a static string. */
259 for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
265 while ((c = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
268 openflags &= ~(O_CREAT|O_EXCL);
270 #ifdef HAVE_MDC_LOOKUP
277 fatal(0, "Invalid - more than one operation "
279 longOpts[index].name);
282 cmd = (char *)longOpts[index].name;
285 if (mode != CREATE && mode != MKNOD) {
286 usage(stderr, "--noexcl only applies to "
287 "--create or --mknod.\n");
289 openflags &= ~O_EXCL;
292 if (mode != UNLINK) {
293 usage(stderr, "--recreate only makes sense"
299 begin = strtol(optarg, &endptr, 0);
300 if ((*endptr != 0) || (begin < 0)) {
301 fatal(0, "Invalid --start value.\n");
305 iters = strtol(optarg, &endptr, 0);
306 if ((*endptr != 0) || (iters <= 0)) {
307 fatal(0, "Invalid --iters value.\n");
309 if (mode != LOOKUP && mode != OPEN && mode != STAT) {
310 usage(stderr, "--iters only makes sense with "
311 "--lookup, --open, or --stat.\n");
315 seconds = strtol(optarg, &endptr, 0);
316 if ((*endptr != 0) || (seconds <= 0)) {
317 fatal(0, "Invalid --time value.\n");
321 if (strlen(optarg) > (PATH_MAX - 16)) {
322 fatal(0, "--dirfmt too long\n");
327 ndirs = strtol(optarg, &endptr, 0);
328 if ((*endptr != 0) || (ndirs <= 0)) {
329 fatal(0, "Invalid --ndirs value.\n");
331 if ((ndirs > nthreads) &&
332 ((mode == CREATE) || (mode == MKNOD))) {
333 fatal(0, "--ndirs=%d must be less than or "
334 "equal to the number of threads (%d).\n",
339 if (strlen(optarg) > 4080) {
340 fatal(0, "--filefmt too long\n");
343 /* Use %%d where you want the file # in the name. */
344 sprintf(filefmt, optarg, myrank);
347 nfiles = strtol(optarg, &endptr, 0);
348 if ((*endptr != 0) || (nfiles <= 0)) {
349 fatal(0, "Invalid --nfiles value.\n");
353 stripes = strtol(optarg, &endptr, 0);
354 if ((*endptr != 0) || (stripes < 0)) {
355 fatal(0, "Invalid --stripes value.\n");
359 openflags |= O_LOV_DELAY_CREATE;
361 fatal(0, "non-zero --stripes value "
362 "not yet supported.\n");
367 seed = strtoul(optarg, &endptr, 0);
369 fatal(0, "bad --seed option %s\n", optarg);
373 seed_file = fopen(optarg, "r");
375 fatal(myrank, "fopen(%s) error: %s\n",
376 optarg, strerror(errno));
379 for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
385 rc = sscanf(tmp, "%d", &seed);
386 if ((rc != 1) || (seed < 0)) {
387 fatal(myrank, "Invalid seed value '%s' "
388 "at line %d in %s.\n",
392 fatal(myrank, "File '%s' too short. Does not "
393 "contain a seed for thread %d.\n",
401 if (mode != LOOKUP && mode != OPEN && mode != STAT) {
402 fatal(0, "--%s can only be specified with "
403 "--lookup, --open, or --stat.\n",
404 (char *)longOpts[index].name);
419 usage(stderr, "unrecognized option: '%c'.\n", optopt);
424 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
427 if (mode == CREATE || mode == MKNOD || mode == UNLINK) {
431 } else if (nfiles == 0) {
432 usage(stderr, "--nfiles or --time must be specified "
435 } else if (mode == LOOKUP || mode == OPEN || mode == STAT) {
439 } else if (iters == 0) {
440 usage(stderr, "--iters or --time must be specifed "
445 usage(stderr, "--nfiles must be specifed with --%s.\n",
450 int fd = open("/dev/urandom", O_RDONLY);
453 if (read(fd, &seed, sizeof(seed)) <
464 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
465 (order == RANDOM) ? "random_order" : "readdir_order");
467 usage(stderr, "one --create, --mknod, --open, --stat,"
468 #ifdef HAVE_MDC_LOOKUP
471 " or --unlink must be specifed.");
474 /* support for multiple threads in a dir, set begin/end appropriately.*/
475 dirnum = myrank % ndirs;
476 dirthreads = nthreads / ndirs;
477 if (nthreads > (ndirs * dirthreads + dirnum))
480 offset = myrank / ndirs;
482 tmpend = begin + nfiles - 1;
486 end = begin + (nfiles / dirthreads) * dirthreads + offset;
487 if ((end > tmpend) || (end <= 0))
496 dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
497 "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
499 if (dirfmt == NULL) {
502 sprintf(dir, dirfmt, dirnum);
504 sprintf(mkdir_cmd, "/bin/mkdir -p %s", dir);
505 #ifdef _LIGHTWEIGHT_KERNEL
506 printf("NOTICE: not running system(%s)\n", mkdir_cmd);
508 rc = system(mkdir_cmd);
510 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
516 fatal(myrank, "unable to chdir to '%s'.\n", dir);
521 static inline char *next_file()
523 if (order == RANDOM) {
524 sprintf(filename, filefmt, random() % nfiles);
530 dir_entry = readdir(directory);
531 if (dir_entry == NULL) {
532 rewinddir(directory);
533 while ((dir_entry = readdir(directory)) != NULL) {
534 if (dir_entry->d_name[0] != '.')
535 return(dir_entry->d_name);
538 fatal(myrank, "unable to read directory %s (%s).\n",
539 dir, strerror(errno));
542 return(dir_entry->d_name);
546 main(int argc, char *argv[])
548 int i, j, fd, rc, nops, lastOps, ag_ops;
550 time_t startTime, lastTime, curTime, interval;
553 rc = MPI_Init(&argc, &argv);
554 if (rc != MPI_SUCCESS)
555 fatal(myrank, "MPI_Init failed: %d\n", rc);
557 rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
558 if (rc != MPI_SUCCESS)
559 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
561 rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
562 if (rc != MPI_SUCCESS)
563 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
565 process_args(argc, argv);
568 if ((myrank == 0) || debug) {
569 printf("%d: %s starting at %s",
570 myrank, hostname, ctime(&startTime));
573 /* if we're not measuring creation rates then precreate
574 * the files we're operating on. */
575 if ((mode != CREATE) && (mode != MKNOD) && !ignore) {
576 /* create the files in reverse order. When we encounter
577 * a file that already exists, assume the remainder of
578 * the files exist to save time. The timed performance
579 * test scripts make use of this behavior. */
580 for (i = end, j = 0; i >= begin; i -= dirthreads) {
581 sprintf(filename, filefmt, i);
582 fd = open(filename, openflags, 0644);
587 fatal(myrank, "precreate open(%s) error: %s\n",
588 filename, strerror(rc));
593 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
595 rc = MPI_Barrier(MPI_COMM_WORLD);
596 if (rc != MPI_SUCCESS)
597 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
600 if (order == READDIR) {
601 directory = opendir(dir);
602 if (directory == NULL) {
604 fatal(myrank, "opendir(%s) error: %s\n",
609 j = random() % nfiles;
610 dmesg("%d: %s initializing dir offset %u: %s",
611 myrank, hostname, j, ctime(&startTime));
613 for (i = 0; i <= j; i++) {
614 if ((dir_entry = readdir(directory)) == NULL) {
615 fatal(myrank, "could not read entry number %d "
616 "in directory %s.\n", i, dir);
621 dmesg("%d: index %d, filename %s, offset %ld: "
622 "%s initialization complete: %s",
623 myrank, i, dir_entry->d_name, telldir(directory),
624 hostname, ctime(&lastTime));
627 rc = MPI_Barrier(MPI_COMM_WORLD);
628 if (rc != MPI_SUCCESS)
629 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
632 act.sa_handler = sigalrm_handler;
633 (void)sigemptyset(&act.sa_mask);
635 sigaction(SIGALRM, &act, NULL);
639 startTime = lastTime = time(0);
644 for (; begin <= end && !alarm_caught; begin += dirthreads) {
645 sprintf(filename, filefmt, begin);
646 if ((fd = open(filename, openflags, 0644)) < 0) {
647 if (((rc = errno) == EINTR) && alarm_caught)
649 fatal(myrank, "open(%s) error: %s\n",
650 filename, strerror(rc));
657 dmesg("%d: created %d files, last file '%s'.\n",
658 myrank, nops, filename);
660 #ifdef HAVE_MDC_LOOKUP
662 fd = open(dir, O_RDONLY);
664 fatal(myrank, "open(dir == '%s') error: %s\n",
665 dir, strerror(errno));
668 for (; nops < iters && !alarm_caught;) {
669 char *filename = next_file();
670 rc = llapi_file_lookup(fd, filename);
672 if (((rc = errno) == EINTR) && alarm_caught)
674 fatal(myrank, "llapi_file_lookup(%s) "
675 "error: %s\n", filename, strerror(rc));
683 for (; begin <= end && !alarm_caught; begin += dirthreads) {
684 sprintf(filename, filefmt, begin);
685 rc = mknod(filename, S_IFREG| 0644, 0);
687 if (((rc = errno) == EINTR) && alarm_caught)
689 fatal(myrank, "mknod(%s) error: %s\n",
690 filename, strerror(rc));
697 for (; nops < iters && !alarm_caught;) {
699 if ((fd = open(file, openflags, 0644)) < 0) {
700 if (((rc = errno) == EINTR) && alarm_caught)
702 fatal(myrank, "open(%s) error: %s\n",
712 for (; nops < iters && !alarm_caught;) {
713 rc = stat(file = next_file(), &statbuf);
715 if (((rc = errno) == EINTR) && alarm_caught)
717 fatal(myrank, "stat(%s) error: %s\n",
725 for (; begin <= end && !alarm_caught; begin += dirthreads) {
726 sprintf(filename, filefmt, begin);
727 rc = unlink(filename);
729 if (((rc = errno) == EINTR) && alarm_caught)
731 if (((rc = errno) == ENOENT) && ignore)
733 fatal(myrank, "unlink(%s) error: %s\n",
734 filename, strerror(rc));
743 interval = curTime - startTime;
744 rate = (float)(nops);
746 rate /= (float)interval;
748 rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
750 if (rc != MPI_SUCCESS) {
751 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
754 rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_FLOAT, MPI_SUM, 0,
756 if (rc != MPI_SUCCESS) {
757 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
761 printf("Rate: %.2f %ss/sec (total: %d threads %d %ss %lu secs)"
762 "\n", ag_rate, cmd, nthreads, ag_ops, cmd, interval);
766 for (begin = beginsave; begin <= end; begin += dirthreads) {
767 sprintf(filename, filefmt, begin);
768 if ((fd = open(filename, openflags, 0644)) < 0) {
772 fatal(myrank, "recreate open(%s) error: %s\n",
773 filename, strerror(rc));
781 if ((myrank == 0) || debug) {
782 printf("%d: %s finished at %s",
783 myrank, hostname, ctime(&curTime));