util: update aggregator to handle x86 checkpoints.
[gem5.git] / util / checkpoint_aggregator.py
1 # Copyright (c) 2009 The Regents of The University of Michigan
2 # Copyright (c) 2011 Advanced Micro Devices, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #
28 # Authors: Lisa Hsu
29
30 from ConfigParser import ConfigParser
31 import gzip
32
33 import sys, re, optparse, os
34
35 class myCP(ConfigParser):
36 def __init__(self):
37 ConfigParser.__init__(self)
38
39 def optionxform(self, optionstr):
40 return optionstr
41
42 def aggregate(options, args):
43 merged = myCP()
44 page_ptr = 0
45
46 allfiles = os.listdir(os.getcwd())
47 cpts = []
48 for arg in args:
49 found = False
50 for f in allfiles:
51 if re.compile("cpt." + arg + ".\d+").search(f):
52 found = True
53 cpts.append(f)
54 break
55 if not found:
56 print "missing checkpoint: ", arg
57 sys.exit(1)
58
59 dirname = "-".join([options.prefix, "cpt"])
60 agg_name = "-".join(args)
61 print agg_name
62 fullpath = os.path.join("..", dirname, "cpt." + agg_name + ".10000")
63 if not os.path.isdir(fullpath):
64 os.system("mkdir -p " + fullpath)
65 elif os.path.isfile(fullpath + "/system.physmem.physmem"):
66 if os.path.isfile(fullpath + "/m5.cpt"):
67 print fullpath, " already done"
68 return
69
70 myfile = open(fullpath + "/system.physmem.physmem", "wb+")
71 merged_mem = gzip.GzipFile(fileobj=myfile, mode="wb")
72
73 max_curtick = 0
74 when = 0
75 for (i, arg) in enumerate(args):
76 print arg
77 config = myCP()
78 config.readfp(open(cpts[i] + "/m5.cpt"))
79
80 for sec in config.sections():
81 if re.compile("cpu").search(sec):
82 newsec = re.sub("cpu", "cpu" + str(i), sec)
83 merged.add_section(newsec)
84 if re.compile("workload$").search(sec):
85 merged.set(newsec, "M5_pid", i)
86
87 items = config.items(sec)
88 if options.alpha:
89 for item in items:
90 if item[0] == "ppn":
91 if config.getint(sec, "tag") != 0:
92 merged.set(newsec, item[0], int(item[1]) + page_ptr)
93 continue
94 elif item[0] == "asn":
95 tmp = re.compile("(.*).Entry(\d+)").search(sec).groups()
96 if config.has_option(tmp[0], "nlu"):
97 size = config.getint(tmp[0], "nlu")
98 if int(tmp[1]) < size:
99 merged.set(newsec, item[0], i)
100 continue
101 else:
102 merged.set(newsec, item[0], i)
103 continue
104 merged.set(newsec, item[0], item[1])
105 else:a #x86
106 for item in items:
107 if item[0] == "paddr":
108 merged.set(newsec, item[0], int(item[1]) + (page_ptr << 12))
109 continue
110 merged.set(newsec, item[0], item[1])
111
112 elif sec == "system":
113 pass
114 elif sec == "Globals":
115 tick = config.getint(sec, "curTick")
116 if tick > max_curtick:
117 max_curtick = tick
118 when = config.getint("system.cpu.tickEvent", "_when")
119 else:
120 if i == 0:
121 merged.add_section(sec)
122 for item in config.items(sec):
123 merged.set(sec, item[0], item[1])
124 if item[0] == "curtick":
125 merged.optionxform(str("curTick"))
126 elif item[0] == "numevents":
127 merged.optionxform(str("numEvents"))
128
129 page_ptr = page_ptr + int(config.get("system", "pagePtr"))
130
131 ### memory stuff
132 f = open(cpts[i] + "/system.physmem.physmem", "rb")
133 gf = gzip.GzipFile(fileobj=f, mode="rb")
134 pages = int(config.get("system", "pagePtr"))
135 print "pages to be read: ", pages
136
137 x = 0
138 while x < pages:
139 if options.alpha:
140 bytesRead = gf.read(1 << 13)
141 else: #x86
142 bytesRead = gf.read(1 << 12)
143 merged_mem.write(bytesRead)
144 x += 1
145
146 gf.close()
147 f.close()
148
149 merged.add_section("system")
150 merged.set("system", "pagePtr", page_ptr)
151 merged.set("system", "nextPID", len(args))
152
153 print "WARNING: "
154 print "Make sure the simulation using this checkpoint has at least ",
155 if options.alpha:
156 print page_ptr, "x 8K of memory"
157 else: # assume x86
158 print page_ptr, "x 4K of memory"
159
160 merged.add_section("Globals")
161 merged.set("Globals", "curTick", max_curtick)
162
163 for i in xrange(len(args)):
164 merged.set("system.cpu" + str(i) + ".tickEvent", "_when", when)
165
166 merged.write(file(fullpath + "/m5.cpt", "wb"))
167 merged_mem.close()
168 myfile.close()
169
170 if __name__ == "__main__":
171
172 parser = optparse.OptionParser()
173 parser.add_option("--prefix", type="string", default="agg")
174 # If not alpha, then assume x86. Any other ISAs would need
175 # extra stuff in this script to appropriately parse their page tables
176 # and understand page sizes.
177 parser.add_option("--alpha", action="store_true")
178
179 (options, args) = parser.parse_args()
180
181 aggregate(options, args)
182