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