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