checkpoint_aggregator.py revision 7443
16821SN/A# Copyright (c) 2009 The Regents of The University of Michigan
26821SN/A# All rights reserved.
36821SN/A#
46821SN/A# Redistribution and use in source and binary forms, with or without
56821SN/A# modification, are permitted provided that the following conditions are
66821SN/A# met: redistributions of source code must retain the above copyright
76821SN/A# notice, this list of conditions and the following disclaimer;
86821SN/A# redistributions in binary form must reproduce the above copyright
96821SN/A# notice, this list of conditions and the following disclaimer in the
106821SN/A# documentation and/or other materials provided with the distribution;
116821SN/A# neither the name of the copyright holders nor the names of its
126821SN/A# contributors may be used to endorse or promote products derived from
136821SN/A# this software without specific prior written permission.
146821SN/A#
156821SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166821SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176821SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186821SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196821SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206821SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216821SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226821SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236821SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246821SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256821SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266821SN/A#
276821SN/A# Authors: Lisa Hsu
286818SN/A
296818SN/Afrom ConfigParser import ConfigParser
306818SN/Aimport gzip
316818SN/A
326818SN/Aimport sys, re, optparse, os
336818SN/A
346818SN/Aclass myCP(ConfigParser):
356818SN/A    def __init__(self):
366818SN/A        ConfigParser.__init__(self)
376818SN/A
386818SN/A    def optionxform(self, optionstr):
396818SN/A        return optionstr
406818SN/A
416818SN/Adef aggregate(options, args):
426818SN/A    merged = myCP()
436818SN/A    page_ptr = 0
446818SN/A
456818SN/A    allfiles = os.listdir(os.getcwd())
466818SN/A    cpts = []
476818SN/A    for arg in args:
486818SN/A        found = False
496818SN/A        for f in allfiles:
506818SN/A            if re.compile("cpt." + arg + ".\d+").search(f):
516818SN/A                found = True
526818SN/A                cpts.append(f)
536818SN/A                break
546818SN/A        if not found:
556818SN/A            print "missing checkpoint: ", arg
566818SN/A            sys.exit(1)
576818SN/A
586818SN/A    dirname = "-".join([options.prefix, "cpt"])
596818SN/A    agg_name = "-".join(args)
606818SN/A    print agg_name
616818SN/A    fullpath = os.path.join("..", dirname, "cpt." + agg_name + ".10000")
626818SN/A    if not os.path.isdir(fullpath):
636818SN/A        os.system("mkdir -p " + fullpath)
647443SLisa.Hsu@amd.com    elif os.path.isfile(fullpath + "/system.physmem.physmem"):
657443SLisa.Hsu@amd.com        if os.path.isfile(fullpath + "/m5.cpt"):
667443SLisa.Hsu@amd.com            print fullpath, " already done"
677443SLisa.Hsu@amd.com            return
686818SN/A
696818SN/A    myfile = open(fullpath + "/system.physmem.physmem", "wb+")
706818SN/A    merged_mem = gzip.GzipFile(fileobj=myfile, mode="wb")
716818SN/A
726818SN/A    max_curtick = 0
736818SN/A    when = 0
746818SN/A    for (i, arg) in enumerate(args):
757443SLisa.Hsu@amd.com        print arg
766818SN/A        config = myCP()
776818SN/A        config.readfp(open(cpts[i] + "/m5.cpt"))
786818SN/A
796818SN/A        for sec in config.sections():
806818SN/A            if re.compile("cpu").search(sec):
816818SN/A                newsec = re.sub("cpu", "cpu" + str(i), sec)
826818SN/A                merged.add_section(newsec)
836820SN/A                if re.compile("workload$").search(sec):
846820SN/A                    merged.set(newsec, "M5_pid", i)
856818SN/A
866818SN/A                items = config.items(sec)
876818SN/A                for item in items:
886818SN/A                    if item[0] == "ppn":
896818SN/A                        if config.getint(sec, "tag") != 0:
906818SN/A                            merged.set(newsec, item[0], int(item[1]) + page_ptr)
916818SN/A                            continue
926818SN/A                    elif item[0] == "asn":
936818SN/A                        tmp = re.compile("(.*).Entry(\d+)").search(sec).groups()
946818SN/A                        if config.has_option(tmp[0], "nlu"):
956818SN/A                            size = config.getint(tmp[0], "nlu")
966818SN/A                            if int(tmp[1]) < size:
976818SN/A                                merged.set(newsec, item[0], i)
986818SN/A                                continue
996818SN/A                        else:
1006818SN/A                            merged.set(newsec, item[0], i)
1016818SN/A                            continue
1026818SN/A                    merged.set(newsec, item[0], item[1])
1036818SN/A            elif sec == "system":
1046818SN/A                pass
1056818SN/A            elif sec == "Globals":
1066818SN/A                tick = config.getint(sec, "curTick")
1076818SN/A                if tick > max_curtick:
1086818SN/A                    max_curtick = tick
1096818SN/A                    when = config.getint("system.cpu.tickEvent", "_when")
1106818SN/A            else:
1116818SN/A                if i == 0:
1126818SN/A                    merged.add_section(sec)
1136818SN/A                    for item in config.items(sec):
1146818SN/A                        merged.set(sec, item[0], item[1])
1156818SN/A                        if item[0] == "curtick":
1166818SN/A                            merged.optionxform(str("curTick"))
1176818SN/A                        elif item[0] == "numevents":
1186818SN/A                            merged.optionxform(str("numEvents"))
1196818SN/A
1206818SN/A        page_ptr = page_ptr + int(config.get("system", "page_ptr"))
1216818SN/A
1226818SN/A        ### memory stuff
1236818SN/A        f = open(cpts[i] + "/system.physmem.physmem", "rb")
1246818SN/A        gf = gzip.GzipFile(fileobj=f, mode="rb")
1257443SLisa.Hsu@amd.com        pages = int(config.get("system", "page_ptr"))
1267443SLisa.Hsu@amd.com        print "pages to be read: ", pages
1276818SN/A
1287443SLisa.Hsu@amd.com        x = 0
1297443SLisa.Hsu@amd.com        while x < pages:
1307443SLisa.Hsu@amd.com            bytesRead = gf.read(1 << 13)
1317443SLisa.Hsu@amd.com            merged_mem.write(bytesRead)
1327443SLisa.Hsu@amd.com            x += 1
1336818SN/A
1346818SN/A        gf.close()
1356818SN/A        f.close()
1366818SN/A
1376818SN/A    merged.add_section("system")
1386818SN/A    merged.set("system", "page_ptr", page_ptr)
1396818SN/A    print "WARNING: "
1406818SN/A    print "Make sure the simulation using this checkpoint has at least "
1416818SN/A    if page_ptr > (1<<20):
1426818SN/A        print "8G ",
1436818SN/A    elif page_ptr > (1<<19):
1446818SN/A        print "4G ",
1456818SN/A    elif page_ptr > (1<<18):
1466818SN/A        print "2G ",
1476818SN/A    elif page_ptr > (1<<17):
1486818SN/A        print "1G ",
1496818SN/A    elif page_ptr > (1<<16):
1506818SN/A        print "512KB ",
1516818SN/A    else:
1526818SN/A        print "this is a small sim, you're probably fine",
1536818SN/A    print "of memory."
1546818SN/A
1556818SN/A    merged.add_section("Globals")
1566818SN/A    merged.set("Globals", "curTick", max_curtick)
1576818SN/A
1586818SN/A    for i in xrange(len(args)):
1596818SN/A        merged.set("system.cpu" + str(i) + ".tickEvent", "_when", when)
1606818SN/A
1616818SN/A    merged.write(file(fullpath + "/m5.cpt", "wb"))
1626818SN/A    merged_mem.close()
1636818SN/A    myfile.close()
1646818SN/A
1656818SN/Aif __name__ == "__main__":
1666818SN/A
1676818SN/A    parser = optparse.OptionParser()
1686818SN/A    parser.add_option("--prefix", type="string", default="agg")
1696818SN/A
1706818SN/A    (options, args) = parser.parse_args()
1716818SN/A
1726818SN/A    aggregate(options, args)
1736818SN/A
174