forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memanalyzer.py
executable file
·183 lines (149 loc) · 5.18 KB
/
memanalyzer.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
#!/usr/bin/env python
########################################################
#
# ESPEasy plugin memory analyser
#
###
# Based on:
# https://raw.githubusercontent.com/SmingHub/Sming/develop/tools/memanalyzer.py
# Memory Analyzer
# Author: Slavey Karadzhov <[email protected]>
# Based on https://github.com/Sermus/ESP8266_memory_analyzer
#
########################################################
from collections import OrderedDict
import os.path
import shlex
import subprocess
import sys
import glob
import os
TOTAL_IRAM = 32786;
TOTAL_DRAM = 81920;
env="spec_memanalyze_ESP8266"
sections = OrderedDict([
("data", "Initialized Data (RAM)"),
("rodata", "ReadOnly Data (RAM)"),
("bss", "Uninitialized Data (RAM)"),
("text", "Cached Code (IRAM)"),
("irom0_text", "Uncached Code (SPI)")
])
def abort(txt):
raise(Exception("error: "+txt))
def analyse_memory(elfFile):
command = "%s -t '%s' " % (objectDumpBin, elfFile)
response = subprocess.check_output(shlex.split(command))
if isinstance(response, bytes):
response = response.decode('utf-8')
lines = response.split('\n')
# print("{0: >10}|{1: >30}|{2: >12}|{3: >12}|{4: >8}".format("Section", "Description", "Start (hex)", "End (hex)", "Used space"));
# print("------------------------------------------------------------------------------");
ret={}
usedRAM = 0
usedIRAM = 0
i = 0
for (id, descr) in list(sections.items()):
sectionStartToken = " _%s_start" % id
sectionEndToken = " _%s_end" % id
sectionStart = -1
sectionEnd = -1
for line in lines:
if sectionStartToken in line:
data = line.split(' ')
sectionStart = int(data[0], 16)
if sectionEndToken in line:
data = line.split(' ')
sectionEnd = int(data[0], 16)
if sectionStart != -1 and sectionEnd != -1:
break
sectionLength = sectionEnd - sectionStart
# if i < 3:
# usedRAM += sectionLength
# if i == 3:
# usedIRAM = TOTAL_IRAM - sectionLength;
ret[id]=sectionLength
# print("{0: >10}|{1: >30}|{2:12X}|{3:12X}|{4:8}".format(id, descr, sectionStart, sectionEnd, sectionLength))
# i += 1
# print("Total Used RAM : %d" % usedRAM)
# print("Free RAM : %d" % (TOTAL_DRAM - usedRAM))
# print("Free IRam : %d" % usedIRAM)
return(ret)
try:
################### start
if len(sys.argv) <= 1:
print("Usage: \n\t%s%s <path_to_objdump>" % sys.argv[0])
print(" e.g.")
print(" ~/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-objdump")
print(" c:/Users/gijs/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-objdump.exe")
sys.exit(1)
# e.g.
# ~/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-objdump
# c:/Users/gijs/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-objdump.exe
objectDumpBin = sys.argv[1]
#get list of all plugins
#which plugins to test?
tmpplugins = []
plugins = []
pluginnames = {}
plugins.append('CORE_ONLY')
if len(sys.argv)>2:
tmpplugins=sys.argv[2:]
else:
tmpplugins=glob.glob("src/_[CPN][0-9][0-9][0-9]*.ino")
tmpplugins.sort()
for plugin in tmpplugins:
pluginname=plugin[plugin.find('_'):]
buildflag= "USES{}".format(pluginname[:5])
pluginnames[buildflag] = plugin
plugins.append(buildflag)
plugins.append('MQTT_ONLY')
plugins.append('FEATURE_SETTINGS_ARCHIVE=1')
plugins.append('WEBSERVER_RULES_DEBUG=1')
plugins.append('WEBSERVER_TIMINGSTATS')
plugins.append('WEBSERVER_NEW_UI')
print("Analysing ESPEasy memory usage for env {} ...\n".format(env))
output_format="{:<30}|{:<11}|{:<11}|{:<11}|{:<11}|{:<11}"
print(output_format.format(
"module",
"cache IRAM",
"init RAM",
"r.o. RAM",
"uninit RAM",
"Flash ROM",
))
##### test per plugin
results={}
base = {}
for plugin in plugins:
buildflag= "-D{}".format(plugin)
my_env = os.environ.copy()
my_env["PLATFORMIO_BUILD_FLAGS"] = buildflag
subprocess.check_call("platformio run --silent --environment {}".format(env), shell=True, env=my_env)
res = analyse_memory(".pio/build/"+env+"/firmware.elf")
if plugin == 'CORE_ONLY':
base = res
print(output_format.format(
"CORE",
base['text'],
base['data'],
base['rodata'],
base['bss'],
base['irom0_text'],
))
else:
results[plugin] = res
name = plugin
if plugin in pluginnames:
name = pluginnames[plugin]
print(output_format.format(
name,
results[plugin]['text']-base['text'],
results[plugin]['data']-base['data'],
results[plugin]['rodata']-base['rodata'],
results[plugin]['bss']-base['bss'],
results[plugin]['irom0_text']-base['irom0_text'],
))
except:
raise
print("\n")