-
Notifications
You must be signed in to change notification settings - Fork 602
/
Copy pathmemstats_xtensa.py
executable file
·383 lines (335 loc) · 12.4 KB
/
memstats_xtensa.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python
###########################################################################
#
# Copyright 2019 Samsung Electronics All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#
###########################################################################
from __future__ import print_function
from optparse import OptionParser
import sys
import re
if sys.version_info[0] < 3:
from sets import Set
parser = OptionParser()
parser.add_option("-f", "--file", dest="infilename",
help="map FILE needed to be parsed", metavar="INPUT_FILE")
parser.add_option("-o", "--output", dest="output",
help="Output written to this file. Default is stdout.", metavar="OUTPUT_FILE")
parser.add_option("-a", "--all", action="store_true", dest="all",
help="Print all information.", default=False)
parser.add_option("-s", "--size", action="store_true", dest="totsize",
help="Print total size of data segments.", default=False)
parser.add_option("-l", "--libsize", action="store_true",
dest="libsize", help="Print Library Sizes.", default=False)
parser.add_option("-d", "--details", action="store_true", dest="details",
help="Print details of Object Sizes.", default=False)
#Example: memstats_xtensa.py <option {-a / -s / -l / -d}> -f tinyara.map -o memory.txt.
(options, args) = parser.parse_args()
if not options.infilename:
parser.print_help()
sys.exit(1)
if (not options.all) and (not options.totsize) and (not options.libsize) and (not options.details):
parser.print_help()
sys.exit(1)
infile = open(options.infilename, 'r')
if options.output:
sys.stdout = open(options.output, 'w')
first = 0
currentSymbol = ""
level1 = {}
action = 0
subSymbol = 0
libObject = ""
librarySize = {}
objectSize = {}
allLibraries = {}
allmap = {}
Required = {
".text": [],
".data": [],
".bss": [],
}
Symnamemap = {
".iram0.vectors": [],
".iram0.text": [],
".flash.text": [],
".dram0.data": [],
".dram0.bss": [],
".flash.rodata": []
}
key_list = list(Required.keys())
Symnamemap[".iram0.vectors"] = key_list[2] # text seg
Symnamemap["flash.rodata"] = key_list[2] # text seg
Symnamemap[".iram0.text"] = key_list[2] # text seg
Symnamemap[".flash.text"] = key_list[2] # text seg
Symnamemap[".dram0.data"] = key_list[0] # data seg
Symnamemap[".dram0.bss"] = key_list[1] # bss seg
def transform(k):
i = 0
while(i < len(k)):
if k[i] == '*':
k = k[:i] + '.'+k[i:]
i = i+1
i = i+1
k = '^' + k + '$'
return k
def isValidSubSymbol(field1, dictKeys):
for k in dictKeys:
tk = transform(k)
if re.search(tk, field1) != None:
return k
return None
def findParent(c):
for r in list(Required.keys()):
if c in Required[r]:
return r
return None
def getListfromString(s):
res = []
s = s[s.find('(')+1:s.rfind(')')]
word = ''
for x in s:
if (x == '(' or x == ')' or x == ' '):
if word != '':
res.append(word)
word = ''
else:
word = word + x
if word != '':
res.append(word)
return res
def PLO(size, libObj, currSym, subSym):
#print currSym, subSym
mapedsym = Symnamemap[currSym]
# print mapedsym
if mapedsym not in list(Required.keys()):
return
if subSym not in Required[mapedsym]:
Required[mapedsym].append(subSym)
# print "================"
libObj = libObj.strip().split('/')
libObj = libObj[len(libObj)-1]
if '(' in libObj:
library = libObj.split('(')[0]
dotO = libObj.split('(')[1][:-1]
else:
library = "NOLIB"
dotO = libObj
if library in list(librarySize.keys()):
librarySize[library] += size
else:
librarySize[library] = size
if dotO in list(objectSize.keys()):
objectSize[dotO] += size
else:
objectSize[dotO] = size
currSymParent = findParent(subSym)
if library in list(allmap.keys()):
if dotO in list(allmap[library].keys()):
if currSymParent in list(allmap[library][dotO].keys()):
allmap[library][dotO][currSymParent] += size
else:
allmap[library][dotO][currSymParent] = size
else:
allmap[library][dotO] = {currSymParent: size}
else:
allmap[library] = {dotO: {currSymParent: size}}
def printLibrarySizes():
nfields = len(list(Required.keys()))
for r in list(Required.keys()):
print("\t"+r, end=' ')
print("\t Total")
for l in list(allmap.keys()):
sizearr = [0] * nfields
for o in allmap[l]:
for s in list(allmap[l][o].keys()):
sizearr[list(Required.keys()).index(s)] += allmap[l][o][s]
for r in list(Required.keys()):
print("\t"+str(sizearr[list(Required.keys()).index(r)]), end=' ')
print("\t" + str(librarySize[l]) + "\t" + l)
def sortByTotal(n):
return int(n[len(n)-2])
def sortPrintLibrarySizes():
results = []
nfields = len(list(Required.keys()))
for r in list(Required.keys()):
print("\t"+r, end=' ')
print("\t Total")
for l in list(allmap.keys()):
sizearr = [0] * nfields
for o in list(allmap[l].keys()):
for s in list(allmap[l][o].keys()):
sizearr[list(Required.keys()).index(s)] += allmap[l][o][s]
one_result = []
for r in list(Required.keys()):
one_result.append(str(sizearr[list(Required.keys()).index(r)]))
one_result.append(str(librarySize[l]))
one_result.append(l)
results.append(one_result)
results.sort(key=sortByTotal, reverse=True)
for r in results:
for i in range(0, len(r) - 1):
print("\t" + r[i].strip(), end=' ')
print("\t" + r[len(r) - 1].strip())
def sortPrintall():
results = []
nfields = len(list(Required.keys()))
for l in list(allmap.keys()):
print(l + "\t" + str(librarySize[l]))
for r in list(Required.keys()):
print("\t"+r, end=' ')
print("\t Total")
for o in list(allmap[l].keys()):
sizearr = [0] * nfields
for s in list(Required.keys()):
if s in list(allmap[l][o].keys()):
sizearr[list(Required.keys()).index(s)] = allmap[l][o][s]
one_result = []
for r in list(Required.keys()):
one_result.append(str(sizearr[list(Required.keys()).index(r)]))
one_result.append(str(objectSize[o]))
one_result.append(o)
results.append(one_result)
results.sort(key=sortByTotal, reverse=True)
for r in results:
for i in range(0, len(r) - 1):
print("\t" + r[i].strip(), end=' ')
print("\t" + r[len(r) - 1].strip())
results = []
def printall():
nfields = len(list(Required.keys()))
for l in list(allmap.keys()):
print(l + "\t" + str(librarySize[l]))
for r in list(Required.keys()):
print("\t"+r, end=' ')
print("\t Total")
for o in list(allmap[l].keys()):
sizearr = [0] * nfields
for s in list(Required.keys()):
if s in list(allmap[l][o].keys()):
sizearr[list(Required.keys()).index(s)] = allmap[l][o][s]
for r in list(Required.keys()):
print(
"\t"+str(sizearr[list(Required.keys()).index(r)]), end=' ')
print("\t" + str(objectSize[o]) + "\t" + o)
def printTotalSize():
ksSize = [0] * len(Required)
for ks in list(level1.keys()):
if ks in list(Required.keys()):
for ks1 in Required[ks]:
if ks1 in list(level1.keys()):
for s in level1[ks1]:
ksSize[list(Required.keys()).index(ks)
] += level1[ks1][s]
for r in list(Required.keys()):
print("\t"+r, end=' ')
print("")
for r in ksSize:
print("\t"+str(r), end=' ')
print("\n")
for line in infile:
if line not in ['\n', '\r\n']:
# Ignore section before "Linker script and memory map" and after "/DISCARD/"
if line.strip() == "Linker script and memory map":
action = 1
continue
if line.strip() == "/DISCARD/":
action = 0
# Ignore section before "Linker script and memory map" and after "/DISCARD/"
if action == 0:
continue
# Ignore the first line of Linker script and Memory map
if re.search("__ebase = ORIGIN", line) != None:
continue
lsplit = line.split()
if (line[0] == '.'):
currentSymbol = lsplit[0]
subSymbol = 0
if currentSymbol == ".comment":
action = 0
level1[currentSymbol] = {}
continue
else:
if re.search('\*\(.*\)', line.strip()) != None:
level2string = getListfromString(line.strip())
for l2strs in level2string:
if l2strs in list(level1[currentSymbol].keys()):
continue
else:
level1[currentSymbol][l2strs] = 0
continue
else:
if currentSymbol == '':
continue
matchedkey = isValidSubSymbol(
lsplit[0], list(level1[currentSymbol].keys()))
if matchedkey != None:
subSymbol = matchedkey
if len(lsplit) > 2:
if int(lsplit[1], 16) != 0:
level1[currentSymbol][subSymbol] += int(
lsplit[2], 16)
libObject = lsplit[len(lsplit)-1]
PLO(int(lsplit[2], 16), libObject,
currentSymbol, subSymbol)
else:
line = next(infile)
line = line.strip()
lsplit = line.split()
if int(lsplit[0], 16) != 0:
level1[currentSymbol][subSymbol] += int(
lsplit[1], 16)
libObject = lsplit[len(lsplit)-1]
PLO(int(lsplit[1], 16), libObject,
currentSymbol, subSymbol)
elif re.search('\*fill\*', lsplit[0]) != None:
if int(lsplit[1], 16) != 0 and subSymbol != 0:
level1[currentSymbol][subSymbol] += int(lsplit[2], 16)
PLO(int(lsplit[2], 16), libObject,
currentSymbol, subSymbol)
infile.close()
if options.all:
options.totsize = options.libsize = options.details = True
if options.totsize:
print("######################################")
print("## Total sizes ##")
print("######################################")
printTotalSize()
print("######################################")
print("## Sizes Calculated Using: ##")
print("######################################")
for r in list(Required.keys()):
print(r+":\n\t", end=' ')
t = 1
for s in Required[r]:
if t % 4 == 0:
print(s + "\t\n\t", end=' ')
else:
print(s+"\t", end=' ')
t += 1
print("")
if options.libsize:
print("######################################")
print("## Library Sizes ##")
print("######################################")
#printLibrarySizes()
sortPrintLibrarySizes()
if options.details:
print("######################################")
print("## Details ##")
print("######################################")
#printall()
sortPrintall()
print("######################################\n")