-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_report.py
More file actions
executable file
·218 lines (191 loc) · 6 KB
/
memory_report.py
File metadata and controls
executable file
·218 lines (191 loc) · 6 KB
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
#!/usr/bin/env python3
"""
Print a RAM/flash summary and top RAM symbols from the linker map.
Usage:
./memory_report.py
./memory_report.py --elf build/PicoUSBKeyBridge.elf
./memory_report.py --map build/PicoUSBKeyBridge.elf.map --top 15
"""
from __future__ import annotations
import argparse
import re
from pathlib import Path
import subprocess
from typing import Dict, Iterable, List, Tuple
PICO_RAM_BYTES = 520 * 1024
# Waveshare RP2350-USB-A ships with 2 MB flash.
PICO_FLASH_BYTES = 2 * 1024 * 1024
PICO_DEVICE_NAME = "RP2350 (Waveshare)"
SECTION_TOTALS = {
".boot2",
".text",
".rodata",
".binary_info",
".ram_vector_table",
".data",
".bss",
".heap",
".stack1_dummy",
".stack_dummy",
}
def format_bytes(value: int) -> str:
if value >= 1024:
return f"{value} bytes ({value / 1024:.1f} KB)"
return f"{value} bytes"
def parse_section_totals(lines: Iterable[str]) -> Dict[str, int]:
totals: Dict[str, int] = {}
for line in lines:
match = re.match(
r"^\s*(\.\S+)\s+0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\b", line
)
if not match:
continue
name = match.group(1)
if name not in SECTION_TOTALS:
continue
size = int(match.group(2), 16)
if size > totals.get(name, 0):
totals[name] = size
return totals
def get_section_totals_from_size(elf_path: Path) -> Dict[str, int]:
try:
result = subprocess.run(
["arm-none-eabi-size", "-A", str(elf_path)],
check=True,
capture_output=True,
text=True,
)
except (FileNotFoundError, subprocess.CalledProcessError):
return {}
totals: Dict[str, int] = {}
for line in result.stdout.splitlines():
match = re.match(r"^(\.\S+)\s+(\d+)\s+0x[0-9a-fA-F]+$", line.strip())
if not match:
continue
name = match.group(1)
if name not in SECTION_TOTALS:
continue
totals[name] = int(match.group(2))
return totals
def parse_bss_data_entries(lines: List[str]) -> List[Tuple[int, str, str]]:
entries: List[Tuple[int, str, str]] = []
for idx, line in enumerate(lines[:-1]):
match = re.match(r"^\s*(\.(bss|data)\.\S+)\s*$", line)
if not match:
continue
name = match.group(1)
next_line = lines[idx + 1]
match_next = re.match(
r"^\s*0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+(.*)$", next_line
)
if not match_next:
continue
size = int(match_next.group(1), 16)
obj = match_next.group(2).strip()
entries.append((size, name, obj))
return sorted(entries, reverse=True)
def print_section_summary(
totals: Dict[str, int],
ram_size: int,
flash_size: int,
device_name: str,
) -> None:
flash_used = sum(
totals.get(name, 0)
for name in (".boot2", ".text", ".rodata", ".binary_info", ".data")
)
ram_used = sum(
totals.get(name, 0)
for name in (
".ram_vector_table",
".data",
".bss",
".heap",
".stack1_dummy",
".stack_dummy",
)
)
print(f"Device: {device_name}")
print()
print("Available memory:")
print(f" {'FLASH':16} {format_bytes(flash_size)}")
print(f" {'RAM':16} {format_bytes(ram_size)}")
print()
print("Flash used:")
for name in (".boot2", ".text", ".rodata", ".binary_info", ".data"):
if name in totals:
print(f" {name:16} {format_bytes(totals[name])}")
print(f" {'TOTAL USED':16} {format_bytes(flash_used)}")
print()
remaining_flash = max(flash_size - flash_used, 0)
print("Flash remaining:")
print(f" {format_bytes(remaining_flash)}")
print()
print("RAM used:")
for name in (
".ram_vector_table",
".data",
".bss",
".heap",
".stack1_dummy",
".stack_dummy",
):
if name in totals:
print(f" {name:16} {format_bytes(totals[name])}")
print(f" {'TOTAL USED':16} {format_bytes(ram_used)}")
remaining = max(ram_size - ram_used, 0)
print()
print("RAM remaining:")
print(f" {format_bytes(remaining)}")
def print_top_entries(entries: List[Tuple[int, str, str]], top: int) -> None:
print()
print(f"Top {top} .bss/.data entries:")
for size, name, obj in entries[:top]:
print(f" {format_bytes(size):18} {name} ({obj})")
def main() -> int:
parser = argparse.ArgumentParser(description="Summarize RP2350 memory usage.")
parser.add_argument(
"--elf",
default="build/PicoUSBKeyBridge.elf",
help="Path to the ELF file.",
)
parser.add_argument(
"--map",
default=None,
help="Path to the ELF map file (defaults to --elf + .map).",
)
parser.add_argument(
"--ram-size",
type=int,
default=PICO_RAM_BYTES,
help="Total RAM size in bytes (default: 520 KB for RP2350).",
)
parser.add_argument(
"--flash-size",
type=int,
default=PICO_FLASH_BYTES,
help="Total flash size in bytes (default: 2 MB for Waveshare RP2350-USB-A).",
)
parser.add_argument(
"--device-name",
default=PICO_DEVICE_NAME,
help="Device model name for report header.",
)
parser.add_argument(
"--top", type=int, default=15, help="Number of top RAM entries to show."
)
args = parser.parse_args()
map_path = Path(args.map) if args.map else Path(args.elf).with_suffix(".elf.map")
if not map_path.exists():
print(f"Map file not found: {map_path}")
return 1
lines = map_path.read_text().splitlines()
totals = get_section_totals_from_size(Path(args.elf))
if not totals:
totals = parse_section_totals(lines)
entries = parse_bss_data_entries(lines)
print_section_summary(totals, args.ram_size, args.flash_size, args.device_name)
print_top_entries(entries, args.top)
return 0
if __name__ == "__main__":
raise SystemExit(main())