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