checkpoint_aggregator.py revision 8166
16821SN/A# Copyright (c) 2009 The Regents of The University of Michigan
28166SLisa.Hsu@amd.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
36821SN/A# All rights reserved.
46821SN/A#
56821SN/A# Redistribution and use in source and binary forms, with or without
66821SN/A# modification, are permitted provided that the following conditions are
76821SN/A# met: redistributions of source code must retain the above copyright
86821SN/A# notice, this list of conditions and the following disclaimer;
96821SN/A# redistributions in binary form must reproduce the above copyright
106821SN/A# notice, this list of conditions and the following disclaimer in the
116821SN/A# documentation and/or other materials provided with the distribution;
126821SN/A# neither the name of the copyright holders nor the names of its
136821SN/A# contributors may be used to endorse or promote products derived from
146821SN/A# this software without specific prior written permission.
156821SN/A#
166821SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176821SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186821SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196821SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206821SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216821SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226821SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236821SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246821SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256821SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266821SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276821SN/A#
286821SN/A# Authors: Lisa Hsu
296818SN/A
306818SN/Afrom ConfigParser import ConfigParser
316818SN/Aimport gzip
326818SN/A
336818SN/Aimport sys, re, optparse, os
346818SN/A
356818SN/Aclass myCP(ConfigParser):
366818SN/A    def __init__(self):
376818SN/A        ConfigParser.__init__(self)
386818SN/A
396818SN/A    def optionxform(self, optionstr):
406818SN/A        return optionstr
416818SN/A
426818SN/Adef aggregate(options, args):
436818SN/A    merged = myCP()
446818SN/A    page_ptr = 0
456818SN/A
466818SN/A    allfiles = os.listdir(os.getcwd())
476818SN/A    cpts = []
486818SN/A    for arg in args:
496818SN/A        found = False
506818SN/A        for f in allfiles:
516818SN/A            if re.compile("cpt." + arg + ".\d+").search(f):
526818SN/A                found = True
536818SN/A                cpts.append(f)
546818SN/A                break
556818SN/A        if not found:
566818SN/A            print "missing checkpoint: ", arg
576818SN/A            sys.exit(1)
586818SN/A
596818SN/A    dirname = "-".join([options.prefix, "cpt"])
606818SN/A    agg_name = "-".join(args)
616818SN/A    print agg_name
626818SN/A    fullpath = os.path.join("..", dirname, "cpt." + agg_name + ".10000")
636818SN/A    if not os.path.isdir(fullpath):
646818SN/A        os.system("mkdir -p " + fullpath)
657443SLisa.Hsu@amd.com    elif os.path.isfile(fullpath + "/system.physmem.physmem"):
667443SLisa.Hsu@amd.com        if os.path.isfile(fullpath + "/m5.cpt"):
677443SLisa.Hsu@amd.com            print fullpath, " already done"
687443SLisa.Hsu@amd.com            return
696818SN/A
706818SN/A    myfile = open(fullpath + "/system.physmem.physmem", "wb+")
716818SN/A    merged_mem = gzip.GzipFile(fileobj=myfile, mode="wb")
726818SN/A
736818SN/A    max_curtick = 0
746818SN/A    when = 0
756818SN/A    for (i, arg) in enumerate(args):
767443SLisa.Hsu@amd.com        print arg
776818SN/A        config = myCP()
786818SN/A        config.readfp(open(cpts[i] + "/m5.cpt"))
796818SN/A
806818SN/A        for sec in config.sections():
816818SN/A            if re.compile("cpu").search(sec):
826818SN/A                newsec = re.sub("cpu", "cpu" + str(i), sec)
836818SN/A                merged.add_section(newsec)
846820SN/A                if re.compile("workload$").search(sec):
856820SN/A                    merged.set(newsec, "M5_pid", i)
866818SN/A
876818SN/A                items = config.items(sec)
888166SLisa.Hsu@amd.com                if options.alpha:
898166SLisa.Hsu@amd.com                    for item in items:
908166SLisa.Hsu@amd.com                        if item[0] == "ppn":
918166SLisa.Hsu@amd.com                            if config.getint(sec, "tag") != 0:
928166SLisa.Hsu@amd.com                                merged.set(newsec, item[0], int(item[1]) + page_ptr)
938166SLisa.Hsu@amd.com                                continue
948166SLisa.Hsu@amd.com                        elif item[0] == "asn":
958166SLisa.Hsu@amd.com                            tmp = re.compile("(.*).Entry(\d+)").search(sec).groups()
968166SLisa.Hsu@amd.com                            if config.has_option(tmp[0], "nlu"):
978166SLisa.Hsu@amd.com                                size = config.getint(tmp[0], "nlu")
988166SLisa.Hsu@amd.com                                if int(tmp[1]) < size:
998166SLisa.Hsu@amd.com                                    merged.set(newsec, item[0], i)
1008166SLisa.Hsu@amd.com                                    continue
1018166SLisa.Hsu@amd.com                            else:
1026818SN/A                                merged.set(newsec, item[0], i)
1036818SN/A                                continue
1048166SLisa.Hsu@amd.com                        merged.set(newsec, item[0], item[1])
1058166SLisa.Hsu@amd.com                else:a #x86
1068166SLisa.Hsu@amd.com                    for item in items:
1078166SLisa.Hsu@amd.com                        if item[0] == "paddr":
1088166SLisa.Hsu@amd.com                            merged.set(newsec, item[0], int(item[1]) + (page_ptr << 12))
1096818SN/A                            continue
1108166SLisa.Hsu@amd.com                        merged.set(newsec, item[0], item[1])
1118166SLisa.Hsu@amd.com
1126818SN/A            elif sec == "system":
1136818SN/A                pass
1146818SN/A            elif sec == "Globals":
1156818SN/A                tick = config.getint(sec, "curTick")
1166818SN/A                if tick > max_curtick:
1176818SN/A                    max_curtick = tick
1186818SN/A                    when = config.getint("system.cpu.tickEvent", "_when")
1196818SN/A            else:
1206818SN/A                if i == 0:
1216818SN/A                    merged.add_section(sec)
1226818SN/A                    for item in config.items(sec):
1236818SN/A                        merged.set(sec, item[0], item[1])
1246818SN/A                        if item[0] == "curtick":
1256818SN/A                            merged.optionxform(str("curTick"))
1266818SN/A                        elif item[0] == "numevents":
1276818SN/A                            merged.optionxform(str("numEvents"))
1286818SN/A
1298166SLisa.Hsu@amd.com        page_ptr = page_ptr + int(config.get("system", "pagePtr"))
1306818SN/A
1316818SN/A        ### memory stuff
1326818SN/A        f = open(cpts[i] + "/system.physmem.physmem", "rb")
1336818SN/A        gf = gzip.GzipFile(fileobj=f, mode="rb")
1348166SLisa.Hsu@amd.com        pages = int(config.get("system", "pagePtr"))
1357443SLisa.Hsu@amd.com        print "pages to be read: ", pages
1366818SN/A
1377443SLisa.Hsu@amd.com        x = 0
1387443SLisa.Hsu@amd.com        while x < pages:
1398166SLisa.Hsu@amd.com            if options.alpha:
1408166SLisa.Hsu@amd.com                bytesRead = gf.read(1 << 13)
1418166SLisa.Hsu@amd.com            else: #x86
1428166SLisa.Hsu@amd.com                bytesRead = gf.read(1 << 12)
1437443SLisa.Hsu@amd.com            merged_mem.write(bytesRead)
1447443SLisa.Hsu@amd.com            x += 1
1456818SN/A
1466818SN/A        gf.close()
1476818SN/A        f.close()
1486818SN/A
1496818SN/A    merged.add_section("system")
1508166SLisa.Hsu@amd.com    merged.set("system", "pagePtr", page_ptr)
1518166SLisa.Hsu@amd.com    merged.set("system", "nextPID", len(args))
1528166SLisa.Hsu@amd.com
1536818SN/A    print "WARNING: "
1548166SLisa.Hsu@amd.com    print "Make sure the simulation using this checkpoint has at least ",
1558166SLisa.Hsu@amd.com    if options.alpha:
1568166SLisa.Hsu@amd.com        print page_ptr, "x 8K of memory"
1578166SLisa.Hsu@amd.com    else:  # assume x86
1588166SLisa.Hsu@amd.com        print page_ptr, "x 4K of memory"
1596818SN/A
1606818SN/A    merged.add_section("Globals")
1616818SN/A    merged.set("Globals", "curTick", max_curtick)
1626818SN/A
1636818SN/A    for i in xrange(len(args)):
1646818SN/A        merged.set("system.cpu" + str(i) + ".tickEvent", "_when", when)
1656818SN/A
1666818SN/A    merged.write(file(fullpath + "/m5.cpt", "wb"))
1676818SN/A    merged_mem.close()
1686818SN/A    myfile.close()
1696818SN/A
1706818SN/Aif __name__ == "__main__":
1716818SN/A
1726818SN/A    parser = optparse.OptionParser()
1736818SN/A    parser.add_option("--prefix", type="string", default="agg")
1748166SLisa.Hsu@amd.com    # If not alpha, then assume x86.  Any other ISAs would need
1758166SLisa.Hsu@amd.com    # extra stuff in this script to appropriately parse their page tables
1768166SLisa.Hsu@amd.com    # and understand page sizes.
1778166SLisa.Hsu@amd.com    parser.add_option("--alpha", action="store_true")
1786818SN/A
1796818SN/A    (options, args) = parser.parse_args()
1806818SN/A
1816818SN/A    aggregate(options, args)
1826818SN/A
183