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