2 # Copyright (c) 2001-2005 The Regents of The University of Michigan
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # This script diffs two SimpleScalar statistics output files.
38 print "\nError: need two file arguments (<reference> <new>).\n";
39 print " Options: -d = Ignore distributions\n";
40 print " -a = Sort errors alphabetically (default: by percentage)\n";
41 print " -h = Diff header info separately from stats\n";
42 print " -n <num> = Print top <num> errors (default 20, 0 for all)\n";
43 print " -t <num> = Ignore errors below <num> percent (default 0)\n\n";
47 open(REF
, "<$ARGV[0]") or die "Error: can't open $ARGV[0].\n";
48 open(NEW
, "<$ARGV[1]") or die "Error: can't open $ARGV[1].\n";
52 # Things that really should be adjustable via the command line
55 # Ignorable error (in percent)
56 $err_thresh = defined($opt_t) ?
$opt_t : 0;
58 # Number of stats to print before omitting
59 $omit_count = defined($opt_n) ?
$opt_n : 20;
63 # First copy everything up to the simulation statistics to a pair of
64 # temporary files, stripping out date-related items, and do a plain
65 # diff. Any differences in the arguments are not necessarily an issue;
66 # any differences in the program output should be caught by the EIO
67 # mechanism if an EIO file is used.
70 # copy_header takes input filehandle and output filename
74 my ($inhandle, $outname) = @_;
76 open(OUTPUT
, ">$outname") or die "Error: can't open $outname.\n";
80 # strip out lines that can vary
81 next if /^(command line:|M5 compiled on |M5 simulation started |M5 executing on )/;
82 last if /Begin Simulation Statistics/;
90 # Diff header separately from stats
92 $refheader = "/tmp/smt-test.refheader.$$";
93 $newheader = "/tmp/smt-test.newheader.$$";
95 copy_header
(\
*REF
, $refheader);
96 copy_header
(\
*NEW
, $newheader);
98 print "\n===== Header and program output differences =====\n\n";
100 print `diff $refheader $newheader`;
102 print "\n===== Statistics differences =====\n\n";
106 # Now parse statistics
110 # This function takes an open filehandle and returns a reference to
111 # a hash containing all the statistics variables and their values.
118 $hashref = { }; # initialize hash for values
120 while (<$stathandle>)
122 next if /^\s*$/; # skip blank lines
123 last if /End Simulation Statistics/;
125 s/ *#.*//; # strip comments
127 if (/^Memory usage: (\d+) KBytes/) {
128 $stat = 'memory usage';
132 if (/(.*)\.end_dist/) {
133 # end line of distribution: clear $in_dist flag
138 next; # bail out if we are ignoring dists...
139 } elsif (/(.*)\.(min|max)_value/) {
140 # treat these like normal stats
141 ($stat, $value) = /^(\S+)\s+(.*)/;
144 /^(\S+(?:.*\S)?)\s+(\d+)\s+\d+\.\d+%/;
145 $stat = $in_dist . '::' . $stat;
149 if (/(.*)\.start_dist/) {
150 # start line of distribution: set $in_dist flag
151 # and save distribution name for future reference
157 ($stat, $value) = /^(\S+)\s+(.*)/;
161 $$hashref{$stat} = $value;
170 # pct_diff($old, $new) returns percent difference from $old to $new.
174 my ($old, $new) = @_;
175 return ($old == 0) ?
(($new == 0) ?
0 : 9999) : 100 * ($new - $old) / $old;
180 # Statistics to ignore: these relate to simulator performance, not
181 # correctness, so don't fail on changes here.
185 'host_tick_rate' => 1,
186 'host_inst_rate' => 1,
188 'host_mem_usage' => 1
192 # List of key statistics (always displayed)
193 # ==> list stats here WITHOUT trailing thread ID
206 $key_stat_pattern = join('|', @key_stat_list);
208 # initialize first statistics from each file
212 $refhash = parse_file
(\
*REF
);
213 $newhash = parse_file
(\
*NEW
);
215 # The string sim-smt prints on a divide by zero
216 $divbyzero = '<err: divide by zero>';
218 foreach $stat (sort keys %$refhash)
220 $refvalue = $$refhash{$stat};
221 $newvalue = $$newhash{$stat};
223 if (!defined($newvalue)) {
224 # stat missing from new file
225 push @missing_stats, $stat;
229 if ($stat =~ /($key_stat_pattern)/o) {
230 # key statistics: always record & display changes in these
231 push @key_stats, [$stat, $refvalue, $newvalue];
234 if ($ignore{$stat} or $refvalue eq $newvalue) {
235 # stat is in "ignore" list, or hasn't changed
238 if ($refvalue eq $divbyzero || $newvalue eq $divbyzero) {
239 # one or the other was a divide by zero:
240 # no point in trying to quantify error
241 print "$stat: $refvalue --> $newvalue\n";
244 $reldiff = pct_diff
($refvalue, $newvalue);
245 $diffmag = abs($reldiff);
247 if ($diffmag > $err_thresh) {
249 [$stat, $refvalue, $newvalue, $reldiff];
252 if ($diffmag > $max_err_mag) {
253 $max_err_mag = $diffmag;
258 # remove from new hash so we can detect added stats
259 delete $$newhash{$stat};
264 # All done. Print comparison summary.
267 printf("Maximum error magnitude: %+f%%\n\n", $max_err_mag);
269 printf(" %-30s %10s %10s %10s %7s\n", ' ', 'Reference', 'New Value', 'Abs Diff', 'Pct Chg');
271 printf("Key statistics:\n\n");
273 foreach $key_stat (@key_stats)
275 ($statname, $refvalue, $newvalue, $reldiff) = @
$key_stat;
277 # deduce format from reference value
278 $pointpos = rindex($refvalue, '.');
279 $digits = ($pointpos < 0) ?
0 :(length($refvalue) - $pointpos - 1);
280 $fmt = "%10.${digits}f";
282 # print differing values with absolute and relative error
283 printf(" %-30s $fmt $fmt $fmt %+7.2f%%\n",
284 $statname, $refvalue, $newvalue,
285 $newvalue - $refvalue, pct_diff
($refvalue, $newvalue));
288 printf("\nDifferences > %d%%:\n\n", $err_thresh);
291 # leave stats sorted alphabetically, doesn't make sense to cut them off
294 # sort differences by percent change
295 @errs = sort { abs($$b[3]) <=> abs($$a[3]) } @errs;
302 ($statname, $refvalue, $newvalue, $reldiff) = @
$err;
304 # deduce format from reference value
305 $pointpos1 = rindex($refvalue, '.');
306 $digits1 = ($pointpos1 < 0) ?
0 :(length($refvalue) - $pointpos1 - 1);
307 $pointpos2 = rindex($newvalue, '.');
308 $digits2 = ($pointpos2 < 0) ?
0 :(length($newvalue) - $pointpos2 - 1);
309 $digits = ($digits1 > $digits2) ?
$digits1 : $digits2;
310 $fmt = "%10.${digits}f";
312 # print differing values with absolute and relative error
313 printf(" %-30s $fmt $fmt $fmt %+7.2f%%\n",
314 $statname, $refvalue, $newvalue, $newvalue - $refvalue, $reldiff);
316 # only print top N errors
317 if ($omit_count > 0 && ++$num_errs >= $omit_count)
319 print "[... showing top $omit_count errors only, additional errors omitted ...]\n";
325 # Report missing stats
328 $missing_stats = scalar(@missing_stats);
332 print "\nMissing $missing_stats reference statistics:\n\n";
333 foreach $stat (@missing_stats)
336 printf " %-50s ", $stat;
337 print "$$refhash{$stat}\n";
342 # Any stats left in newhash are added since the reference file
345 @added_stats = keys %$newhash;
348 $added_stats = scalar(@added_stats);
352 print "\nFound $added_stats new statistics:\n\n";
353 foreach $stat (sort @added_stats)
356 printf " %-50s ", $stat;
357 print "$$newhash{$stat}\n";
363 # 0 if all stats are found (with no extras) & no stats error
364 # 1 if there are additional stats, but no stat errors
366 $no_hard_errors = $missing_stats == 0 && $max_err_mag == 0.0;
367 $status = $no_hard_errors ?
($added_stats == 0 ?
0 : 1) : 2;
372 unlink($refheader) if ($refheader);
373 unlink($newheader) if ($newheader);