[project @ 1996-07-19 18:36:04 by partain]
[ghc-hetmet.git] / glafp-utils / scripts / runstdtest.prl
1 #! /usr/local/bin/perl
2 #
3 # Given:
4 #       * a program to run (1st arg)
5 #       * some "command-line opts" ( -O<opt1> -O<opt2> ... )
6 #           [default: anything on the cmd line this script doesn't recognise ]
7 #         the first opt not starting w/ "-" is taken to be an input
8 #         file and (if it exists) is grepped for "what's going on here"
9 #         comments (^--!!!).
10 #       * a file to feed to stdin ( -i<file> ) [default: /dev/null ]
11 #       * a "time" command to use (-t <cmd>).
12 #
13 #       * alternatively, a "-script <script>" argument says: run the
14 #         named Bourne-shell script to do the test.  It's passed the
15 #         pgm-to-run as the one-and-only arg.
16 #
17 # Run the program with those options and that input, and check:
18 # if we get...
19
20 #       * an expected exit status ( -x <val> ) [ default 0 ]
21 #       * expected output on stdout ( -o1 <file> ) [ default /dev/null ]
22 #               ( we'll accept one of several...)
23 #       * expected output on stderr ( -o2 <file> ) [ default /dev/null ]
24 #               ( we'll accept one of several...)
25 #
26 #       (if the expected-output files' names end in .Z, then
27 #        they are uncompressed before doing the comparison)
28
29 # (This is supposed to be a "prettier" replacement for runstdtest.)
30 #
31 ($Pgm = $0) =~ s|.*/||;
32 $Verbose = 0;
33 $Status = 0;
34 @PgmArgs = ();
35 $PgmExitStatus = 0;
36 $PgmStdinFile  = '/dev/null';
37 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
38     $TmpPrefix = $ENV{'TMPDIR'};
39 } else {
40     $TmpPrefix ="$(TMPDIR)";
41     $ENV{'TMPDIR'} = '$(TMPDIR)'; # set the env var as well
42 }
43 $ScriptFile = "$TmpPrefix/run_me$$";
44 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
45 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
46 @PgmStdoutFile = ();
47 @PgmStderrFile = ();
48 $PreScript = '';
49 $PostScript = '';
50 $TimeCmd = '';
51 $StatsFile = "$TmpPrefix/stats$$";
52 $SysSpecificTiming = '';
53 $SpixTiming = 'no';
54
55 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
56 $ToRun = $ARGV[0]; shift(@ARGV);
57 # avoid picking up same-named thing from somewhere else on $PATH...
58 $ToRun = "./$ToRun" if $ToRun !~ /^\//;
59
60 arg: while ($_ = $ARGV[0]) {
61     shift(@ARGV);
62     
63     /^--$/      && do { # let anything past after --
64                         push(@PgmArgs, @ARGV);
65                         last arg; };
66
67     /^-v$/      && do { $Verbose = 1; next arg; };
68     /^-O(.*)/   && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
69     /^-i(.*)/   && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
70                         $Status++,
71                         print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
72                             if ! -f $PgmStdinFile;
73                         next arg; };
74     /^-x(.*)/   && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
75                         $Status++ ,
76                         print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
77                             if $PgmExitStatus !~ /^\d+$/;
78                         next arg; };
79     /^-o1(.*)/  && do { $out_file = &grab_arg_arg('-o1',$1);
80                         push(@PgmStdoutFile, $out_file);
81                         next arg; };
82     /^-o2(.*)/  && do { $out_file = &grab_arg_arg('-o2',$1);
83                         push(@PgmStderrFile, $out_file);
84                         next arg; };
85     /^-prescript(.*)/  && do { $PreScript = &grab_arg_arg('-prescript',$1);
86                                 next arg; };
87     /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
88                                 next arg; };
89     /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
90                                   next arg; };
91     /^-spix-timing$/ && do { $SysSpecificTiming = 'ghcspix';
92                              $SpixTiming = 'yes';
93                              next arg; };
94     /^-t(.*)/   && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
95
96     # anything else is taken to be a pgm arg
97     push(@PgmArgs, $_);
98 }
99
100 foreach $out_file ( @PgmStdoutFile ) {
101     $Status++ ,
102     print STDERR "$Pgm: bogus -o1 expected-output file: $out_file\n"
103         if ! -f $out_file;
104 }
105
106 foreach $out_file ( @PgmStderrFile ) {
107     $Status++,
108     print STDERR "$Pgm: bogus -o2 expected-stderr file: $out_file\n"
109         if ! -f $out_file;
110 }
111
112 exit 1 if $Status;
113
114 # add on defaults if none specified
115 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
116 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
117
118 # tidy up the pgm args:
119 # (1) look for the "first input file"
120 #     and grep it for "interesting" comments (--!!! )
121 # (2) quote any args w/ whitespace in them.
122 $grep_done = 0;
123 foreach $a ( @PgmArgs ) {
124     if (! $grep_done && $a !~ /^-/ && -f $a) {
125         print `egrep "^--!!!" $a`;
126         $grep_done = 1;
127     }
128     if ($a =~ /\s/ || $a =~ /'/) {
129         $a =~ s/'/\\'/g;    # backslash the quotes;
130         $a = "\"$a\"";      # quote the arg
131     }
132 }
133
134 # deal with system-specific timing options
135 $TimingMagic = '';
136 if ( $SysSpecificTiming =~ /^ghc/ ) {
137     $TimingMagic = "+RTS -s$StatsFile -RTS"
138 } elsif ( $SysSpecificTiming eq 'hbc' ) {
139     $TimingMagic = "-S$StatsFile";
140 }
141
142 $ToRunOrig = $ToRun;
143 if ( $SpixTiming eq 'yes' ) {
144     $ToRun .= '.spix';
145
146     # gotta find first/last addresses in the mutator code
147     $FirstSpix = '_callWrapper';
148     $LastSpix  = '???'; # usually _mpz_get_si, but can't be sure
149
150     open(SPIXNM, "nm -n $ToRunOrig |") || die "nm -n $ToRunOrig open failed!\n";
151     spix: while (<SPIXNM>) {
152         if ( / T +(_FreeMallocPtr|_([A-Za-z]+)Hook|_xmalloc|_mpz_get_si)$/ ) {
153             $LastSpix = $1;
154             last spix;
155         }
156     }
157     close(SPIXNM); # || die "nm -n $ToRunOrig close failed!\n";
158
159     $SpixifyLine1 = "spix -o $ToRun -t$FirstSpix,$LastSpix $ToRunOrig";
160     $SpixstatsLine1 = "spixstats -b $TmpPrefix/runtest$$.3 $ToRunOrig > $ToRunOrig.spixstats1";
161     $SpixifyLine2 = "spix -o $ToRun +t$FirstSpix,$LastSpix $ToRunOrig";
162     $SpixstatsLine2 = "spixstats -b $TmpPrefix/runtest$$.3 $ToRunOrig > $ToRunOrig.spixstats2";
163 } else {
164     $SpixifyLine1 = '';
165     $SpixstatsLine1 = '';
166     $SpixifyLine2 = '';
167     $SpixstatsLine2 = '';
168 }
169
170 if ($PreScript ne '') {
171     local($to_do);
172     $PreScriptLines = `cat $PreScript`;
173 } else {
174     $PreScriptLines = '';
175 }
176
177 if ($PostScript ne '') {
178     local($to_do);
179     $PostScriptLines = `cat $PostScript`;
180     $* = 1;
181     $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#g;
182     $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#g;
183 } else {
184     $PostScriptLines = '';
185 }
186
187 # OK, so we're gonna do the normal thing...
188
189 $Script = <<EOSCRIPT;
190 #! /bin/sh
191 myexit=0
192 diffsShown=0
193 rm -f $DefaultStdoutFile $DefaultStderrFile
194 cat /dev/null > $DefaultStdoutFile
195 cat /dev/null > $DefaultStderrFile
196 $PreScriptLines
197 $SpixifyLine1
198 $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1 2> $TmpPrefix/runtest$$.2 3> $TmpPrefix/runtest$$.3\'
199 progexit=\$?
200 if [ \$progexit -ne $PgmExitStatus ]; then
201     echo $ToRun @PgmArgs \\< $PgmStdinFile
202     echo expected exit status $PgmExitStatus not seen \\; got \$progexit
203     myexit=1
204 else
205     $PostScriptLines
206     hit='NO'
207     for out_file in @PgmStdoutFile ; do
208         if cmp -s \$out_file $TmpPrefix/runtest$$.1 ; then
209             hit='YES'
210         fi
211     done
212     if [ \$hit = 'NO' ] ; then
213         echo $ToRun @PgmArgs \\< $PgmStdinFile
214         echo expected stdout not matched by reality
215         $(CONTEXT_DIFF) $PgmStdoutFile[0] $TmpPrefix/runtest$$.1
216         myexit=1
217         diffsShown=1
218     fi
219 fi
220 egrep -v '^ld\.so:.*has older revision than expected' < $TmpPrefix/runtest$$.2 > $TmpPrefix/runtest$$.2b
221 mv -f $TmpPrefix/runtest$$.2b $TmpPrefix/runtest$$.2
222
223 hit='NO'
224 for out_file in @PgmStderrFile ; do
225     if cmp -s \$out_file $TmpPrefix/runtest$$.2 ; then
226         hit='YES'
227     fi
228 done
229 if [ \$hit = 'NO' ] ; then
230     echo $ToRun @PgmArgs \\< $PgmStdinFile
231     echo expected stderr not matched by reality
232     $(CONTEXT_DIFF) $PgmStderrFile[0] $TmpPrefix/runtest$$.2
233     myexit=1
234     diffsShown=1
235 fi
236 $SpixstatsLine1
237
238 if [ $SpixTiming = 'yes' -a \$myexit = 0 ] ; then
239     $SpixifyLine2
240     $TimeCmd /bin/sh -c \'$ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> /dev/null 2> /dev/null 3> $TmpPrefix/runtest$$.3\'
241     $SpixstatsLine2
242 fi
243
244 $(RM) core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3
245 exit \$myexit
246 EOSCRIPT
247
248 # bung script into a file
249 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
250 print SCR $Script;
251 close(SCR) || die "Failed closing script file!\n";
252 chmod 0755, $ScriptFile;
253
254 print STDERR $Script if $Verbose;
255
256 &run_something($ScriptFile);
257
258 if ( $SysSpecificTiming eq '' ) {
259     unlink $StatsFile;
260     unlink $ScriptFile;
261     exit 0;
262 }
263
264 &process_stats_file();
265 &process_spixstats_files() if $SpixTiming eq 'yes';
266
267 # print out what we found
268 if ( $SpixTiming ne 'yes' ) {
269     print STDERR "<<$SysSpecificTiming: ",
270         "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed)",
271         " :$SysSpecificTiming>>\n";
272 } else {
273     print STDERR "<<$SysSpecificTiming: ",
274         "$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",
275         " :$SysSpecificTiming>>\n";
276 }
277
278 # OK, party over
279 unlink $StatsFile;
280 unlink $ScriptFile;
281 exit 0;
282
283 sub grab_arg_arg {
284     local($option, $rest_of_arg) = @_;
285     
286     if ($rest_of_arg) {
287         return($rest_of_arg);
288     } elsif ($#ARGV >= 0) {
289         local($temp) = $ARGV[0]; shift(@ARGV); 
290         return($temp);
291     } else {
292         print STDERR "$Pgm: no argument following $option option\n";
293         $Status++;
294     }
295 }
296
297 sub run_something {
298     local($str_to_do) = @_;
299
300 #   print STDERR "$str_to_do\n" if $Verbose;
301
302     local($return_val) = 0;
303     system($str_to_do);
304     $return_val = $?;
305
306     if ($return_val != 0) {
307 #ToDo: this return-value mangling is wrong
308 #       local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
309 #       $die_msg .= " (program not found)" if $return_val == 255;
310 #       $die_msg .= " ($!)" if $Verbose && $! != 0;
311 #       $die_msg .= "\n";
312         unlink $ScriptFile;
313         unlink $StatsFile;
314
315         exit (($return_val == 0) ? 0 : 1);
316     }
317 }
318
319 sub process_stats_file {
320
321     # OK, process system-specific stats file
322     if ( $SysSpecificTiming =~ /^ghc/ ) {
323
324         #NB: nearly the same as in GHC driver's -ghc-timing stuff
325
326         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
327
328         local($max_live)    = 0; 
329         local($tot_live)    = 0; # for calculating residency stuff
330         local($tot_samples) = 0;
331
332         while (<STATS>) {
333             if (! /Minor/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+\%/ ) {
334                 $max_live = $1 if $max_live < $1;
335                 $tot_live += $1;
336                 $tot_samples += 1;
337             }
338
339             $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
340
341 #           if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
342 #               $MaxResidency = $1; $ResidencySamples = $2;
343 #           }
344
345             $GCs = $1 if /^\s*([0-9,]+) garbage collections? performed/;
346
347             if ( /^\s*INIT\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
348                 $InitTime = $1; $InitElapsed = $2;
349             } elsif ( /^\s*MUT\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
350                 $MutTime = $1; $MutElapsed = $2;
351             } elsif ( /^\s*GC\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
352                 $GcTime = $1; $GcElapsed = $2;
353             }
354         }
355         close(STATS) || die "Failed when closing $StatsFile\n";
356         if ( $tot_samples > 0 ) {
357             $ResidencySamples = $tot_samples;
358             $MaxResidency = $max_live;
359             $AvgResidency = int ($tot_live / $tot_samples) ;
360         }
361
362     } elsif ( $SysSpecificTiming eq 'hbc' ) {
363
364         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
365         while (<STATS>) {
366             $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
367
368             $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
369
370             if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
371                 $MutTime = $1; $MutElapsed = $2; # will fix up later
372
373                 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
374
375             } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
376                 $GcTime = $1; $GcElapsed = $2;
377
378                 # fix up mutator time now
379                 $MutTime    = sprintf("%.2f", ($MutTime    - $GcTime));
380                 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
381             }
382         }
383         close(STATS) || die "Failed when closing $StatsFile\n";
384     }
385
386     # warn about what we didn't find
387     print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
388     print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
389     print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
390     print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
391     print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
392     print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
393     print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
394     print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
395
396     # things we didn't necessarily expect to find
397     $MaxResidency     = 0 unless defined($MaxResidency);
398     $AvgResidency     = 0 unless defined($AvgResidency);
399     $ResidencySamples = 0 unless defined($ResidencySamples);
400
401     # a bit of tidying
402     $BytesAlloc =~ s/,//g;
403     $MaxResidency =~ s/,//g;
404     $GCs =~ s/,//g;
405     $InitTime =~ s/,//g;
406     $InitElapsed =~ s/,//g;
407     $MutTime =~ s/,//g;
408     $MutElapsed =~ s/,//g;
409     $GcTime =~ s/,//g;
410     $GcElapsed =~ s/,//g;
411 }
412
413 sub process_spixstats_files { # 2 of them; one for mutator, one for GC
414
415     @TotalInsns = ();
416     @LoadInsns  = ();
417     @StoreInsns = ();
418     @BranchInsns= ();
419     @OtherInsns = ();
420
421     foreach $f (1, 2) {
422
423       open(STATS, "< $ToRunOrig.spixstats$f") || die "Failed when opening $ToRunOrig.spixstats$f\n";
424       while (<STATS>) {
425           last if /^OPCODES \(STATIC\):/; # party over
426
427           next if /^OPCODES \(DYNAMIC\):/;
428           next if /^$/;
429           next if /^opcode\s+#executed/;
430           next if /^SUBTOTAL/;
431
432           if ( /^ld\S*\s+(\d+)/ ) {
433               $LoadInsns[$f] += $1;
434
435           } elsif ( /^st\S*\s+(\d+)/ ) {
436               $StoreInsns[$f] += $1;
437
438           } elsif ( /^(jmpl|call|b\S*)\s+(\d+)/ ) {
439               $BranchInsns[$f] += $2;
440
441           } elsif ( /^TOTAL\s+(\d+)/ ) {
442               $TotalInsns[$f] = $1;
443               print STDERR "TotalInsns doesn't match categories total!\n"
444                   if $TotalInsns[$f] !=
445                      ($LoadInsns[$f] + $StoreInsns[$f] + $BranchInsns[$f] + $OtherInsns[$f]);
446
447           } elsif ( /^\S+\s+(\d+)/ ) {
448               $OtherInsns[$f] += $1;
449
450           } else {
451               die "Funny line?? $_";
452           }
453       }
454       close(STATS) || die "Failed when closing $ToRunOrig.spixstats\n";
455     }
456 }