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