forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reduces the stack frame size of mp_builtin___import__ by limiting the support path length of files from 256 to 96. This function can be called recursively for nested imports so it adds up. Also reduce mp_execute_bytecode (vm.c) from 206 a bc call to 124. This too is recursive and adds up. It is reduced by preventing some inlining. It may decrease performance slightly when importing and unpacking. Adds two new scripts for debugging. One is used from gdb to print frame sizes in a backtrace. The other prints what pcs use a particular stack offset. This helps find infrequently used stack space. Fixes #8053.
- Loading branch information
Showing
4 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Source this file into gdb `source ../../tools/gdb-stack-size.py` then run | ||
`stack-size` to print a backtrace with each frame size next to it.""" | ||
|
||
|
||
class StackSize(gdb.Command): | ||
def __init__(self): | ||
super(StackSize, self).__init__("stack-size", gdb.COMMAND_USER) | ||
|
||
def invoke(self, arg, from_tty): | ||
frame = gdb.newest_frame() | ||
total_size = 0 | ||
while frame: | ||
sp = frame.read_register("sp") | ||
frame_up = frame.older() | ||
if not frame_up: | ||
break | ||
f = frame.function() | ||
l = frame.level() | ||
if l < 10: | ||
l = "#" + str(l) + " " | ||
else: | ||
l = "#" + str(l) | ||
size = frame_up.read_register("sp") - sp | ||
total_size += size | ||
print(l, sp, frame.type(), f, " " * (40 - len(str(f))), size) | ||
# print(dir(f)) | ||
# Tweak this if for more detail for a specific function. | ||
if False and f.name == "mp_execute_bytecode": | ||
b = frame.block() | ||
prev_b = None | ||
while not b.is_static: | ||
print(" block", hex(b.start), hex(b.end), b.function) | ||
for sym in b: | ||
if not sym.needs_frame: | ||
continue | ||
v = sym.value(frame) | ||
print(" ", sym.addr_class, v.address, sym.type.sizeof, sym, sym.type, v) | ||
prev_b = b | ||
b = b.superblock | ||
|
||
if b.function == f: | ||
break | ||
b = prev_b | ||
print("pc scan", hex(b.start), hex(b.end)) | ||
seen = set() | ||
for pc in range(b.start, b.end, 2): | ||
b = gdb.block_for_pc(pc) | ||
r = (b.start, b.end) | ||
if r in seen: | ||
continue | ||
seen.add(r) | ||
print(" ", hex(pc), hex(b.start), hex(b.end), b.function) | ||
for sym in b: | ||
if not sym.needs_frame: | ||
continue | ||
# if sym.type.sizeof <= 4: | ||
# continue | ||
v = sym.value(frame) | ||
print(" ", sym.addr_class, v.address, sym.type.sizeof, sym, sym.type, v) | ||
frame = frame_up | ||
print("total size:", total_size) | ||
|
||
|
||
StackSize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Prints the pcs that access each stack location in a function. Useful for finding | ||
infrequently used stack space. | ||
Pipe in disassembly like so: | ||
arm-none-eabi-objdump --disassemble=mp_execute_bytecode build-metro_m0_express/firmware.elf | python ../../tools/stack-loc-to-pc.py | ||
""" | ||
|
||
import sys | ||
import re | ||
|
||
offset = re.compile(r"sp, #(\d+)") | ||
|
||
offsets = {} | ||
for line in sys.stdin: | ||
if "sp" in line: | ||
m = offset.search(line) | ||
o = int(m.groups()[0]) | ||
pc = line.split(":")[0] | ||
if o not in offsets: | ||
offsets[o] = [] | ||
offsets[o].append(pc.strip()) | ||
|
||
print("Offset", "Size", "PCs", sep="\t") | ||
last_o = 0 | ||
for o in sorted(offsets): | ||
print(o, o - last_o, offsets[o], sep="\t") | ||
last_o = o |