[project @ 2000-04-27 13:54:11 by chak]
[ghc-hetmet.git] / glafp-utils / runstdtest / runstdtest.prl
1 #
2 # The perl script requires the following variables to be bound
3 # to something meaningful before it will operate correctly:
4 #   
5 #   TMPDIR
6 #   CONTEXT_DIFF
7 #   RM
8 #
9 # Given:
10 #       * a program to run (1st arg)
11 #       * some "command-line opts" ( -O<opt1> -O<opt2> ... )
12 #           [default: anything on the cmd line this script doesn't recognise ]
13 #         the first opt not starting w/ "-" is taken to be an input
14 #         file and (if it exists) is grepped for "what's going on here"
15 #         comments (^-- !!!).
16 #       * a file to feed to stdin ( -i<file> ) [default: /dev/null ]
17 #       * a "time" command to use (-t <cmd>).
18 #
19 #       * alternatively, a "-script <script>" argument says: run the
20 #         named Bourne-shell script to do the test.  It's passed the
21 #         pgm-to-run as the one-and-only arg.
22 #
23 # Run the program with those options and that input, and check:
24 # if we get...
25
26 #       * an expected exit status ( -x <val> ) [ default 0 ]
27 #       * expected output on stdout ( -o1 <file> ) [ default /dev/null ]
28 #               ( we'll accept one of several...)
29 #       * expected output on stderr ( -o2 <file> ) [ default /dev/null ]
30 #               ( we'll accept one of several...)
31 #
32 #       (if the expected-output files' names end in .Z, then
33 #        they are uncompressed before doing the comparison)
34
35 # (This is supposed to be a "prettier" replacement for runstdtest.)
36 #
37 #       Flags
38 #       ~~~~~
39 #       -accept-output  replace output files with the ones actually generated by running
40 #                       the program
41 #
42 ($Pgm = $0) =~ s|.*/||;
43 $Verbose = 0;
44 $SaveStderr = 0;
45 $SaveStdout = 0;
46 $Status = 0;
47 @PgmArgs = ();
48 $PgmFail=0;
49 $PgmExitStatus = 0;
50 $PgmStdinFile  = '/dev/null';
51 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
52     $TmpPrefix = $ENV{'TMPDIR'};
53 } else {
54     $TmpPrefix ="$TMPDIR";
55     $ENV{'TMPDIR'} = "$TMPDIR"; # set the env var as well
56 }
57 $ScriptFile = "$TmpPrefix/run_me$$";
58 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
59 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
60 @PgmStdoutFile = ();
61 @PgmStderrFile = ();
62 $PreScript = '';
63 $PostScript = '';
64 $TimeCmd = '';
65 $StatsFile = "$TmpPrefix/stats$$";
66 $CacheProfStats = "cacheprof.out.summary";
67 $SysSpecificTiming = '';
68 $CacheProf = 'no';
69
70 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
71 $ToRun = $ARGV[0]; shift(@ARGV);
72 # avoid picking up same-named thing from somewhere else on $PATH...
73 $ToRun = "./$ToRun" if $ToRun !~ /^\//;
74
75 arg: while ($_ = $ARGV[0]) {
76     shift(@ARGV);
77     
78     /^--$/      && do { # let anything past after --
79                         push(@PgmArgs, @ARGV);
80                         last arg; };
81
82     /^-v$/             && do { $Verbose = 1; next arg; };
83     /^-accept-output-stderr$/ && do { $SaveStderr = 1; next arg; };
84     /^-accept-output-stdout$/ && do { $SaveStdout = 1; next arg; };
85     /^-accept-output$/        && do { $SaveStdout = 1; $SaveStderr = 1; next arg; };
86
87     /^-O(.*)/   && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
88     /^-i(.*)/   && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
89                         $Status++,
90                         print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
91                             if $PgmStdinFile !~ /^\/dev\/.*$/ && ! -f $PgmStdinFile;
92                         next arg; };
93     /^-fail/    && do { $PgmFail=1; next arg; };
94     /^-x(.*)/   && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
95                         $Status++ ,
96                         print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
97                             if $PgmExitStatus !~ /^\d+$/;
98                         next arg; };
99     /^-o1(.*)/  && do { $out_file = &grab_arg_arg('-o1',$1);
100                         push(@PgmStdoutFile, $out_file);
101                         next arg; };
102     /^-o2(.*)/  && do { $out_file = &grab_arg_arg('-o2',$1);
103                         push(@PgmStderrFile, $out_file);
104                         next arg; };
105     /^-prescript(.*)/  && do { $PreScript = &grab_arg_arg('-prescript',$1);
106                                 next arg; };
107     /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
108                                 next arg; };
109     /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
110                     $Status++;
111                     next arg; };
112     /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
113                                   next arg; };
114     /^-cacheprof$/ && do { $SysSpecificTiming = 'ghc-instrs';
115                            $CacheProf = 'yes'; 
116                            next arg };
117     /^-t(.*)/   && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
118
119     # anything else is taken to be a pgm arg
120     push(@PgmArgs, $_);
121 }
122
123 foreach $out_file ( @PgmStdoutFile ) {
124     if ( ! -f $out_file && !$SaveStdout ) {
125             print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
126             pop(@PgmStdoutFile);
127     }
128 }
129
130 foreach $out_file ( @PgmStderrFile ) {
131     if ( ! -f $out_file && !$SaveStderr ) {
132             print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
133             pop(@PgmStderrFile);
134     }
135 }
136
137 exit 1 if $Status;
138
139 # add on defaults if none specified
140 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
141 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
142
143 # tidy up the pgm args:
144 # (1) look for the "first input file"
145 #     and grep it for "interesting" comments (-- !!! )
146 # (2) quote any args w/ whitespace in them.
147 $grep_done = 0;
148 foreach $a ( @PgmArgs ) {
149     if (! $grep_done && $a !~ /^-/ && -f $a) {
150         print `egrep "^--[ ]?!!!" $a`;
151         $grep_done = 1;
152     }
153     if ($a =~ /\s/ || $a =~ /'/) {
154         $a =~ s/'/\\'/g;    # backslash the quotes;
155         $a = "\"$a\"";      # quote the arg
156     }
157 }
158
159 # deal with system-specific timing options
160 $TimingMagic = '';
161 if ( $SysSpecificTiming =~ /^ghc/ ) {
162     $TimingMagic = "+RTS -S$StatsFile -RTS"
163 } elsif ( $SysSpecificTiming eq 'hbc' ) {
164     $TimingMagic = "-S$StatsFile";
165 }
166
167 if ($PreScript ne '') {
168     local($to_do);
169     $PreScriptLines = `cat $PreScript`;
170 } else {
171     $PreScriptLines = '';
172 }
173
174 if ($PostScript ne '') {
175     local($to_do);
176     $PostScriptLines = `cat $PostScript`;
177     $* = 1;
178     $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
179     $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
180 } else {
181     $PostScriptLines = '';
182 }
183
184 # OK, so we're gonna do the normal thing...
185
186 $Script = <<EOSCRIPT;
187 #! /bin/sh
188 myexit=0
189 diffsShown=0
190 rm -f $DefaultStdoutFile $DefaultStderrFile
191 cat /dev/null > $DefaultStdoutFile
192 cat /dev/null > $DefaultStderrFile
193 $PreScriptLines
194 $SpixifyLine1
195 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
196 progexit=\$?
197 if [ \$progexit -eq 0 ] && [ $PgmFail -ne 0 ]; then
198     echo $ToRun @PgmArgs \\< $PgmStdinFile
199     echo "****" expected a failure, but was successful
200     myexit=1
201 fi
202 if [ \$progexit -ne $PgmExitStatus ] && [ $PgmFail -eq 0 ]; then
203     echo $ToRun @PgmArgs \\< $PgmStdinFile
204     echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
205     myexit=1
206 else
207     $PostScriptLines
208     hit='NO'
209     for out_file in @PgmStdoutFile ; do
210         if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
211             hit='YES'
212         fi
213     done
214     if [ \$hit = 'NO' ] ; then
215         echo $ToRun @PgmArgs \\< $PgmStdinFile
216         echo expected stdout not matched by reality
217         orig_file="$PgmStdoutFile[0]";
218         [ ! -f \$orig_file ] && orig_file="/dev/null"
219         ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.1
220         myexit=\$?
221         diffsShown=1
222     fi
223     if [ $SaveStdout = 1 ] && 
224        [ $PgmStdoutFile[0] != $DefaultStdoutFile ] && [ -s $TmpPrefix/runtest$$.1 ]; then
225         echo Saving away stdout output in $PgmStdoutFile[0] ...
226         if [ -f $PgmStdoutFile[0] ]; then
227              rm -f $PgmStdoutFile[0].bak
228              cp $PgmStdoutFile[0] $PgmStdoutFile[0].bak
229         fi;
230         cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
231     fi
232 fi
233 egrep -v '^ld\.so:.*has older revision than expected' < $TmpPrefix/runtest$$.2 > $TmpPrefix/runtest$$.2b
234 mv -f $TmpPrefix/runtest$$.2b $TmpPrefix/runtest$$.2
235
236 hit='NO'
237 for out_file in @PgmStderrFile ; do
238     if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
239         hit='YES'
240     fi
241 done
242 if [ \$hit = 'NO' ] ; then
243     echo $ToRun @PgmArgs \\< $PgmStdinFile
244     echo expected stderr not matched by reality
245     orig_file="$PgmStderrFile[0]"
246     [ ! -f \$orig_file ] && orig_file="/dev/null"
247     ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.2
248     myexit=\$?
249     diffsShown=1
250 fi
251 if [ $SaveStderr = 1 ] &&
252    [ $PgmStderrFile[0] != $DefaultStderrFile ] && [ -s $TmpPrefix/runtest$$.2 ]; then
253         echo Saving away stderr output in $PgmStderrFile[0] ...
254         if [ -f $PgmStderrFile[0] ]; then
255            rm -f $PgmStderrFile[0].bak
256            cp $PgmStderrFile[0] $PgmStderrFile[0].bak
257         fi;
258         cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
259 fi
260
261 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
262 exit \$myexit
263 EOSCRIPT
264
265 # bung script into a file
266 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
267 print SCR $Script;
268 close(SCR) || die "Failed closing script file!\n";
269 chmod 0755, $ScriptFile;
270
271 print STDERR $Script if $Verbose;
272
273 &run_something($ScriptFile);
274
275 if ( $SysSpecificTiming eq '' ) {
276     unlink $StatsFile;
277     unlink $ScriptFile;
278     exit 0;
279 }
280
281 &process_stats_file();
282 &process_cacheprof_files() if $CacheProf eq 'yes';
283
284 # print out what we found
285 print STDERR "<<$SysSpecificTiming: ";
286 if ( $CacheProf ne 'yes' ) {
287         print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed)";
288 } else {
289         print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed), $TotInstrs instructions, $TotReads memory reads, $TotWrites memory writes";
290 };
291 print STDERR " :$SysSpecificTiming>>\n";
292
293 # OK, party over
294 unlink $StatsFile;
295 unlink $ScriptFile;
296 exit 0;
297
298 sub grab_arg_arg {
299     local($option, $rest_of_arg) = @_;
300     
301     if ($rest_of_arg) {
302         return($rest_of_arg);
303     } elsif ($#ARGV >= 0) {
304         local($temp) = $ARGV[0]; shift(@ARGV); 
305         return($temp);
306     } else {
307         print STDERR "$Pgm: no argument following $option option\n";
308         $Status++;
309     }
310 }
311
312 sub run_something {
313     local($str_to_do) = @_;
314
315 #   print STDERR "$str_to_do\n" if $Verbose;
316
317     local($return_val) = 0;
318     system($str_to_do);
319     $return_val = $?;
320
321     if ($return_val != 0) {
322 #ToDo: this return-value mangling is wrong
323 #       local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
324 #       $die_msg .= " (program not found)" if $return_val == 255;
325 #       $die_msg .= " ($!)" if $Verbose && $! != 0;
326 #       $die_msg .= "\n";
327         unlink $ScriptFile;
328         unlink $StatsFile;
329
330         exit (($return_val == 0) ? 0 : 1);
331     }
332 }
333
334 sub process_stats_file {
335
336     # OK, process system-specific stats file
337     if ( $SysSpecificTiming =~ /^ghc/ ) {
338
339         #NB: nearly the same as in GHC driver's -ghc-timing stuff
340
341         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
342
343         local($max_live)    = 0; 
344         local($tot_live)    = 0; # for calculating residency stuff
345         local($tot_samples) = 0;
346
347         while (<STATS>) {
348             if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
349                 $max_live = $1 if $max_live < $1;
350                 $tot_live += $1;
351                 $tot_samples += 1;
352             }
353
354             $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
355             $GCWork = $1     if /^\s*([0-9,]+) bytes copied during GC/;
356
357 #           if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
358 #               $MaxResidency = $1; $ResidencySamples = $2;
359 #           }
360
361             $GCs = $1 if /^\s*([0-9,]+) collections? in generation 0/;
362
363             if ( /^\s+([0-9]+)\s+Mb total memory/ ) {
364                 $TotMem = $1;
365             }
366
367             if ( /^\s*INIT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
368                 $InitTime = $1; $InitElapsed = $2;
369             } elsif ( /^\s*MUT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
370                 $MutTime = $1; $MutElapsed = $2;
371             } elsif ( /^\s*GC\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
372                 $GcTime = $1; $GcElapsed = $2;
373             }
374         }
375         close(STATS) || die "Failed when closing $StatsFile\n";
376         if ( $tot_samples > 0 ) {
377             $ResidencySamples = $tot_samples;
378             $MaxResidency = $max_live;
379             $AvgResidency = int ($tot_live / $tot_samples) ;
380         }
381
382     } elsif ( $SysSpecificTiming eq 'hbc' ) {
383
384         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
385         while (<STATS>) {
386             $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
387
388             $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
389
390             if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
391                 $MutTime = $1; $MutElapsed = $2; # will fix up later
392
393                 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
394
395             } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
396                 $GcTime = $1; $GcElapsed = $2;
397
398                 # fix up mutator time now
399                 $MutTime    = sprintf("%.2f", ($MutTime    - $GcTime));
400                 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
401             }
402         }
403         close(STATS) || die "Failed when closing $StatsFile\n";
404     }
405
406     # warn about what we didn't find
407     print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
408     print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
409     print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
410     print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
411     print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
412     print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
413     print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
414     print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
415     print STDERR "Warning: total memory not found in stats file\n" unless defined($TotMem);
416     print STDERR "Warning: GC work not found in stats file\n" unless defined($GCWork);
417
418     # things we didn't necessarily expect to find
419     $MaxResidency     = 0 unless defined($MaxResidency);
420     $AvgResidency     = 0 unless defined($AvgResidency);
421     $ResidencySamples = 0 unless defined($ResidencySamples);
422
423     # a bit of tidying
424     $BytesAlloc =~ s/,//g;
425     $GCWork =~ s/,//g;
426     $MaxResidency =~ s/,//g;
427     $GCs =~ s/,//g;
428     $InitTime =~ s/,//g;
429     $InitElapsed =~ s/,//g;
430     $MutTime =~ s/,//g;
431     $MutElapsed =~ s/,//g;
432     $GcTime =~ s/,//g;
433     $GcElapsed =~ s/,//g;
434 }
435
436 sub process_cacheprof_files {
437
438     open(STATS, "< $CacheProfStats") || die("Can't open $CacheProfStats\n");
439
440     # the format of the info in this file is:
441     #    OTHER(intrs,reads,writes,read-misses,write-misses)
442     # where read-misses and write-misses will both be zero if we're
443     # just counting instructions.
444     while (<STATS>) {
445        /OTHER\(\s*([0-9]+),\s*([0-9]+),\s*([0-9]+),\s*([0-9]+),\s*([0-9]+)\)/ && do {
446            $TotInstrs = $1;
447            $TotReads  = $2;
448            $TotWrites = $3;
449       }
450   }
451   close(STATS);
452 }