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 <liblustre.h>
36 #include <lustre/liblustreapi.h> /* for O_LOV_DELAY_CREATE */
38 #define CHECK_COUNT 10000
39 #define DISPLAY_COUNT (CHECK_COUNT * 10)
40 #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 {"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;
131 #define dmesg if (debug) printf
133 #define DISPLAY_PROGRESS() { \
134 if ((++nops % CHECK_COUNT) == 0 && verbose) { \
136 interval = curTime - lastTime; \
137 if (interval > DISPLAY_TIME || nops % DISPLAY_COUNT == 0) { \
138 rate = (float)(nops - lastOps); \
140 rate /= (float)interval; \
141 printf("Rank %d: %.2f %ss/sec %lu secs " \
142 "(total: %d %ss %lu secs)\n", \
143 myrank, rate, cmd, interval, \
144 nops, cmd, curTime - startTime); \
146 lastTime = curTime; \
151 char *usage_msg = "usage: %s\n"
152 " { --create [ --noexcl ] | --lookup | --mknod |\n"
153 " --open | --stat | --unlink [ --recreate ] }\n"
154 " [ --help ] [ --verbose ] [ --debug ]\n"
155 " { [ --begin <num> ] --nfiles <num> }\n"
156 " [ --iters <num> ] [ --time <secs> ]\n"
157 " [ --dirfmt <str> ] [ --ndirs <num> ]\n"
158 " [ --filefmt <str> ] [ --stripes <num> ]\n"
159 " [ --random_order [--seed <num> | --seedfile <file>] ]\n"
160 " [ --readdir_order ]\n";
163 usage(FILE *stream, char *fmt, ...)
169 fprintf(stream, "%s: ", prog);
171 vfprintf(stderr, fmt, ap);
174 fprintf(stream, usage_msg, prog);
178 exit(stream == stderr);
181 /* Print process myrank and message, and exit (i.e. a fatal error) */
183 fatal(int rank, const char *fmt, ...)
185 if (rank == myrank) {
188 fprintf(stderr, "rank %d: ", rank);
190 vfprintf(stderr, fmt, ap);
194 MPI_Abort(MPI_COMM_WORLD, 1);
199 sigalrm_handler(int signum)
204 /* HAVE_LLAPI_FILE_LOOKUP is defined by liblustreapi.h if this function is
205 * defined therein. Otherwise we can do the equivalent operation via ioctl
206 * if we have access to a complete lustre build tree to get the various
207 * definitions - then compile with USE_MDC_LOOKUP defined. */
208 #if defined(HAVE_LLAPI_FILE_LOOKUP)
209 #define HAVE_MDC_LOOKUP
210 #elif defined(USE_MDC_LOOKUP)
212 #include <liblustre.h>
213 #include <linux/lustre_lib.h>
215 int llapi_file_lookup(int dirfd, const char *name)
217 struct obd_ioctl_data data = { 0 };
222 if (dirfd < 0 || name == NULL)
225 data.ioc_version = OBD_IOCTL_VERSION;
226 data.ioc_len = sizeof(data);
227 data.ioc_inlbuf1 = name;
228 data.ioc_inllen1 = strlen(name) + 1;
230 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
232 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
236 return ioctl(fd, IOC_MDC_LOOKUP, buf);
238 #define HAVE_MDC_LOOKUP
242 process_args(int argc, char *argv[])
244 char c, *cp, *endptr;
245 int i, index, offset, tmpend, rc;
252 prog = basename(argv[0]);
253 strcpy(filefmt, "f%d");
254 gethostname(hostname, sizeof(hostname));
256 /* auto create shortOpts rather than maintaining a static string. */
257 for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
263 while ((c = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
266 openflags &= ~(O_CREAT|O_EXCL);
268 #ifdef HAVE_MDC_LOOKUP
275 fatal(0, "Invalid - more than one operation "
277 longOpts[index].name);
280 cmd = (char *)longOpts[index].name;
283 if (mode != CREATE && mode != MKNOD) {
284 usage(stderr, "--noexcl only applies to "
285 "--create or --mknod.\n");
287 openflags &= ~O_EXCL;
290 if (mode != UNLINK) {
291 usage(stderr, "--recreate only makes sense"
297 begin = strtol(optarg, &endptr, 0);
298 if ((*endptr != 0) || (begin < 0)) {
299 fatal(0, "Invalid --start value.\n");
303 iters = strtol(optarg, &endptr, 0);
304 if ((*endptr != 0) || (iters <= 0)) {
305 fatal(0, "Invalid --iters value.\n");
307 if (mode != LOOKUP && mode != OPEN && mode != STAT) {
308 usage(stderr, "--iters only makes sense with "
309 "--lookup, --open, or --stat.\n");
313 seconds = strtol(optarg, &endptr, 0);
314 if ((*endptr != 0) || (seconds <= 0)) {
315 fatal(0, "Invalid --time value.\n");
319 if (strlen(optarg) > (PATH_MAX - 16)) {
320 fatal(0, "--dirfmt too long\n");
325 ndirs = strtol(optarg, &endptr, 0);
326 if ((*endptr != 0) || (ndirs <= 0)) {
327 fatal(0, "Invalid --ndirs value.\n");
329 if ((ndirs > nthreads) &&
330 ((mode == CREATE) || (mode == MKNOD))) {
331 fatal(0, "--ndirs=%d must be less than or "
332 "equal to the number of threads (%d).\n",
337 if (strlen(optarg) > 4080) {
338 fatal(0, "--filefmt too long\n");
341 /* Use %%d where you want the file # in the name. */
342 sprintf(filefmt, optarg, myrank);
345 nfiles = strtol(optarg, &endptr, 0);
346 if ((*endptr != 0) || (nfiles <= 0)) {
347 fatal(0, "Invalid --nfiles value.\n");
351 stripes = strtol(optarg, &endptr, 0);
352 if ((*endptr != 0) || (stripes < 0)) {
353 fatal(0, "Invalid --stripes value.\n");
357 openflags |= O_LOV_DELAY_CREATE;
359 fatal(0, "non-zero --stripes value "
360 "not yet supported.\n");
365 seed = strtoul(optarg, &endptr, 0);
367 fatal(0, "bad --seed option %s\n", optarg);
371 seed_file = fopen(optarg, "r");
373 fatal(myrank, "fopen(%s) error: %s\n",
374 optarg, strerror(errno));
377 for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
383 rc = sscanf(tmp, "%d", &seed);
384 if ((rc != 1) || (seed < 0)) {
385 fatal(myrank, "Invalid seed value '%s' "
386 "at line %d in %s.\n",
390 fatal(myrank, "File '%s' too short. Does not "
391 "contain a seed for thread %d.\n",
399 if (mode != LOOKUP && mode != OPEN && mode != STAT) {
400 fatal(0, "--%s can only be specified with "
401 "--lookup, --open, or --stat.\n",
402 (char *)longOpts[index].name);
414 usage(stderr, "unrecognized option: '%c'.\n", optopt);
419 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
422 if (mode == CREATE || mode == MKNOD || mode == UNLINK) {
426 } else if (nfiles == 0) {
427 usage(stderr, "--nfiles or --time must be specified "
430 } else if (mode == LOOKUP || mode == OPEN || mode == STAT) {
434 } else if (iters == 0) {
435 usage(stderr, "--iters or --time must be specifed "
440 usage(stderr, "--nfiles must be specifed with --%s.\n",
445 int fd = open("/dev/urandom", O_RDONLY);
448 if (read(fd, &seed, sizeof(seed)) <
459 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
460 (order == RANDOM) ? "random_order" : "readdir_order");
462 usage(stderr, "one --create, --mknod, --open, --stat,"
463 #ifdef HAVE_MDC_LOOKUP
466 " or --unlink must be specifed.");
469 /* support for multiple threads in a dir, set begin/end appropriately.*/
470 dirnum = myrank % ndirs;
471 dirthreads = nthreads / ndirs;
472 if (nthreads > (ndirs * dirthreads + dirnum))
475 offset = myrank / ndirs;
477 tmpend = begin + nfiles - 1;
481 end = begin + (nfiles / dirthreads) * dirthreads + offset;
482 if ((end > tmpend) || (end <= 0))
491 dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
492 "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
494 if (dirfmt == NULL) {
497 sprintf(dir, dirfmt, dirnum);
499 sprintf(mkdir_cmd, "/bin/mkdir -p %s", dir);
500 #ifdef _LIGHTWEIGHT_KERNEL
501 printf("NOTICE: not running system(%s)\n", mkdir_cmd);
503 rc = system(mkdir_cmd);
505 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
511 fatal(myrank, "unable to chdir to '%s'.\n", dir);
516 static inline char *next_file()
518 if (order == RANDOM) {
519 sprintf(filename, filefmt, random() % nfiles);
525 dir_entry = readdir(directory);
526 if (dir_entry == NULL) {
527 rewinddir(directory);
528 while ((dir_entry = readdir(directory)) != NULL) {
529 if (dir_entry->d_name[0] != '.')
530 return(dir_entry->d_name);
533 fatal(myrank, "unable to read directory %s (%s).\n",
534 dir, strerror(errno));
537 return(dir_entry->d_name);
541 main(int argc, char *argv[])
543 int i, j, fd, rc, nops, lastOps, ag_ops;
545 time_t startTime, lastTime, curTime, interval;
548 rc = MPI_Init(&argc, &argv);
549 if (rc != MPI_SUCCESS)
550 fatal(myrank, "MPI_Init failed: %d\n", rc);
552 rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
553 if (rc != MPI_SUCCESS)
554 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
556 rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
557 if (rc != MPI_SUCCESS)
558 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
560 process_args(argc, argv);
563 if ((myrank == 0) || debug) {
564 printf("%d: %s starting at %s",
565 myrank, hostname, ctime(&startTime));
568 /* if we're not measuring creation rates then precreate
569 * the files we're operating on. */
570 if ((mode != CREATE) && (mode != MKNOD)) {
571 /* create the files in reverse order. When we encounter
572 * a file that already exists, assume the remainder of
573 * the files exist to save time. The timed performance
574 * test scripts make use of this behavior. */
575 for (i = end, j = 0; i >= begin; i -= dirthreads) {
576 sprintf(filename, filefmt, i);
577 fd = open(filename, openflags, 0644);
582 fatal(myrank, "precreate open(%s) error: %s\n",
583 filename, strerror(rc));
588 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
590 rc = MPI_Barrier(MPI_COMM_WORLD);
591 if (rc != MPI_SUCCESS)
592 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
595 if (order == READDIR) {
596 directory = opendir(dir);
597 if (directory == NULL) {
599 fatal(myrank, "opendir(%s) error: %s\n",
604 j = random() % nfiles;
605 dmesg("%d: %s initializing dir offset %u: %s",
606 myrank, hostname, j, ctime(&startTime));
608 for (i = 0; i <= j; i++) {
609 if ((dir_entry = readdir(directory)) == NULL) {
610 fatal(myrank, "could not read entry number %d "
611 "in directory %s.\n", i, dir);
616 dmesg("%d: index %d, filename %s, offset %ld: "
617 "%s initialization complete: %s",
618 myrank, i, dir_entry->d_name, telldir(directory),
619 hostname, ctime(&lastTime));
622 rc = MPI_Barrier(MPI_COMM_WORLD);
623 if (rc != MPI_SUCCESS)
624 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
627 act.sa_handler = sigalrm_handler;
628 (void)sigemptyset(&act.sa_mask);
630 sigaction(SIGALRM, &act, NULL);
634 startTime = lastTime = time(0);
639 for (; begin <= end && !alarm_caught; begin += dirthreads) {
640 sprintf(filename, filefmt, begin);
641 if ((fd = open(filename, openflags, 0644)) < 0) {
642 if (((rc = errno) == EINTR) && alarm_caught)
644 fatal(myrank, "open(%s) error: %s\n",
645 filename, strerror(rc));
652 dmesg("%d: created %d files, last file '%s'.\n",
653 myrank, nops, filename);
655 #ifdef HAVE_MDC_LOOKUP
657 fd = open(dir, O_RDONLY);
659 fatal(myrank, "open(dir == '%s') error: %s\n",
660 dir, strerror(errno));
663 for (; nops < iters && !alarm_caught;) {
664 char *filename = next_file();
665 rc = llapi_file_lookup(fd, filename);
667 if (((rc = errno) == EINTR) && alarm_caught)
669 fatal(myrank, "llapi_file_lookup(%s) "
670 "error: %s\n", filename, strerror(rc));
678 for (; begin <= end && !alarm_caught; begin += dirthreads) {
679 sprintf(filename, filefmt, begin);
680 rc = mknod(filename, S_IFREG| 0644, 0);
682 if (((rc = errno) == EINTR) && alarm_caught)
684 fatal(myrank, "mknod(%s) error: %s\n",
685 filename, strerror(rc));
692 for (; nops < iters && !alarm_caught;) {
694 if ((fd = open(file, openflags, 0644)) < 0) {
695 if (((rc = errno) == EINTR) && alarm_caught)
697 fatal(myrank, "open(%s) error: %s\n",
707 for (; nops < iters && !alarm_caught;) {
708 rc = stat(file = next_file(), &statbuf);
710 if (((rc = errno) == EINTR) && alarm_caught)
712 fatal(myrank, "stat(%s) error: %s\n",
720 for (; begin <= end && !alarm_caught; begin += dirthreads) {
721 sprintf(filename, filefmt, begin);
722 rc = unlink(filename);
724 if (((rc = errno) == EINTR) && alarm_caught)
726 fatal(myrank, "unlink(%s) error: %s\n",
727 filename, strerror(rc));
736 interval = curTime - startTime;
737 rate = (float)(nops);
739 rate /= (float)interval;
741 rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
743 if (rc != MPI_SUCCESS) {
744 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
747 rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_FLOAT, MPI_SUM, 0,
749 if (rc != MPI_SUCCESS) {
750 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
754 printf("Rate: %.2f %ss/sec (total: %d threads %d %ss %lu secs)"
755 "\n", ag_rate, cmd, nthreads, ag_ops, cmd, interval);
759 for (begin = beginsave; begin <= end; begin += dirthreads) {
760 sprintf(filename, filefmt, begin);
761 if ((fd = open(filename, openflags, 0644)) < 0) {
765 fatal(myrank, "recreate open(%s) error: %s\n",
766 filename, strerror(rc));
774 if ((myrank == 0) || debug) {
775 printf("%d: %s finished at %s",
776 myrank, hostname, ctime(&curTime));