rundiff revision 196
1196Sstever@eecs.umich.edu#! /usr/bin/env perl
22Ssraasch@umich.edu
3196Sstever@eecs.umich.edu# Copyright (c) 2003 The Regents of The University of Michigan
42Ssraasch@umich.edu# All rights reserved.
52Ssraasch@umich.edu#
6196Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
7196Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
8196Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
9196Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
10196Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
11196Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
12196Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
13196Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
14196Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
15196Sstever@eecs.umich.edu# this software without specific prior written permission.
162Ssraasch@umich.edu#
17196Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18196Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19196Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20196Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21196Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22196Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23196Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24196Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25196Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26196Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27196Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28196Sstever@eecs.umich.edu
29196Sstever@eecs.umich.edu# Diff two streams.
302Ssraasch@umich.edu#
31196Sstever@eecs.umich.edu# Unlike regular diff, this script does not read in the entire input
32196Sstever@eecs.umich.edu# before doing a diff, so it can be used on lengthy outputs piped from
33196Sstever@eecs.umich.edu# other programs (e.g., M5 traces).  The best way to do this is to
34196Sstever@eecs.umich.edu# take advantage of the power of Perl's open function, which will
35196Sstever@eecs.umich.edu# automatically fork a subprocess if the last character in the
36196Sstever@eecs.umich.edu# "filename" is a pipe (|).  Thus to compare the instruction traces
37196Sstever@eecs.umich.edu# from two versions of m5 (m5a and m5b), you can do this:
38196Sstever@eecs.umich.edu#
39196Sstever@eecs.umich.edu# rundiff 'm5a --trace:flags=InstExec |' 'm5b --trace:flags=InstExec |'
402Ssraasch@umich.edu#
412Ssraasch@umich.edu
42196Sstever@eecs.umich.eduuse strict;
432Ssraasch@umich.edu
44196Sstever@eecs.umich.edu#
45196Sstever@eecs.umich.edu# For the highest-quality (minimal) diffs, we can use the
46196Sstever@eecs.umich.edu# Algorithm::Diff package.  If you don't have this installed, or want
47196Sstever@eecs.umich.edu# the script to run faster (like 3-4x faster, based on informal
48196Sstever@eecs.umich.edu# observation), set $use_complexdiff to 0; then a built-in, simple,
49196Sstever@eecs.umich.edu# and generally quite adequate algorithm will be used instead.
50196Sstever@eecs.umich.edumy $use_complexdiff = 0;
512Ssraasch@umich.edu
52196Sstever@eecs.umich.eduif ($use_complexdiff) {
53196Sstever@eecs.umich.edu    use Algorithm::Diff qw(traverse_sequences);
54196Sstever@eecs.umich.edu};
552Ssraasch@umich.edu
56196Sstever@eecs.umich.edumy $lookahead_lines = 200;
57196Sstever@eecs.umich.edumy $precontext_lines = 3;
58196Sstever@eecs.umich.edumy $postcontext_lines = 3;
592Ssraasch@umich.edu
60196Sstever@eecs.umich.edumy $file1 = $ARGV[0];
61196Sstever@eecs.umich.edumy $file2 = $ARGV[1];
622Ssraasch@umich.edu
63196Sstever@eecs.umich.edudie "Need two args." if (!(defined($file1) && defined($file2)));
642Ssraasch@umich.edu
65196Sstever@eecs.umich.edumy ($fh1, $fh2);
66196Sstever@eecs.umich.eduopen($fh1, $file1) or die "Can't open $file1";
67196Sstever@eecs.umich.eduopen($fh2, $file2) or die "Can't open $file2";
682Ssraasch@umich.edu
69196Sstever@eecs.umich.edu# buffer of matching lines for pre-diff context
70196Sstever@eecs.umich.edumy @precontext = ();
71196Sstever@eecs.umich.edu# number of post-diff matching lines remaining to print
72196Sstever@eecs.umich.edumy $postcontext = 0;
732Ssraasch@umich.edu
74196Sstever@eecs.umich.edu# lookahead buffers for $file1 and $file2 respectively
75196Sstever@eecs.umich.edumy @lines1 = ();
76196Sstever@eecs.umich.edumy @lines2 = ();
772Ssraasch@umich.edu
78196Sstever@eecs.umich.edu# Next line number available to print from each file.  Generally this
79196Sstever@eecs.umich.edu# corresponds to the oldest line in @precontext, or the oldest line in
80196Sstever@eecs.umich.edu# @lines1 and @lines2 if @precontext is empty.
81196Sstever@eecs.umich.edumy $lineno1 = 1;
82196Sstever@eecs.umich.edumy $lineno2 = 1;
832Ssraasch@umich.edu
84196Sstever@eecs.umich.edu# Fill a lookahead buffer to $lookahead_lines lines (or until EOF).
85196Sstever@eecs.umich.edusub fill
86196Sstever@eecs.umich.edu{
87196Sstever@eecs.umich.edu    my ($fh, $array) = @_;
882Ssraasch@umich.edu
89196Sstever@eecs.umich.edu    while (@$array < $lookahead_lines) {
90196Sstever@eecs.umich.edu	my $line = <$fh>;
91196Sstever@eecs.umich.edu	last if (!defined($line));
92196Sstever@eecs.umich.edu	push @$array, $line;
932Ssraasch@umich.edu    }
942Ssraasch@umich.edu}
952Ssraasch@umich.edu
96196Sstever@eecs.umich.edu# Print and delete n lines from front of given array with given prefix.
97196Sstever@eecs.umich.edusub printlines
98196Sstever@eecs.umich.edu{
99196Sstever@eecs.umich.edu    my ($array, $n, $prefix) = @_;
1002Ssraasch@umich.edu
101196Sstever@eecs.umich.edu    while ($n--) {
102196Sstever@eecs.umich.edu	my $line = shift @$array;
103196Sstever@eecs.umich.edu	last if (!defined($line));
104196Sstever@eecs.umich.edu	print $prefix, $line;
105196Sstever@eecs.umich.edu    }
1062Ssraasch@umich.edu}
1072Ssraasch@umich.edu
108196Sstever@eecs.umich.edu# Print a difference region where n1 lines of file1 were replaced by
109196Sstever@eecs.umich.edu# n2 lines of file2 (where either n1 or n2 could be zero).
110196Sstever@eecs.umich.edusub printdiff
111196Sstever@eecs.umich.edu{
112196Sstever@eecs.umich.edu    my ($n1, $n2)= @_;
1132Ssraasch@umich.edu
114196Sstever@eecs.umich.edu    # If the precontext buffer is full or we're at the beginning of a
115196Sstever@eecs.umich.edu    # file, then this is a new diff region, so we should print a
116196Sstever@eecs.umich.edu    # header indicating the current line numbers.  If we're past the
117196Sstever@eecs.umich.edu    # beginning and the precontext buffer isn't full, then whatever
118196Sstever@eecs.umich.edu    # we're about to print is contiguous with the end of the last
119196Sstever@eecs.umich.edu    # region we printed, so we just concatenate them on the output.
120196Sstever@eecs.umich.edu    if (@precontext == $precontext_lines || ($lineno1 == 0 && $lineno2 == 0)) {
121196Sstever@eecs.umich.edu	print "@@ -$lineno1 +$lineno2 @@\n";
1222Ssraasch@umich.edu    }
1232Ssraasch@umich.edu
124196Sstever@eecs.umich.edu    # Print and clear the precontext buffer.
125196Sstever@eecs.umich.edu    if (@precontext) {
126196Sstever@eecs.umich.edu	print ' ', join(' ', @precontext);
127196Sstever@eecs.umich.edu	$lineno1 += scalar(@precontext);
128196Sstever@eecs.umich.edu	$lineno2 += scalar(@precontext);
129196Sstever@eecs.umich.edu	@precontext = ();
130196Sstever@eecs.umich.edu    }
1312Ssraasch@umich.edu
132196Sstever@eecs.umich.edu    # Print the differing lines.
133196Sstever@eecs.umich.edu    printlines(\@lines1, $n1, '-');
134196Sstever@eecs.umich.edu    printlines(\@lines2, $n2, '+');
135196Sstever@eecs.umich.edu    $lineno1 += $n1;
136196Sstever@eecs.umich.edu    $lineno2 += $n2;
137196Sstever@eecs.umich.edu
138196Sstever@eecs.umich.edu    # Set $postcontext to print the next $postcontext_lines matching lines.
139196Sstever@eecs.umich.edu    $postcontext = $postcontext_lines;
1402Ssraasch@umich.edu}
1412Ssraasch@umich.edu
1422Ssraasch@umich.edu
143196Sstever@eecs.umich.edu########################
144196Sstever@eecs.umich.edu#
145196Sstever@eecs.umich.edu# Complex diff algorithm
146196Sstever@eecs.umich.edu#
147196Sstever@eecs.umich.edu########################
1482Ssraasch@umich.edu
149196Sstever@eecs.umich.edu{
150196Sstever@eecs.umich.edu    my $match_found;
151196Sstever@eecs.umich.edu    my $discard_lines1;
152196Sstever@eecs.umich.edu    my $discard_lines2;
153196Sstever@eecs.umich.edu
154196Sstever@eecs.umich.edu    sub match { $match_found = 1; }
155196Sstever@eecs.umich.edu    sub discard1 { $discard_lines1++ unless $match_found; }
156196Sstever@eecs.umich.edu    sub discard2 { $discard_lines2++ unless $match_found; }
157196Sstever@eecs.umich.edu
158196Sstever@eecs.umich.edu    sub complex_diff
159196Sstever@eecs.umich.edu    {
160196Sstever@eecs.umich.edu	$match_found = 0;
161196Sstever@eecs.umich.edu	$discard_lines1 = 0;
162196Sstever@eecs.umich.edu	$discard_lines2 = 0;
163196Sstever@eecs.umich.edu
164196Sstever@eecs.umich.edu	# See Diff.pm.  Note that even though this call generates a
165196Sstever@eecs.umich.edu	# complete diff of both lookahead buffers, all we use it for
166196Sstever@eecs.umich.edu	# is to figure out how many lines to discard off the front of
167196Sstever@eecs.umich.edu	# each buffer to resync the streams.
168196Sstever@eecs.umich.edu	traverse_sequences( \@lines1, \@lines2,
169196Sstever@eecs.umich.edu			    { MATCH => \&match,
170196Sstever@eecs.umich.edu			      DISCARD_A => \&discard1,
171196Sstever@eecs.umich.edu			      DISCARD_B => \&discard2 });
172196Sstever@eecs.umich.edu
173196Sstever@eecs.umich.edu	die "Lost sync!" if (!$match_found);
174196Sstever@eecs.umich.edu
175196Sstever@eecs.umich.edu	# Since we shouldn't get here unless the first lines of the
176196Sstever@eecs.umich.edu	# buffers are different, then we must discard some lines off
177196Sstever@eecs.umich.edu	# at least one of the buffers.
178196Sstever@eecs.umich.edu	die if ($discard_lines1 == 0 && $discard_lines2 == 0);
179196Sstever@eecs.umich.edu
180196Sstever@eecs.umich.edu	printdiff($discard_lines1, $discard_lines2);
181196Sstever@eecs.umich.edu    }
1822Ssraasch@umich.edu}
1832Ssraasch@umich.edu
184196Sstever@eecs.umich.edu#######################
185196Sstever@eecs.umich.edu#
186196Sstever@eecs.umich.edu# Simple diff algorithm
187196Sstever@eecs.umich.edu#
188196Sstever@eecs.umich.edu#######################
1892Ssraasch@umich.edu
190196Sstever@eecs.umich.edu# Check for a pair of matching lines; if found, generate appropriate
191196Sstever@eecs.umich.edu# diff output.
192196Sstever@eecs.umich.edusub checkmatch
193196Sstever@eecs.umich.edu{
194196Sstever@eecs.umich.edu    my ($n1, $n2) = @_;
1952Ssraasch@umich.edu
196196Sstever@eecs.umich.edu    # Check if two adjacent lines match, to reduce false resyncs
197196Sstever@eecs.umich.edu    # (particularly on unrelated blank lines).  This generates
198196Sstever@eecs.umich.edu    # larger-than-necessary diffs when a single line really should be
199196Sstever@eecs.umich.edu    # treated as common; if that bugs you, use Algorithm::Diff.
200196Sstever@eecs.umich.edu    if ($lines1[$n1] eq $lines2[$n2] && $lines1[$n1+1] eq $lines2[$n2+1]) {
201196Sstever@eecs.umich.edu	printdiff($n1, $n2);
202196Sstever@eecs.umich.edu    }
203196Sstever@eecs.umich.edu}
204196Sstever@eecs.umich.edu
205196Sstever@eecs.umich.edusub simple_diff
206196Sstever@eecs.umich.edu{
207196Sstever@eecs.umich.edu    # Look for differences of $cnt lines to resync,
208196Sstever@eecs.umich.edu    # increasing $cnt from 1 to $lookahead_lines until we find
209196Sstever@eecs.umich.edu    # something.
210196Sstever@eecs.umich.edu    for (my $cnt = 1; $cnt < $lookahead_lines-1; ++$cnt) {
211196Sstever@eecs.umich.edu	# Check for n lines in one file being replaced by
212196Sstever@eecs.umich.edu	# n lines in the other.
213196Sstever@eecs.umich.edu	return if checkmatch($cnt, $cnt);
214196Sstever@eecs.umich.edu	# Find differences where n lines in one file were
215196Sstever@eecs.umich.edu	# replaced by m lines in the other.  We let m = $cnt
216196Sstever@eecs.umich.edu	# and iterate for n = 0 to $cnt-1.
217196Sstever@eecs.umich.edu	for (my $n = 0; $n < $cnt; ++$n) {
218196Sstever@eecs.umich.edu	    return if checkmatch($n, $cnt);
219196Sstever@eecs.umich.edu	    return if checkmatch($cnt, $n);
220196Sstever@eecs.umich.edu	}
221196Sstever@eecs.umich.edu    }
222196Sstever@eecs.umich.edu    die "Lost sync!";
223196Sstever@eecs.umich.edu}
224196Sstever@eecs.umich.edu
225196Sstever@eecs.umich.edu# Set the pointer to the appropriate diff function.
226196Sstever@eecs.umich.edu#
227196Sstever@eecs.umich.edu# Note that in either case the function determines how many lines to
228196Sstever@eecs.umich.edu# discard from the front of each lookahead buffer to resync the
229196Sstever@eecs.umich.edu# streams, then prints the appropriate diff output and discards them.
230196Sstever@eecs.umich.edu# After the function returns, it should always be the case that
231196Sstever@eecs.umich.edu# $lines1[0] eq $lines2[0].
232196Sstever@eecs.umich.edumy $find_diff = $use_complexdiff ? \&complex_diff : \&simple_diff;
233196Sstever@eecs.umich.edu
234196Sstever@eecs.umich.edu# The main loop.
235196Sstever@eecs.umich.eduwhile (1) {
236196Sstever@eecs.umich.edu    # keep lookahead buffers topped up
237196Sstever@eecs.umich.edu    fill($fh1, \@lines1);
238196Sstever@eecs.umich.edu    fill($fh2, \@lines2);
239196Sstever@eecs.umich.edu
240196Sstever@eecs.umich.edu    # peek at first line in each buffer
241196Sstever@eecs.umich.edu    my $l1 = $lines1[0];
242196Sstever@eecs.umich.edu    my $l2 = $lines2[0];
243196Sstever@eecs.umich.edu
244196Sstever@eecs.umich.edu    if (!defined($l1) && !defined($l2)) {
245196Sstever@eecs.umich.edu	# reached EOF on both streams: exit
246196Sstever@eecs.umich.edu	exit(1);
247196Sstever@eecs.umich.edu    }
248196Sstever@eecs.umich.edu
249196Sstever@eecs.umich.edu    if ($l1 eq $l2) {
250196Sstever@eecs.umich.edu	# matching lines: delete from lookahead buffer
251196Sstever@eecs.umich.edu	shift @lines1;
252196Sstever@eecs.umich.edu	shift @lines2;
253196Sstever@eecs.umich.edu	# figure out what to do with this line
254196Sstever@eecs.umich.edu	if ($postcontext > 0) {
255196Sstever@eecs.umich.edu	    # we're in the post-context of a diff: print it
256196Sstever@eecs.umich.edu	    $postcontext--;
257196Sstever@eecs.umich.edu	    print ' ', $l1;
258196Sstever@eecs.umich.edu	    $lineno1++;
259196Sstever@eecs.umich.edu	    $lineno2++;
260196Sstever@eecs.umich.edu	}
261196Sstever@eecs.umich.edu	else {
262196Sstever@eecs.umich.edu	    # we're in the middle of a matching region... save this
263196Sstever@eecs.umich.edu	    # line for precontext in case we run into a difference.
264196Sstever@eecs.umich.edu	    push @precontext, $l1;
265196Sstever@eecs.umich.edu	    # don't let precontext buffer get bigger than needed
266196Sstever@eecs.umich.edu	    while (@precontext > $precontext_lines) {
267196Sstever@eecs.umich.edu		shift @precontext;
268196Sstever@eecs.umich.edu		$lineno1++;
269196Sstever@eecs.umich.edu		$lineno2++;
270196Sstever@eecs.umich.edu	    }
271196Sstever@eecs.umich.edu	}
272196Sstever@eecs.umich.edu    }
273196Sstever@eecs.umich.edu    else {
274196Sstever@eecs.umich.edu	# Mismatch.  Deal with it.
275196Sstever@eecs.umich.edu	&$find_diff();
276196Sstever@eecs.umich.edu    }
277196Sstever@eecs.umich.edu}
278