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