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