checkpoint_aggregator.py revision 6818
111988Sandreas.sandberg@arm.com#! /usr/bin/env python2.6
211988Sandreas.sandberg@arm.com
311988Sandreas.sandberg@arm.comfrom ConfigParser import ConfigParser
411988Sandreas.sandberg@arm.comimport gzip
511988Sandreas.sandberg@arm.com
611988Sandreas.sandberg@arm.comimport sys, re, optparse, os
711988Sandreas.sandberg@arm.com
811988Sandreas.sandberg@arm.comclass myCP(ConfigParser):
911988Sandreas.sandberg@arm.com    def __init__(self):
1011988Sandreas.sandberg@arm.com        ConfigParser.__init__(self)
1111988Sandreas.sandberg@arm.com
1211988Sandreas.sandberg@arm.com    def optionxform(self, optionstr):
1311988Sandreas.sandberg@arm.com        return optionstr
1411988Sandreas.sandberg@arm.com
1511988Sandreas.sandberg@arm.comdef aggregate(options, args):
1611988Sandreas.sandberg@arm.com    merged = myCP()
1711988Sandreas.sandberg@arm.com    page_ptr = 0
1811988Sandreas.sandberg@arm.com
1911988Sandreas.sandberg@arm.com    allfiles = os.listdir(os.getcwd())
2011988Sandreas.sandberg@arm.com    cpts = []
2111988Sandreas.sandberg@arm.com    for arg in args:
2211988Sandreas.sandberg@arm.com        found = False
2311988Sandreas.sandberg@arm.com        for f in allfiles:
2411988Sandreas.sandberg@arm.com            if re.compile("cpt." + arg + ".\d+").search(f):
2511988Sandreas.sandberg@arm.com                found = True
2611988Sandreas.sandberg@arm.com                cpts.append(f)
2711988Sandreas.sandberg@arm.com                break
2811988Sandreas.sandberg@arm.com        if not found:
2911988Sandreas.sandberg@arm.com            print "missing checkpoint: ", arg
3011988Sandreas.sandberg@arm.com            sys.exit(1)
3111988Sandreas.sandberg@arm.com
3211988Sandreas.sandberg@arm.com    dirname = "-".join([options.prefix, "cpt"])
3311988Sandreas.sandberg@arm.com    print dirname
3411988Sandreas.sandberg@arm.com    agg_name = "-".join(args)
3511988Sandreas.sandberg@arm.com    print agg_name
3611988Sandreas.sandberg@arm.com    fullpath = os.path.join("..", dirname, "cpt." + agg_name + ".10000")
3711988Sandreas.sandberg@arm.com    if not os.path.isdir(fullpath):
3811988Sandreas.sandberg@arm.com        os.system("mkdir -p " + fullpath)
3911988Sandreas.sandberg@arm.com
4011988Sandreas.sandberg@arm.com    myfile = open(fullpath + "/system.physmem.physmem", "wb+")
4111988Sandreas.sandberg@arm.com    merged_mem = gzip.GzipFile(fileobj=myfile, mode="wb")
4211988Sandreas.sandberg@arm.com
4311988Sandreas.sandberg@arm.com    max_curtick = 0
4411988Sandreas.sandberg@arm.com    when = 0
4511988Sandreas.sandberg@arm.com    for (i, arg) in enumerate(args):
4611988Sandreas.sandberg@arm.com        config = myCP()
4711988Sandreas.sandberg@arm.com        config.readfp(open(cpts[i] + "/m5.cpt"))
4811988Sandreas.sandberg@arm.com
4911988Sandreas.sandberg@arm.com        for sec in config.sections():
5011988Sandreas.sandberg@arm.com            if re.compile("cpu").search(sec):
5111988Sandreas.sandberg@arm.com                newsec = re.sub("cpu", "cpu" + str(i), sec)
5211988Sandreas.sandberg@arm.com                merged.add_section(newsec)
5311988Sandreas.sandberg@arm.com
5411988Sandreas.sandberg@arm.com                items = config.items(sec)
5511988Sandreas.sandberg@arm.com                for item in items:
5611988Sandreas.sandberg@arm.com                    if item[0] == "ppn":
5711988Sandreas.sandberg@arm.com                        if config.getint(sec, "tag") != 0:
5811988Sandreas.sandberg@arm.com                            merged.set(newsec, item[0], int(item[1]) + page_ptr)
5911988Sandreas.sandberg@arm.com                            continue
6011988Sandreas.sandberg@arm.com                    elif item[0] == "asn":
6111988Sandreas.sandberg@arm.com                        tmp = re.compile("(.*).Entry(\d+)").search(sec).groups()
6211988Sandreas.sandberg@arm.com                        if config.has_option(tmp[0], "nlu"):
6311988Sandreas.sandberg@arm.com                            size = config.getint(tmp[0], "nlu")
6411988Sandreas.sandberg@arm.com                            if int(tmp[1]) < size:
6511988Sandreas.sandberg@arm.com                                merged.set(newsec, item[0], i)
6611988Sandreas.sandberg@arm.com                                continue
6711988Sandreas.sandberg@arm.com                        else:
6811988Sandreas.sandberg@arm.com                            merged.set(newsec, item[0], i)
6911988Sandreas.sandberg@arm.com                            continue
7011988Sandreas.sandberg@arm.com                    merged.set(newsec, item[0], item[1])
7111988Sandreas.sandberg@arm.com            elif sec == "system":
7211988Sandreas.sandberg@arm.com                pass
7311988Sandreas.sandberg@arm.com            elif sec == "Globals":
7411988Sandreas.sandberg@arm.com                tick = config.getint(sec, "curTick")
7511988Sandreas.sandberg@arm.com                if tick > max_curtick:
7611988Sandreas.sandberg@arm.com                    max_curtick = tick
7711988Sandreas.sandberg@arm.com                    when = config.getint("system.cpu.tickEvent", "_when")
7811988Sandreas.sandberg@arm.com            else:
7911988Sandreas.sandberg@arm.com                if i == 0:
8011988Sandreas.sandberg@arm.com                    print sec
8111988Sandreas.sandberg@arm.com                    merged.add_section(sec)
8211988Sandreas.sandberg@arm.com                    for item in config.items(sec):
8311988Sandreas.sandberg@arm.com                        merged.set(sec, item[0], item[1])
8411988Sandreas.sandberg@arm.com                        if item[0] == "curtick":
8511988Sandreas.sandberg@arm.com                            merged.optionxform(str("curTick"))
8611988Sandreas.sandberg@arm.com                        elif item[0] == "numevents":
8711988Sandreas.sandberg@arm.com                            merged.optionxform(str("numEvents"))
8811988Sandreas.sandberg@arm.com
8911988Sandreas.sandberg@arm.com        page_ptr = page_ptr + int(config.get("system", "page_ptr"))
9011988Sandreas.sandberg@arm.com
9111988Sandreas.sandberg@arm.com        ### memory stuff
9211988Sandreas.sandberg@arm.com        f = open(cpts[i] + "/system.physmem.physmem", "rb")
9311988Sandreas.sandberg@arm.com        gf = gzip.GzipFile(fileobj=f, mode="rb")
9411988Sandreas.sandberg@arm.com        bytes = int(config.get("system", "page_ptr")) << 13
9511988Sandreas.sandberg@arm.com        print "bytes to be read: ", bytes
9611988Sandreas.sandberg@arm.com
9711988Sandreas.sandberg@arm.com        bytesRead = gf.read(int(config.get("system", "page_ptr")) << 13)
9811988Sandreas.sandberg@arm.com        merged_mem.write(bytesRead)
9911988Sandreas.sandberg@arm.com
10011988Sandreas.sandberg@arm.com        gf.close()
10111988Sandreas.sandberg@arm.com        f.close()
10211988Sandreas.sandberg@arm.com
10311988Sandreas.sandberg@arm.com    merged.add_section("system")
10411988Sandreas.sandberg@arm.com    merged.set("system", "page_ptr", page_ptr)
10511988Sandreas.sandberg@arm.com    print "WARNING: "
10611988Sandreas.sandberg@arm.com    print "Make sure the simulation using this checkpoint has at least "
10711988Sandreas.sandberg@arm.com    if page_ptr > (1<<20):
10811988Sandreas.sandberg@arm.com        print "8G ",
10911988Sandreas.sandberg@arm.com    elif page_ptr > (1<<19):
11011988Sandreas.sandberg@arm.com        print "4G ",
11111988Sandreas.sandberg@arm.com    elif page_ptr > (1<<18):
11211988Sandreas.sandberg@arm.com        print "2G ",
11311988Sandreas.sandberg@arm.com    elif page_ptr > (1<<17):
11411988Sandreas.sandberg@arm.com        print "1G ",
11511988Sandreas.sandberg@arm.com    elif page_ptr > (1<<16):
11611988Sandreas.sandberg@arm.com        print "512KB ",
11711988Sandreas.sandberg@arm.com    else:
11811988Sandreas.sandberg@arm.com        print "this is a small sim, you're probably fine",
11911988Sandreas.sandberg@arm.com    print "of memory."
12011988Sandreas.sandberg@arm.com
12111988Sandreas.sandberg@arm.com    merged.add_section("Globals")
12211988Sandreas.sandberg@arm.com    merged.set("Globals", "curTick", max_curtick)
12311988Sandreas.sandberg@arm.com
12411988Sandreas.sandberg@arm.com    for i in xrange(len(args)):
12511988Sandreas.sandberg@arm.com        merged.set("system.cpu" + str(i) + ".tickEvent", "_when", when)
12611988Sandreas.sandberg@arm.com
12711988Sandreas.sandberg@arm.com    merged.write(file(fullpath + "/m5.cpt", "wb"))
12811988Sandreas.sandberg@arm.com    merged_mem.close()
12911988Sandreas.sandberg@arm.com    myfile.close()
13011988Sandreas.sandberg@arm.com
13111988Sandreas.sandberg@arm.comif __name__ == "__main__":
13211988Sandreas.sandberg@arm.com
13311988Sandreas.sandberg@arm.com    parser = optparse.OptionParser()
13411988Sandreas.sandberg@arm.com    parser.add_option("--prefix", type="string", default="agg")
13511988Sandreas.sandberg@arm.com
13611988Sandreas.sandberg@arm.com    (options, args) = parser.parse_args()
13711988Sandreas.sandberg@arm.com
13811988Sandreas.sandberg@arm.com    aggregate(options, args)
13911988Sandreas.sandberg@arm.com
14011988Sandreas.sandberg@arm.com