Merging in the new codegen branch
[ghc-hetmet.git] / 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 #   DEFAULT_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 #       Flags
38 #       ~~~~~
39 #       -accept-output  replace output files with the ones actually generated by running
40 #                       the program
41 #
42 ($Pgm = $0) =~ s|.*/||;
43 $Verbose = 0;
44 $SaveStderr = 0;
45 $SaveStdout = 0;
46 $StdoutBinary = 0;
47 $StderrBinary = 0;
48 $Status = 0;
49 @PgmArgs = ();
50 $PgmFail=0;
51 $PgmExitStatus = 0;
52 $PgmStdinFile  = '/dev/null';
53 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
54     $TmpPrefix = $ENV{'TMPDIR'};
55 } else {
56     $TmpPrefix ="$DEFAULT_TMPDIR";
57     $ENV{'TMPDIR'} = "$DEFAULT_TMPDIR"; # set the env var as well
58 }
59 # If this is Cygwin, ignore eol and CR characters.
60 # Perhaps required for MSYS too, although the cygpath
61 # bit is hopefully unnecessary.
62 if ( `uname | grep CYGWIN` ) {
63     $CONTEXT_DIFF=$CONTEXT_DIFF . " --strip-trailing-cr" ;
64     $TmpPrefix = `cygpath -m $TmpPrefix | tr -d \\\\n`;
65 }
66 $ScriptFile = "$TmpPrefix/run_me$$";
67 $DefaultStdoutFile = "$TmpPrefix/no_stdout$$"; # can't use /dev/null (e.g. Alphas)
68 $DefaultStderrFile = "$TmpPrefix/no_stderr$$";
69 @PgmStdoutFile = ();
70 @PgmStderrFile = ();
71 $PreScript = '';
72 $PostScript = '';
73 $TimeCmd = 'time';
74 $StatsFile = "$TmpPrefix/stats$$";
75 $CachegrindStats = "cachegrind.out.summary";
76 $SysSpecificTiming = '';
77 $SysCPUCounting = 0; # Use CPU counters
78 $Cachegrind = 'no';
79 $Counters = "";
80
81 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
82 $ToRun = $ARGV[0]; shift(@ARGV);
83 # avoid picking up same-named thing from somewhere else on $PATH...
84 $ToRun = "./$ToRun" if -e "./$ToRun";
85
86 arg: while ($_ = $ARGV[0]) {
87     shift(@ARGV);
88     
89     /^--$/      && do { # let anything past after --
90                         push(@PgmArgs, @ARGV);
91                         last arg; };
92
93     /^-v$/             && do { $Verbose = 1; next arg; };
94     /^-accept-output-stderr$/ && do { $SaveStderr = 1; next arg; };
95     /^-accept-output-stdout$/ && do { $SaveStdout = 1; next arg; };
96     /^-accept-output$/        && do { $SaveStdout = 1; $SaveStderr = 1; next arg; };
97
98     /^-stdout-binary/ && do { $StdoutBinary=1; next arg; };
99     /^-stderr-binary/ && do { $StderrBinary=1; next arg; };
100
101     /^-O(.*)/   && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
102     /^-i(.*)/   && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
103                         $Status++,
104                         print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
105                             if $PgmStdinFile !~ /^\/dev\/.*$/ && ! -f $PgmStdinFile;
106                         next arg; };
107     /^-fail/    && do { $PgmFail=1; next arg; };
108     /^-x(.*)/   && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
109                         $Status++ ,
110                         print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
111                             if $PgmExitStatus !~ /^\d+$/;
112                         next arg; };
113     /^-o1(.*)/  && do { $out_file = &grab_arg_arg('-o1',$1);
114                         push(@PgmStdoutFile, $out_file);
115                         next arg; };
116     /^-o2(.*)/  && do { $out_file = &grab_arg_arg('-o2',$1);
117                         push(@PgmStderrFile, $out_file);
118                         next arg; };
119     /^-prescript(.*)/  && do { $PreScript = &grab_arg_arg('-prescript',$1);
120                                 next arg; };
121     /^-postscript(.*)/ && do { $PostScript = &grab_arg_arg('-postscript',$1);
122                                 next arg; };
123     /^-script/ && do { print STDERR "$Pgm: -script argument is obsolete;\nUse -prescript and -postscript instead.\n";
124                     $Status++;
125                     next arg; };
126     /^-(ghc|hbc)-timing$/ && do { $SysSpecificTiming = $1;
127                                   next arg; };
128     /^-cpu-counting-(.)$/ && do { $SysCPUCounting = "$1";
129                                   next arg; };
130     /^-cachegrind$/ && do { $SysSpecificTiming = 'ghc-instrs';
131                             $Cachegrind = 'yes'; 
132                             next arg };
133     /^-t(.*)/   && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
134
135     # anything else is taken to be a pgm arg
136     push(@PgmArgs, $_);
137 }
138
139 foreach $out_file ( @PgmStdoutFile ) {
140     if ( ! -f $out_file && !$SaveStdout ) {
141             print STDERR "$Pgm: warning: expected-stdout file missing: $out_file\n";
142             pop(@PgmStdoutFile);
143     }
144 }
145
146 foreach $out_file ( @PgmStderrFile ) {
147     if ( ! -f $out_file && !$SaveStderr ) {
148             print STDERR "$Pgm: warning: expected-stderr file missing: $out_file\n";
149             pop(@PgmStderrFile);
150     }
151 }
152
153 exit 1 if $Status;
154
155 # add on defaults if none specified
156 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
157 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
158
159 # tidy up the pgm args:
160 # (1) look for the "first input file"
161 #     and grep it for "interesting" comments (-- !!! )
162 # (2) quote any args w/ whitespace in them.
163 $grep_done = 0;
164 foreach $a ( @PgmArgs ) {
165     if (! $grep_done && $a !~ /^-/ && -f $a) {
166         print `egrep "^--[ ]?!!!" $a`;
167         $grep_done = 1;
168     }
169     if ($a =~ /\s/ || $a =~ /'/) {
170         $a =~ s/'/\\'/g;    # backslash the quotes;
171         $a = "\"$a\"";      # quote the arg
172     }
173 }
174
175 # deal with system-specific timing options
176 $TimingMagic = '';
177 if ( $SysSpecificTiming =~ /^ghc/ ) {
178     if ($SysCPUCounting) {
179         # Count specified CPU events
180         $cpu_counting_ghc = "-a$SysCPUCounting";
181     } else {
182         $cpu_counting_ghc = "";
183     }
184     $TimingMagic = "+RTS -S$StatsFile $cpu_counting_ghc -RTS"
185 } elsif ( $SysSpecificTiming eq 'hbc' ) {
186     $TimingMagic = "-S$StatsFile";
187 }
188
189 if ($PreScript ne '') {
190     local($to_do);
191     $PreScriptLines = `cat $PreScript`;
192     $PreScriptLines =~ s/\r//g;
193 } else {
194     $PreScriptLines = '';
195 }
196
197 if ($PostScript ne '') {
198     local($to_do);
199     $PostScriptLines = `cat $PostScript`;
200     $PostScriptLines =~ s/\r//g;
201     $PostScriptLines =~ s#\$o1#$TmpPrefix/runtest$$.1#gm;
202     $PostScriptLines =~ s#\$o2#$TmpPrefix/runtest$$.2#gm;
203 # The postfix 'm' deals with recent versions of 
204 # Perl that removed the $* feature
205 } else {
206     $PostScriptLines = '';
207 }
208
209 # OK, so we're gonna do the normal thing...
210
211 if ($Cachegrind eq 'yes') {
212   $CachegrindPrefix = "valgrind --tool=cachegrind --log-fd=9 9>$CachegrindStats";
213 } else {
214   $CachegrindPrefix = '';
215 }
216
217 $Script = <<EOSCRIPT;
218 #! /bin/sh
219 myexit=0
220 diffsShown=0
221 rm -f $DefaultStdoutFile $DefaultStderrFile
222 cat /dev/null > $DefaultStdoutFile
223 cat /dev/null > $DefaultStderrFile
224 $PreScriptLines
225 $SpixifyLine1
226 $TimeCmd /bin/sh -c \'$CachegrindPrefix $ToRun $TimingMagic @PgmArgs < $PgmStdinFile 1> $TmpPrefix/runtest$$.1.raw 2> $TmpPrefix/runtest$$.2.raw 3> $TmpPrefix/runtest$$.3.raw\'
227 progexit=\$?
228 if [ "$StdoutBinary" = "0" ]; then
229     dos2unix < $TmpPrefix/runtest$$.1.raw > $TmpPrefix/runtest$$.1
230 else
231     cp $TmpPrefix/runtest$$.1.raw $TmpPrefix/runtest$$.1
232 fi
233 if [ "$StderrBinary" = "0" ]; then
234     dos2unix < $TmpPrefix/runtest$$.2.raw > $TmpPrefix/runtest$$.2
235 else
236     cp $TmpPrefix/runtest$$.2.raw $TmpPrefix/runtest$$.2
237 fi
238 dos2unix < $TmpPrefix/runtest$$.3.raw > $TmpPrefix/runtest$$.3
239 if [ \$progexit -eq 0 ] && [ $PgmFail -ne 0 ]; then
240     echo $ToRun @PgmArgs \\< $PgmStdinFile
241     echo "****" expected a failure, but was successful
242     myexit=1
243 fi
244 if [ \$progexit -ne $PgmExitStatus ] && [ $PgmFail -eq 0 ]; then
245     echo $ToRun @PgmArgs \\< $PgmStdinFile
246     echo "****" expected exit status $PgmExitStatus not seen \\; got \$progexit
247     myexit=1
248 else
249     $PostScriptLines
250     hit='NO'
251     for out_file in @PgmStdoutFile ; do
252         if sed "s/\r\$//" $TmpPrefix/runtest$$.1 | cmp -s \$out_file - ; then
253             hit='YES'
254         fi
255     done
256     if [ \$hit = 'NO' ] ; then
257         echo $ToRun @PgmArgs \\< $PgmStdinFile
258         echo expected stdout not matched by reality
259         orig_file="$PgmStdoutFile[0]";
260         [ ! -f \$orig_file ] && orig_file="/dev/null"
261         ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.1
262         myexit=\$?
263         diffsShown=1
264     fi
265     if [ $SaveStdout = 1 ] && 
266        [ $PgmStdoutFile[0] != $DefaultStdoutFile ] && [ -s $TmpPrefix/runtest$$.1 ]; then
267         echo Saving away stdout output in $PgmStdoutFile[0] ...
268         if [ -f $PgmStdoutFile[0] ]; then
269              rm -f $PgmStdoutFile[0].bak
270              cp $PgmStdoutFile[0] $PgmStdoutFile[0].bak
271         fi;
272         cp $TmpPrefix/runtest$$.1 $PgmStdoutFile[0]
273     fi
274 fi
275
276 hit='NO'
277 for out_file in @PgmStderrFile ; do
278     if sed "s/\r\$//" $TmpPrefix/runtest$$.2 | cmp -s \$out_file - ; then
279         hit='YES'
280     fi
281 done
282 if [ \$hit = 'NO' ] ; then
283     echo $ToRun @PgmArgs \\< $PgmStdinFile
284     echo expected stderr not matched by reality
285     orig_file="$PgmStderrFile[0]"
286     [ ! -f \$orig_file ] && orig_file="/dev/null"
287     ${CONTEXT_DIFF} \$orig_file $TmpPrefix/runtest$$.2
288     myexit=\$?
289     diffsShown=1
290 fi
291 if [ $SaveStderr = 1 ] &&
292    [ $PgmStderrFile[0] != $DefaultStderrFile ] && [ -s $TmpPrefix/runtest$$.2 ]; then
293         echo Saving away stderr output in $PgmStderrFile[0] ...
294         if [ -f $PgmStderrFile[0] ]; then
295            rm -f $PgmStderrFile[0].bak
296            cp $PgmStderrFile[0] $PgmStderrFile[0].bak
297         fi;
298         cp $TmpPrefix/runtest$$.2 $PgmStderrFile[0]
299 fi
300
301 ${RM} core $ToRunOrig.spix $DefaultStdoutFile $DefaultStderrFile $TmpPrefix/runtest$$.1 $TmpPrefix/runtest$$.2 $TmpPrefix/runtest$$.3 $TmpPrefix/runtest$$.1.raw $TmpPrefix/runtest$$.2.raw $TmpPrefix/runtest$$.3.raw
302 exit \$myexit
303 EOSCRIPT
304
305 # bung script into a file
306 open(SCR, "> $ScriptFile") || die "Failed opening script file $ScriptFile!\n";
307 print SCR $Script;
308 close(SCR) || die "Failed closing script file!\n";
309 chmod 0755, $ScriptFile;
310
311 print STDERR $Script if $Verbose;
312
313 &run_something($ScriptFile);
314
315 if ( $SysSpecificTiming eq '' ) {
316     unlink $StatsFile;
317     unlink $ScriptFile;
318     exit 0;
319 }
320
321 &process_stats_file();
322 &process_cachegrind_files() if $Cachegrind eq 'yes';
323
324 # print out what we found
325 print STDERR "<<$SysSpecificTiming: ";
326 if ( $Cachegrind ne 'yes') {
327         print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed), $Gc0Time GC(0) ($Gc0Elapsed elapsed), $Gc1Time GC(1) ($Gc1Elapsed elapsed), $Balance balance$Counters";
328 } else {
329         print STDERR "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), $GCWork bytes GC work, ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed), $Gc0Time GC(0) ($Gc0Elapsed elapsed), $Gc1Time GC(1) ($Gc1Elapsed elapsed), $Balance balance, $TotInstrs instructions, $TotReads memory reads, $TotWrites memory writes, $TotMisses L2 cache misses";
330 };
331 print STDERR " :$SysSpecificTiming>>\n";
332
333 # OK, party over
334 unlink $StatsFile;
335 unlink $ScriptFile;
336 exit 0;
337
338 sub grab_arg_arg {
339     local($option, $rest_of_arg) = @_;
340     
341     if ($rest_of_arg ne "") {
342         return($rest_of_arg);
343     } elsif ($#ARGV >= 0) {
344         local($temp) = $ARGV[0]; shift(@ARGV); 
345         return($temp);
346     } else {
347         print STDERR "$Pgm: no argument following $option option\n";
348         $Status++;
349     }
350 }
351
352 sub run_something {
353     local($str_to_do) = @_;
354
355 #   print STDERR "$str_to_do\n" if $Verbose;
356
357     local($return_val) = 0;
358     system($str_to_do);
359     $return_val = $?;
360
361     if ($return_val != 0) {
362 #ToDo: this return-value mangling is wrong
363 #       local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
364 #       $die_msg .= " (program not found)" if $return_val == 255;
365 #       $die_msg .= " ($!)" if $Verbose && $! != 0;
366 #       $die_msg .= "\n";
367         unlink $ScriptFile;
368         unlink $StatsFile;
369
370         exit (($return_val == 0) ? 0 : 1);
371     }
372 }
373
374 sub process_stats_file {
375
376     # OK, process system-specific stats file
377     if ( $SysSpecificTiming =~ /^ghc/ ) {
378
379         #NB: nearly the same as in GHC driver's -ghc-timing stuff
380
381         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
382
383         local($max_live)    = 0; 
384         local($tot_live)    = 0; # for calculating residency stuff
385         local($tot_samples) = 0;
386         local($into_gc_counters) = 0; # once we reach into the GC counters part
387         local($counters) = "";
388         local($counter) = "";
389         local($count) = 0;
390
391         $GCWork = 0;
392         $GCs = 0;
393         $Balance = 1;
394         while (<STATS>) {
395             if (! /Gen:\s+0/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
396                 $max_live = $1 if $max_live < $1;
397                 $tot_live += $1;
398                 $tot_samples += 1;
399             }
400
401             $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
402             
403             if (/^\s*([0-9,]+) bytes copied during GC/) {
404                $tmp = $1;
405                $tmp =~ s/,//g;
406                $GCWork += $tmp;
407             }
408
409 #           if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
410 #               $MaxResidency = $1; $ResidencySamples = $2;
411 #           }
412
413             $GCs += $1 if /^\s*Generation\s*\d+:\s*([0-9,]+) collections/;
414
415             if ( /^\s+([0-9]+)\s+M[Bb] total memory/ ) {
416                 $TotMem = $1;
417             }
418
419             if ( /^\s*INIT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
420                 $InitTime = $1; $InitElapsed = $2;
421             } elsif ( /^\s*MUT\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
422                 $MutTime = $1; $MutElapsed = $2;
423             } elsif ( /^\s*GC\s+time\s*(-*\d+\.\d\d)s\s*\(\s*(-*\d+\.\d\d)s elapsed\)/ ) {
424                 $GcTime = $1; $GcElapsed = $2;
425             }
426
427             if (/Generation (\d+):\s*\d+ collections,\s*\d+ parallel,\s*(-*\d+\.\d\d)s\s*,\s*(-*\d+\.\d\d)s elapsed/) {
428                 if ($1 == 0) {
429                     $Gc0Time = $2; $Gc0Elapsed = $3;
430                 } elsif ($1 == 1) {
431                     $Gc1Time = $2; $Gc1Elapsed = $3;
432                 }
433             }
434
435             if (/work balance: ([0-9.]+)/) {
436                 $Balance = $1;
437             }
438
439
440             if ( /CPU GC counters/ ) {
441                 # Counters that follow correspond to GC
442                 $into_gc_counters = 1;
443             }
444
445             if ( /^\s+\(([A-Z_0-9]+)\)\s+:\s+([0-9,]+)/ ) {
446                 $counter = $1;
447                 $count   = $2;
448                 $count =~ s/,//g; # Remove commas in numbers
449                 # Pretty printing elements of a list with type [(String,[Float])]
450                 # It's a bit lame for passing values but it works.
451                 if($into_gc_counters) {
452                     $counters = "$counters(\"GC:$counter\",[$count]),";
453                 } else {
454                     $counters = "$counters(\"$counter\",[$count]),";
455                 }
456             }
457
458             if ( /^\s+\(([A-Z_0-9]+)\)\s+\%\s+of\s+\(([A-Z_0-9]+)\)\s+:\s+([0-9.]+)/ ) {
459                 $counter = "$1/$2"; # Relative quantity
460                 $count   = $3;
461                 # Pretty printing elements of a list with type [(String,[Float])]
462                 # It's a bit lame for passing values but it works.
463                 if($into_gc_counters) {
464                     $counters = "$counters(\"GC:$counter\",[$count]),";
465                 } else {
466                     $counters = "$counters(\"$counter\",[$count]),";
467                 }
468             }
469
470         }
471         close(STATS) || die "Failed when closing $StatsFile\n";
472         if ( $tot_samples > 0 ) {
473             $ResidencySamples = $tot_samples;
474             $MaxResidency = $max_live;
475             $AvgResidency = int ($tot_live / $tot_samples) ;
476         }
477
478         if ( length($counters) == 0 ) {
479             $Counters = "";
480         } else {
481             chop($counters); # remove trailing comma from the last entry
482             $Counters = " [$counters]";
483         }
484
485     } elsif ( $SysSpecificTiming eq 'hbc' ) {
486
487         open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
488         while (<STATS>) {
489             $BytesAlloc = $1 if /^\s*([0-9]+) bytes allocated from the heap/;
490
491             $GCs = $1 if /^\s*([0-9]+) GCs?,$/;
492
493             if ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds total time,$/ ) {
494                 $MutTime = $1; $MutElapsed = $2; # will fix up later
495
496                 $InitTime = 0; $InitElapsed = 0; # hbc doesn't report these
497
498             } elsif ( /^\s*(\d+\.\d\d) \((\d+\.\d)\) seconds GC time/ ) {
499                 $GcTime = $1; $GcElapsed = $2;
500
501                 # fix up mutator time now
502                 $MutTime    = sprintf("%.2f", ($MutTime    - $GcTime));
503                 $MutElapsed = sprintf("%.1f", ($MutElapsed - $GcElapsed));
504             }
505         }
506         close(STATS) || die "Failed when closing $StatsFile\n";
507     }
508
509     # warn about what we didn't find
510     print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
511     print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
512     print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
513     print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
514     print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
515     print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
516     print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
517     print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
518     print STDERR "Warning: total memory not found in stats file\n" unless defined($TotMem);
519     print STDERR "Warning: GC work not found in stats file\n" unless defined($GCWork);
520     print STDERR "Warning: Gc0Time not found in stats file\n" unless defined($Gc0Time);
521     print STDERR "Warning: Gc0Elapsed not found in stats file\n" unless defined($Gc0Elapsed);
522     print STDERR "Warning: Gc1Time not found in stats file\n" unless defined($Gc1Time);
523     print STDERR "Warning: Gc1Elapsed not found in stats file\n" unless defined($Gc1Elapsed);
524     print STDERR "Warning: Balance not found in stats file\n" unless defined($Gc1Elapsed);
525
526     # things we didn't necessarily expect to find
527     $MaxResidency     = 0 unless defined($MaxResidency);
528     $AvgResidency     = 0 unless defined($AvgResidency);
529     $ResidencySamples = 0 unless defined($ResidencySamples);
530     $Gc0Time          = 0.0 unless defined($Gc0Time);
531     $Gc0Elapsed       = 0.0 unless defined($Gc0Elapsed);
532     $Gc1Time          = 0.0 unless defined($Gc1Time);
533     $Gc1Elapsed       = 0.0 unless defined($Gc1Elapsed);
534
535     # a bit of tidying
536     $BytesAlloc =~ s/,//g;
537     $MaxResidency =~ s/,//g;
538     $GCs =~ s/,//g;
539     $InitTime =~ s/,//g;
540     $InitElapsed =~ s/,//g;
541     $MutTime =~ s/,//g;
542     $MutElapsed =~ s/,//g;
543     $GcTime =~ s/,//g;
544     $GcElapsed =~ s/,//g;
545 }
546
547 sub process_cachegrind_files {
548
549     open(STATS, "< $CachegrindStats") || die("Can't open $CachegrindStats\n");
550
551     while (<STATS>) {
552         /^==\d+==\s+I\s+refs:\s+([0-9,]*)/ && do {
553            $TotInstrs = $1;
554            $TotInstrs =~ s/,//g;
555         };
556
557         /^==\d+==\s+D\s+refs:\s+[0-9,]+\s+\(([0-9,]+)\s+rd\s+\+\s+([0-9,]+)\s+wr/ && do {
558            $TotReads  = $1;
559            $TotWrites = $2;
560            $TotReads  =~ s/,//g;
561            $TotWrites =~ s/,//g;
562         };
563
564         /^==\d+==\s+L2d\s+misses:\s+([0-9,]+)/ && do {
565            $TotMisses = $1;
566            $TotMisses =~ s/,//g;
567         };
568     }
569     close(STATS);
570 }
571