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.)
37 ($Pgm = $0) =~ s|.*/||;
43 $PgmStdinFile = '/dev/null';
44 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
45 $TmpPrefix = $ENV{'TMPDIR'};
47 $TmpPrefix ="$TMPDIR";
48 $ENV{'TMPDIR'} = "$TMPDIR"; # set the env var as well
50 $ScriptFile = "$TmpPrefix/run_me$$";
51 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
52 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
58 $StatsFile = "$TmpPrefix/stats$$";
59 $SysSpecificTiming = '';
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 !~ /^\//;
67 arg: while ($_ = $ARGV[0]) {
70 /^--$/ && do { # let anything past after --
71 push(@PgmArgs, @ARGV);
74 /^-v$/ && do { $Verbose = 1; next arg; };
75 /^-accept-output$/ && do { $SaveTmpFile = 1; next arg; };
77 /^-O(.*)/ && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
78 /^-i(.*)/ && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
80 print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
81 if ! -f $PgmStdinFile;
83 /^-x(.*)/ && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
85 print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
86 if $PgmExitStatus !~ /^\d+$/;
88 /^-o1(.*)/ && do { $out_file = &grab_arg_arg('-o1',$1);
89 push(@PgmStdoutFile, $out_file);
91 /^-o2(.*)/ && do { $out_file = &grab_arg_arg('-o2',$1);
92 push(@PgmStderrFile, $out_file);
94 /^-prescript(.*)/ && do { $PreScript = &grab_arg_arg('-prescript',$1);
96 /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
98 /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
101 /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
103 /^-spix-timing$/ && do { $SysSpecificTiming = 'ghcspix';
106 /^-t(.*)/ && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
108 # anything else is taken to be a pgm arg
112 foreach $out_file ( @PgmStdoutFile ) {
115 print STDERR "$Pgm: warning: expected-output file missing: $out_file\n"
119 foreach $out_file ( @PgmStderrFile ) {
122 print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n"
128 # add on defaults if none specified
129 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
130 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
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.
137 foreach $a ( @PgmArgs ) {
138 if (! $grep_done && $a !~ /^-/ && -f $a) {
139 print `egrep "^--!!!" $a`;
142 if ($a =~ /\s/ || $a =~ /'/) {
143 $a =~ s/'/\\'/g; # backslash the quotes;
144 $a = "\"$a\""; # quote the arg
148 # deal with system-specific timing options
150 if ( $SysSpecificTiming =~ /^ghc/ ) {
151 $TimingMagic = "+RTS -s$StatsFile -RTS"
152 } elsif ( $SysSpecificTiming eq 'hbc' ) {
153 $TimingMagic = "-S$StatsFile";
157 if ( $SpixTiming eq 'yes' ) {
160 # gotta find first/last addresses in the mutator code
161 $FirstSpix = '_callWrapper';
162 $LastSpix = '???'; # usually _mpz_get_si, but can't be sure
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)$/ ) {
171 close(SPIXNM); # || die "nm -n $ToRunOrig close failed!\n";
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";
179 $SpixstatsLine1 = '';
181 $SpixstatsLine2 = '';
184 if ($PreScript ne '') {
186 $PreScriptLines = `cat $PreScript`;
188 $PreScriptLines = '';
191 if ($PostScript ne '') {
193 $PostScriptLines = `cat $PostScript`;
195 $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
196 $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
198 $PostScriptLines = '';
201 # OK, so we're gonna do the normal thing...
203 $Script = <<EOSCRIPT;
207 rm -f $DefaultStdoutFile $DefaultStderrFile
208 cat /dev/null > $DefaultStdoutFile
209 cat /dev/null > $DefaultStderrFile
212 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
214 if [ \$progexit -ne $PgmExitStatus ]; then
215 echo $ToRun @PgmArgs \\< $PgmStdinFile
216 echo expected exit status $PgmExitStatus not seen \\; got \$progexit
221 for out_file in @PgmStdoutFile ; do
222 if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
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
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]
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
244 for out_file in @PgmStderrFile ; do
245 if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
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
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]
264 if [ $SpixTiming = 'yes' -a \$myexit = 0 ] ; then
266 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> /dev/null 2> /dev/null 3> $TmpPrefix/runtest$$.3\'
270 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
274 # bung script into a file
275 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
277 close(SCR) || die "Failed closing script file!\n";
278 chmod 0755, $ScriptFile;
280 print STDERR $Script if $Verbose;
282 &run_something($ScriptFile);
284 if ( $SysSpecificTiming eq '' ) {
290 &process_stats_file();
291 &process_spixstats_files() if $SpixTiming eq 'yes';
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";
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";
310 local($option, $rest_of_arg) = @_;
313 return($rest_of_arg);
314 } elsif ($#ARGV >= 0) {
315 local($temp) = $ARGV[0]; shift(@ARGV);
318 print STDERR "$Pgm: no argument following $option option\n";
324 local($str_to_do) = @_;
326 # print STDERR "$str_to_do\n" if $Verbose;
328 local($return_val) = 0;
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;
341 exit (($return_val == 0) ? 0 : 1);
345 sub process_stats_file {
347 # OK, process system-specific stats file
348 if ( $SysSpecificTiming =~ /^ghc/ ) {
350 #NB: nearly the same as in GHC driver's -ghc-timing stuff
352 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
354 local($max_live) = 0;
355 local($tot_live) = 0; # for calculating residency stuff
356 local($tot_samples) = 0;
359 if (! /Minor/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+\%/ ) {
360 $max_live = $1 if $max_live < $1;
365 $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
367 # if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
368 # $MaxResidency = $1; $ResidencySamples = $2;
371 $GCs = $1 if /^\s*([0-9,]+) garbage collections? performed/;
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;
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) ;
388 } elsif ( $SysSpecificTiming eq 'hbc' ) {
390 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
392 $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
394 $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
396 if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
397 $MutTime = $1; $MutElapsed = $2; # will fix up later
399 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
401 } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
402 $GcTime = $1; $GcElapsed = $2;
404 # fix up mutator time now
405 $MutTime = sprintf("%.2f", ($MutTime - $GcTime));
406 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
409 close(STATS) || die "Failed when closing $StatsFile\n";
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);
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);
428 $BytesAlloc =~ s/,//g;
429 $MaxResidency =~ s/,//g;
432 $InitElapsed =~ s/,//g;
434 $MutElapsed =~ s/,//g;
436 $GcElapsed =~ s/,//g;
439 sub process_spixstats_files { # 2 of them; one for mutator, one for GC
449 open(STATS, "< $ToRunOrig.spixstats$f") || die "Failed when opening $ToRunOrig.spixstats$f\n";
451 last if /^OPCODES \(STATIC\):/; # party over
453 next if /^OPCODES \(DYNAMIC\):/;
455 next if /^opcode\s+#executed/;
458 if ( /^ld\S*\s+(\d+)/ ) {
459 $LoadInsns[$f] += $1;
461 } elsif ( /^st\S*\s+(\d+)/ ) {
462 $StoreInsns[$f] += $1;
464 } elsif ( /^(jmpl|call|b\S*)\s+(\d+)/ ) {
465 $BranchInsns[$f] += $2;
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]);
473 } elsif ( /^\S+\s+(\d+)/ ) {
474 $OtherInsns[$f] += $1;
477 die "Funny line?? $_";
480 close(STATS) || die "Failed when closing $ToRunOrig.spixstats\n";