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|.*/||;
50 $PgmStdinFile = '/dev/null';
51 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
52 $TmpPrefix = $ENV{'TMPDIR'};
54 $TmpPrefix ="$DEFAULT_TMPDIR";
55 $ENV{'TMPDIR'} = "$DEFAULT_TMPDIR"; # set the env var as well
57 $ScriptFile = "$TmpPrefix/run_me$$";
58 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
59 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
67 $HeimdallStats = "heimdall.summary";
68 $StatsFile = "$TmpPrefix/stats$$";
69 $CacheProfStats = "cacheprof.out.summary";
70 $SysSpecificTiming = '';
73 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
74 $ToRun = $ARGV[0]; shift(@ARGV);
75 # avoid picking up same-named thing from somewhere else on $PATH...
76 $ToRun = "./$ToRun" if -e "./$ToRun";
78 arg: while ($_ = $ARGV[0]) {
81 /^--$/ && do { # let anything past after --
82 push(@PgmArgs, @ARGV);
85 /^-v$/ && do { $Verbose = 1; next arg; };
86 /^-accept-output-stderr$/ && do { $SaveStderr = 1; next arg; };
87 /^-accept-output-stdout$/ && do { $SaveStdout = 1; next arg; };
88 /^-accept-output$/ && do { $SaveStdout = 1; $SaveStderr = 1; next arg; };
90 /^-O(.*)/ && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
91 /^-i(.*)/ && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
93 print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
94 if $PgmStdinFile !~ /^\/dev\/.*$/ && ! -f $PgmStdinFile;
96 /^-fail/ && do { $PgmFail=1; next arg; };
97 /^-x(.*)/ && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
99 print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
100 if $PgmExitStatus !~ /^\d+$/;
102 /^-o1(.*)/ && do { $out_file = &grab_arg_arg('-o1',$1);
103 push(@PgmStdoutFile, $out_file);
105 /^-o2(.*)/ && do { $out_file = &grab_arg_arg('-o2',$1);
106 push(@PgmStderrFile, $out_file);
108 /^-prescript(.*)/ && do { $PreScript = &grab_arg_arg('-prescript',$1);
110 /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
112 /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
115 /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
117 /^-cacheprof$/ && do { $SysSpecificTiming = 'ghc-instrs';
120 /^-heimdall$/ && do { $HeimdallPrefix = 'heimdall_cp '; # must have trailing space!
121 $SysSpecificTiming = 'ghc-instrs';
124 /^-t(.*)/ && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
126 # anything else is taken to be a pgm arg
130 foreach $out_file ( @PgmStdoutFile ) {
131 if ( ! -f $out_file && !$SaveStdout ) {
132 print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
137 foreach $out_file ( @PgmStderrFile ) {
138 if ( ! -f $out_file && !$SaveStderr ) {
139 print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
146 # add on defaults if none specified
147 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
148 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
150 # tidy up the pgm args:
151 # (1) look for the "first input file"
152 # and grep it for "interesting" comments (-- !!! )
153 # (2) quote any args w/ whitespace in them.
155 foreach $a ( @PgmArgs ) {
156 if (! $grep_done && $a !~ /^-/ && -f $a) {
157 print `egrep "^--[ ]?!!!" $a`;
160 if ($a =~ /\s/ || $a =~ /'/) {
161 $a =~ s/'/\\'/g; # backslash the quotes;
162 $a = "\"$a\""; # quote the arg
166 # deal with system-specific timing options
168 if ( $SysSpecificTiming =~ /^ghc/ ) {
169 $TimingMagic = "+RTS -S$StatsFile -RTS"
170 } elsif ( $SysSpecificTiming eq 'hbc' ) {
171 $TimingMagic = "-S$StatsFile";
174 if ($PreScript ne '') {
176 $PreScriptLines = `cat $PreScript`;
177 $PreScriptLines =~ s/\r//g;
179 $PreScriptLines = '';
182 if ($PostScript ne '') {
184 $PostScriptLines = `cat $PostScript`;
185 $PostScriptLines =~ s/\r//g;
187 $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
188 $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
190 $PostScriptLines = '';
193 # OK, so we're gonna do the normal thing...
195 $Script = <<EOSCRIPT;
199 rm -f $DefaultStdoutFile $DefaultStderrFile
200 cat /dev/null > $DefaultStdoutFile
201 cat /dev/null > $DefaultStderrFile
204 $TimeCmd /bin/sh -c \'$HeimdallPrefix$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
206 if [ \$progexit -eq 0 ] && [ $PgmFail -ne 0 ]; then
207 echo $ToRun @PgmArgs \\< $PgmStdinFile
208 echo "****" expected a failure, but was successful
211 if [ \$progexit -ne $PgmExitStatus ] && [ $PgmFail -eq 0 ]; then
212 echo $ToRun @PgmArgs \\< $PgmStdinFile
213 echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
218 for out_file in @PgmStdoutFile ; do
219 if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
223 if [ \$hit = 'NO' ] ; then
224 echo $ToRun @PgmArgs \\< $PgmStdinFile
225 echo expected stdout not matched by reality
226 orig_file="$PgmStdoutFile[0]";
227 [ ! -f \$orig_file ] && orig_file="/dev/null"
228 ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.1
232 if [ $SaveStdout = 1 ] &&
233 [ $PgmStdoutFile[0] != $DefaultStdoutFile ] && [ -s $TmpPrefix/runtest$$.1 ]; then
234 echo Saving away stdout output in $PgmStdoutFile[0] ...
235 if [ -f $PgmStdoutFile[0] ]; then
236 rm -f $PgmStdoutFile[0].bak
237 cp $PgmStdoutFile[0] $PgmStdoutFile[0].bak
239 cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
242 if [ "$Heimdall" = "1" ]; then
243 egrep '^=== HEIMDALL: ' < $TmpPrefix/runtest$$.2 > $HeimdallStats
245 # egrep -v '^=== HEIMDALL: ' < $TmpPrefix/runtest$$.2 > $TmpPrefix/runtest$$.2b
246 # mv -f $TmpPrefix/runtest$$.2b $TmpPrefix/runtest$$.2
249 for out_file in @PgmStderrFile ; do
250 if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
254 if [ \$hit = 'NO' ] ; then
255 echo $ToRun @PgmArgs \\< $PgmStdinFile
256 echo expected stderr not matched by reality
257 orig_file="$PgmStderrFile[0]"
258 [ ! -f \$orig_file ] && orig_file="/dev/null"
259 ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.2
263 if [ $SaveStderr = 1 ] &&
264 [ $PgmStderrFile[0] != $DefaultStderrFile ] && [ -s $TmpPrefix/runtest$$.2 ]; then
265 echo Saving away stderr output in $PgmStderrFile[0] ...
266 if [ -f $PgmStderrFile[0] ]; then
267 rm -f $PgmStderrFile[0].bak
268 cp $PgmStderrFile[0] $PgmStderrFile[0].bak
270 cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
273 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
277 # bung script into a file
278 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
280 close(SCR) || die "Failed closing script file!\n";
281 chmod 0755, $ScriptFile;
283 print STDERR $Script if $Verbose;
285 &run_something($ScriptFile);
287 if ( $SysSpecificTiming eq '' ) {
293 &process_stats_file();
294 &process_cacheprof_files() if $CacheProf eq 'yes';
295 &process_heimdall_file() if $Heimdall;
297 # print out what we found
298 print STDERR "<<$SysSpecificTiming: ";
299 if ( $CacheProf ne 'yes' && !$Heimdall) {
300 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)";
302 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";
304 print STDERR " :$SysSpecificTiming>>\n";
312 local($option, $rest_of_arg) = @_;
315 return($rest_of_arg);
316 } elsif ($#ARGV >= 0) {
317 local($temp) = $ARGV[0]; shift(@ARGV);
320 print STDERR "$Pgm: no argument following $option option\n";
326 local($str_to_do) = @_;
328 # print STDERR "$str_to_do\n" if $Verbose;
330 local($return_val) = 0;
334 if ($return_val != 0) {
335 #ToDo: this return-value mangling is wrong
336 # local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
337 # $die_msg .= " (program not found)" if $return_val == 255;
338 # $die_msg .= " ($!)" if $Verbose && $! != 0;
343 exit (($return_val == 0) ? 0 : 1);
347 sub process_stats_file {
349 # OK, process system-specific stats file
350 if ( $SysSpecificTiming =~ /^ghc/ ) {
352 #NB: nearly the same as in GHC driver's -ghc-timing stuff
354 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
356 local($max_live) = 0;
357 local($tot_live) = 0; # for calculating residency stuff
358 local($tot_samples) = 0;
361 if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
362 $max_live = $1 if $max_live < $1;
367 $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
368 $GCWork = $1 if /^\s*([0-9,]+) bytes copied during GC/;
370 # if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
371 # $MaxResidency = $1; $ResidencySamples = $2;
374 $GCs = $1 if /^\s*([0-9,]+) collections? in generation 0/;
376 if ( /^\s+([0-9]+)\s+Mb total memory/ ) {
380 if ( /^\s*INIT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
381 $InitTime = $1; $InitElapsed = $2;
382 } elsif ( /^\s*MUT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
383 $MutTime = $1; $MutElapsed = $2;
384 } elsif ( /^\s*GC\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
385 $GcTime = $1; $GcElapsed = $2;
388 close(STATS) || die "Failed when closing $StatsFile\n";
389 if ( $tot_samples > 0 ) {
390 $ResidencySamples = $tot_samples;
391 $MaxResidency = $max_live;
392 $AvgResidency = int ($tot_live / $tot_samples) ;
395 } elsif ( $SysSpecificTiming eq 'hbc' ) {
397 open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
399 $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
401 $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
403 if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
404 $MutTime = $1; $MutElapsed = $2; # will fix up later
406 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
408 } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
409 $GcTime = $1; $GcElapsed = $2;
411 # fix up mutator time now
412 $MutTime = sprintf("%.2f", ($MutTime - $GcTime));
413 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
416 close(STATS) || die "Failed when closing $StatsFile\n";
419 # warn about what we didn't find
420 print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
421 print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
422 print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
423 print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
424 print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
425 print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
426 print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
427 print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
428 print STDERR "Warning: total memory not found in stats file\n" unless defined($TotMem);
429 print STDERR "Warning: GC work not found in stats file\n" unless defined($GCWork);
431 # things we didn't necessarily expect to find
432 $MaxResidency = 0 unless defined($MaxResidency);
433 $AvgResidency = 0 unless defined($AvgResidency);
434 $ResidencySamples = 0 unless defined($ResidencySamples);
437 $BytesAlloc =~ s/,//g;
439 $MaxResidency =~ s/,//g;
442 $InitElapsed =~ s/,//g;
444 $MutElapsed =~ s/,//g;
446 $GcElapsed =~ s/,//g;
449 sub process_cacheprof_files {
451 open(STATS, "< $CacheProfStats") || die("Can't open $CacheProfStats\n");
453 # the format of the info in this file is:
454 # OTHER(intrs,reads,writes,read-misses,write-misses)
455 # where read-misses and write-misses will both be zero if we're
456 # just counting instructions.
458 /OTHER\(\s*([0-9]+),\s*([0-9]+),\s*([0-9]+),\s*([0-9]+),\s*([0-9]+)\)/ && do {
467 sub process_heimdall_file {
469 open(STATS, "< $HeimdallStats") || die("Can't open $HeimdallStats\n");
471 # instruction counts from Julian Seward's HEIMDALL:
473 # === HEIMDALL: instruction counts: 815000 instrs, 230040 reads, 138692 writes
475 /^=== HEIMDALL: instruction counts: ([0-9]+) instrs, ([0-9]+) reads, ([0-9]+) writes/