2 # The perl script requires the following variables to be bound
3 # to something meaningful before it will operate correctly:
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"
16 # * a file to feed to stdin ( -i<file> ) [default: /dev/null ]
17 # * a "time" command to use (-t <cmd>).
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.
23 # Run the program with those options and that input, and check:
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...)
32 # (if the expected-output files' names end in .Z, then
33 # they are uncompressed before doing the comparison)
35 # (This is supposed to be a "prettier" replacement for runstdtest.)
39 # -accept-output replace output files with the ones actually generated by running
42 ($Pgm = $0) =~ s|.*/||;
49 $PgmStdinFile = '/dev/null';
50 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
51 $TmpPrefix = $ENV{'TMPDIR'};
53 $TmpPrefix ="$TMPDIR";
54 $ENV{'TMPDIR'} = "$TMPDIR"; # set the env var as well
56 $ScriptFile = "$TmpPrefix/run_me$$";
57 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
58 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
64 $StatsFile = "$TmpPrefix/stats$$";
65 $SysSpecificTiming = '';
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 !~ /^\//;
73 arg: while ($_ = $ARGV[0]) {
76 /^--$/ && do { # let anything past after --
77 push(@PgmArgs, @ARGV);
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; };
85 /^-O(.*)/ && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
86 /^-i(.*)/ && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
88 print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
89 if ! -f $PgmStdinFile;
91 /^-x(.*)/ && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
93 print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
94 if $PgmExitStatus !~ /^\d+$/;
96 /^-o1(.*)/ && do { $out_file = &grab_arg_arg('-o1',$1);
97 push(@PgmStdoutFile, $out_file);
99 /^-o2(.*)/ && do { $out_file = &grab_arg_arg('-o2',$1);
100 push(@PgmStderrFile, $out_file);
102 /^-prescript(.*)/ && do { $PreScript = &grab_arg_arg('-prescript',$1);
104 /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
106 /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
109 /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
111 /^-spix-timing$/ && do { $SysSpecificTiming = 'ghcspix';
114 /^-t(.*)/ && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
116 # anything else is taken to be a pgm arg
120 foreach $out_file ( @PgmStdoutFile ) {
121 if ( ! -f $out_file && !$SaveStdout ) {
122 print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
127 foreach $out_file ( @PgmStderrFile ) {
128 if ( ! -f $out_file && !$SaveStderr ) {
129 print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
136 # add on defaults if none specified
137 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
138 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
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.
145 foreach $a ( @PgmArgs ) {
146 if (! $grep_done && $a !~ /^-/ && -f $a) {
147 print `egrep "^--[ ]?!!!" $a`;
150 if ($a =~ /\s/ || $a =~ /'/) {
151 $a =~ s/'/\\'/g; # backslash the quotes;
152 $a = "\"$a\""; # quote the arg
156 # deal with system-specific timing options
158 if ( $SysSpecificTiming =~ /^ghc/ ) {
159 $TimingMagic = "+RTS -s$StatsFile -RTS"
160 } elsif ( $SysSpecificTiming eq 'hbc' ) {
161 $TimingMagic = "-S$StatsFile";
165 if ( $SpixTiming eq 'yes' ) {
168 # gotta find first/last addresses in the mutator code
169 $FirstSpix = '_callWrapper';
170 $LastSpix = '???'; # usually _mpz_get_si, but can't be sure
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)$/ ) {
179 close(SPIXNM); # || die "nm -n $ToRunOrig close failed!\n";
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";
187 $SpixstatsLine1 = '';
189 $SpixstatsLine2 = '';
192 if ($PreScript ne '') {
194 $PreScriptLines = `cat $PreScript`;
196 $PreScriptLines = '';
199 if ($PostScript ne '') {
201 $PostScriptLines = `cat $PostScript`;
203 $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
204 $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
206 $PostScriptLines = '';
209 # OK, so we're gonna do the normal thing...
211 $Script = <<EOSCRIPT;
215 rm -f $DefaultStdoutFile $DefaultStderrFile
216 cat /dev/null > $DefaultStdoutFile
217 cat /dev/null > $DefaultStderrFile
220 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
222 if [ \$progexit -ne $PgmExitStatus ]; then
223 echo $ToRun @PgmArgs \\< $PgmStdinFile
224 echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
229 for out_file in @PgmStdoutFile ; do
230 if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
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
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
250 cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
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
257 for out_file in @PgmStderrFile ; do
258 if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
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
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
278 cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
282 if [ $SpixTiming = 'yes' -a \$myexit = 0 ] ; then
284 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> /dev/null 2> /dev/null 3> $TmpPrefix/runtest$$.3\'
288 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
292 # bung script into a file
293 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
295 close(SCR) || die "Failed closing script file!\n";
296 chmod 0755, $ScriptFile;
298 print STDERR $Script if $Verbose;
300 &run_something($ScriptFile);
302 if ( $SysSpecificTiming eq '' ) {
308 &process_stats_file();
309 &process_spixstats_files() if $SpixTiming eq 'yes';
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";
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";
328 local($option, $rest_of_arg) = @_;
331 return($rest_of_arg);
332 } elsif ($#ARGV >= 0) {
333 local($temp) = $ARGV[0]; shift(@ARGV);
336 print STDERR "$Pgm: no argument following $option option\n";
342 local($str_to_do) = @_;
344 # print STDERR "$str_to_do\n" if $Verbose;
346 local($return_val) = 0;
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;
359 exit (($return_val == 0) ? 0 : 1);
363 sub process_stats_file {
365 # OK, process system-specific stats file
366 if ( $SysSpecificTiming =~ /^ghc/ ) {
368 #NB: nearly the same as in GHC driver's -ghc-timing stuff
370 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
372 local($max_live) = 0;
373 local($tot_live) = 0; # for calculating residency stuff
374 local($tot_samples) = 0;
377 if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
378 $max_live = $1 if $max_live < $1;
383 $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
385 # if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
386 # $MaxResidency = $1; $ResidencySamples = $2;
389 $GCs = $1 if /^\s*([0-9,]+) collections? in generation 0/;
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;
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) ;
406 } elsif ( $SysSpecificTiming eq 'hbc' ) {
408 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
410 $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
412 $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
414 if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
415 $MutTime = $1; $MutElapsed = $2; # will fix up later
417 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
419 } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
420 $GcTime = $1; $GcElapsed = $2;
422 # fix up mutator time now
423 $MutTime = sprintf("%.2f", ($MutTime - $GcTime));
424 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
427 close(STATS) || die "Failed when closing $StatsFile\n";
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);
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);
446 $BytesAlloc =~ s/,//g;
447 $MaxResidency =~ s/,//g;
450 $InitElapsed =~ s/,//g;
452 $MutElapsed =~ s/,//g;
454 $GcElapsed =~ s/,//g;
457 sub process_spixstats_files { # 2 of them; one for mutator, one for GC
467 open(STATS, "< $ToRunOrig.spixstats$f") || die "Failed when opening $ToRunOrig.spixstats$f\n";
469 last if /^OPCODES \(STATIC\):/; # party over
471 next if /^OPCODES \(DYNAMIC\):/;
473 next if /^opcode\s+#executed/;
476 if ( /^ld\S*\s+(\d+)/ ) {
477 $LoadInsns[$f] += $1;
479 } elsif ( /^st\S*\s+(\d+)/ ) {
480 $StoreInsns[$f] += $1;
482 } elsif ( /^(jmpl|call|b\S*)\s+(\d+)/ ) {
483 $BranchInsns[$f] += $2;
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]);
491 } elsif ( /^\S+\s+(\d+)/ ) {
492 $OtherInsns[$f] += $1;
495 die "Funny line?? $_";
498 close(STATS) || die "Failed when closing $ToRunOrig.spixstats\n";