Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow mpich3-tests to run with valgrind
[simgrid.git] / teshsuite / smpi / mpich3-test / runtests
1 #! /usr/bin/env perl
2 # -*- Mode: perl; -*-
3 #
4 # This script is the beginnings of a script to run a sequence of test
5 # programs.  See the MPICH document for a description of the test
6 # strategy and requirements.
7 #
8 # Description
9 #   Tests are controlled by a file listing test programs; if the file is
10 #   a directory, then all of the programs in the directory and subdirectories
11 #   are run
12 #
13 #   To run a test, the following steps are executed
14 #   Build the executable:
15 #      make programname
16 #   Run the executable
17 #      mpiexec -n <np> ./programname >out 2>err
18 #   Check the return code (non zero is failure)
19 #   Check the stderr output (non empty is failure)
20 #   Check the stdout output (No Errors or Test passed are the only valid
21 #      output)
22 #   Remove executable, out, err files
23 #
24 # The format of a list file is
25 # programname number-of-processes
26 # If number-of-processes is missing, $np_default is used (this is 2 but can
27 # be overridden with -np=new-value)
28 #
29 # Special feature:
30 # Because these tests can take a long time to run, there is an
31 # option to cause the tests to stop is a "stopfile" is found.
32 # The stopfile can be created by a separate, watchdog process, to ensure that
33 # tests end at a certain time.
34 # The name of this file is (by default) .stoptest
35 # in the  top-level run directory.  The environment variable
36 #    MPITEST_STOPTEST
37 # can specify a different file name.
38 #
39 # Import the mkpath command
40 use File::Path;
41
42 # Global variables
43 $MPIMajorVersion = "1";
44 $MPIMinorVersion = "1";
45 $mpiexec = "smpirun";    # Name of mpiexec program (including path, if necessary)
46 $testIsStrict = "true";
47 $MPIhasMPIX   = "no";
48 $np_arg  = "-np";         # Name of argument to specify the number of processes
49 $err_count = 0;          # Number of programs that failed.
50 $total_run = 0;          # Number of programs tested
51 $total_seen = 0;         # Number of programs considered for testing
52 $np_default = 2;         # Default number of processes to use
53 $np_max     = -1;        # Maximum number of processes to use (overrides any
54                          # value in the test list files.  -1 is Infinity
55 $defaultTimeLimit = 180; # default timeout
56
57 $srcdir = ".";           # Used to set the source dir for testlist files
58
59 $curdir = ".";           # used to track the relative current directory
60
61 # Output forms
62 $xmloutput = 0;          # Set to true to get xml output (also specify file)
63 $closeXMLOutput = 1;     # Set to false to leave XML output file open to
64                          # accept additional data
65 $verbose = 1;            # Set to true to get more output
66 $showProgress = 0;       # Set to true to get a "." with each run program.
67 $newline = "\r\n";       # Set to \r\n for Windows-friendly, \n for Unix only
68 $batchRun = 0;           # Set to true to batch the execution of the tests
69                          # (i.e., run them together, then test output,
70                          # rather than build/run/check for each test)
71 $testCount = 0;          # Used with batchRun to count tests.
72 $batrundir = ".";        # Set to the directory into which to run the examples
73
74 $execarg="";
75 $wrapparg="";
76 # TAP (Test Anything Protocol) output
77 my $tapoutput = 0;
78 my $tapfile = '';
79 my $tapfullfile = '';
80
81 $debug = 1;
82
83 $depth = 0;              # This is used to manage multiple open list files
84
85 # Build flags
86 $remove_this_pgm = 0;
87 $clean_pgms      = 0;
88
89 my $program_wrapper = '';
90
91 #---------------------------------------------------------------------------
92 # Get some arguments from the environment
93 #   Currently, only the following are understood:
94 #   VERBOSE
95 #   RUNTESTS_VERBOSE  (an alias for VERBOSE in case you want to
96 #                      reserve VERBOSE)
97 #   RUNTESTS_SHOWPROGRESS
98 #   MPITEST_STOPTEST
99 #   MPITEST_TIMEOUT
100 #   MPITEST_PROGRAM_WRAPPER (Value is added after -np but before test
101 #                            executable.  Tools like valgrind may be inserted
102 #                            this way.)
103 #---------------------------------------------------------------------------
104 if ( defined($ENV{"VERBOSE"}) || defined($ENV{"V"}) || defined($ENV{"RUNTESTS_VERBOSE"}) ) {
105     $verbose = 1;
106 }
107 if ( defined($ENV{"RUNTESTS_SHOWPROGRESS"} ) ) {
108     $showProgress = 1;
109 }
110 if (defined($ENV{"MPITEST_STOPTEST"})) {
111     $stopfile = $ENV{"MPITEST_STOPTEST"};
112 }
113 else {
114     $stopfile = `pwd` . "/.stoptest";
115     $stopfile =~ s/\r*\n*//g;    # Remove any newlines (from pwd)
116 }
117
118 if (defined($ENV{"MPITEST_TIMEOUT"})) {
119     $defaultTimeLimit = $ENV{"MPITEST_TIMEOUT"};
120 }
121
122 # Define this to leave the XML output file open to receive additional data
123 if (defined($ENV{'NOXMLCLOSE'}) && $ENV{'NOXMLCLOSE'} eq 'YES') {
124     $closeXMLOutput = 0;
125 }
126
127 if (defined($ENV{'MPITEST_PROGRAM_WRAPPER'})) {
128     $program_wrapper = $ENV{'MPITEST_PROGRAM_WRAPPER'};
129 }
130
131 if (defined($ENV{'MPITEST_BATCH'})) {
132     if ($ENV{'MPITEST_BATCH'} eq 'YES' || $ENV{'MPITEST_BATCH'} eq 'yes') {
133         $batchRun = 1;
134     } elsif ($ENV{'MPITEST_BATCH'} eq 'NO' || $ENV{'MPITEST_BATCH'} eq 'no') {
135         $batchRun = 0;
136     }
137     else {
138         print STDERR "Unrecognized value for MPITEST_BATCH = $ENV{'MPITEST_BATCH'}\n";
139     }
140 }
141 if (defined($ENV{'MPITEST_BATCHDIR'})) {
142     $batrundir = $ENV{'MPITEST_BATCHDIR'};
143 }
144
145 #---------------------------------------------------------------------------
146 # Process arguments and override any defaults
147 #---------------------------------------------------------------------------
148 foreach $_ (@ARGV) {
149     if (/--?mpiexec=(.*)/) {
150         # Use mpiexec as given - it may be in the path, and
151         # we don't want to bother to try and find it.
152         $mpiexec = $1;
153     }
154     elsif (/--?np=(.*)/)   { $np_default = $1; }
155     elsif (/--?maxnp=(.*)/) { $np_max = $1; }
156     elsif (/--?tests=(.*)/) { $listfiles = $1; }
157     elsif (/--?srcdir=(.*)/) { $srcdir = $1;
158         $mpiexec="$mpiexec  -platform ${srcdir}/../../../../examples/platforms/small_platform_with_routers.xml -hostfile ${srcdir}/../../hostfile_coll --log=root.thr:critical --cfg=smpi/running_power:1e9  --cfg=smpi/async_small_thresh:65536"; }
159     elsif (/--?verbose/) { $verbose = 1; }
160     elsif (/--?showprogress/) { $showProgress = 1; }
161     elsif (/--?debug/) { $debug = 1; }
162     elsif (/--?batch/) { $batchRun = 1; }
163     elsif (/--?batchdir=(.*)/) { $batrundir = $1; }
164     elsif (/--?timeoutarg=(.*)/) { $timeoutArgPattern = $1; }
165     elsif (/--?execarg=(.*)/) { $execarg = "$execarg $1"; }
166     elsif (/--?setenv/) {  }
167     elsif (/VALGRIND_COMMAND=(.*)/) { 
168         $valgrind = $1; }
169     elsif (/VALGRIND_OPTIONS=(.*)/) {
170          $wrapparg = "-wrapper \"$valgrind $1\""; }
171     elsif (/--?xmlfile=(.*)/) {
172         $xmlfile   = $1;
173         if (! ($xmlfile =~ /^\//)) {
174             $thisdir = `pwd`;
175             chop $thisdir;
176             $xmlfullfile = $thisdir . "/" . $xmlfile ;
177         }
178         else {
179             $xmlfullfile = $xmlfile;
180         }
181         $xmloutput = 1;
182         open( XMLOUT, ">$xmlfile" ) || die "Cannot open $xmlfile\n";
183         my $date = `date "+%Y-%m-%d-%H-%M"`;
184         $date =~ s/\r?\n//;
185         # MPISOURCE can be used to describe the source of MPI for this
186         # test.
187         print XMLOUT "<?xml version='1.0' ?>$newline";
188         print XMLOUT "<?xml-stylesheet href=\"TestResults.xsl\" type=\"text/xsl\" ?>$newline";
189         print XMLOUT "<MPITESTRESULTS>$newline";
190         print XMLOUT "<DATE>$date</DATE>$newline";
191         print XMLOUT "<MPISOURCE></MPISOURCE>$newline";
192     }
193     elsif (/--?noxmlclose/) {
194         $closeXMLOutput = 0;
195     }
196     elsif (/--?tapfile=(.*)/) {
197         $tapfile = $1;
198         if ($tapfile !~ m|^/|) {
199             $thisdir = `pwd`;
200             chomp $thisdir;
201             $tapfullfile = $thisdir . "/" . $tapfile ;
202         }
203         else {
204             $tapfullfile = $tapfile;
205         }
206         $tapoutput = 1;
207         open( TAPOUT, ">$tapfile" ) || die "Cannot open $tapfile\n";
208         my $date = `date "+%Y-%m-%d-%H-%M"`;
209         $date =~ s/\r?\n//;
210         print TAPOUT "TAP version 13\n";
211         print TAPOUT "# MPICH test suite results (TAP format)\n";
212         print TAPOUT "# date ${date}\n";
213         # we do not know at this point how many tests will be run, so do
214         # not print a test plan line like "1..450" until the very end
215     }
216     else {
217         print STDERR "Unrecognized argument $_\n";
218         print STDERR "runtests [-tests=testfile] [-np=nprocesses] \
219         [-maxnp=max-nprocesses] [-srcdir=location-of-tests] \
220         [-xmlfile=filename ] [-noxmlclose] \
221         [-verbose] [-showprogress] [-debug] [-batch]\n";
222         exit(1);
223     }
224 }
225
226 # Perform any post argument processing
227 if ($batchRun) {
228     if (! -d $batrundir) {
229         mkpath $batrundir || die "Could not create $batrundir\n";
230     }
231     open( BATOUT, ">$batrundir/runtests.batch" ) || die "Could not open $batrundir/runtests.batch\n";
232 }
233 else {
234     # We must have mpiexec
235     if ("$mpiexec" eq "") {
236         print STDERR "No mpiexec found!\n";
237         exit(1);
238     }
239 }
240
241 #
242 # Process any files
243 if ($listfiles eq "") {
244     if ($batchRun) {
245         print STDERR "An implicit list of tests is not permitted in batch mode\n";
246         exit(1);
247     }
248     else {
249         &ProcessImplicitList;
250     }
251 }
252 elsif (-d $listfiles) {
253     print STDERR "Testing by directories not yet supported\n";
254 }
255 else {
256     &RunList( $listfiles );
257 }
258
259 if ($xmloutput && $closeXMLOutput) {
260     print XMLOUT "</MPITESTRESULTS>$newline";
261     close XMLOUT;
262 }
263
264 if ($tapoutput) {
265     print TAPOUT "1..$total_seen\n";
266     close TAPOUT;
267 }
268
269 # Output a summary:
270 if ($batchRun) {
271     print "Programs created along with a runtest.batch file in $batrundir\n";
272     print "Run that script and then use checktests to summarize the results\n";
273 }
274 else {
275     if ($err_count) {
276         print "$err_count tests failed out of $total_run\n";
277         if ($xmloutput) {
278             print "Details in $xmlfullfile\n";
279         }
280     }
281     else {
282         print " All $total_run tests passed!\n";
283     }
284     if ($tapoutput) {
285         print "TAP formatted results in $tapfullfile\n";
286     }
287 }
288 #\f
289 # ---------------------------------------------------------------------------
290 # Routines
291 #
292 # Enter a new directory and process a list file.
293 #  ProcessDir( directory-name, list-file-name )
294 sub ProcessDir {
295     my $dir = $_[0]; $dir =~ s/\/$//;
296     my $listfile = $_[1];
297     my $savedir = `pwd`;
298     my $savecurdir = $curdir;
299     my $savesrcdir = $srcdir;
300
301     chop $savedir;
302     if (substr($srcdir,0,3) eq "../") {
303       $srcdir = "../$srcdir";
304     }
305
306     print "Processing directory $dir\n" if ($verbose || $debug);
307     chdir $dir;
308     if ($dir =~ /\//) {
309         print STDERR "only direct subdirectories allowed in list files";
310     }
311     $curdir .= "/$dir";
312
313     &RunList( $listfile );
314     print "\n" if $showProgress; # Terminate line from progress output
315     chdir $savedir;
316     $curdir = $savecurdir;
317     $srcdir = $savesrcdir;
318 }
319 # ---------------------------------------------------------------------------
320 # Run the programs listed in the file given as the argument.
321 # This file describes the tests in the format
322 #  programname number-of-processes [ key=value ... ]
323 # If the second value is not given, the default value is used.
324 #
325 sub RunList {
326     my $LIST = "LIST$depth"; $depth++;
327     my $listfile = $_[0];
328     my $ResultTest = "";
329     my $InitForRun = "";
330     my $listfileSource = $listfile;
331
332     print "Looking in $curdir/$listfile\n" if $debug;
333     if (! -s "$listfile" && -s "$srcdir/$curdir/$listfile" ) {
334         $listfileSource = "$srcdir/$curdir/$listfile";
335     }
336     open( $LIST, "<$listfileSource" ) ||
337         die "Could not open $listfileSource\n";
338     while (<$LIST>) {
339         # Check for stop file
340         if (-s $stopfile) {
341             # Exit because we found a stopfile
342             print STDERR "Terminating test because stopfile $stopfile found\n";
343             last;
344         }
345         # Skip comments
346         s/#.*//g;
347         # Remove any trailing newlines/returns
348         s/\r?\n//;
349         # Remove any leading whitespace
350         s/^\s*//;
351         # Some tests require that support routines are built first
352         # This is specified with !<dir>:<target>
353         if (/^\s*\!([^:]*):(.*)/) {
354             # Hack: just execute in a subshell.  This discards any
355             # output.
356             `cd $1 && make $2`;
357             next;
358         }
359         # List file entries have the form:
360         # program [ np [ name=value ... ] ]
361         # See files errhan/testlist, init/testlist, and spawn/testlist
362         # for examples of using the key=value form
363         my @args = split(/\s+/,$_);
364         my $programname = $args[0];
365         my $np = "";
366         my $ResultTest = "";
367         my $InitForRun = "";
368         my $timeLimit  = "";
369         my $progArgs   = "";
370         my $mpiexecArgs = "$execarg";
371         my $requiresStrict = "";
372         my $requiresMPIX   = "";
373         my $progEnv    = "";
374         my $mpiVersion = "";
375         my $xfail = "";
376         if ($#args >= 1) { $np = $args[1]; }
377         # Process the key=value arguments
378         for (my $i=2; $i <= $#args; $i++) {
379             if ($args[$i] =~ /([^=]+)=(.*)/) {
380                 my $key = $1;
381                 my $value = $2;
382                 if ($key eq "resultTest") {
383                     $ResultTest = $value;
384                 }
385                 elsif ($key eq "init") {
386                     $InitForRun = $value;
387                 }
388                 elsif ($key eq "timeLimit") {
389                     $timeLimit = $value;
390                 }
391                 elsif ($key eq "arg") {
392                     $progArgs = "$progArgs $value";
393                 }
394                 elsif ($key eq "mpiexecarg") {
395                     $mpiexecArgs = "$mpiexecArgs $value";
396                 }
397                 elsif ($key eq "env") {
398                     $progEnv = "$progEnv $value";
399                 }
400                 elsif ($key eq "mpiversion") {
401                     $mpiVersion = $value;
402                 }
403                 elsif ($key eq "strict") {
404                     $requiresStrict = $value
405                 }
406                 elsif ($key eq "mpix") {
407                     $requiresMPIX = $value
408                 }
409                 elsif ($key eq "xfail") {
410                     if ($value eq "") {
411                         print STDERR "\"xfail=\" requires an argument\n";
412                     }
413                     $xfail = $value;
414                 }
415                 else {
416                     print STDERR "Unrecognized key $key in $listfileSource\n";
417                 }
418             }
419         }
420
421         # skip empty lines
422         if ($programname eq "") { next; }
423
424         if ($np eq "") { $np = $np_default; }
425         if ($np_max > 0 && $np > $np_max) { $np = $np_max; }
426
427         # allows us to accurately output TAP test numbers without disturbing the
428         # original totals that have traditionally been reported
429         #
430         # These "unless" blocks are ugly, but permit us to honor skipping
431         # criteria for directories as well without counting directories as tests
432         # in our XML/TAP output.
433         unless (-d $programname) {
434             $total_seen++;
435         }
436
437         # If a minimum MPI version is specified, check against the
438         # available MPI.  If the version is unknown, we ignore this
439         # test (thus, all tests will be run).
440         if ($mpiVersion ne "" && $MPIMajorVersion ne "unknown" &&
441             $MPIMinorVersion ne "unknown") {
442             my ($majorReq,$minorReq) = split(/\./,$mpiVersion);
443             if ($majorReq > $MPIMajorVersion or
444                 ($majorReq == $MPIMajorVersion && $minorReq > $MPIMinorVersion))
445             {
446                 unless (-d $programname) {
447                     SkippedTest($programname, $np, $workdir, "requires MPI version $mpiVersion");
448                 }
449                 next;
450             }
451         }
452         # Check whether strict is required by MPI but not by the
453         # test (use strict=false for tests that use non-standard extensions)
454         if (lc($requiresStrict) eq "false" && lc($testIsStrict) eq "true") {
455             unless (-d $programname) {
456                 SkippedTest($programname, $np, $workdir, "non-strict test, strict MPI mode requested");
457             }
458             next;
459         }
460
461         if (lc($testIsStrict) eq "true") {
462             # Strict MPI testing was requested, so assume that a non-MPICH MPI
463             # implementation is being tested and the "xfail" implementation
464             # assumptions do not hold.
465             $xfail = '';
466         }
467
468         if (lc($requiresMPIX) eq "true" && lc($MPIHasMPIX) eq "no") {
469             unless (-d $programname) {
470                 SkippedTest($programname, $np, $workdir, "tests MPIX extensions, MPIX testing disabled");
471             }
472             next;
473         }
474
475         if (-d $programname) {
476             # If a directory, go into the that directory and
477             # look for a new list file
478             &ProcessDir( $programname, $listfile );
479         }
480         else {
481             $total_run++;
482             if (&BuildMPIProgram( $programname, $xfail ) == 0) {
483                 if ($batchRun == 1) {
484                     &AddMPIProgram( $programname, $np, $ResultTest,
485                                     $InitForRun, $timeLimit, $progArgs,
486                                     $progEnv, $mpiexecArgs, $xfail );
487                 }
488                 else {
489                     &RunMPIProgram( $programname, $np, $ResultTest,
490                                     $InitForRun, $timeLimit, $progArgs,
491                                     $progEnv, $mpiexecArgs, $xfail );
492                 }
493             }
494             elsif ($xfail ne '') {
495                 # We expected to run this program, so failure to build
496                 # is an error
497                 $found_error = 1;
498                 $err_count++;
499             }
500             if ($batchRun == 0) {
501                 &CleanUpAfterRun( $programname );
502             }
503         }
504     }
505     close( $LIST );
506 }
507 #
508 # This routine tries to run all of the files in the current
509 # directory
510 sub ProcessImplicitList {
511     # The default is to run every file in the current directory.
512     # If there are no built programs, build and run every file
513     # WARNING: This assumes that anything executable should be run as
514     # an MPI test.
515     $found_exec = 0;
516     $found_src  = 0;
517     open (PGMS, "ls -1 |" ) || die "Cannot list directory\n";
518     while (<PGMS>) {
519         s/\r?\n//;
520         $programname = $_;
521         if (-d $programname) { next; }  # Ignore directories
522         if ($programname eq "runtests") { next; } # Ignore self
523         if ($programname eq "checktests") { next; } # Ignore helper
524         if ($programname eq "configure") { next; } # Ignore configure script
525         if ($programname eq "config.status") { next; } # Ignore configure helper
526         if (-x $programname) { $found_exec++; }
527         if ($programname =~ /\.[cf]$/) { $found_src++; }
528     }
529     close PGMS;
530
531     if ($found_exec) {
532         print "Found executables\n" if $debug;
533         open (PGMS, "ls -1 |" ) || die "Cannot list programs\n";
534         while (<PGMS>) {
535             # Check for stop file
536             if (-s $stopfile) {
537                 # Exit because we found a stopfile
538                 print STDERR "Terminating test because stopfile $stopfile found\n";
539                 last;
540             }
541             s/\r?\n//;
542             $programname = $_;
543             if (-d $programname) { next; }  # Ignore directories
544             if ($programname eq "runtests") { next; } # Ignore self
545             if (-x $programname) {
546                 $total_run++;
547                 &RunMPIProgram( $programname, $np_default, "", "", "", "", "", "", "" );
548             }
549         }
550         close PGMS;
551     }
552     elsif ($found_src) {
553         print "Found source files\n" if $debug;
554         open (PGMS, "ls -1 *.c |" ) || die "Cannot list programs\n";
555         while (<PGMS>) {
556             if (-s $stopfile) {
557                 # Exit because we found a stopfile
558                 print STDERR "Terminating test because stopfile $stopfile found\n";
559                 last;
560             }
561             s/\r?\n//;
562             $programname = $_;
563             # Skip messages from ls about no files
564             if (! -s $programname) { next; }
565             $programname =~ s/\.c//;
566             $total_run++;
567             if (&BuildMPIProgram( $programname, "") == 0) {
568                 &RunMPIProgram( $programname, $np_default, "", "", "", "", "", "", "" );
569             }
570             else {
571                 # We expected to run this program, so failure to build
572                 # is an error
573                 $found_error = 1;
574                 $err_count++;
575             }
576             &CleanUpAfterRun( $programname );
577         }
578         close PGMS;
579     }
580 }
581 # Run the program.
582 # ToDo: Add a way to limit the time that any particular program may run.
583 # The arguments are
584 #    name of program, number of processes, name of routine to check results
585 #    init for testing, timelimit, and any additional program arguments
586 # If the 3rd arg is not present, the a default that simply checks that the
587 # return status is 0 and that the output is " No Errors" is used.
588 sub RunMPIProgram {
589     my ($programname,$np,$ResultTest,$InitForTest,$timeLimit,$progArgs,$progEnv,$mpiexecArgs,$xfail) = @_;
590     my $found_error   = 0;
591     my $found_noerror = 0;
592     my $inline = "";
593
594     &RunPreMsg( $programname, $np, $curdir );
595
596     unlink "err";
597
598     # Set a default timeout on tests (3 minutes for now)
599     my $timeout = $defaultTimeLimit;
600     if (defined($timeLimit) && $timeLimit =~ /^\d+$/) {
601         $timeout = $timeLimit;
602     }
603     $ENV{"MPIEXEC_TIMEOUT"} = $timeout;
604
605     # Run the optional setup routine. For example, the timeout tests could
606     # be set to a shorter timeout.
607     if ($InitForTest ne "") {
608         &$InitForTest();
609     }
610     print STDOUT "Env includes $progEnv\n" if $verbose;
611     print STDOUT "$mpiexec $wrapparg $mpiexecArgs $np_arg $np $program_wrapper ./$programname $progArgs\n" if $verbose;
612     print STDOUT "." if $showProgress;
613     # Save and restore the environment if necessary before running mpiexec.
614     if ($progEnv ne "") {
615         %saveEnv = %ENV;
616         foreach $val (split(/\s+/, $progEnv)) {
617             if ($val =~ /([^=]+)=(.*)/) {
618                 $ENV{$1} = $2;
619             }
620             else {
621                 print STDERR "Environment variable/value $val not in a=b form\n";
622             }
623         }
624     }
625     open ( MPIOUT, "$mpiexec $wrapparg $np_arg $np $mpiexecArgs $program_wrapper ./$programname $progArgs 2>&1 |" ) ||
626         die "Could not run ./$programname\n";
627     if ($progEnv ne "") {
628         %ENV = %saveEnv;
629     }
630     if ($ResultTest ne "") {
631         # Read and process the output
632         ($found_error, $inline) = &$ResultTest( MPIOUT, $programname );
633     }
634     else {
635         if ($verbose) {
636             $inline = "$mpiexec $wrapparg $np_arg $np $program_wrapper ./$programname\n";
637         }
638         else {
639             $inline = "";
640         }
641         while (<MPIOUT>) {
642             print STDOUT $_ if $verbose;
643             # Skip FORTRAN STOP
644             if (/FORTRAN STOP/) { next; }
645             $inline .= $_;
646             if (/^\s*No [Ee]rrors\s*$/ && $found_noerror == 0) {
647                 $found_noerror = 1;
648             }
649             if (! /^\s*No [Ee]rrors\s*$/ && !/^\s*Test Passed\s*$/) {
650                 print STDERR "Unexpected output in $programname: $_";
651                 if (!$found_error) {
652                     $found_error = 1;
653                     $err_count ++;
654                 }
655             }
656         }
657         if ($found_noerror == 0) {
658             print STDERR "Program $programname exited without No Errors\n";
659             if (!$found_error) {
660                 $found_error = 1;
661                 $err_count ++;
662             }
663         }
664         $rc = close ( MPIOUT );
665         if ($rc == 0) {
666             # Only generate a message if we think that the program
667             # passed the test.
668             if (!$found_error) {
669                 $run_status = $?;
670                 $signal_num = $run_status & 127;
671                 if ($run_status > 255) { $run_status >>= 8; }
672                 print STDERR "Program $programname exited with non-zero status $run_status\n";
673                 if ($signal_num != 0) {
674                     print STDERR "Program $programname exited with signal $signal_num\n";
675                 }
676                 $found_error = 1;
677                 $err_count ++;
678             }
679         }
680     }
681     if ($found_error) {
682         &RunTestFailed( $programname, $np, $curdir, $inline, $xfail );
683     }
684     else {
685         &RunTestPassed( $programname, $np, $curdir, $xfail );
686     }
687     &RunPostMsg( $programname, $np, $curdir );
688 }
689
690 # This version simply writes the mpiexec command out, with the output going
691 # into a file, and recording the output status of the run.
692 sub AddMPIProgram {
693     my ($programname,$np,$ResultTest,$InitForTest,$timeLimit,$progArgs,$progEnv,$mpiexecArgs, $xfail) = @_;
694
695     if (! -x $programname) {
696         print STDERR "Could not find $programname!";
697         return;
698     }
699
700     if ($ResultTest ne "") {
701         # This test really needs to be run manually, with this test
702         # Eventually, we can update this to include handleing in checktests.
703         print STDERR "Run $curdir/$programname with $np processes and use $ResultTest to check the results\n";
704         return;
705     }
706
707     # Set a default timeout on tests (3 minutes for now)
708     my $timeout = $defaultTimeLimit;
709     if (defined($timeLimit) && $timeLimit =~ /^\d+$/) {
710         # On some systems, there is no effective time limit on
711         # individual mpi program runs.  In that case, we may
712         # want to treat these also as "run manually".
713         $timeout = $timeLimit;
714     }
715     print BATOUT "export MPIEXEC_TIMEOUT=$timeout\n";
716
717     # Run the optional setup routine. For example, the timeout tests could
718     # be set to a shorter timeout.
719     if ($InitForTest ne "") {
720         &$InitForTest();
721     }
722
723     # For non-MPICH versions of mpiexec, a timeout may require a different
724     # environment variable or command line option (e.g., for Cray aprun,
725     # the option -t <sec> must be given, there is no environment variable
726     # to set the timeout.
727     $extraArgs = "";
728     if (defined($timeoutArgPattern) && $timeoutArgPattern ne "") {
729         my $timeArg = $timeoutArgPattern;
730         $timeoutArg =~ s/<SEC>/$timeout/;
731         $extraArgs .= $timeoutArg
732     }
733
734     print STDOUT "Env includes $progEnv\n" if $verbose;
735     print STDOUT "$mpiexec $np_arg $np $extraArgs $program_wrapper ./$programname $progArgs\n" if $verbose;
736     print STDOUT "." if $showProgress;
737     # Save and restore the environment if necessary before running mpiexec.
738     if ($progEnv ne "") {
739         # Need to fix:
740         # save_NAME_is_set=is old name set
741         # save_NAME=oldValue
742         # export NAME=newvalue
743         # (run)
744         # export NAME=oldValue (if set!)
745         print STDERR "Batch output does not permit changes to environment\n";
746     }
747     # The approach here is to move the test codes to a single directory from
748     # which they can be run; this avoids complex code to change directories
749     # and ensure that the output goes "into the right place".
750     $testCount++;
751     rename $programname, "$batrundir/$programname";
752     print BATOUT "echo \"# $mpiexec $np_arg $np $extraArgs $mpiexecArgs $program_wrapper $curdir/$programname $progArgs\" > runtests.$testCount.out\n";
753     # Some programs expect to run in the same directory as the executable
754     print BATOUT "$mpiexec $np_arg $np $extraArgs $mpiexecArgs $program_wrapper ./$programname $progArgs >> runtests.$testCount.out 2>&1\n";
755     print BATOUT "echo \$? > runtests.$testCount.status\n";
756 }
757
758 #
759 # Return value is 0 on success, non zero on failure
760 sub BuildMPIProgram {
761     my $programname = shift;
762     if (! -x $programname) {
763         die "Could not find $programname. Aborting.\n";
764     }
765     return 0;
766     # THE FOLLOWING IS DISABLED.
767     my $xfail = shift;
768     my $rc = 0;
769     if ($verbose) { print STDERR "making $programname\n"; }
770     if (! -x $programname) { $remove_this_pgm = 1; }
771     else { $remove_this_pgm = 0; }
772     my $output = `make $programname 2>&1`;
773     $rc = $?;
774     if ($rc > 255) { $rc >>= 8; }
775     if (! -x $programname) {
776         print STDERR "Failed to build $programname; $output\n";
777         if ($rc == 0) {
778             $rc = 1;
779         }
780         # Add a line to the summary file describing the failure
781         # This will ensure that failures to build will end up
782         # in the summary file (which is otherwise written by the
783         # RunMPIProgram step)
784         &RunPreMsg( $programname, $np, $curdir );
785         &RunTestFailed( $programname, $np, $curdir, "Failed to build $programname; $output", $xfail );
786         &RunPostMsg( $programname, $np, $curdir );
787     }
788     return $rc;
789 }
790
791 sub CleanUpAfterRun {
792     my $programname = $_[0];
793
794     # Check for that this program has exited.  If it is still running,
795     # issue a warning and leave the application.  Of course, this
796     # check is complicated by the lack of a standard access to the
797     # running processes for this user in Unix.
798     @stillRunning = &FindRunning( $programname );
799
800     if ($#stillRunning > -1) {
801         print STDERR "Some programs ($programname) may still be running:\npids = ";
802         for (my $i=0; $i <= $#stillRunning; $i++ ) {
803             print STDERR $stillRunning[$i] . " ";
804         }
805         print STDERR "\n";
806         # Remind the user that the executable remains; we leave it around
807         # to allow the programmer to debug the running program, for which
808         # the executable is needed.
809         print STDERR "The executable ($programname) will not be removed.\n";
810     }
811     else {
812         if ($remove_this_pgm && $clean_pgms) {
813             unlink $programname, "$programname.o";
814         }
815         $remove_this_pgm = 0;
816     }
817 }
818 # ----------------------------------------------------------------------------
819 sub FindRunning {
820     my $programname = $_[0];
821     my @pids = ();
822
823     my $logname = $ENV{'USER'};
824     my $pidloc = 1;
825     my $rc = open PSFD, "ps auxw -U $logname 2>&1 |";
826
827     if ($rc == 0) {
828         $rc = open PSFD, "ps -fu $logname 2>&1 |";
829     }
830     if ($rc == 0) {
831         print STDERR "Could not execute ps command\n";
832         return @pids;
833     }
834
835     while (<PSFD>) {
836         if (/$programname/) {
837             @fields = split(/\s+/);
838             my $pid = $fields[$pidloc];
839             # Check that we've found a numeric pid
840             if ($pid =~ /^\d+$/) {
841                 $pids[$#pids + 1] = $pid;
842             }
843         }
844     }
845     close PSFD;
846
847     return @pids;
848 }
849 # ----------------------------------------------------------------------------
850 #
851 # TestStatus is a special test that reports success *only* when the
852 # status return is NONZERO
853 sub TestStatus {
854     my $MPIOUT = $_[0];
855     my $programname = $_[1];
856     my $found_error = 0;
857
858     my $inline = "";
859     while (<$MPIOUT>) {
860         #print STDOUT $_ if $verbose;
861         # Skip FORTRAN STOP
862         if (/FORTRAN STOP/) { next; }
863         $inline .= $_;
864         # ANY output is an error. We have the following output
865         # exception for the Hydra process manager.
866         if (/=*/) { last; }
867         if (! /^\s*$/) {
868             print STDERR "Unexpected output in $programname: $_";
869             if (!$found_error) {
870                 $found_error = 1;
871                 $err_count ++;
872             }
873         }
874     }
875     $rc = close ( MPIOUT );
876     if ($rc == 0) {
877         $run_status = $?;
878         $signal_num = $run_status & 127;
879         if ($run_status > 255) { $run_status >>= 8; }
880     }
881     else {
882         # This test *requires* non-zero return codes
883         if (!$found_error) {
884             $found_error = 1;
885             $err_count ++;
886         }
887         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
888     }
889     return ($found_error,$inline);
890 }
891 #
892 # TestTimeout is a special test that reports success *only* when the
893 # status return is NONZERO and there are no processes left over.
894 # This test currently checks only for the return status.
895 sub TestTimeout {
896     my $MPIOUT = $_[0];
897     my $programname = $_[1];
898     my $found_error = 0;
899
900     my $inline = "";
901     while (<$MPIOUT>) {
902         #print STDOUT $_ if $verbose;
903         # Skip FORTRAN STOP
904         if (/FORTRAN STOP/) { next; }
905         $inline .= $_;
906         if (/[Tt]imeout/) { next; }
907         # Allow 'signaled with Interrupt' (see gforker mpiexec)
908         if (/signaled with Interrupt/) { next; }
909         # Allow 'job ending due to env var MPIEXEC_TIMEOUT' (mpd)
910         if (/job ending due to env var MPIEXEC_TIMEOUT/) { next; }
911         # Allow 'APPLICATION TIMED OUT' (hydra)
912         if (/\[mpiexec@.*\] APPLICATION TIMED OUT/) { last; }
913         # ANY output is an error (other than timeout)
914         if (! /^\s*$/) {
915             print STDERR "Unexpected output in $programname: $_";
916             if (!$found_error) {
917                 $found_error = 1;
918                 $err_count ++;
919             }
920         }
921     }
922     $rc = close ( MPIOUT );
923     if ($rc == 0) {
924         $run_status = $?;
925         $signal_num = $run_status & 127;
926         if ($run_status > 255) { $run_status >>= 8; }
927     }
928     else {
929         # This test *requires* non-zero return codes
930         if (!$found_error) {
931             $found_error = 1;
932             $err_count ++;
933         }
934         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
935     }
936     #
937     # Here should go a check of the processes
938     # open( PFD, "ps -fu $LOGNAME | grep -v grep | grep $programname |" );
939     # while (<PFD>) {
940     #
941     # }
942     # close PFD;
943     return ($found_error,$inline);
944 }
945 #
946 # TestErrFatal is a special test that reports success *only* when the
947 # status return is NONZERO; it ignores error messages
948 sub TestErrFatal {
949     my $MPIOUT = $_[0];
950     my $programname = $_[1];
951     my $found_error = 0;
952
953     my $inline = "";
954     while (<$MPIOUT>) {
955         #print STDOUT $_ if $verbose;
956         # Skip FORTRAN STOP
957         if (/FORTRAN STOP/) { next; }
958         $inline .= $_;
959         # ALL output is allowed.
960     }
961     $rc = close ( MPIOUT );
962     if ($rc == 0) {
963         $run_status = $?;
964         $signal_num = $run_status & 127;
965         if ($run_status > 255) { $run_status >>= 8; }
966     }
967     else {
968         # This test *requires* non-zero return codes
969         if (!$found_error) {
970             $found_error = 1;
971             $err_count ++;
972         }
973         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
974     }
975     return ($found_error,$inline);
976 }
977
978 # ----------------------------------------------------------------------------
979 # Output routines:
980 #  RunPreMsg( programname, np, workdir ) - Call before running a program
981 #  RunTestFailed, RunTestPassed - Call after test
982 #  RunPostMsg               - Call at end of each test
983 #
984 sub RunPreMsg {
985     my ($programname,$np,$workdir) = @_;
986     if ($xmloutput) {
987         print XMLOUT "<MPITEST>$newline<NAME>$programname</NAME>$newline";
988         print XMLOUT "<NP>$np</NP>$newline";
989         print XMLOUT "<WORKDIR>$workdir</WORKDIR>$newline";
990     }
991 }
992 sub RunPostMsg {
993     my ($programname, $np, $workdir) = @_;
994     if ($xmloutput) {
995         print XMLOUT "</MPITEST>$newline";
996     }
997 }
998 sub RunTestPassed {
999     my ($programname, $np, $workdir, $xfail) = @_;
1000     if ($xmloutput) {
1001         print XMLOUT "<STATUS>pass</STATUS>$newline";
1002     }
1003     if ($tapoutput) {
1004         my $xfailstr = '';
1005         if ($xfail ne '') {
1006             $xfailstr = " # TODO $xfail";
1007         }
1008         print TAPOUT "ok ${total_run} - $workdir/$programname ${np}${xfailstr}\n";
1009     }
1010 }
1011 sub RunTestFailed {
1012     my $programname = shift;
1013     my $np = shift;
1014     my $workdir = shift;
1015     my $output = shift;
1016     my $xfail = shift;
1017
1018     if ($xmloutput) {
1019         my $xout = $output;
1020         # basic escapes that wreck the XML output
1021         $xout =~ s/</\*AMP\*lt;/g;
1022         $xout =~ s/>/\*AMP\*gt;/g;
1023         $xout =~ s/&/\*AMP\*amp;/g;
1024         $xout =~ s/\*AMP\*/&/g;
1025         # TODO: Also capture any non-printing characters (XML doesn't like them
1026         # either).
1027         print XMLOUT "<STATUS>fail</STATUS>$newline";
1028         print XMLOUT "<TESTDIFF>$newline$xout</TESTDIFF>$newline";
1029     }
1030
1031     if ($tapoutput) {
1032         my $xfailstr = '';
1033         if ($xfail ne '') {
1034             $xfailstr = " # TODO $xfail";
1035         }
1036         print TAPOUT "not ok ${total_run} - $workdir/$programname ${np}${xfailstr}\n";
1037         print TAPOUT "  ---\n";
1038         print TAPOUT "  Directory: $workdir\n";
1039         print TAPOUT "  File: $programname\n";
1040         print TAPOUT "  Num-procs: $np\n";
1041         print TAPOUT "  Date: \"" . localtime . "\"\n";
1042
1043         # The following would be nice, but it leads to unfortunate formatting in
1044         # the Jenkins web output for now.  Using comment lines instead, since
1045         # they are easier to read/find in a browser.
1046 ##        print TAPOUT "  Output: |\n";
1047 ##        # using block literal format, requires that all chars are printable
1048 ##        # UTF-8 (or UTF-16, but we won't encounter that)
1049 ##        foreach my $line (split m/\r?\n/, $output) {
1050 ##            chomp $line;
1051 ##            # 4 spaces, 2 for TAP indent, 2 more for YAML block indent
1052 ##            print TAPOUT "    $line\n";
1053 ##        }
1054
1055         print TAPOUT "  ...\n";
1056
1057         # Alternative to the "Output:" YAML block literal above.  Do not put any
1058         # spaces before the '#', this causes some TAP parsers (including Perl's
1059         # TAP::Parser) to treat the line as "unknown" instead of a proper
1060         # comment.
1061         print TAPOUT "## Test output (expected 'No Errors'):\n";
1062         foreach my $line (split m/\r?\n/, $output) {
1063             chomp $line;
1064             print TAPOUT "## $line\n";
1065         }
1066     }
1067 }
1068
1069 sub SkippedTest {
1070     my $programname = shift;
1071     my $np = shift;
1072     my $workdir = shift;
1073     my $reason = shift;
1074
1075     # simply omit from the XML output
1076
1077     if ($tapoutput) {
1078         print TAPOUT "ok ${total_seen} - $workdir/$programname $np  # SKIP $reason\n";
1079     }
1080 }
1081
1082 # ----------------------------------------------------------------------------
1083 # Alternate init routines
1084 sub InitQuickTimeout {
1085     $ENV{"MPIEXEC_TIMEOUT"} = 10;
1086 }