[project @ 1999-07-15 09:56:04 by simonmar]
[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 $SysSpecificTiming = '';
67 $SpixTiming = 'no';
68
69 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
70 $ToRun = $ARGV[0]; shift(@ARGV);
71 # avoid picking up same-named thing from somewhere else on $PATH...
72 $ToRun = "./$ToRun" if $ToRun !~ /^\//;
73
74 arg: while ($_ = $ARGV[0]) {
75     shift(@ARGV);
76     
77     /^--$/      && do { # let anything past after --
78                         push(@PgmArgs, @ARGV);
79                         last arg; };
80
81     /^-v$/             && do { $Verbose = 1; next arg; };
82     /^-accept-output-stderr$/ && do { $SaveStderr = 1; next arg; };
83     /^-accept-output-stdout$/ && do { $SaveStdout = 1; next arg; };
84     /^-accept-output$/        && do { $SaveStdout = 1; $SaveStderr = 1; next arg; };
85
86     /^-O(.*)/   && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
87     /^-i(.*)/   && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
88                         $Status++,
89                         print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
90                             if $PgmStdinFile !~ /^\/dev\/.*$/ && ! -f $PgmStdinFile;
91                         next arg; };
92     /^-fail/    && do { $PgmFail=1; next arg; };
93     /^-x(.*)/   && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
94                         $Status++ ,
95                         print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
96                             if $PgmExitStatus !~ /^\d+$/;
97                         next arg; };
98     /^-o1(.*)/  && do { $out_file = &grab_arg_arg('-o1',$1);
99                         push(@PgmStdoutFile, $out_file);
100                         next arg; };
101     /^-o2(.*)/  && do { $out_file = &grab_arg_arg('-o2',$1);
102                         push(@PgmStderrFile, $out_file);
103                         next arg; };
104     /^-prescript(.*)/  && do { $PreScript = &grab_arg_arg('-prescript',$1);
105                                 next arg; };
106     /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
107                                 next arg; };
108     /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
109                     $Status++;
110                     next arg; };
111     /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
112                                   next arg; };
113     /^-spix-timing$/ && do { $SysSpecificTiming = 'ghcspix';
114                              $SpixTiming = 'yes';
115                              next arg; };
116     /^-t(.*)/   && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
117
118     # anything else is taken to be a pgm arg
119     push(@PgmArgs, $_);
120 }
121
122 foreach $out_file ( @PgmStdoutFile ) {
123     if ( ! -f $out_file && !$SaveStdout ) {
124             print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
125             pop(@PgmStdoutFile);
126     }
127 }
128
129 foreach $out_file ( @PgmStderrFile ) {
130     if ( ! -f $out_file && !$SaveStderr ) {
131             print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
132             pop(@PgmStderrFile);
133     }
134 }
135
136 exit 1 if $Status;
137
138 # add on defaults if none specified
139 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
140 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
141
142 # tidy up the pgm args:
143 # (1) look for the "first input file"
144 #     and grep it for "interesting" comments (-- !!! )
145 # (2) quote any args w/ whitespace in them.
146 $grep_done = 0;
147 foreach $a ( @PgmArgs ) {
148     if (! $grep_done && $a !~ /^-/ && -f $a) {
149         print `egrep "^--[ ]?!!!" $a`;
150         $grep_done = 1;
151     }
152     if ($a =~ /\s/ || $a =~ /'/) {
153         $a =~ s/'/\\'/g;    # backslash the quotes;
154         $a = "\"$a\"";      # quote the arg
155     }
156 }
157
158 # deal with system-specific timing options
159 $TimingMagic = '';
160 if ( $SysSpecificTiming =~ /^ghc/ ) {
161     $TimingMagic = "+RTS -S$StatsFile -RTS"
162 } elsif ( $SysSpecificTiming eq 'hbc' ) {
163     $TimingMagic = "-S$StatsFile";
164 }
165
166 $ToRunOrig = $ToRun;
167 if ( $SpixTiming eq 'yes' ) {
168     $ToRun .= '.spix';
169
170     # gotta find first/last addresses in the mutator code
171     $FirstSpix = '_callWrapper';
172     $LastSpix  = '???'; # usually _mpz_get_si, but can't be sure
173
174     open(SPIXNM, "nm -n $ToRunOrig |") || die "nm -n $ToRunOrig open failed!\n";
175     spix: while (<SPIXNM>) {
176         if ( / T +(_freeForeignObj|_([A-Za-z]+)Hook|_xmalloc|_mpz_get_si)$/ ) {
177             $LastSpix = $1;
178             last spix;
179         }
180     }
181     close(SPIXNM); # || die "nm -n $ToRunOrig close failed!\n";
182
183     $SpixifyLine1 = "spix -o $ToRun -t$FirstSpix,$LastSpix $ToRunOrig";
184     $SpixstatsLine1 = "spixstats -b $TmpPrefix/runtest$$.3 $ToRunOrig > $ToRunOrig.spixstats1";
185     $SpixifyLine2 = "spix -o $ToRun +t$FirstSpix,$LastSpix $ToRunOrig";
186     $SpixstatsLine2 = "spixstats -b $TmpPrefix/runtest$$.3 $ToRunOrig > $ToRunOrig.spixstats2";
187 } else {
188     $SpixifyLine1 = '';
189     $SpixstatsLine1 = '';
190     $SpixifyLine2 = '';
191     $SpixstatsLine2 = '';
192 }
193
194 if ($PreScript ne '') {
195     local($to_do);
196     $PreScriptLines = `cat $PreScript`;
197 } else {
198     $PreScriptLines = '';
199 }
200
201 if ($PostScript ne '') {
202     local($to_do);
203     $PostScriptLines = `cat $PostScript`;
204     $* = 1;
205     $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
206     $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
207 } else {
208     $PostScriptLines = '';
209 }
210
211 # OK, so we're gonna do the normal thing...
212
213 $Script = <<EOSCRIPT;
214 #! /bin/sh
215 myexit=0
216 diffsShown=0
217 rm -f $DefaultStdoutFile $DefaultStderrFile
218 cat /dev/null > $DefaultStdoutFile
219 cat /dev/null > $DefaultStderrFile
220 $PreScriptLines
221 $SpixifyLine1
222 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
223 progexit=\$?
224 if [ \$progexit -eq 0 ] && [ $PgmFail -ne 0 ]; then
225     echo $ToRun @PgmArgs \\< $PgmStdinFile
226     echo "****" expected a failure, but was successful
227     myexit=1
228 fi
229 if [ \$progexit -ne $PgmExitStatus ] && [ $PgmFail -eq 0 ]; then
230     echo $ToRun @PgmArgs \\< $PgmStdinFile
231     echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
232     myexit=1
233 else
234     $PostScriptLines
235     hit='NO'
236     for out_file in @PgmStdoutFile ; do
237         if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
238             hit='YES'
239         fi
240     done
241     if [ \$hit = 'NO' ] ; then
242         echo $ToRun @PgmArgs \\< $PgmStdinFile
243         echo expected stdout not matched by reality
244         orig_file="$PgmStdoutFile[0]";
245         [ ! -f \$orig_file ] && orig_file="/dev/null"
246         ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.1
247         myexit=\$?
248         diffsShown=1
249     fi
250     if [ $SaveStdout = 1 ] && [ \$progexit = $PgmExitStatus ] && 
251        [ $PgmStdoutFile[0] != $DefaultStdoutFile ] && [ -s $TmpPrefix/runtest$$.1 ]; then
252         echo Saving away stdout output in $PgmStdoutFile[0] ...
253         if [ -f $PgmStdoutFile[0] ]; then
254              rm -f $PgmStdoutFile[0].bak
255              cp $PgmStdoutFile[0] $PgmStdoutFile[0].bak
256         fi;
257         cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
258     fi
259 fi
260 egrep -v '^ld\.so:.*has older revision than expected' < $TmpPrefix/runtest$$.2 > $TmpPrefix/runtest$$.2b
261 mv -f $TmpPrefix/runtest$$.2b $TmpPrefix/runtest$$.2
262
263 hit='NO'
264 for out_file in @PgmStderrFile ; do
265     if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
266         hit='YES'
267     fi
268 done
269 if [ \$hit = 'NO' ] ; then
270     echo $ToRun @PgmArgs \\< $PgmStdinFile
271     echo expected stderr not matched by reality
272     orig_file="$PgmStderrFile[0]"
273     [ ! -f \$orig_file ] && orig_file="/dev/null"
274     ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.2
275     myexit=\$?
276     diffsShown=1
277 fi
278 if [ $SaveStderr = 1 ] && [ \$progexit = $PgmExitStatus ] && 
279    [ $PgmStderrFile[0] != $DefaultStderrFile ] && [ -s $TmpPrefix/runtest$$.2 ]; then
280         echo Saving away stderr output in $PgmStderrFile[0] ...
281         if [ -f $PgmStderrFile[0] ]; then
282            rm -f $PgmStderrFile[0].bak
283            cp $PgmStderrFile[0] $PgmStderrFile[0].bak
284         fi;
285         cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
286 fi
287 $SpixstatsLine1
288
289 if [ $SpixTiming = 'yes' -a \$myexit = 0 ] ; then
290     $SpixifyLine2
291     $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> /dev/null 2> /dev/null 3> $TmpPrefix/runtest$$.3\'
292     $SpixstatsLine2
293 fi
294
295 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
296 exit \$myexit
297 EOSCRIPT
298
299 # bung script into a file
300 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
301 print SCR $Script;
302 close(SCR) || die "Failed closing script file!\n";
303 chmod 0755, $ScriptFile;
304
305 print STDERR $Script if $Verbose;
306
307 &run_something($ScriptFile);
308
309 if ( $SysSpecificTiming eq '' ) {
310     unlink $StatsFile;
311     unlink $ScriptFile;
312     exit 0;
313 }
314
315 &process_stats_file();
316 &process_spixstats_files() if $SpixTiming eq 'yes';
317
318 # print out what we found
319 if ( $SpixTiming ne 'yes' ) {
320     print STDERR "<<$SysSpecificTiming: ",
321         "$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)",
322         " :$SysSpecificTiming>>\n";
323 } else {
324     print STDERR "<<$SysSpecificTiming: ",
325         "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $TotalInsns[1]/$TotalInsns[2] instructions, $LoadInsns[1]/$LoadInsns[2] loads, $StoreInsns[1]/$StoreInsns[2] stores, $BranchInsns[1]/$BranchInsns[2] branches, $OtherInsns[1]/$OtherInsns[2] others",
326         " :$SysSpecificTiming>>\n";
327 }
328
329 # OK, party over
330 unlink $StatsFile;
331 unlink $ScriptFile;
332 exit 0;
333
334 sub grab_arg_arg {
335     local($option, $rest_of_arg) = @_;
336     
337     if ($rest_of_arg) {
338         return($rest_of_arg);
339     } elsif ($#ARGV >= 0) {
340         local($temp) = $ARGV[0]; shift(@ARGV); 
341         return($temp);
342     } else {
343         print STDERR "$Pgm: no argument following $option option\n";
344         $Status++;
345     }
346 }
347
348 sub run_something {
349     local($str_to_do) = @_;
350
351 #   print STDERR "$str_to_do\n" if $Verbose;
352
353     local($return_val) = 0;
354     system($str_to_do);
355     $return_val = $?;
356
357     if ($return_val != 0) {
358 #ToDo: this return-value mangling is wrong
359 #       local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
360 #       $die_msg .= " (program not found)" if $return_val == 255;
361 #       $die_msg .= " ($!)" if $Verbose && $! != 0;
362 #       $die_msg .= "\n";
363         unlink $ScriptFile;
364         unlink $StatsFile;
365
366         exit (($return_val == 0) ? 0 : 1);
367     }
368 }
369
370 sub process_stats_file {
371
372     # OK, process system-specific stats file
373     if ( $SysSpecificTiming =~ /^ghc/ ) {
374
375         #NB: nearly the same as in GHC driver's -ghc-timing stuff
376
377         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
378
379         local($max_live)    = 0; 
380         local($tot_live)    = 0; # for calculating residency stuff
381         local($tot_samples) = 0;
382
383         while (<STATS>) {
384             if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
385                 $max_live = $1 if $max_live < $1;
386                 $tot_live += $1;
387                 $tot_samples += 1;
388             }
389
390             $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
391             $GCWork = $1     if /^\s*([0-9,]+) bytes copied during GC/;
392
393 #           if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
394 #               $MaxResidency = $1; $ResidencySamples = $2;
395 #           }
396
397             $GCs = $1 if /^\s*([0-9,]+) collections? in generation 0/;
398
399             if ( /^\s+([0-9]+)\s+Mb total memory/ ) {
400                 $TotMem = $1;
401             }
402
403             if ( /^\s*INIT\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
404                 $InitTime = $1; $InitElapsed = $2;
405             } elsif ( /^\s*MUT\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
406                 $MutTime = $1; $MutElapsed = $2;
407             } elsif ( /^\s*GC\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
408                 $GcTime = $1; $GcElapsed = $2;
409             }
410         }
411         close(STATS) || die "Failed when closing $StatsFile\n";
412         if ( $tot_samples > 0 ) {
413             $ResidencySamples = $tot_samples;
414             $MaxResidency = $max_live;
415             $AvgResidency = int ($tot_live / $tot_samples) ;
416         }
417
418     } elsif ( $SysSpecificTiming eq 'hbc' ) {
419
420         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
421         while (<STATS>) {
422             $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
423
424             $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
425
426             if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
427                 $MutTime = $1; $MutElapsed = $2; # will fix up later
428
429                 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
430
431             } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
432                 $GcTime = $1; $GcElapsed = $2;
433
434                 # fix up mutator time now
435                 $MutTime    = sprintf("%.2f", ($MutTime    - $GcTime));
436                 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
437             }
438         }
439         close(STATS) || die "Failed when closing $StatsFile\n";
440     }
441
442     # warn about what we didn't find
443     print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
444     print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
445     print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
446     print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
447     print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
448     print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
449     print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
450     print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
451     print STDERR "Warning: total memory not found in stats file\n" unless defined($TotMem);
452     print STDERR "Warning: GC work not found in stats file\n" unless defined($GCWork);
453
454     # things we didn't necessarily expect to find
455     $MaxResidency     = 0 unless defined($MaxResidency);
456     $AvgResidency     = 0 unless defined($AvgResidency);
457     $ResidencySamples = 0 unless defined($ResidencySamples);
458
459     # a bit of tidying
460     $BytesAlloc =~ s/,//g;
461     $GCWork =~ s/,//g;
462     $MaxResidency =~ s/,//g;
463     $GCs =~ s/,//g;
464     $InitTime =~ s/,//g;
465     $InitElapsed =~ s/,//g;
466     $MutTime =~ s/,//g;
467     $MutElapsed =~ s/,//g;
468     $GcTime =~ s/,//g;
469     $GcElapsed =~ s/,//g;
470 }
471
472 sub process_spixstats_files { # 2 of them; one for mutator, one for GC
473
474     @TotalInsns = ();
475     @LoadInsns  = ();
476     @StoreInsns = ();
477     @BranchInsns= ();
478     @OtherInsns = ();
479
480     foreach $f (1, 2) {
481
482       open(STATS, "< $ToRunOrig.spixstats$f") || die "Failed when opening $ToRunOrig.spixstats$f\n";
483       while (<STATS>) {
484           last if /^OPCODES \(STATIC\):/; # party over
485
486           next if /^OPCODES \(DYNAMIC\):/;
487           next if /^$/;
488           next if /^opcode\s+#executed/;
489           next if /^SUBTOTAL/;
490
491           if ( /^ld\S*\s+(\d+)/ ) {
492               $LoadInsns[$f] += $1;
493
494           } elsif ( /^st\S*\s+(\d+)/ ) {
495               $StoreInsns[$f] += $1;
496
497           } elsif ( /^(jmpl|call|b\S*)\s+(\d+)/ ) {
498               $BranchInsns[$f] += $2;
499
500           } elsif ( /^TOTAL\s+(\d+)/ ) {
501               $TotalInsns[$f] = $1;
502               print STDERR "TotalInsns doesn't match categories total!\n"
503                   if $TotalInsns[$f] !=
504                      ($LoadInsns[$f] + $StoreInsns[$f] + $BranchInsns[$f] + $OtherInsns[$f]);
505
506           } elsif ( /^\S+\s+(\d+)/ ) {
507               $OtherInsns[$f] += $1;
508
509           } else {
510               die "Funny line?? $_";
511           }
512       }
513       close(STATS) || die "Failed when closing $ToRunOrig.spixstats\n";
514     }
515 }