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