-
Notifications
You must be signed in to change notification settings - Fork 13
/
Gem5ToMcPAT-Parser.py
296 lines (270 loc) · 12.1 KB
/
Gem5ToMcPAT-Parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import argparse
import sys
import json
import re
from xml.etree import ElementTree as ET
from xml.dom import minidom
import copy
import types
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def create_parser():
parser = argparse.ArgumentParser(
formatter_class = argparse.RawDescriptionHelpFormatter,
description = "Gem5 to McPAT parser")
parser.add_argument(
'--config', '-c', type = str, required = True,
metavar = 'PATH',
help = "Input config.json from Gem5 output.")
parser.add_argument(
'--stats', '-s', type = str, required = True,
metavar = 'PATH',
help = "Input stats.txt from Gem5 output.")
parser.add_argument(
'--template', '-t', type = str, required = True,
metavar = 'PATH',
help = "Template XML file")
parser.add_argument(
'--output', '-o', type = argparse.FileType('w'), default = "mcpat-in.xml",
metavar = 'PATH',
help = "Output file for McPAT input in XML format (default: mcpat-in.xml)")
return parser
class PIParser(ET.XMLTreeBuilder):
def __init__(self):
ET.XMLTreeBuilder.__init__(self)
# assumes ElementTree 1.2.X
self._parser.CommentHandler = self.handle_comment
self._parser.ProcessingInstructionHandler = self.handle_pi
self._target.start("document", {})
def close(self):
self._target.end("document")
return ET.XMLTreeBuilder.close(self)
def handle_comment(self, data):
self._target.start(ET.Comment, {})
self._target.data(data)
self._target.end(ET.Comment)
def handle_pi(self, target, data):
self._target.start(ET.PI, {})
self._target.data(target + " " + data)
self._target.end(ET.PI)
def parse(source):
return ET.parse(source, PIParser())
def readStatsFile(statsFile):
global stats
stats = {}
F = open(statsFile)
ignores = re.compile(r'^---|^$')
statLine = re.compile(r'([a-zA-Z0-9_\.:-]+)\s+([-+]?[0-9]+\.[0-9]+|[-+]?[0-9]+|nan|inf)')
count = 0
for line in F:
#ignore empty lines and lines starting with "---"
if not ignores.match(line):
count += 1
statKind = statLine.match(line).group(1)
statValue = statLine.match(line).group(2)
if statValue == 'nan':
print "\tWarning (stats): %s is nan. Setting it to 0" % statKind
statValue = '0'
stats[statKind] = statValue
F.close()
def readConfigFile(configFile):
global config
F = open(configFile)
config = json.load(F)
#print config
#print config["system"]["membus"]
#print config["system"]["cpu"][0]["clock"]
F.close()
def readMcpatFile(templateFile):
global templateMcpat
templateMcpat = parse(templateFile)
#ET.dump(templateMcpat)
def prepareTemplate(outputFile):
numCores = len(config["system"]["cpu"])
privateL2 = config["system"]["cpu"][0].has_key('l2cache')
sharedL2 = config["system"].has_key('l2')
if privateL2:
numL2 = numCores
elif sharedL2:
numL2 = 1
else:
numL2 = 0
elemCounter = 0
root = templateMcpat.getroot()
for child in root[0][0]:
elemCounter += 1 # to add elements in correct sequence
if child.attrib.get("name") == "number_of_cores":
child.attrib['value'] = str(numCores)
if child.attrib.get("name") == "number_of_L2s":
child.attrib['value'] = str(numL2)
if child.attrib.get("name") == "Private_L2":
if sharedL2:
Private_L2 = str(0)
else:
Private_L2 = str(1)
child.attrib['value'] = Private_L2
temp = child.attrib.get('value')
# to consider all the cpus in total cycle calculation
if isinstance(temp, basestring) and "cpu." in temp and temp.split('.')[0] == "stats":
value = "(" + temp.replace("cpu.", "cpu0.") + ")"
for i in range(1, numCores):
value = value + " + (" + temp.replace("cpu.", "cpu"+str(i)+".") +")"
child.attrib['value'] = value
# remove a core template element and replace it with number of cores template elements
if child.attrib.get("name") == "core":
coreElem = copy.deepcopy(child)
coreElemCopy = copy.deepcopy(coreElem)
for coreCounter in range(numCores):
coreElem.attrib["name"] = "core" + str(coreCounter)
coreElem.attrib["id"] = "system.core" + str(coreCounter)
for coreChild in coreElem:
childId = coreChild.attrib.get("id")
childValue = coreChild.attrib.get("value")
childName = coreChild.attrib.get("name")
if isinstance(childName, basestring) and childName == "x86":
if config["system"]["cpu"][coreCounter]["isa"][0]["type"] == "X86ISA":
childValue = "1"
else:
childValue = "0"
if isinstance(childId, basestring) and "core" in childId:
childId = childId.replace("core", "core" + str(coreCounter))
if isinstance(childValue, basestring) and "cpu." in childValue and "stats" in childValue.split('.')[0]:
childValue = childValue.replace("cpu." , "cpu" + str(coreCounter)+ ".")
if isinstance(childValue, basestring) and "cpu." in childValue and "config" in childValue.split('.')[0]:
childValue = childValue.replace("cpu." , "cpu." + str(coreCounter)+ ".")
if len(list(coreChild)) is not 0:
for level2Child in coreChild:
level2ChildValue = level2Child.attrib.get("value")
if isinstance(level2ChildValue, basestring) and "cpu." in level2ChildValue and "stats" in level2ChildValue.split('.')[0]:
level2ChildValue = level2ChildValue.replace("cpu." , "cpu" + str(coreCounter)+ ".")
if isinstance(level2ChildValue, basestring) and "cpu." in level2ChildValue and "config" in level2ChildValue.split('.')[0]:
level2ChildValue = level2ChildValue.replace("cpu." , "cpu." + str(coreCounter)+ ".")
level2Child.attrib["value"] = level2ChildValue
if isinstance(childId, basestring):
coreChild.attrib["id"] = childId
if isinstance(childValue, basestring):
coreChild.attrib["value"] = childValue
root[0][0].insert(elemCounter, coreElem)
coreElem = copy.deepcopy(coreElemCopy)
elemCounter += 1
root[0][0].remove(child)
elemCounter -= 1
# # remove a L2 template element and replace it with the private L2 template elements
# if child.attrib.get("name") == "L2.shared":
# print child
# if sharedL2:
# child.attrib["name"] = "L20"
# child.attrib["id"] = "system.L20"
# else:
# root[0][0].remove(child)
# remove a L2 template element and replace it with number of L2 template elements
if child.attrib.get("name") == "L2":
if privateL2:
l2Elem = copy.deepcopy(child)
l2ElemCopy = copy.deepcopy(l2Elem)
for l2Counter in range(numL2):
l2Elem.attrib["name"] = "L2" + str(l2Counter)
l2Elem.attrib["id"] = "system.L2" + str(l2Counter)
for l2Child in l2Elem:
childValue = l2Child.attrib.get("value")
if isinstance(childValue, basestring) and "cpu." in childValue and "stats" in childValue.split('.')[0]:
childValue = childValue.replace("cpu." , "cpu" + str(l2Counter)+ ".")
if isinstance(childValue, basestring) and "cpu." in childValue and "config" in childValue.split('.')[0]:
childValue = childValue.replace("cpu." , "cpu." + str(l2Counter)+ ".")
if isinstance(childValue, basestring):
l2Child.attrib["value"] = childValue
root[0][0].insert(elemCounter, l2Elem)
l2Elem = copy.deepcopy(l2ElemCopy)
elemCounter += 1
root[0][0].remove(child)
else:
child.attrib["name"] = "L20"
child.attrib["id"] = "system.L20"
for l2Child in child:
childValue = l2Child.attrib.get("value")
if isinstance(childValue, basestring) and "cpu.l2cache." in childValue:
childValue = childValue.replace("cpu.l2cache." , "l2.")
prettify(root)
#templateMcpat.write(outputFile)
def getConfValue(confStr):
spltConf = re.split('\.', confStr)
currConf = config
currHierarchy = ""
for x in spltConf:
currHierarchy += x
if x.isdigit():
currConf = currConf[int(x)]
elif x in currConf:
# if isinstance(currConf, types.ListType):
# #this is mostly for system.cpu* as system.cpu is an array
# #This could be made better
# if x not in currConf[0]:
# print "%s does not exist in config" % currHierarchy
# else:
# currConf = currConf[0][x]
# else:
# print "***WARNING: %s does not exist in config.***" % currHierarchy
# print "\t Please use the right config param in your McPAT template file"
# else:
currConf = currConf[x]
currHierarchy += "."
return currConf
# def getConfValue(confStr):
# spltConf = re.split('\.', confStr)
# currConf = config
# for index in spltConf:
# currConf = currConf[index]
# return currConf
def dumpMcpatOut(outFile):
rootElem = templateMcpat.getroot()
configMatch = re.compile(r'config\.([][a-zA-Z0-9_:\.]+)')
#replace params with values from the GEM5 config file
for param in rootElem.iter('param'):
name = param.attrib['name']
value = param.attrib['value']
if 'config' in value:
allConfs = configMatch.findall(value)
for conf in allConfs:
confValue = getConfValue(conf)
value = re.sub("config."+ conf, str(confValue), value)
if "," in value:
exprs = re.split(',', value)
for i in range(len(exprs)):
exprs[i] = str(eval(exprs[i]))
param.attrib['value'] = ','.join(exprs)
else:
param.attrib['value'] = str(eval(str(value)))
#replace stats with values from the GEM5 stats file
statRe = re.compile(r'stats\.([a-zA-Z0-9_:\.]+)')
for stat in rootElem.iter('stat'):
name = stat.attrib['name']
value = stat.attrib['value']
if 'stats' in value:
allStats = statRe.findall(value)
expr = value
for i in range(len(allStats)):
if allStats[i] in stats:
expr = re.sub('stats.%s' % allStats[i], stats[allStats[i]], expr)
else:
expr = re.sub('stats.%s' % allStats[i], str(1), expr)
print "***WARNING: %s does not exist in stats***" % allStats[i]
print "\t Please use the right stats in your McPAT template file"
if 'config' not in expr and 'stats' not in expr:
stat.attrib['value'] = str(eval(expr))
#Write out the xml file
templateMcpat.write(outFile)
def main():
global args
parser = create_parser()
args = parser.parse_args()
readStatsFile(args.stats)
readConfigFile(args.config)
readMcpatFile(args.template)
prepareTemplate(args.output)
dumpMcpatOut(args.output)
if __name__ == '__main__':
main()