rundiff revision 404
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
44354Sstever@eecs.umich.eduuse Getopt::Std;
45354Sstever@eecs.umich.edu
46354Sstever@eecs.umich.edu#
47354Sstever@eecs.umich.edu# Options:
48354Sstever@eecs.umich.edu#  -c <n> : print n lines of context before & after changes
49354Sstever@eecs.umich.edu#  -l <n> : use n lines of lookahead
50354Sstever@eecs.umich.edu#  -x     : use "complex" diff from Algorithm::Diff (see below)
51354Sstever@eecs.umich.edu#
52354Sstever@eecs.umich.eduour ($opt_c, $opt_l, $opt_x);
53354Sstever@eecs.umich.edugetopts('c:l:x');
54354Sstever@eecs.umich.edu
55196Sstever@eecs.umich.edu#
56196Sstever@eecs.umich.edu# For the highest-quality (minimal) diffs, we can use the
57354Sstever@eecs.umich.edu# Algorithm::Diff package.  By default, a built-in, simple, and
58354Sstever@eecs.umich.edu# generally quite adequate algorithm will be used.  If you have
59354Sstever@eecs.umich.edu# Algorithm::Diff installed on your system, and don't mind having the
60354Sstever@eecs.umich.edu# script go slower (like 3-4x slower, based on informal observation),
61354Sstever@eecs.umich.edu# then specify '-x' on the command line to use it.
62354Sstever@eecs.umich.edumy $use_complexdiff = defined($opt_x);
632Ssraasch@umich.edu
64354Sstever@eecs.umich.eduif ($use_complexdiff) {
65354Sstever@eecs.umich.edu    # Don't use 'use', as that's a compile-time option and will fail
66354Sstever@eecs.umich.edu    # on systems that don't have Algorithm::Diff installed even if
67354Sstever@eecs.umich.edu    # $use_complexdiff is false.  'require' is evaluated at runtime,
68354Sstever@eecs.umich.edu    # so it's OK.
69354Sstever@eecs.umich.edu    require Algorithm::Diff;
70354Sstever@eecs.umich.edu    import Algorithm::Diff qw(traverse_sequences);
71354Sstever@eecs.umich.edu};
722Ssraasch@umich.edu
73354Sstever@eecs.umich.edumy $lookahead_lines = $opt_l || 200;
74354Sstever@eecs.umich.edu
75354Sstever@eecs.umich.edu# in theory you could have different amounts of context before and
76354Sstever@eecs.umich.edu# after a diff, but until someone needs that there's only one arg to
77354Sstever@eecs.umich.edu# set both.
78354Sstever@eecs.umich.edumy $precontext_lines = $opt_c || 3;
79354Sstever@eecs.umich.edumy $postcontext_lines = $precontext_lines;
802Ssraasch@umich.edu
81196Sstever@eecs.umich.edumy $file1 = $ARGV[0];
82196Sstever@eecs.umich.edumy $file2 = $ARGV[1];
832Ssraasch@umich.edu
84196Sstever@eecs.umich.edudie "Need two args." if (!(defined($file1) && defined($file2)));
852Ssraasch@umich.edu
86196Sstever@eecs.umich.edumy ($fh1, $fh2);
87196Sstever@eecs.umich.eduopen($fh1, $file1) or die "Can't open $file1";
88196Sstever@eecs.umich.eduopen($fh2, $file2) or die "Can't open $file2";
892Ssraasch@umich.edu
90196Sstever@eecs.umich.edu# buffer of matching lines for pre-diff context
91196Sstever@eecs.umich.edumy @precontext = ();
92196Sstever@eecs.umich.edu# number of post-diff matching lines remaining to print
93196Sstever@eecs.umich.edumy $postcontext = 0;
942Ssraasch@umich.edu
95196Sstever@eecs.umich.edu# lookahead buffers for $file1 and $file2 respectively
96196Sstever@eecs.umich.edumy @lines1 = ();
97196Sstever@eecs.umich.edumy @lines2 = ();
982Ssraasch@umich.edu
99196Sstever@eecs.umich.edu# Next line number available to print from each file.  Generally this
100196Sstever@eecs.umich.edu# corresponds to the oldest line in @precontext, or the oldest line in
101196Sstever@eecs.umich.edu# @lines1 and @lines2 if @precontext is empty.
102196Sstever@eecs.umich.edumy $lineno1 = 1;
103196Sstever@eecs.umich.edumy $lineno2 = 1;
1042Ssraasch@umich.edu
105196Sstever@eecs.umich.edu# Fill a lookahead buffer to $lookahead_lines lines (or until EOF).
106196Sstever@eecs.umich.edusub fill
107196Sstever@eecs.umich.edu{
108196Sstever@eecs.umich.edu    my ($fh, $array) = @_;
1092Ssraasch@umich.edu
110196Sstever@eecs.umich.edu    while (@$array < $lookahead_lines) {
111196Sstever@eecs.umich.edu	my $line = <$fh>;
112196Sstever@eecs.umich.edu	last if (!defined($line));
113196Sstever@eecs.umich.edu	push @$array, $line;
1142Ssraasch@umich.edu    }
1152Ssraasch@umich.edu}
1162Ssraasch@umich.edu
117196Sstever@eecs.umich.edu# Print and delete n lines from front of given array with given prefix.
118196Sstever@eecs.umich.edusub printlines
119196Sstever@eecs.umich.edu{
120196Sstever@eecs.umich.edu    my ($array, $n, $prefix) = @_;
1212Ssraasch@umich.edu
122196Sstever@eecs.umich.edu    while ($n--) {
123196Sstever@eecs.umich.edu	my $line = shift @$array;
124196Sstever@eecs.umich.edu	last if (!defined($line));
125196Sstever@eecs.umich.edu	print $prefix, $line;
126196Sstever@eecs.umich.edu    }
1272Ssraasch@umich.edu}
1282Ssraasch@umich.edu
129196Sstever@eecs.umich.edu# Print a difference region where n1 lines of file1 were replaced by
130196Sstever@eecs.umich.edu# n2 lines of file2 (where either n1 or n2 could be zero).
131196Sstever@eecs.umich.edusub printdiff
132196Sstever@eecs.umich.edu{
133196Sstever@eecs.umich.edu    my ($n1, $n2)= @_;
1342Ssraasch@umich.edu
135196Sstever@eecs.umich.edu    # If the precontext buffer is full or we're at the beginning of a
136196Sstever@eecs.umich.edu    # file, then this is a new diff region, so we should print a
137196Sstever@eecs.umich.edu    # header indicating the current line numbers.  If we're past the
138196Sstever@eecs.umich.edu    # beginning and the precontext buffer isn't full, then whatever
139196Sstever@eecs.umich.edu    # we're about to print is contiguous with the end of the last
140196Sstever@eecs.umich.edu    # region we printed, so we just concatenate them on the output.
141196Sstever@eecs.umich.edu    if (@precontext == $precontext_lines || ($lineno1 == 0 && $lineno2 == 0)) {
142196Sstever@eecs.umich.edu	print "@@ -$lineno1 +$lineno2 @@\n";
1432Ssraasch@umich.edu    }
1442Ssraasch@umich.edu
145196Sstever@eecs.umich.edu    # Print and clear the precontext buffer.
146196Sstever@eecs.umich.edu    if (@precontext) {
147196Sstever@eecs.umich.edu	print ' ', join(' ', @precontext);
148196Sstever@eecs.umich.edu	$lineno1 += scalar(@precontext);
149196Sstever@eecs.umich.edu	$lineno2 += scalar(@precontext);
150196Sstever@eecs.umich.edu	@precontext = ();
151196Sstever@eecs.umich.edu    }
1522Ssraasch@umich.edu
153196Sstever@eecs.umich.edu    # Print the differing lines.
154196Sstever@eecs.umich.edu    printlines(\@lines1, $n1, '-');
155196Sstever@eecs.umich.edu    printlines(\@lines2, $n2, '+');
156196Sstever@eecs.umich.edu    $lineno1 += $n1;
157196Sstever@eecs.umich.edu    $lineno2 += $n2;
158196Sstever@eecs.umich.edu
159196Sstever@eecs.umich.edu    # Set $postcontext to print the next $postcontext_lines matching lines.
160196Sstever@eecs.umich.edu    $postcontext = $postcontext_lines;
1612Ssraasch@umich.edu}
1622Ssraasch@umich.edu
1632Ssraasch@umich.edu
164196Sstever@eecs.umich.edu########################
165196Sstever@eecs.umich.edu#
166196Sstever@eecs.umich.edu# Complex diff algorithm
167196Sstever@eecs.umich.edu#
168196Sstever@eecs.umich.edu########################
1692Ssraasch@umich.edu
170196Sstever@eecs.umich.edu{
171196Sstever@eecs.umich.edu    my $match_found;
172196Sstever@eecs.umich.edu    my $discard_lines1;
173196Sstever@eecs.umich.edu    my $discard_lines2;
174196Sstever@eecs.umich.edu
175196Sstever@eecs.umich.edu    sub match { $match_found = 1; }
176196Sstever@eecs.umich.edu    sub discard1 { $discard_lines1++ unless $match_found; }
177196Sstever@eecs.umich.edu    sub discard2 { $discard_lines2++ unless $match_found; }
178196Sstever@eecs.umich.edu
179196Sstever@eecs.umich.edu    sub complex_diff
180196Sstever@eecs.umich.edu    {
181196Sstever@eecs.umich.edu	$match_found = 0;
182196Sstever@eecs.umich.edu	$discard_lines1 = 0;
183196Sstever@eecs.umich.edu	$discard_lines2 = 0;
184196Sstever@eecs.umich.edu
185196Sstever@eecs.umich.edu	# See Diff.pm.  Note that even though this call generates a
186196Sstever@eecs.umich.edu	# complete diff of both lookahead buffers, all we use it for
187196Sstever@eecs.umich.edu	# is to figure out how many lines to discard off the front of
188196Sstever@eecs.umich.edu	# each buffer to resync the streams.
189196Sstever@eecs.umich.edu	traverse_sequences( \@lines1, \@lines2,
190196Sstever@eecs.umich.edu			    { MATCH => \&match,
191196Sstever@eecs.umich.edu			      DISCARD_A => \&discard1,
192196Sstever@eecs.umich.edu			      DISCARD_B => \&discard2 });
193196Sstever@eecs.umich.edu
194404Sstever@eecs.umich.edu	if (!$match_found) {
195404Sstever@eecs.umich.edu	    printdiff(scalar(@lines1), scalar(@lines2));
196404Sstever@eecs.umich.edu	    die "Lost sync!";
197404Sstever@eecs.umich.edu	}
198196Sstever@eecs.umich.edu
199196Sstever@eecs.umich.edu	# Since we shouldn't get here unless the first lines of the
200196Sstever@eecs.umich.edu	# buffers are different, then we must discard some lines off
201196Sstever@eecs.umich.edu	# at least one of the buffers.
202196Sstever@eecs.umich.edu	die if ($discard_lines1 == 0 && $discard_lines2 == 0);
203196Sstever@eecs.umich.edu
204196Sstever@eecs.umich.edu	printdiff($discard_lines1, $discard_lines2);
205196Sstever@eecs.umich.edu    }
2062Ssraasch@umich.edu}
2072Ssraasch@umich.edu
208196Sstever@eecs.umich.edu#######################
209196Sstever@eecs.umich.edu#
210196Sstever@eecs.umich.edu# Simple diff algorithm
211196Sstever@eecs.umich.edu#
212196Sstever@eecs.umich.edu#######################
2132Ssraasch@umich.edu
214196Sstever@eecs.umich.edu# Check for a pair of matching lines; if found, generate appropriate
215196Sstever@eecs.umich.edu# diff output.
216196Sstever@eecs.umich.edusub checkmatch
217196Sstever@eecs.umich.edu{
218196Sstever@eecs.umich.edu    my ($n1, $n2) = @_;
2192Ssraasch@umich.edu
220196Sstever@eecs.umich.edu    # Check if two adjacent lines match, to reduce false resyncs
221196Sstever@eecs.umich.edu    # (particularly on unrelated blank lines).  This generates
222196Sstever@eecs.umich.edu    # larger-than-necessary diffs when a single line really should be
223196Sstever@eecs.umich.edu    # treated as common; if that bugs you, use Algorithm::Diff.
224196Sstever@eecs.umich.edu    if ($lines1[$n1] eq $lines2[$n2] && $lines1[$n1+1] eq $lines2[$n2+1]) {
225196Sstever@eecs.umich.edu	printdiff($n1, $n2);
226404Sstever@eecs.umich.edu	return 1;
227196Sstever@eecs.umich.edu    }
228404Sstever@eecs.umich.edu
229404Sstever@eecs.umich.edu    return 0;
230196Sstever@eecs.umich.edu}
231196Sstever@eecs.umich.edu
232196Sstever@eecs.umich.edusub simple_diff
233196Sstever@eecs.umich.edu{
234196Sstever@eecs.umich.edu    # Look for differences of $cnt lines to resync,
235196Sstever@eecs.umich.edu    # increasing $cnt from 1 to $lookahead_lines until we find
236196Sstever@eecs.umich.edu    # something.
237196Sstever@eecs.umich.edu    for (my $cnt = 1; $cnt < $lookahead_lines-1; ++$cnt) {
238196Sstever@eecs.umich.edu	# Check for n lines in one file being replaced by
239196Sstever@eecs.umich.edu	# n lines in the other.
240196Sstever@eecs.umich.edu	return if checkmatch($cnt, $cnt);
241196Sstever@eecs.umich.edu	# Find differences where n lines in one file were
242196Sstever@eecs.umich.edu	# replaced by m lines in the other.  We let m = $cnt
243196Sstever@eecs.umich.edu	# and iterate for n = 0 to $cnt-1.
244196Sstever@eecs.umich.edu	for (my $n = 0; $n < $cnt; ++$n) {
245196Sstever@eecs.umich.edu	    return if checkmatch($n, $cnt);
246196Sstever@eecs.umich.edu	    return if checkmatch($cnt, $n);
247196Sstever@eecs.umich.edu	}
248196Sstever@eecs.umich.edu    }
249404Sstever@eecs.umich.edu
250404Sstever@eecs.umich.edu    printdiff(scalar(@lines1), scalar(@lines2));
251196Sstever@eecs.umich.edu    die "Lost sync!";
252196Sstever@eecs.umich.edu}
253196Sstever@eecs.umich.edu
254196Sstever@eecs.umich.edu# Set the pointer to the appropriate diff function.
255196Sstever@eecs.umich.edu#
256196Sstever@eecs.umich.edu# Note that in either case the function determines how many lines to
257196Sstever@eecs.umich.edu# discard from the front of each lookahead buffer to resync the
258196Sstever@eecs.umich.edu# streams, then prints the appropriate diff output and discards them.
259196Sstever@eecs.umich.edu# After the function returns, it should always be the case that
260196Sstever@eecs.umich.edu# $lines1[0] eq $lines2[0].
261196Sstever@eecs.umich.edumy $find_diff = $use_complexdiff ? \&complex_diff : \&simple_diff;
262196Sstever@eecs.umich.edu
263196Sstever@eecs.umich.edu# The main loop.
264196Sstever@eecs.umich.eduwhile (1) {
265196Sstever@eecs.umich.edu    # keep lookahead buffers topped up
266196Sstever@eecs.umich.edu    fill($fh1, \@lines1);
267196Sstever@eecs.umich.edu    fill($fh2, \@lines2);
268196Sstever@eecs.umich.edu
269196Sstever@eecs.umich.edu    # peek at first line in each buffer
270196Sstever@eecs.umich.edu    my $l1 = $lines1[0];
271196Sstever@eecs.umich.edu    my $l2 = $lines2[0];
272196Sstever@eecs.umich.edu
273196Sstever@eecs.umich.edu    if (!defined($l1) && !defined($l2)) {
274196Sstever@eecs.umich.edu	# reached EOF on both streams: exit
275196Sstever@eecs.umich.edu	exit(1);
276196Sstever@eecs.umich.edu    }
277196Sstever@eecs.umich.edu
278196Sstever@eecs.umich.edu    if ($l1 eq $l2) {
279196Sstever@eecs.umich.edu	# matching lines: delete from lookahead buffer
280196Sstever@eecs.umich.edu	shift @lines1;
281196Sstever@eecs.umich.edu	shift @lines2;
282196Sstever@eecs.umich.edu	# figure out what to do with this line
283196Sstever@eecs.umich.edu	if ($postcontext > 0) {
284196Sstever@eecs.umich.edu	    # we're in the post-context of a diff: print it
285196Sstever@eecs.umich.edu	    $postcontext--;
286196Sstever@eecs.umich.edu	    print ' ', $l1;
287196Sstever@eecs.umich.edu	    $lineno1++;
288196Sstever@eecs.umich.edu	    $lineno2++;
289196Sstever@eecs.umich.edu	}
290196Sstever@eecs.umich.edu	else {
291196Sstever@eecs.umich.edu	    # we're in the middle of a matching region... save this
292196Sstever@eecs.umich.edu	    # line for precontext in case we run into a difference.
293196Sstever@eecs.umich.edu	    push @precontext, $l1;
294196Sstever@eecs.umich.edu	    # don't let precontext buffer get bigger than needed
295196Sstever@eecs.umich.edu	    while (@precontext > $precontext_lines) {
296196Sstever@eecs.umich.edu		shift @precontext;
297196Sstever@eecs.umich.edu		$lineno1++;
298196Sstever@eecs.umich.edu		$lineno2++;
299196Sstever@eecs.umich.edu	    }
300196Sstever@eecs.umich.edu	}
301196Sstever@eecs.umich.edu    }
302196Sstever@eecs.umich.edu    else {
303196Sstever@eecs.umich.edu	# Mismatch.  Deal with it.
304196Sstever@eecs.umich.edu	&$find_diff();
305196Sstever@eecs.umich.edu    }
306196Sstever@eecs.umich.edu}
307