idf_size.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/usr/bin/env python
  2. #
  3. # esp-idf alternative to "size" to print ELF file sizes, also analyzes
  4. # the linker map file to dump higher resolution details.
  5. #
  6. # Includes information which is not shown in "xtensa-esp32-elf-size",
  7. # or easy to parse from "xtensa-esp32-elf-objdump" or raw map files.
  8. #
  9. # Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. #
  23. from __future__ import print_function
  24. from __future__ import unicode_literals
  25. from __future__ import division
  26. import argparse
  27. import collections
  28. import json
  29. import os.path
  30. import re
  31. import sys
  32. DEFAULT_TOOLCHAIN_PREFIX = "xtensa-esp32-elf-"
  33. GLOBAL_JSON_INDENT = 4
  34. GLOBAL_JSON_SEPARATORS = (',', ': ')
  35. def scan_to_header(f, header_line):
  36. """ Scan forward in a file until you reach 'header_line', then return """
  37. for line in f:
  38. if line.strip() == header_line:
  39. return
  40. raise RuntimeError("Didn't find line '%s' in file" % header_line)
  41. def format_json(json_object):
  42. return json.dumps(json_object, indent=GLOBAL_JSON_INDENT, separators=GLOBAL_JSON_SEPARATORS) + "\n"
  43. def load_map_data(map_file):
  44. memory_config = load_memory_config(map_file)
  45. sections = load_sections(map_file)
  46. return memory_config, sections
  47. def load_memory_config(map_file):
  48. """ Memory Configuration section is the total size of each output section """
  49. result = {}
  50. scan_to_header(map_file, "Memory Configuration")
  51. RE_MEMORY_SECTION = re.compile(r"(?P<name>[^ ]+) +0x(?P<origin>[\da-f]+) +0x(?P<length>[\da-f]+)")
  52. for line in map_file:
  53. m = RE_MEMORY_SECTION.match(line)
  54. if m is None:
  55. if len(result) == 0:
  56. continue # whitespace or a header, before the content we want
  57. else:
  58. return result # we're at the end of the Memory Configuration
  59. section = {
  60. "name": m.group("name"),
  61. "origin": int(m.group("origin"), 16),
  62. "length": int(m.group("length"), 16),
  63. }
  64. if section["name"] != "*default*":
  65. result[section["name"]] = section
  66. raise RuntimeError("End of file while scanning memory configuration?")
  67. def load_sections(map_file):
  68. """ Load section size information from the MAP file.
  69. Returns a dict of 'sections', where each key is a section name and the value
  70. is a dict with details about this section, including a "sources" key which holds a list of source file line
  71. information for each symbol linked into the section.
  72. """
  73. scan_to_header(map_file, "Linker script and memory map")
  74. # output section header, ie '.iram0.text 0x0000000040080400 0x129a5'
  75. RE_SECTION_HEADER = re.compile(r"(?P<name>[^ ]+) +0x(?P<address>[\da-f]+) +0x(?P<size>[\da-f]+)$")
  76. # source file line, ie
  77. # 0x0000000040080400 0xa4 /home/gus/esp/32/idf/examples/get-started/hello_world/build/esp32/libesp32.a(cpu_start.o)
  78. # cmake build system links some object files directly, not part of any archive, so make that part optional
  79. # .xtensa.info 0x0000000000000000 0x38 CMakeFiles/hello-world.elf.dir/project_elf_src.c.obj
  80. RE_SOURCE_LINE = re.compile(r"\s*(?P<sym_name>\S*) +0x(?P<address>[\da-f]+) +0x(?P<size>[\da-f]+) (?P<archive>.+\.a)?\(?(?P<object_file>.+\.(o|obj))\)?")
  81. # Fast check to see if line is a potential source line before running the slower full regex against it
  82. RE_PRE_FILTER = re.compile(r".*\.(o|obj)\)?")
  83. # Check for lines which only contain the sym name (and rest is on following lines)
  84. RE_SYMBOL_ONLY_LINE = re.compile(r"^ (?P<sym_name>\S*)$")
  85. sections = {}
  86. section = None
  87. sym_backup = None
  88. for line in map_file:
  89. if line.strip() == "Cross Reference Table":
  90. # stop processing lines because we are at the next section in the map file
  91. break
  92. m = RE_SECTION_HEADER.match(line)
  93. if m is not None: # start of a new section
  94. section = {
  95. "name": m.group("name"),
  96. "address": int(m.group("address"), 16),
  97. "size": int(m.group("size"), 16),
  98. "sources": [],
  99. }
  100. sections[section["name"]] = section
  101. continue
  102. if section is not None:
  103. m = RE_SYMBOL_ONLY_LINE.match(line)
  104. if m is not None:
  105. # In some cases the section name appears on the previous line, back it up in here
  106. sym_backup = m.group("sym_name")
  107. continue
  108. if not RE_PRE_FILTER.match(line):
  109. # line does not match our quick check, so skip to next line
  110. continue
  111. m = RE_SOURCE_LINE.match(line)
  112. if m is not None: # input source file details=ma,e
  113. sym_name = m.group("sym_name") if len(m.group("sym_name")) > 0 else sym_backup
  114. archive = m.group("archive")
  115. if archive is None:
  116. # optional named group "archive" was not matched, so assign a value to it
  117. archive = "(exe)"
  118. source = {
  119. "size": int(m.group("size"), 16),
  120. "address": int(m.group("address"), 16),
  121. "archive": os.path.basename(archive),
  122. "object_file": os.path.basename(m.group("object_file")),
  123. "sym_name": sym_name,
  124. }
  125. source["file"] = "%s:%s" % (source["archive"], source["object_file"])
  126. section["sources"] += [source]
  127. return sections
  128. def sizes_by_key(sections, key):
  129. """ Takes a dict of sections (from load_sections) and returns
  130. a dict keyed by 'key' with aggregate output size information.
  131. Key can be either "archive" (for per-archive data) or "file" (for per-file data) in the result.
  132. """
  133. result = {}
  134. for section in sections.values():
  135. for s in section["sources"]:
  136. if not s[key] in result:
  137. result[s[key]] = {}
  138. archive = result[s[key]]
  139. if not section["name"] in archive:
  140. archive[section["name"]] = 0
  141. archive[section["name"]] += s["size"]
  142. return result
  143. def main():
  144. parser = argparse.ArgumentParser("idf_size - a tool to print IDF elf file sizes")
  145. parser.add_argument(
  146. '--toolchain-prefix',
  147. help="Triplet prefix to add before objdump executable",
  148. default=DEFAULT_TOOLCHAIN_PREFIX)
  149. parser.add_argument(
  150. '--json',
  151. help="Output results as JSON",
  152. action="store_true")
  153. parser.add_argument(
  154. 'map_file', help='MAP file produced by linker',
  155. type=argparse.FileType('r'))
  156. parser.add_argument(
  157. '--archives', help='Print per-archive sizes', action='store_true')
  158. parser.add_argument(
  159. '--archive_details', help='Print detailed symbols per archive')
  160. parser.add_argument(
  161. '--files', help='Print per-file sizes', action='store_true')
  162. parser.add_argument(
  163. '-o',
  164. '--output-file',
  165. type=argparse.FileType('w'),
  166. default=sys.stdout,
  167. help="Print output to the specified file instead of stdout")
  168. args = parser.parse_args()
  169. output = ""
  170. memory_config, sections = load_map_data(args.map_file)
  171. if not args.json or not (args.archives or args.files or args.archive_details):
  172. output += get_summary(memory_config, sections, args.json)
  173. if args.archives:
  174. output += get_detailed_sizes(sections, "archive", "Archive File", args.json)
  175. if args.files:
  176. output += get_detailed_sizes(sections, "file", "Object File", args.json)
  177. if args.archive_details:
  178. output += get_archive_symbols(sections, args.archive_details, args.json)
  179. args.output_file.write(output)
  180. def get_summary(memory_config, sections, as_json=False):
  181. def get_size(section):
  182. try:
  183. return sections[section]["size"]
  184. except KeyError:
  185. return 0
  186. # if linker script changes, these need to change
  187. total_iram = memory_config["iram0_0_seg"]["length"]
  188. total_dram = memory_config["dram0_0_seg"]["length"]
  189. used_data = get_size(".dram0.data")
  190. used_bss = get_size(".dram0.bss")
  191. used_dram = used_data + used_bss
  192. try:
  193. used_dram_ratio = used_dram / total_dram
  194. except ZeroDivisionError:
  195. used_dram_ratio = float('nan')
  196. used_iram = sum(get_size(s) for s in sections if s.startswith(".iram0"))
  197. try:
  198. used_iram_ratio = used_iram / total_iram
  199. except ZeroDivisionError:
  200. used_iram_ratio = float('nan')
  201. flash_code = get_size(".flash.text")
  202. flash_rodata = get_size(".flash.rodata")
  203. total_size = used_data + used_iram + flash_code + flash_rodata
  204. output = ""
  205. if as_json:
  206. output = format_json(collections.OrderedDict([
  207. ("dram_data", used_data),
  208. ("dram_bss", used_bss),
  209. ("used_dram", used_dram),
  210. ("available_dram", total_dram - used_dram),
  211. ("used_dram_ratio", used_dram_ratio),
  212. ("used_iram", used_iram),
  213. ("available_iram", total_iram - used_iram),
  214. ("used_iram_ratio", used_iram_ratio),
  215. ("flash_code", flash_code),
  216. ("flash_rodata", flash_rodata),
  217. ("total_size", total_size)
  218. ]))
  219. else:
  220. output += "Total sizes:\n"
  221. output += " DRAM .data size: {:>7} bytes\n".format(used_data)
  222. output += " DRAM .bss size: {:>7} bytes\n".format(used_bss)
  223. output += "Used static DRAM: {:>7} bytes ({:>7} available, {:.1%} used)\n".format(
  224. used_dram, total_dram - used_dram, used_dram_ratio)
  225. output += "Used static IRAM: {:>7} bytes ({:>7} available, {:.1%} used)\n".format(
  226. used_iram, total_iram - used_iram, used_iram_ratio)
  227. output += " Flash code: {:>7} bytes\n".format(flash_code)
  228. output += " Flash rodata: {:>7} bytes\n".format(flash_rodata)
  229. output += "Total image size:~{:>7} bytes (.bin may be padded larger)\n".format(total_size)
  230. return output
  231. def get_detailed_sizes(sections, key, header, as_json=False):
  232. sizes = sizes_by_key(sections, key)
  233. result = {}
  234. for k in sizes:
  235. v = sizes[k]
  236. result[k] = collections.OrderedDict()
  237. result[k]["data"] = v.get(".dram0.data", 0)
  238. result[k]["bss"] = v.get(".dram0.bss", 0)
  239. result[k]["iram"] = sum(t for (s,t) in v.items() if s.startswith(".iram0"))
  240. result[k]["flash_text"] = v.get(".flash.text", 0)
  241. result[k]["flash_rodata"] = v.get(".flash.rodata", 0)
  242. result[k]["total"] = sum(result[k].values())
  243. def return_total_size(elem):
  244. val = elem[1]
  245. return val["total"]
  246. def return_header(elem):
  247. return elem[0]
  248. s = sorted(list(result.items()), key=return_header)
  249. # do a secondary sort in order to have consistent order (for diff-ing the output)
  250. s = sorted(s, key=return_total_size, reverse=True)
  251. output = ""
  252. if as_json:
  253. output = format_json(collections.OrderedDict(s))
  254. else:
  255. header_format = "{:>24} {:>10} {:>6} {:>6} {:>10} {:>8} {:>7}\n"
  256. output += "Per-{} contributions to ELF file:\n".format(key)
  257. output += header_format.format(header,
  258. "DRAM .data",
  259. "& .bss",
  260. "IRAM",
  261. "Flash code",
  262. "& rodata",
  263. "Total")
  264. for k,v in s:
  265. if ":" in k: # print subheadings for key of format archive:file
  266. sh,k = k.split(":")
  267. output += header_format.format(k[:24],
  268. v["data"],
  269. v["bss"],
  270. v["iram"],
  271. v["flash_text"],
  272. v["flash_rodata"],
  273. v["total"])
  274. return output
  275. def get_archive_symbols(sections, archive, as_json=False):
  276. interested_sections = [".dram0.data", ".dram0.bss", ".iram0.text", ".iram0.vectors", ".flash.text", ".flash.rodata"]
  277. result = {}
  278. for t in interested_sections:
  279. result[t] = {}
  280. for section in sections.values():
  281. section_name = section["name"]
  282. if section_name not in interested_sections:
  283. continue
  284. for s in section["sources"]:
  285. if archive != s["archive"]:
  286. continue
  287. s["sym_name"] = re.sub("(.text.|.literal.|.data.|.bss.|.rodata.)", "", s["sym_name"])
  288. result[section_name][s["sym_name"]] = result[section_name].get(s["sym_name"], 0) + s["size"]
  289. # build a new ordered dict of each section, where each entry is an ordereddict of symbols to sizes
  290. section_symbols = collections.OrderedDict()
  291. for t in interested_sections:
  292. s = sorted(list(result[t].items()), key=lambda k_v: k_v[0])
  293. # do a secondary sort in order to have consistent order (for diff-ing the output)
  294. s = sorted(s, key=lambda k_v: k_v[1], reverse=True)
  295. section_symbols[t] = collections.OrderedDict(s)
  296. output = ""
  297. if as_json:
  298. output = format_json(section_symbols)
  299. else:
  300. output += "Symbols within the archive: {} (Not all symbols may be reported)\n".format(archive)
  301. for t,s in section_symbols.items():
  302. section_total = 0
  303. output += "\nSymbols from section: {}\n".format(t)
  304. for key, val in s.items():
  305. output += "{}({}) ".format(key.replace(t + ".", ""), val)
  306. section_total += val
  307. output += "\nSection total: {}\n".format(section_total)
  308. return output
  309. if __name__ == "__main__":
  310. main()