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|.*/||;
52 $PgmStdinFile = '/dev/null';
53 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
54 $TmpPrefix = $ENV{'TMPDIR'};
56 $TmpPrefix ="$DEFAULT_TMPDIR";
57 $ENV{'TMPDIR'} = "$DEFAULT_TMPDIR"; # set the env var as well
59 # If this is Cygwin, ignore eol and CR characters.
60 # Perhaps required for MSYS too, although the cygpath
61 # bit is hopefully unnecessary.
62 if ( `uname | grep CYGWIN` ) {
63 $CONTEXT_DIFF=$CONTEXT_DIFF . " --strip-trailing-cr" ;
64 $TmpPrefix = `cygpath -m $TmpPrefix | tr -d \\\\n`;
66 $ScriptFile = "$TmpPrefix/run_me$$";
67 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
68 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
74 $StatsFile = "$TmpPrefix/stats$$";
75 $CachegrindStats = "cachegrind.out.summary";
76 $SysSpecificTiming = '';
77 $SysCPUCounting = 0; # Use CPU counters
81 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
82 $ToRun = $ARGV[0]; shift(@ARGV);
83 # avoid picking up same-named thing from somewhere else on $PATH...
84 $ToRun = "./$ToRun" if -e "./$ToRun";
86 arg: while ($_ = $ARGV[0]) {
89 /^--$/ && do { # let anything past after --
90 push(@PgmArgs, @ARGV);
93 /^-v$/ && do { $Verbose = 1; next arg; };
94 /^-accept-output-stderr$/ && do { $SaveStderr = 1; next arg; };
95 /^-accept-output-stdout$/ && do { $SaveStdout = 1; next arg; };
96 /^-accept-output$/ && do { $SaveStdout = 1; $SaveStderr = 1; next arg; };
98 /^-stdout-binary/ && do { $StdoutBinary=1; next arg; };
99 /^-stdout-binary/ && do { $StderrBinary=1; next arg; };
101 /^-O(.*)/ && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
102 /^-i(.*)/ && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
104 print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
105 if $PgmStdinFile !~ /^\/dev\/.*$/ && ! -f $PgmStdinFile;
107 /^-fail/ && do { $PgmFail=1; next arg; };
108 /^-x(.*)/ && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
110 print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
111 if $PgmExitStatus !~ /^\d+$/;
113 /^-o1(.*)/ && do { $out_file = &grab_arg_arg('-o1',$1);
114 push(@PgmStdoutFile, $out_file);
116 /^-o2(.*)/ && do { $out_file = &grab_arg_arg('-o2',$1);
117 push(@PgmStderrFile, $out_file);
119 /^-prescript(.*)/ && do { $PreScript = &grab_arg_arg('-prescript',$1);
121 /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
123 /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
126 /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
128 /^-cpu-counting-(.)$/ && do { $SysCPUCounting = "$1";
130 /^-cachegrind$/ && do { $SysSpecificTiming = 'ghc-instrs';
133 /^-t(.*)/ && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
135 # anything else is taken to be a pgm arg
139 foreach $out_file ( @PgmStdoutFile ) {
140 if ( ! -f $out_file && !$SaveStdout ) {
141 print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
146 foreach $out_file ( @PgmStderrFile ) {
147 if ( ! -f $out_file && !$SaveStderr ) {
148 print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
155 # add on defaults if none specified
156 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
157 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
159 # tidy up the pgm args:
160 # (1) look for the "first input file"
161 # and grep it for "interesting" comments (-- !!! )
162 # (2) quote any args w/ whitespace in them.
164 foreach $a ( @PgmArgs ) {
165 if (! $grep_done && $a !~ /^-/ && -f $a) {
166 print `egrep "^--[ ]?!!!" $a`;
169 if ($a =~ /\s/ || $a =~ /'/) {
170 $a =~ s/'/\\'/g; # backslash the quotes;
171 $a = "\"$a\""; # quote the arg
175 # deal with system-specific timing options
177 if ( $SysSpecificTiming =~ /^ghc/ ) {
178 if ($SysCPUCounting) {
179 # Count specified CPU events
180 $cpu_counting_ghc = "-a$SysCPUCounting";
182 $cpu_counting_ghc = "";
184 $TimingMagic = "+RTS -S$StatsFile $cpu_counting_ghc -RTS"
185 } elsif ( $SysSpecificTiming eq 'hbc' ) {
186 $TimingMagic = "-S$StatsFile";
189 if ($PreScript ne '') {
191 $PreScriptLines = `cat $PreScript`;
192 $PreScriptLines =~ s/\r//g;
194 $PreScriptLines = '';
197 if ($PostScript ne '') {
199 $PostScriptLines = `cat $PostScript`;
200 $PostScriptLines =~ s/\r//g;
202 $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
203 $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
205 $PostScriptLines = '';
208 # OK, so we're gonna do the normal thing...
210 if ($Cachegrind eq 'yes') {
211 $CachegrindPrefix = "valgrind --tool=cachegrind --log-fd=9 9>$CachegrindStats";
213 $CachegrindPrefix = '';
216 $Script = <<EOSCRIPT;
220 rm -f $DefaultStdoutFile $DefaultStderrFile
221 cat /dev/null > $DefaultStdoutFile
222 cat /dev/null > $DefaultStderrFile
225 $TimeCmd /bin/sh -c \'$CachegrindPrefix $ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1.raw 2> $TmpPrefix/runtest$$.2.raw 3> $TmpPrefix/runtest$$.3.raw\'
227 if [ "$StdoutBinary" = "0" ]; then
228 dos2unix < $TmpPrefix/runtest$$.1.raw > $TmpPrefix/runtest$$.1
230 cp $TmpPrefix/runtest$$.1.raw $TmpPrefix/runtest$$.1
232 if [ "$StderrBinary" = "0" ]; then
233 dos2unix < $TmpPrefix/runtest$$.2.raw > $TmpPrefix/runtest$$.2
235 cp $TmpPrefix/runtest$$.2.raw $TmpPrefix/runtest$$.2
237 dos2unix < $TmpPrefix/runtest$$.3.raw > $TmpPrefix/runtest$$.3
238 if [ \$progexit -eq 0 ] && [ $PgmFail -ne 0 ]; then
239 echo $ToRun @PgmArgs \\< $PgmStdinFile
240 echo "****" expected a failure, but was successful
243 if [ \$progexit -ne $PgmExitStatus ] && [ $PgmFail -eq 0 ]; then
244 echo $ToRun @PgmArgs \\< $PgmStdinFile
245 echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
250 for out_file in @PgmStdoutFile ; do
251 if sed "s/\\r\$//" $TmpPrefix/runtest$$.1 | cmp -s \$out_file - ; then
255 if [ \$hit = 'NO' ] ; then
256 echo $ToRun @PgmArgs \\< $PgmStdinFile
257 echo expected stdout not matched by reality
258 orig_file="$PgmStdoutFile[0]";
259 [ ! -f \$orig_file ] && orig_file="/dev/null"
260 ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.1
264 if [ $SaveStdout = 1 ] &&
265 [ $PgmStdoutFile[0] != $DefaultStdoutFile ] && [ -s $TmpPrefix/runtest$$.1 ]; then
266 echo Saving away stdout output in $PgmStdoutFile[0] ...
267 if [ -f $PgmStdoutFile[0] ]; then
268 rm -f $PgmStdoutFile[0].bak
269 cp $PgmStdoutFile[0] $PgmStdoutFile[0].bak
271 cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
276 for out_file in @PgmStderrFile ; do
277 if sed "s/\\r\$//" $TmpPrefix/runtest$$.2 | cmp -s \$out_file - ; then
281 if [ \$hit = 'NO' ] ; then
282 echo $ToRun @PgmArgs \\< $PgmStdinFile
283 echo expected stderr not matched by reality
284 orig_file="$PgmStderrFile[0]"
285 [ ! -f \$orig_file ] && orig_file="/dev/null"
286 ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.2
290 if [ $SaveStderr = 1 ] &&
291 [ $PgmStderrFile[0] != $DefaultStderrFile ] && [ -s $TmpPrefix/runtest$$.2 ]; then
292 echo Saving away stderr output in $PgmStderrFile[0] ...
293 if [ -f $PgmStderrFile[0] ]; then
294 rm -f $PgmStderrFile[0].bak
295 cp $PgmStderrFile[0] $PgmStderrFile[0].bak
297 cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
300 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3 $TmpPrefix/runtest$$.1.raw $TmpPrefix/runtest$$.2.raw $TmpPrefix/runtest$$.3.raw
304 # bung script into a file
305 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
307 close(SCR) || die "Failed closing script file!\n";
308 chmod 0755, $ScriptFile;
310 print STDERR $Script if $Verbose;
312 &run_something($ScriptFile);
314 if ( $SysSpecificTiming eq '' ) {
320 &process_stats_file();
321 &process_cachegrind_files() if $Cachegrind eq 'yes';
323 # print out what we found
324 print STDERR "<<$SysSpecificTiming: ";
325 if ( $Cachegrind ne 'yes') {
326 print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed)$Counters";
328 print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed), $TotInstrs instructions, $TotReads memory reads, $TotWrites memory writes, $TotMisses L2 cache misses";
330 print STDERR " :$SysSpecificTiming>>\n";
338 local($option, $rest_of_arg) = @_;
340 if ($rest_of_arg ne "") {
341 return($rest_of_arg);
342 } elsif ($#ARGV >= 0) {
343 local($temp) = $ARGV[0]; shift(@ARGV);
346 print STDERR "$Pgm: no argument following $option option\n";
352 local($str_to_do) = @_;
354 # print STDERR "$str_to_do\n" if $Verbose;
356 local($return_val) = 0;
360 if ($return_val != 0) {
361 #ToDo: this return-value mangling is wrong
362 # local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
363 # $die_msg .= " (program not found)" if $return_val == 255;
364 # $die_msg .= " ($!)" if $Verbose && $! != 0;
369 exit (($return_val == 0) ? 0 : 1);
373 sub process_stats_file {
375 # OK, process system-specific stats file
376 if ( $SysSpecificTiming =~ /^ghc/ ) {
378 #NB: nearly the same as in GHC driver's -ghc-timing stuff
380 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
382 local($max_live) = 0;
383 local($tot_live) = 0; # for calculating residency stuff
384 local($tot_samples) = 0;
385 local($into_gc_counters) = 0; # once we reach into the GC counters part
386 local($counters) = "";
387 local($counter) = "";
392 if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
393 $max_live = $1 if $max_live < $1;
398 $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
400 if (/^\s*([0-9,]+) bytes copied during GC/) {
406 # if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
407 # $MaxResidency = $1; $ResidencySamples = $2;
410 $GCs = $1 if /^\s*([0-9,]+) collections? in generation 0/;
412 if ( /^\s+([0-9]+)\s+Mb total memory/ ) {
416 if ( /^\s*INIT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
417 $InitTime = $1; $InitElapsed = $2;
418 } elsif ( /^\s*MUT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
419 $MutTime = $1; $MutElapsed = $2;
420 } elsif ( /^\s*GC\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
421 $GcTime = $1; $GcElapsed = $2;
424 if ( /CPU GC counters/ ) {
425 # Counters that follow correspond to GC
426 $into_gc_counters = 1;
429 if ( /^\s+\(([A-Z_0-9]+)\)\s+:\s+([0-9,]+)/ ) {
432 $count =~ s/,//g; # Remove commas in numbers
433 # Pretty printing elements of a list with type [(String,[Float])]
434 # It's a bit lame for passing values but it works.
435 if($into_gc_counters) {
436 $counters = "$counters(\"GC:$counter\",[$count]),";
438 $counters = "$counters(\"$counter\",[$count]),";
442 if ( /^\s+\(([A-Z_0-9]+)\)\s+\%\s+of\s+\(([A-Z_0-9]+)\)\s+:\s+([0-9.]+)/ ) {
443 $counter = "$1/$2"; # Relative quantity
445 # Pretty printing elements of a list with type [(String,[Float])]
446 # It's a bit lame for passing values but it works.
447 if($into_gc_counters) {
448 $counters = "$counters(\"GC:$counter\",[$count]),";
450 $counters = "$counters(\"$counter\",[$count]),";
455 close(STATS) || die "Failed when closing $StatsFile\n";
456 if ( $tot_samples > 0 ) {
457 $ResidencySamples = $tot_samples;
458 $MaxResidency = $max_live;
459 $AvgResidency = int ($tot_live / $tot_samples) ;
462 if ( length($counters) == 0 ) {
465 chop($counters); # remove trailing comma from the last entry
466 $Counters = " [$counters]";
469 } elsif ( $SysSpecificTiming eq 'hbc' ) {
471 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
473 $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
475 $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
477 if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
478 $MutTime = $1; $MutElapsed = $2; # will fix up later
480 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
482 } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
483 $GcTime = $1; $GcElapsed = $2;
485 # fix up mutator time now
486 $MutTime = sprintf("%.2f", ($MutTime - $GcTime));
487 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
490 close(STATS) || die "Failed when closing $StatsFile\n";
493 # warn about what we didn't find
494 print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
495 print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
496 print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
497 print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
498 print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
499 print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
500 print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
501 print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
502 print STDERR "Warning: total memory not found in stats file\n" unless defined($TotMem);
503 print STDERR "Warning: GC work not found in stats file\n" unless defined($GCWork);
505 # things we didn't necessarily expect to find
506 $MaxResidency = 0 unless defined($MaxResidency);
507 $AvgResidency = 0 unless defined($AvgResidency);
508 $ResidencySamples = 0 unless defined($ResidencySamples);
511 $BytesAlloc =~ s/,//g;
512 $MaxResidency =~ s/,//g;
515 $InitElapsed =~ s/,//g;
517 $MutElapsed =~ s/,//g;
519 $GcElapsed =~ s/,//g;
522 sub process_cachegrind_files {
524 open(STATS, "< $CachegrindStats") || die("Can't open $CachegrindStats\n");
527 /^==\d+==\s+I\s+refs:\s+([0-9,]*)/ && do {
529 $TotInstrs =~ s/,//g;
532 /^==\d+==\s+D\s+refs:\s+[0-9,]+\s+\(([0-9,]+)\s+rd\s+\+\s+([0-9,]+)\s+wr/ && do {
536 $TotWrites =~ s/,//g;
539 /^==\d+==\s+L2d\s+misses:\s+([0-9,]+)/ && do {
541 $TotMisses =~ s/,//g;