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