diff_config.pl revision 9257
19257Ssascha.bischoff@arm.com# Copyright (c) 2012 ARM Limited
29257Ssascha.bischoff@arm.com# All rights reserved.
39257Ssascha.bischoff@arm.com#
49257Ssascha.bischoff@arm.com# The license below extends only to copyright in the software and shall
59257Ssascha.bischoff@arm.com# not be construed as granting a license to any other intellectual
69257Ssascha.bischoff@arm.com# property including but not limited to intellectual property relating
79257Ssascha.bischoff@arm.com# to a hardware implementation of the functionality of the software
89257Ssascha.bischoff@arm.com# licensed hereunder.  You may use the software subject to the license
99257Ssascha.bischoff@arm.com# terms below provided that you ensure that this notice is replicated
109257Ssascha.bischoff@arm.com# unmodified and in its entirety in all distributions of the software,
119257Ssascha.bischoff@arm.com# modified or unmodified, in source code or in binary form.
129257Ssascha.bischoff@arm.com#
139257Ssascha.bischoff@arm.com# Redistribution and use in source and binary forms, with or without
149257Ssascha.bischoff@arm.com# modification, are permitted provided that the following conditions are
159257Ssascha.bischoff@arm.com# met: redistributions of source code must retain the above copyright
169257Ssascha.bischoff@arm.com# notice, this list of conditions and the following disclaimer;
179257Ssascha.bischoff@arm.com# redistributions in binary form must reproduce the above copyright
189257Ssascha.bischoff@arm.com# notice, this list of conditions and the following disclaimer in the
199257Ssascha.bischoff@arm.com# documentation and/or other materials provided with the distribution;
209257Ssascha.bischoff@arm.com# neither the name of the copyright holders nor the names of its
219257Ssascha.bischoff@arm.com# contributors may be used to endorse or promote products derived from
229257Ssascha.bischoff@arm.com# this software without specific prior written permission.
239257Ssascha.bischoff@arm.com#
249257Ssascha.bischoff@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
259257Ssascha.bischoff@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
269257Ssascha.bischoff@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
279257Ssascha.bischoff@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
289257Ssascha.bischoff@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
299257Ssascha.bischoff@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
309257Ssascha.bischoff@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
319257Ssascha.bischoff@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
329257Ssascha.bischoff@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
339257Ssascha.bischoff@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
349257Ssascha.bischoff@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
359257Ssascha.bischoff@arm.com#
369257Ssascha.bischoff@arm.com# Author: Uri Wiener
379257Ssascha.bischoff@arm.com#
389257Ssascha.bischoff@arm.com
399257Ssascha.bischoff@arm.com# Script which takes two config.ini files and generates a semantic diff. The
409257Ssascha.bischoff@arm.com# resulting diff shows which parts of the configurations differed, and in the
419257Ssascha.bischoff@arm.com# case that there is a difference it displays it. This allows rapid comparision
429257Ssascha.bischoff@arm.com# of two gem5 runs and therefore provides an easy method to ensure that
439257Ssascha.bischoff@arm.com# configurations are similar, or not.
449257Ssascha.bischoff@arm.com
459257Ssascha.bischoff@arm.com#!/usr/bin/perl
469257Ssascha.bischoff@arm.comuse strict;
479257Ssascha.bischoff@arm.com
489257Ssascha.bischoff@arm.comdie "Please check args... " unless ($#ARGV == 1);
499257Ssascha.bischoff@arm.commy $config1FileName = $ARGV[0];
509257Ssascha.bischoff@arm.commy $config2FileName = $ARGV[1];
519257Ssascha.bischoff@arm.com
529257Ssascha.bischoff@arm.com# Get just the name of the file, rather than the full path
539257Ssascha.bischoff@arm.commy $config1ShortName = getFilenameFromPath($config1FileName);
549257Ssascha.bischoff@arm.commy $config2ShortName = getFilenameFromPath($config2FileName);
559257Ssascha.bischoff@arm.com
569257Ssascha.bischoff@arm.com# If the file names are the same, use the full path
579257Ssascha.bischoff@arm.comif ($config1ShortName == $config2ShortName) {
589257Ssascha.bischoff@arm.com    $config1ShortName = $config1FileName;
599257Ssascha.bischoff@arm.com    $config2ShortName = $config2FileName;
609257Ssascha.bischoff@arm.com}
619257Ssascha.bischoff@arm.com
629257Ssascha.bischoff@arm.comprint "\nComparing the following files:\n",
639257Ssascha.bischoff@arm.com    "\t$config1FileName\n",
649257Ssascha.bischoff@arm.com    "\tvs.\n",
659257Ssascha.bischoff@arm.com    "\t$config2FileName\n\n";
669257Ssascha.bischoff@arm.com
679257Ssascha.bischoff@arm.com# Read in the two config files
689257Ssascha.bischoff@arm.commy %config1 = readConfig($config1FileName);
699257Ssascha.bischoff@arm.commy %config2 = readConfig($config2FileName);
709257Ssascha.bischoff@arm.com
719257Ssascha.bischoff@arm.com# Compare the two config files. For the first comparision we also compare the
729257Ssascha.bischoff@arm.com# values (setting the first parameter to 1). There is little point doing this
739257Ssascha.bischoff@arm.com# for the second comparison as it will yield the same information.
749257Ssascha.bischoff@arm.comcompareConfigs( 1, \%config1, $config1ShortName, \%config2, $config2ShortName );
759257Ssascha.bischoff@arm.comcompareConfigs( 0, \%config2, $config2ShortName, \%config1, $config1ShortName );
769257Ssascha.bischoff@arm.com
779257Ssascha.bischoff@arm.com
789257Ssascha.bischoff@arm.com########################################################
799257Ssascha.bischoff@arm.com# Compare values and return unique values
809257Ssascha.bischoff@arm.com########################################################
819257Ssascha.bischoff@arm.comsub compareValues {
829257Ssascha.bischoff@arm.com    my $values1 = shift;
839257Ssascha.bischoff@arm.com    my $values2 = shift;
849257Ssascha.bischoff@arm.com    my @splitValues1 = split(/ /, $values1);
859257Ssascha.bischoff@arm.com    my @splitValues2 = split(/ /, $values2);
869257Ssascha.bischoff@arm.com    my @uniqueValues;
879257Ssascha.bischoff@arm.com
889257Ssascha.bischoff@arm.com    foreach my $val1 (@splitValues1) {
899257Ssascha.bischoff@arm.com        my $foundMatch = 0;
909257Ssascha.bischoff@arm.com
919257Ssascha.bischoff@arm.com        # if both values equal set match flag, then break loop
929257Ssascha.bischoff@arm.com        foreach my $val2 (@splitValues2) {
939257Ssascha.bischoff@arm.com            if ($val1 eq $val2) {
949257Ssascha.bischoff@arm.com                $foundMatch = 1;
959257Ssascha.bischoff@arm.com                last;
969257Ssascha.bischoff@arm.com            }
979257Ssascha.bischoff@arm.com
989257Ssascha.bischoff@arm.com            # in case of ports, ignore port number and match port name only
999257Ssascha.bischoff@arm.com            if ($val1 =~ /\[/ and $val2 =~ /\[/) {
1009257Ssascha.bischoff@arm.com                $val1 =~ m/^(.*)\[.*\]/;
1019257Ssascha.bischoff@arm.com                my $val1Name = $1;
1029257Ssascha.bischoff@arm.com                $val2 =~ m/^(.*)\[.*\]/;
1039257Ssascha.bischoff@arm.com                my $val2Name = $1;
1049257Ssascha.bischoff@arm.com
1059257Ssascha.bischoff@arm.com                # if both values equal set match flag, then break loop
1069257Ssascha.bischoff@arm.com                if ($val1Name eq $val2Name) {
1079257Ssascha.bischoff@arm.com                    $foundMatch = 1;
1089257Ssascha.bischoff@arm.com                    last;
1099257Ssascha.bischoff@arm.com                }
1109257Ssascha.bischoff@arm.com            }
1119257Ssascha.bischoff@arm.com        }
1129257Ssascha.bischoff@arm.com
1139257Ssascha.bischoff@arm.com        # Otherwise, the value is unique.
1149257Ssascha.bischoff@arm.com        if (not $foundMatch) {
1159257Ssascha.bischoff@arm.com            push(@uniqueValues, $val1);
1169257Ssascha.bischoff@arm.com        }
1179257Ssascha.bischoff@arm.com    }
1189257Ssascha.bischoff@arm.com
1199257Ssascha.bischoff@arm.com    return join(", ", @uniqueValues);
1209257Ssascha.bischoff@arm.com}
1219257Ssascha.bischoff@arm.com
1229257Ssascha.bischoff@arm.com
1239257Ssascha.bischoff@arm.com########################################################
1249257Ssascha.bischoff@arm.com# Compare two config files. Print differences.
1259257Ssascha.bischoff@arm.com########################################################
1269257Ssascha.bischoff@arm.comsub compareConfigs {
1279257Ssascha.bischoff@arm.com    my $compareFields   = shift; # Specfy if the fields should be compared
1289257Ssascha.bischoff@arm.com    my $config1Ref      = shift; # Config 1
1299257Ssascha.bischoff@arm.com    my $config1Name     = shift; # Config 1 name
1309257Ssascha.bischoff@arm.com    my $config2Ref      = shift; # Config 2
1319257Ssascha.bischoff@arm.com    my $config2Name     = shift; # Config 2 name
1329257Ssascha.bischoff@arm.com    my @uniqueSections;
1339257Ssascha.bischoff@arm.com
1349257Ssascha.bischoff@arm.com    foreach my $sectionName ( sort keys %$config1Ref ) {
1359257Ssascha.bischoff@arm.com        # check if section exists in config2
1369257Ssascha.bischoff@arm.com        if ( not exists $config2Ref->{$sectionName} ) {
1379257Ssascha.bischoff@arm.com            push(@uniqueSections, $sectionName);
1389257Ssascha.bischoff@arm.com            next;
1399257Ssascha.bischoff@arm.com        }
1409257Ssascha.bischoff@arm.com        my %section1 = %{ $config1Ref->{$sectionName} };
1419257Ssascha.bischoff@arm.com        my %section2 = %{ $config2Ref->{$sectionName} };
1429257Ssascha.bischoff@arm.com        my $firstDifInSection = 1;
1439257Ssascha.bischoff@arm.com
1449257Ssascha.bischoff@arm.com        if (not $compareFields) {
1459257Ssascha.bischoff@arm.com            next;
1469257Ssascha.bischoff@arm.com        }
1479257Ssascha.bischoff@arm.com
1489257Ssascha.bischoff@arm.com        # Compare the values of each field; print any differences
1499257Ssascha.bischoff@arm.com        foreach my $field ( sort keys %section1 ) {
1509257Ssascha.bischoff@arm.com            if ($section1{$field} ne $section2{$field}) {
1519257Ssascha.bischoff@arm.com                   my $diff1 = compareValues($section1{$field}, $section2{$field});
1529257Ssascha.bischoff@arm.com                   my $diff2 = compareValues($section2{$field}, $section1{$field});
1539257Ssascha.bischoff@arm.com
1549257Ssascha.bischoff@arm.com                # If same, skip to next iteration
1559257Ssascha.bischoff@arm.com                if ($diff1 eq "" and $diff2 eq "") {
1569257Ssascha.bischoff@arm.com                    next;
1579257Ssascha.bischoff@arm.com                }
1589257Ssascha.bischoff@arm.com
1599257Ssascha.bischoff@arm.com                # If it is the first difference in this section, print section
1609257Ssascha.bischoff@arm.com                # name
1619257Ssascha.bischoff@arm.com                if ($firstDifInSection) {
1629257Ssascha.bischoff@arm.com                    print "$sectionName\n";
1639257Ssascha.bischoff@arm.com                    $firstDifInSection = 0;
1649257Ssascha.bischoff@arm.com                }
1659257Ssascha.bischoff@arm.com
1669257Ssascha.bischoff@arm.com                # Print the actual differences
1679257Ssascha.bischoff@arm.com                   print "\t$field\n";
1689257Ssascha.bischoff@arm.com                   if ($diff1 ne "") {
1699257Ssascha.bischoff@arm.com                       print "\t\t$config1Name: ", $diff1, "\n";
1709257Ssascha.bischoff@arm.com                   }
1719257Ssascha.bischoff@arm.com                   if ($diff2 ne "") {
1729257Ssascha.bischoff@arm.com                       print "\t\t$config2Name: ", $diff2, "\n";
1739257Ssascha.bischoff@arm.com                   }
1749257Ssascha.bischoff@arm.com            } # end if
1759257Ssascha.bischoff@arm.com        } # end foreach field
1769257Ssascha.bischoff@arm.com    } # end foreach section
1779257Ssascha.bischoff@arm.com
1789257Ssascha.bischoff@arm.com    # If there are unique sections, print them
1799257Ssascha.bischoff@arm.com    if ($#uniqueSections != -1) {
1809257Ssascha.bischoff@arm.com        print "Sections which exist only in $config1Name: ",
1819257Ssascha.bischoff@arm.com            join(", ", @uniqueSections), "\n";
1829257Ssascha.bischoff@arm.com    }
1839257Ssascha.bischoff@arm.com}
1849257Ssascha.bischoff@arm.com
1859257Ssascha.bischoff@arm.com
1869257Ssascha.bischoff@arm.com########################################################
1879257Ssascha.bischoff@arm.com# Split the path to get just the filename
1889257Ssascha.bischoff@arm.com########################################################
1899257Ssascha.bischoff@arm.comsub getFilenameFromPath {
1909257Ssascha.bischoff@arm.com    my $filename = shift; # the input filename including path
1919257Ssascha.bischoff@arm.com    my @splitName = split(/\//, $filename);
1929257Ssascha.bischoff@arm.com    return $splitName[$#splitName]; # return just the filename, without path
1939257Ssascha.bischoff@arm.com}
1949257Ssascha.bischoff@arm.com
1959257Ssascha.bischoff@arm.com
1969257Ssascha.bischoff@arm.com########################################################
1979257Ssascha.bischoff@arm.com# Read in the config file section by section.
1989257Ssascha.bischoff@arm.com########################################################
1999257Ssascha.bischoff@arm.comsub readConfig {
2009257Ssascha.bischoff@arm.com    my $filename = shift;
2019257Ssascha.bischoff@arm.com    my %config;
2029257Ssascha.bischoff@arm.com    open CONFIG, "<$filename" or die $!;
2039257Ssascha.bischoff@arm.com    while ( my $line = <CONFIG> ) {
2049257Ssascha.bischoff@arm.com        if ( $line =~ /^\[.*\]$/ ) {
2059257Ssascha.bischoff@arm.com            readSection( $line, \%config );
2069257Ssascha.bischoff@arm.com        }
2079257Ssascha.bischoff@arm.com    }
2089257Ssascha.bischoff@arm.com    close CONFIG;
2099257Ssascha.bischoff@arm.com    return %config;
2109257Ssascha.bischoff@arm.com}
2119257Ssascha.bischoff@arm.com
2129257Ssascha.bischoff@arm.com
2139257Ssascha.bischoff@arm.com########################################################
2149257Ssascha.bischoff@arm.com# Read in each section of the config file.
2159257Ssascha.bischoff@arm.com########################################################
2169257Ssascha.bischoff@arm.comsub readSection {
2179257Ssascha.bischoff@arm.com    my $line       = shift;
2189257Ssascha.bischoff@arm.com    my $config_ref = shift;
2199257Ssascha.bischoff@arm.com    $line =~ m/\[(.*)\]/;
2209257Ssascha.bischoff@arm.com    my $sectionName = $1;
2219257Ssascha.bischoff@arm.com    while ( my $line = <CONFIG> ) {
2229257Ssascha.bischoff@arm.com        if ( $line =~ /^$/ ) {
2239257Ssascha.bischoff@arm.com            last;
2249257Ssascha.bischoff@arm.com        }
2259257Ssascha.bischoff@arm.com        my @field     = split( /=/, $line );
2269257Ssascha.bischoff@arm.com        my $fieldName = $field[0];
2279257Ssascha.bischoff@arm.com        my $value     = $field[1];
2289257Ssascha.bischoff@arm.com        chomp $value;
2299257Ssascha.bischoff@arm.com        $config_ref->{$sectionName}{$fieldName} = $value;
2309257Ssascha.bischoff@arm.com    }
2319257Ssascha.bischoff@arm.com}
232