summaryrefslogtreecommitdiff
path: root/tests/diff-out
blob: 1eeac81379980eefe00b4dc3db1cbedfed5f86eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/perl
# Copyright (c) 2001-2005 The Regents of The University of Michigan
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Steve Reinhardt

#
# This script diffs two SimpleScalar statistics output files.
#

use Getopt::Std;

getopts('adn:t:h');

if ($#ARGV < 1)
{
    print "\nError: need two file arguments (<reference> <new>).\n";
    print "   Options: -d = Ignore distributions\n";
    print "            -a = Sort errors alphabetically (default: by percentage)\n";
    print "            -h = Diff header info separately from stats\n";
    print "            -n <num> = Print top <num> errors (default 20, 0 for all)\n";
    print "            -t <num> = Ignore errors below <num> percent (default 0)\n\n";
    exit;
}

open(REF, "<$ARGV[0]") or die "Error: can't open $ARGV[0].\n";
open(NEW, "<$ARGV[1]") or die "Error: can't open $ARGV[1].\n";


#
# Things that really should be adjustable via the command line
#

# Ignorable error (in percent)
$err_thresh = defined($opt_t) ? $opt_t : 0;

# Number of stats to print before omitting
$omit_count = defined($opt_n) ? $opt_n : 20;


#
# First copy everything up to the simulation statistics to a pair of
# temporary files, stripping out date-related items, and do a plain
# diff.  Any differences in the arguments are not necessarily an issue;
# any differences in the program output should be caught by the EIO
# mechanism if an EIO file is used.
# 

# copy_header takes input filehandle and output filename

sub copy_header
{
    my ($inhandle, $outname) = @_;

    open(OUTPUT, ">$outname") or die "Error: can't open $outname.\n";

    while (<$inhandle>)
    {
	# strip out lines that can vary
	next if /^(command line:|M5 compiled on |M5 simulation started |M5 executing on )/;
	last if /Begin Simulation Statistics/;
	print OUTPUT;
    }
    close OUTPUT;
}

if ($opt_h) {

    # Diff header separately from stats

    $refheader = "/tmp/smt-test.refheader.$$";
    $newheader = "/tmp/smt-test.newheader.$$";

    copy_header(\*REF, $refheader);
    copy_header(\*NEW, $newheader);

    print "\n===== Header and program output differences =====\n\n";

    print `diff $refheader $newheader`;

    print "\n===== Statistics differences =====\n\n";
}

#
# Now parse statistics
#

#
# This function takes an open filehandle and returns a reference to
# a hash containing all the statistics variables and their values.
#
sub parse_file
{
    $stathandle = shift;

    $in_dist = undef;
    $hashref = { };	# initialize hash for values

    while (<$stathandle>)
    {
	next if /^\s*$/;	# skip blank lines
	last if /End Simulation Statistics/;

	s/ *#.*//;		# strip comments

	if (/^Memory usage: (\d+) KBytes/) {
	    $stat = 'memory usage';
	    $value = $1;
	}
	elsif ($in_dist) {
	    if (/(.*)\.end_dist/) {
		# end line of distribution: clear $in_dist flag
		$in_dist = undef;
		next;
	    }
	    if ($opt_d) {
		next;		#  bail out if we are ignoring dists...
	    } elsif (/(.*)\.(min|max)_value/) {
		# treat these like normal stats
		($stat, $value) = /^(\S+)\s+(.*)/;
	    } else {
		($stat, $value) =
		  /^(\S+(?:.*\S)?)\s+(\d+)\s+\d+\.\d+%/;
		$stat = $in_dist . '::' . $stat;
	    }
	}
	else {
	    if (/(.*)\.start_dist/) {
		# start line of distribution: set $in_dist flag
		# and save distribution name for future reference
		$in_dist = $1;
		$stat = $1;
		$value = 0;
	    }
	    else {
		($stat, $value) = /^(\S+)\s+(.*)/;
	    }
	}

	$$hashref{$stat} = $value;
    }

    close($stathandle);
    return $hashref;
}


#
# pct_diff($old, $new) returns percent difference from $old to $new.
#
sub pct_diff
{
    my ($old, $new) = @_;
    return ($old == 0) ? (($new == 0) ? 0 : 9999) : 100 * ($new - $old) / $old;
}


#
# Statistics to ignore: these relate to simulator performance, not
# correctness, so don't fail on changes here.
#
%ignore = (
  'host_seconds' => 1,
  'host_tick_rate' => 1,
  'host_inst_rate' => 1,
  'host_op_rate' => 1,
  'host_mem_usage' => 1
);

#
# List of key statistics (always displayed)
#  ==> list stats here WITHOUT trailing thread ID
#
@key_stat_list = (
  'ipc',
  'committedInsts',
  'committedOps',
  'sim_insts',
  'sim_ops',
  'sim_ticks',
  'host_inst_rate',
  'host_mem_usage'
);

$key_stat_pattern = join('|', @key_stat_list);

# initialize first statistics from each file

$max_err_mag = 0;

$refhash = parse_file(\*REF);
$newhash = parse_file(\*NEW);

# The string sim-smt prints on a divide by zero
$divbyzero = '<err: divide by zero>';

foreach $stat (sort keys %$refhash)
{
    $refvalue = $$refhash{$stat};
    $newvalue = $$newhash{$stat};

    if (!defined($newvalue)) {
	# stat missing from new file
	push @missing_stats, $stat;
	next;
    }

    if ($stat =~ /($key_stat_pattern)/o) {
	# key statistics: always record & display changes in these
	push @key_stats, [$stat, $refvalue, $newvalue];
    }

    if ($ignore{$stat} or $refvalue eq $newvalue) {
	# stat is in "ignore" list, or hasn't changed
    }
    else {
	if ($refvalue eq $divbyzero || $newvalue eq $divbyzero) {
	    # one or the other was a divide by zero:
	    # no point in trying to quantify error
	    print "$stat: $refvalue --> $newvalue\n";
	}
	else {
	    $reldiff = pct_diff($refvalue, $newvalue);
	    $diffmag = abs($reldiff);

	    if ($diffmag > $err_thresh) {
		push @errs,
		[$stat, $refvalue, $newvalue, $reldiff];
	    }

	    if ($diffmag > $max_err_mag) {
		$max_err_mag = $diffmag;
	    }
	}
    }

    # remove from new hash so we can detect added stats
    delete $$newhash{$stat};
}


#
# All done.  Print comparison summary.
#

printf("Maximum error magnitude: %+f%%\n\n", $max_err_mag);

printf("  %-30s %10s %10s %10s   %7s\n", ' ', 'Reference', 'New Value', 'Abs Diff', 'Pct Chg');

printf("Key statistics:\n\n");

foreach $key_stat (@key_stats)
{
    ($statname, $refvalue, $newvalue, $reldiff) = @$key_stat;

    # deduce format from reference value
    $pointpos = rindex($refvalue, '.');
    $digits = ($pointpos < 0) ? 0 :(length($refvalue) - $pointpos - 1);
    $fmt = "%10.${digits}f";

    # print differing values with absolute and relative error
    printf("  %-30s $fmt $fmt $fmt  %+7.2f%%\n",
	   $statname, $refvalue, $newvalue,
	   $newvalue - $refvalue, pct_diff($refvalue, $newvalue));
}

printf("\nDifferences > %d%%:\n\n", $err_thresh);

if ($opt_a) {
    # leave stats sorted alphabetically, doesn't make sense to cut them off
    $omit_count = 0;
} else {
    # sort differences by percent change
    @errs = sort { abs($$b[3]) <=> abs($$a[3]) } @errs;
}

$num_errs = 0;

foreach $err (@errs)
{
    ($statname, $refvalue, $newvalue, $reldiff) = @$err;

    # deduce format from reference value
    $pointpos1 = rindex($refvalue, '.');
    $digits1 = ($pointpos1 < 0) ? 0 :(length($refvalue) - $pointpos1 - 1);
    $pointpos2 = rindex($newvalue, '.');
    $digits2 = ($pointpos2 < 0) ? 0 :(length($newvalue) - $pointpos2 - 1);
    $digits = ($digits1 > $digits2) ? $digits1 : $digits2;
    $fmt = "%10.${digits}f";

    # print differing values with absolute and relative error
    printf("  %-30s $fmt $fmt $fmt  %+7.2f%%\n",
	   $statname, $refvalue, $newvalue, $newvalue - $refvalue, $reldiff);

    # only print top N errors
    if ($omit_count > 0 && ++$num_errs >= $omit_count)
    {
	print "[... showing top $omit_count errors only, additional errors omitted ...]\n";
	last;
    }
}

#
# Report missing stats
#
# get count
$missing_stats = scalar(@missing_stats);

if ($missing_stats)
{
    print "\nMissing $missing_stats reference statistics:\n\n";
    foreach $stat (@missing_stats)
    {
#	print "\t$stat\n";
	printf "  %-50s    ", $stat;
	print  "$$refhash{$stat}\n";
    }
}

#
# Any stats left in newhash are added since the reference file
#

@added_stats = keys %$newhash;

# get count
$added_stats = scalar(@added_stats);

if ($added_stats)
{
    print "\nFound $added_stats new statistics:\n\n";
    foreach $stat (sort @added_stats)
    {
#	print "\t$stat\n";
	printf "  %-50s    ", $stat;
	print  "$$newhash{$stat}\n";
    }
}

cleanup();
# Exit codes:
# 0 if all stats are found (with no extras) & no stats error
# 1 if there are additional stats, but no stat errors
# 2 otherwise
$no_hard_errors = $missing_stats == 0 && $max_err_mag == 0.0;
$status = $no_hard_errors ? ($added_stats == 0 ? 0 : 1) : 2;
exit $status;

sub cleanup
{
    unlink($refheader) if ($refheader);
    unlink($newheader) if ($newheader);
}