checkpoint_aggregator.py revision 6818
1#! /usr/bin/env python2.6
2
3from ConfigParser import ConfigParser
4import gzip
5
6import sys, re, optparse, os
7
8class myCP(ConfigParser):
9    def __init__(self):
10        ConfigParser.__init__(self)
11
12    def optionxform(self, optionstr):
13        return optionstr
14
15def aggregate(options, args):
16    merged = myCP()
17    page_ptr = 0
18
19    allfiles = os.listdir(os.getcwd())
20    cpts = []
21    for arg in args:
22        found = False
23        for f in allfiles:
24            if re.compile("cpt." + arg + ".\d+").search(f):
25                found = True
26                cpts.append(f)
27                break
28        if not found:
29            print "missing checkpoint: ", arg
30            sys.exit(1)
31
32    dirname = "-".join([options.prefix, "cpt"])
33    print dirname
34    agg_name = "-".join(args)
35    print agg_name
36    fullpath = os.path.join("..", dirname, "cpt." + agg_name + ".10000")
37    if not os.path.isdir(fullpath):
38        os.system("mkdir -p " + fullpath)
39
40    myfile = open(fullpath + "/system.physmem.physmem", "wb+")
41    merged_mem = gzip.GzipFile(fileobj=myfile, mode="wb")
42
43    max_curtick = 0
44    when = 0
45    for (i, arg) in enumerate(args):
46        config = myCP()
47        config.readfp(open(cpts[i] + "/m5.cpt"))
48
49        for sec in config.sections():
50            if re.compile("cpu").search(sec):
51                newsec = re.sub("cpu", "cpu" + str(i), sec)
52                merged.add_section(newsec)
53
54                items = config.items(sec)
55                for item in items:
56                    if item[0] == "ppn":
57                        if config.getint(sec, "tag") != 0:
58                            merged.set(newsec, item[0], int(item[1]) + page_ptr)
59                            continue
60                    elif item[0] == "asn":
61                        tmp = re.compile("(.*).Entry(\d+)").search(sec).groups()
62                        if config.has_option(tmp[0], "nlu"):
63                            size = config.getint(tmp[0], "nlu")
64                            if int(tmp[1]) < size:
65                                merged.set(newsec, item[0], i)
66                                continue
67                        else:
68                            merged.set(newsec, item[0], i)
69                            continue
70                    merged.set(newsec, item[0], item[1])
71            elif sec == "system":
72                pass
73            elif sec == "Globals":
74                tick = config.getint(sec, "curTick")
75                if tick > max_curtick:
76                    max_curtick = tick
77                    when = config.getint("system.cpu.tickEvent", "_when")
78            else:
79                if i == 0:
80                    print sec
81                    merged.add_section(sec)
82                    for item in config.items(sec):
83                        merged.set(sec, item[0], item[1])
84                        if item[0] == "curtick":
85                            merged.optionxform(str("curTick"))
86                        elif item[0] == "numevents":
87                            merged.optionxform(str("numEvents"))
88
89        page_ptr = page_ptr + int(config.get("system", "page_ptr"))
90
91        ### memory stuff
92        f = open(cpts[i] + "/system.physmem.physmem", "rb")
93        gf = gzip.GzipFile(fileobj=f, mode="rb")
94        bytes = int(config.get("system", "page_ptr")) << 13
95        print "bytes to be read: ", bytes
96
97        bytesRead = gf.read(int(config.get("system", "page_ptr")) << 13)
98        merged_mem.write(bytesRead)
99
100        gf.close()
101        f.close()
102
103    merged.add_section("system")
104    merged.set("system", "page_ptr", page_ptr)
105    print "WARNING: "
106    print "Make sure the simulation using this checkpoint has at least "
107    if page_ptr > (1<<20):
108        print "8G ",
109    elif page_ptr > (1<<19):
110        print "4G ",
111    elif page_ptr > (1<<18):
112        print "2G ",
113    elif page_ptr > (1<<17):
114        print "1G ",
115    elif page_ptr > (1<<16):
116        print "512KB ",
117    else:
118        print "this is a small sim, you're probably fine",
119    print "of memory."
120
121    merged.add_section("Globals")
122    merged.set("Globals", "curTick", max_curtick)
123
124    for i in xrange(len(args)):
125        merged.set("system.cpu" + str(i) + ".tickEvent", "_when", when)
126
127    merged.write(file(fullpath + "/m5.cpt", "wb"))
128    merged_mem.close()
129    myfile.close()
130
131if __name__ == "__main__":
132
133    parser = optparse.OptionParser()
134    parser.add_option("--prefix", type="string", default="agg")
135
136    (options, args) = parser.parse_args()
137
138    aggregate(options, args)
139
140