Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / tests / mpi / mdsrate.c
1 /*
2  * 2003, Copyright, Hewlett-Packard Development Compnay, LP.
3  *
4  * Developed under the sponsorship of the U.S. Government
5  *     under Subcontract No. B514193
6  */
7
8 /*
9  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
10  * Use is subject to license terms.
11  */
12
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <time.h>
19 #include <limits.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <signal.h>
27 #include <sys/ioctl.h>
28 #include <dirent.h>
29
30 #include "mpi.h"
31
32 /* lustre */
33 #include <liblustre.h>
34 #include <lustre/liblustreapi.h>        /* for O_LOV_DELAY_CREATE */
35
36 #define CHECK_COUNT 10000
37 #define DISPLAY_COUNT (CHECK_COUNT * 10)
38 #define DISPLAY_TIME 100
39
40 enum {
41         CREATE   = 'c',
42         LOOKUP   = 'l',
43         MKNOD    = 'm',
44         OPEN     = 'o',
45         STAT     = 's',
46         UNLINK   = 'u',
47         BEGIN    = 'b',
48         ITERS    = 'i',
49         TIME     = 't',
50         DIRFMT   = 'd',
51         NDIRS    = 'D',
52         FILEFMT  = 'f',
53         NFILES   = 'F',
54         NOEXCL   = 'X',
55         STRIPES  = 'S',
56         SEED     = 'r',
57         SEEDFILE = 'R',
58         RANDOM   = 'A',
59         READDIR  = 'B',
60         RECREATE = 'C',
61         IGNORE   = 'E',
62         VERBOSE  = 'V',
63         DEBUG    = 'v',
64         HELP     = 'h',
65 };
66
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       },
92         { 0,              0, NULL, 0          }
93 };
94
95 int foo1, foo2;
96
97 char   shortOpts[128];
98 int    myrank = -1;
99 int    nthreads = -1;
100 char * prog;
101 char   hostname[512] = "unknown";
102 char   mode;
103 char * cmd;
104 int    openflags = O_RDWR|O_CREAT|O_EXCL;
105 int    ndirs = 1;
106 char * dirfmt;
107 char   dir[PATH_MAX];
108 char   mkdir_cmd[PATH_MAX+14];
109 int    dirthreads;
110 int    dirnum;
111 DIR *  directory;
112 struct dirent *dir_entry;
113 int    nfiles;
114 char   filefmt[PATH_MAX];
115 char   filename[PATH_MAX];
116 int    stripes = -1;
117 int    begin;
118 int    beginsave;
119 int    end;
120 int    iters;
121 int    seconds;
122 int    alarm_caught;
123 struct sigaction act;
124 int    order = RANDOM;
125 int    seed;
126 int    recreate;
127 int    ignore;
128 int    verbose;
129 int    debug;
130 struct stat statbuf;
131
132 #define dmesg if (debug) printf
133
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);             \
144                         lastOps = nops;                                     \
145                         lastTime = curTime;                                 \
146                 }                                                           \
147         }                                                                   \
148 }
149
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";
160
161 static void
162 usage(FILE *stream, char *fmt, ...)
163 {
164         if (myrank == 0) {
165                 if (fmt != NULL) {
166                         va_list       ap;
167
168                         fprintf(stream, "%s: ", prog);
169                         va_start(ap, fmt);
170                         vfprintf(stderr, fmt, ap);
171                         va_end(ap);
172                 }
173                 fprintf(stream, usage_msg, prog);
174         }
175
176         MPI_Finalize();
177         exit(stream == stderr);
178 }
179
180 /* Print process myrank and message, and exit (i.e. a fatal error) */
181 static int
182 fatal(int rank, const char *fmt, ...)
183 {
184         if (rank == myrank) {
185                 va_list       ap;
186
187                 fprintf(stderr, "rank %d: ", rank);
188                 va_start(ap, fmt);
189                 vfprintf(stderr, fmt, ap);
190                 va_end(ap);
191         }
192
193         MPI_Abort(MPI_COMM_WORLD, 1);
194         exit(1);
195 }
196
197 static void
198 sigalrm_handler(int signum)
199 {
200         alarm_caught++;
201 }
202
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)
210 #include <config.h>
211 #include <liblustre.h>
212 #include <linux/lustre_lib.h>
213
214 int llapi_file_lookup(int dirfd, const char *name)
215 {
216         struct obd_ioctl_data data = { 0 };
217         char rawbuf[8192];
218         char *buf = rawbuf;
219         int rc;
220
221         if (dirfd < 0 || name == NULL)
222                 return -EINVAL;
223
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;
228
229         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
230         if (rc) {
231                 fatal(myrank, "ioctl_pack failed: rc = %d\n", rc);
232                 return rc;
233         }
234
235         return ioctl(fd, IOC_MDC_LOOKUP, buf);
236 }
237 #define HAVE_MDC_LOOKUP
238 #endif
239
240 static void
241 process_args(int argc, char *argv[])
242 {
243         char   *cp, *endptr;
244         int    i, index, offset, tmpend, rc;
245         char   tmp[16];
246         FILE * seed_file;
247         struct option *opt;
248
249         setbuf(stdout, 0);
250         setbuf(stderr, 0);
251         prog = basename(argv[0]);
252         strcpy(filefmt, "f%d");
253         gethostname(hostname, sizeof(hostname));
254
255         /* auto create shortOpts rather than maintaining a static string. */
256         for (opt = longOpts, cp = shortOpts; opt->name != NULL; opt++, cp++) {
257                 *cp = opt->val;
258                 if (opt->has_arg)
259                         *++cp = ':';
260         }
261
262         while ((rc = getopt_long(argc,argv, shortOpts, longOpts,&index)) != -1) {
263                 switch (rc) {
264                 case OPEN:
265                         openflags &= ~(O_CREAT|O_EXCL);
266                 case CREATE:
267 #ifdef HAVE_MDC_LOOKUP
268                 case LOOKUP:
269 #endif
270                 case MKNOD:
271                 case STAT:
272                 case UNLINK:
273                         if (cmd != NULL) {
274                                 fatal(0, "Invalid - more than one operation "
275                                            "specified: --%s\n",
276                                         longOpts[index].name);
277                         }
278                         mode = rc;
279                         cmd = (char *)longOpts[index].name;
280                         break;
281                 case NOEXCL:
282                         if (mode != CREATE && mode != MKNOD) {
283                                 usage(stderr, "--noexcl only applies to "
284                                               "--create or --mknod.\n");
285                         }
286                         openflags &= ~O_EXCL;
287                         break;
288                 case RECREATE:
289                         if (mode != UNLINK) {
290                                 usage(stderr, "--recreate only makes sense"
291                                               "with --unlink.\n");
292                         }
293                         recreate++;
294                         break;
295                 case BEGIN:
296                         begin = strtol(optarg, &endptr, 0);
297                         if ((*endptr != 0) || (begin < 0)) {
298                                 fatal(0, "Invalid --start value.\n");
299                         }
300                         break;
301                 case ITERS:
302                         iters = strtol(optarg, &endptr, 0);
303                         if ((*endptr != 0) || (iters <= 0)) {
304                                 fatal(0, "Invalid --iters value.\n");
305                         }
306                         if (mode != LOOKUP && mode != OPEN) {
307                                 usage(stderr, "--iters only makes sense with "
308                                               "--lookup or --open.\n");
309                         }
310                         break;
311                 case TIME:
312                         seconds = strtol(optarg, &endptr, 0);
313                         if ((*endptr != 0) || (seconds <= 0)) {
314                                 fatal(0, "Invalid --time value.\n");
315                         }
316                         break;
317                 case DIRFMT:
318                         if (strlen(optarg) > (PATH_MAX - 16)) {
319                                 fatal(0, "--dirfmt too long\n");
320                         }
321                         dirfmt = optarg;
322                         break;
323                 case NDIRS:
324                         ndirs = strtol(optarg, &endptr, 0);
325                         if ((*endptr != 0) || (ndirs <= 0)) {
326                                 fatal(0, "Invalid --ndirs value.\n");
327                         }
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",
332                                       ndirs, nthreads);
333                         }
334                         break;
335                 case FILEFMT:
336                         if (strlen(optarg) > 4080) {
337                                 fatal(0, "--filefmt too long\n");
338                         }
339
340                         /* Use %%d where you want the file # in the name. */
341                         sprintf(filefmt, optarg, myrank);
342                         break;
343                 case NFILES:
344                         nfiles = strtol(optarg, &endptr, 0);
345                         if ((*endptr != 0) || (nfiles <= 0)) {
346                                 fatal(0, "Invalid --nfiles value.\n");
347                         }
348                         break;
349                 case STRIPES:
350                         stripes = strtol(optarg, &endptr, 0);
351                         if ((*endptr != 0) || (stripes < 0)) {
352                                 fatal(0, "Invalid --stripes value.\n");
353                         }
354
355                         if (stripes == 0) {
356                                 openflags |= O_LOV_DELAY_CREATE;
357                         } else {
358                                 fatal(0, "non-zero --stripes value "
359                                          "not yet supported.\n");
360                         }
361
362                         break;
363                 case SEED:
364                         seed = strtoul(optarg, &endptr, 0);
365                         if (*endptr) {
366                                 fatal(0, "bad --seed option %s\n", optarg);
367                         }
368                         break;
369                 case SEEDFILE:
370                         seed_file = fopen(optarg, "r");
371                         if (!seed_file) {
372                               fatal(myrank, "fopen(%s) error: %s\n",
373                                       optarg, strerror(errno));
374                         }
375
376                         for (i = -1; fgets(tmp, 16, seed_file) != NULL;) {
377                                 if (++i == myrank)
378                                         break;
379                         }
380
381                         if (i == myrank) {
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",
386                                               tmp, i, optarg);
387                                 }
388                         } else {
389                                 fatal(myrank, "File '%s' too short. Does not "
390                                       "contain a seed for thread %d.\n",
391                                       optarg, myrank);
392                         }
393
394                         fclose(seed_file);
395                         break;
396                 case RANDOM:
397                 case READDIR:
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);
402                         }
403                         order = rc;
404                         break;
405                 case IGNORE:
406                         ++ignore;
407                         break;
408                 case DEBUG:
409                         ++debug;
410                 case VERBOSE:
411                         ++verbose;
412                         break;
413                 case HELP:
414                         usage(stdout, NULL);
415                 default:
416                         usage(stderr, "unrecognized option: '%c'.\n", optopt);
417                 }
418         }
419
420         if (optind < argc) {
421                 usage(stderr, "too many arguments %d >= %d.\n", optind, argc);
422         }
423
424         if (mode == CREATE || mode == MKNOD || mode == UNLINK || mode == STAT) {
425                 if (seconds != 0) {
426                         if (nfiles == 0)
427                                 nfiles = INT_MAX;
428                 } else if (nfiles == 0) {
429                         usage(stderr, "--nfiles or --time must be specified "
430                                       "with %s.\n", cmd);
431                 }
432         } else if (mode == LOOKUP || mode == OPEN) {
433                 if (seconds != 0) {
434                         if (iters == 0)
435                                 iters = INT_MAX;
436                 } else if (iters == 0) {
437                         usage(stderr, "--iters or --time must be specifed "
438                                       "with %s.\n", cmd);
439                 }
440
441                 if (nfiles == 0) {
442                         usage(stderr, "--nfiles must be specifed with --%s.\n",
443                               cmd);
444                 }
445
446                 if (seed == 0) {
447                         int fd = open("/dev/urandom", O_RDONLY);
448
449                         if (fd >= 0) {
450                                 if (read(fd, &seed, sizeof(seed)) <
451                                     sizeof(seed))
452                                         seed = time(0);
453                                 close(fd);
454                         } else {
455                                 seed = time(0);
456                         }
457                 }
458
459                 srand(seed);
460
461                 dmesg("%s: rank %d seed %d (%s).\n", prog, myrank, seed,
462                       (order == RANDOM) ? "random_order" : "readdir_order");
463         } else {
464                 usage(stderr, "one --create, --mknod, --open, --stat,"
465 #ifdef HAVE_MDC_LOOKUP
466                       " --lookup,"
467 #endif
468                       " or --unlink must be specifed.");
469         }
470
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))
475                 ++dirthreads;
476
477         offset = myrank / ndirs;
478
479         tmpend = begin + nfiles - 1;
480         if (tmpend <= 0)
481                 tmpend = INT_MAX;
482
483         end = begin + (nfiles / dirthreads) * dirthreads + offset;
484         if ((end > tmpend) || (end <= 0))
485                 end -= dirthreads;
486
487         begin += offset;
488         if (begin < 0)
489                 begin = INT_MAX;
490
491         beginsave = begin;
492
493         dmesg("%d: iters %d nfiles %d time %d begin %d end %d dirthreads %d."
494               "\n", myrank, iters, nfiles, seconds, begin, end, dirthreads);
495
496         if (dirfmt == NULL) {
497                 strcpy(dir, ".");
498         } else {
499                 sprintf(dir, dirfmt, dirnum);
500
501                 sprintf(mkdir_cmd, "/bin/mkdir -p %s", dir);
502                 #ifdef _LIGHTWEIGHT_KERNEL
503                         printf("NOTICE: not running system(%s)\n", mkdir_cmd);
504                 #else
505                         rc = system(mkdir_cmd);
506                         if (rc) {
507                                 fatal(myrank, "'%s' failed.\n", mkdir_cmd);
508                         }
509                 #endif
510
511                 rc = chdir(dir);
512                 if (rc) {
513                         fatal(myrank, "unable to chdir to '%s'.\n", dir);
514                 }
515         }
516 }
517
518 static inline char *next_file()
519 {
520         if (order == RANDOM) {
521                 sprintf(filename, filefmt, random() % nfiles);
522                 return(filename);
523         }
524
525         /* readdir order */
526
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);
533                 }
534
535                 fatal(myrank, "unable to read directory %s (%s).\n",
536                       dir, strerror(errno));
537         }
538
539         return(dir_entry->d_name);
540 }
541
542 int
543 main(int argc, char *argv[])
544 {
545         int    i, j, fd, rc, nops, lastOps;
546         int ag_ops = 0;
547         double ag_interval = 0;
548         double ag_rate = 0;
549         double rate, avg_rate, effective_rate;
550         double startTime, curTime, lastTime, interval;
551         time_t timestamp;
552         char * file;
553
554         rc = MPI_Init(&argc, &argv);
555         if (rc != MPI_SUCCESS)
556                 fatal(myrank, "MPI_Init failed: %d\n", rc);
557
558         rc = MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
559         if (rc != MPI_SUCCESS)
560                 fatal(myrank, "MPI_Comm_size failed: %d\n", rc);
561
562         rc = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
563         if (rc != MPI_SUCCESS)
564                 fatal(myrank, "MPI_Comm_rank failed: %d\n", rc);
565
566         process_args(argc, argv);
567
568         timestamp = time(0);
569         if ((myrank == 0) || debug) {
570                 printf("%d: %s starting at %s",
571                        myrank, hostname, ctime(&timestamp));
572         }
573
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                 /* create the files in reverse order. When we encounter
578                  * a file that already exists, assume the remainder of 
579                  * the files exist to save time. The timed performance
580                  * test scripts make use of this behavior. */
581                 for (i = end, j = 0; i >= begin; i -= dirthreads) {
582                         sprintf(filename, filefmt, i);
583                         fd = open(filename, openflags, 0644);
584                         if (fd < 0) {
585                                 if (errno == EEXIST)
586                                         break;
587                                 rc = errno;
588                                 fatal(myrank, "precreate open(%s) error: %s\n",
589                                       filename, strerror(rc));
590                         }
591                         j++;
592                         close(fd);
593                 }
594                 dmesg("%d: %s pre-created %d files.\n",myrank,hostname,j);
595
596                 rc = MPI_Barrier(MPI_COMM_WORLD);
597                 if (rc != MPI_SUCCESS)
598                         fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
599         }
600
601         if (order == READDIR) {
602                 directory = opendir(dir);
603                 if (directory == NULL) {
604                         rc = errno;
605                         fatal(myrank, "opendir(%s) error: %s\n",
606                               dir, strerror(rc));
607                 }
608
609                 timestamp = time(0);
610                 j = random() % nfiles;
611                 dmesg("%d: %s initializing dir offset %u: %s",
612                       myrank, hostname, j, ctime(&timestamp));
613
614                 for (i = 0; i <= j; i++) {
615                         if ((dir_entry = readdir(directory)) == NULL) {
616                                 fatal(myrank, "could not read entry number %d "
617                                       "in directory %s.\n", i, dir);
618                         }
619                 }
620
621                 timestamp = time(0);
622                 dmesg("%d: index %d, filename %s, offset %ld: "
623                       "%s initialization complete: %s",
624                       myrank, i, dir_entry->d_name, telldir(directory),
625                       hostname, ctime(&timestamp));
626         }
627
628         if (seconds) {
629                 act.sa_handler = sigalrm_handler;
630                 (void)sigemptyset(&act.sa_mask);
631                 act.sa_flags = 0;
632                 sigaction(SIGALRM, &act, NULL);
633                 alarm(seconds);
634         }
635
636         rc = MPI_Barrier(MPI_COMM_WORLD);
637         if (rc != MPI_SUCCESS)
638                 fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
639
640         startTime = lastTime = MPI_Wtime();
641         nops = lastOps = 0;
642
643         switch (mode) {
644         case CREATE:
645                 for (; begin <= end && !alarm_caught; begin += dirthreads) {
646                         sprintf(filename, filefmt, begin);
647                         if ((fd = open(filename, openflags, 0644)) < 0) {
648                                 if (((rc = errno) == EINTR) && alarm_caught)
649                                         break;
650                                 fatal(myrank, "open(%s) error: %s\n",
651                                       filename, strerror(rc));
652                         }
653
654                         close(fd);
655                         nops++;
656                         DISPLAY_PROGRESS();
657                 }
658
659                 dmesg("%d: created %d files, last file '%s'.\n",
660                       myrank, nops, filename);
661                 break;
662 #ifdef HAVE_MDC_LOOKUP
663         case LOOKUP:
664                 fd = open(dir, O_RDONLY);
665                 if (fd < 0) {
666                         fatal(myrank, "open(dir == '%s') error: %s\n",
667                               dir, strerror(errno));
668                 }
669
670                 for (; nops < iters && !alarm_caught;) {
671                         char *filename = next_file();
672                         rc = llapi_file_lookup(fd, filename);
673                         if (rc < 0) {
674                                 if (((rc = errno) == EINTR) && alarm_caught)
675                                         break;
676                                 fatal(myrank, "llapi_file_lookup(%s) "
677                                       "error: %s\n", filename, strerror(rc));
678                         }
679
680                         nops++;
681                         DISPLAY_PROGRESS();
682                 }
683                 break;
684 #endif
685         case MKNOD:
686                 for (; begin <= end && !alarm_caught; begin += dirthreads) {
687                         sprintf(filename, filefmt, begin);
688                         rc = mknod(filename, S_IFREG| 0644, 0);
689                         if (rc) {
690                                 if (((rc = errno) == EINTR) && alarm_caught)
691                                         break;
692                                 fatal(myrank, "mknod(%s) error: %s\n",
693                                       filename, strerror(rc));
694                         }
695
696                         nops++;
697                         DISPLAY_PROGRESS();
698                 }
699                 break;
700         case OPEN:
701                 for (; nops < iters && !alarm_caught;) {
702                         file = next_file();
703                         if ((fd = open(file, openflags, 0644)) < 0) {
704                                 if (((rc = errno) == EINTR) && alarm_caught)
705                                         break;
706                                 fatal(myrank, "open(%s) error: %s\n",
707                                       file, strerror(rc));
708                         }
709
710                         close(fd);
711
712                         nops++;
713                         DISPLAY_PROGRESS();
714                 }
715                 break;
716         case STAT:
717                 for (; begin <= end && !alarm_caught; begin += dirthreads) {
718                         sprintf(filename, filefmt, begin);
719                         rc = stat(filename, &statbuf);
720                         if (rc) {
721                                 if (((rc = errno) == EINTR) && alarm_caught)
722                                         break;
723                                 if (((rc = errno) == ENOENT) && ignore)
724                                         continue;
725                                 fatal(myrank, "stat(%s) error: %s\n",
726                                       filename, strerror(rc));
727                         }
728
729                         nops++;
730                         DISPLAY_PROGRESS();
731                 }
732                 break;
733         case UNLINK:
734                 for (; begin <= end && !alarm_caught; begin += dirthreads) {
735                         sprintf(filename, filefmt, begin);
736                         rc = unlink(filename);
737                         if (rc) {
738                                 if (((rc = errno) == EINTR) && alarm_caught)
739                                         break;
740                                 if (((rc = errno) == ENOENT) && ignore)
741                                         continue;
742                                 fatal(myrank, "unlink(%s) error: %s\n",
743                                       filename, strerror(rc));
744                         }
745
746                         nops++;
747                         DISPLAY_PROGRESS();
748                 }
749                 break;
750         }
751
752         rc = MPI_Barrier(MPI_COMM_WORLD);
753         if (rc != MPI_SUCCESS)
754                fatal(myrank, "prep MPI_Barrier failed: %d\n", rc);
755         curTime = MPI_Wtime();
756         interval = curTime - startTime;
757         rate = (double) (nops) / interval;
758
759         rc = MPI_Reduce(&nops, &ag_ops, 1, MPI_INT, MPI_SUM, 0,
760                         MPI_COMM_WORLD);
761         if (rc != MPI_SUCCESS) {
762                 fatal(myrank, "Failure in MPI_Reduce of total ops.\n");
763         }
764
765         rc = MPI_Reduce(&interval, &ag_interval, 1, MPI_DOUBLE, MPI_SUM, 0,
766                         MPI_COMM_WORLD);
767         if (rc != MPI_SUCCESS) {
768                 fatal(myrank, "Failure in MPI_Reduce of total interval.\n");
769         }
770
771         rc = MPI_Reduce(&rate, &ag_rate, 1, MPI_DOUBLE, MPI_SUM, 0,
772                         MPI_COMM_WORLD);
773         if (rc != MPI_SUCCESS) {
774                 fatal(myrank, "Failure in MPI_Reduce of aggregated rate.\n");
775         }
776
777         if (myrank == 0) {
778
779                 curTime = MPI_Wtime();
780                 interval = curTime - startTime;
781                 effective_rate = (double) ag_ops / interval;
782                 avg_rate = (double) ag_ops / ag_interval;
783
784                 printf("Rate: %.2f eff %.2f aggr %.2f avg client %ss/sec "
785                        "(total: %d threads %d %ss %d dirs %d threads/dir %.2f secs)\n",
786                        effective_rate, ag_rate, avg_rate, cmd, nthreads, ag_ops,
787                        cmd, ndirs, dirthreads, interval);
788         }
789
790         if (recreate) {
791                 for (begin = beginsave; begin <= end; begin += dirthreads) {
792                         sprintf(filename, filefmt, begin);
793                         if ((fd = open(filename, openflags, 0644)) < 0) {
794                                 rc = errno;
795                                 if (rc == EEXIST)
796                                         break;
797                                 fatal(myrank, "recreate open(%s) error: %s\n",
798                                       filename, strerror(rc));
799                         }
800
801                         close(fd);
802                 }
803         }
804
805         timestamp = time(0);
806         if ((myrank == 0) || debug) {
807                 printf("%d: %s finished at %s",
808                        myrank, hostname, ctime(&timestamp));
809         }
810
811         MPI_Finalize();
812         return(0);
813 }