Skip to content

Commit

Permalink
Merge branch 'fix/diram_total_size' into 'master'
Browse files Browse the repository at this point in the history
fix: correctly handle DIRAM and overflows reports

Closes IDFGH-12745

See merge request espressif/esp-idf-size!45
  • Loading branch information
fhrbata committed May 22, 2024
2 parents 287e974 + 88a8b13 commit f0ace23
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 54 deletions.
45 changes: 37 additions & 8 deletions esp_idf_size/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Includes information which is not shown in "xtensa-esp32-elf-size",
# or easy to parse from "xtensa-esp32-elf-objdump" or raw map files.
#
# SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
#
import argparse
Expand Down Expand Up @@ -100,23 +100,28 @@ def __init__(self, target: str) -> None:
def _get_first_region(self, start: int, length: int) -> Tuple[Union['MemRegions.MemRegDef', None], int]:
for region in self.chip_mem_regions: # type: ignore
if region.primary_addr <= start < region.primary_addr + region.length:
return (region, length)
return (region, min(length, region.primary_addr + region.length - start))
if region.secondary_addr and region.secondary_addr <= start < region.secondary_addr + region.length:
return (region, length)
return (region, min(length, region.secondary_addr + region.length - start))
print('WARNING: Given section not found in any memory region.')
print('Check whether the LD file is compatible with the definitions in get_mem_regions in esp_idf_size')
return (None, length)

def _get_regions(self, start: int, length: int, name: Optional[str]=None) -> List:
ret = []
last_region = None
while length > 0:
(region, cur_len) = self._get_first_region(start, length)
# If region/section does not fit into any memory type, assign it
# to the last memory type so the overflow is correctly reported.
region = region or last_region
if region is None:
# skip regions that not in given section
length -= cur_len
start += cur_len
continue
ret.append(MemRegions.Region(start, cur_len, region, name))
last_region = region
length -= cur_len
start += cur_len

Expand Down Expand Up @@ -601,20 +606,44 @@ def in_dram(x: MemRegions.Region) -> bool:
def in_iram(x: MemRegions.Region) -> bool:
return x.region.type == MemRegions.IRAM_ID and x.region.secondary_addr == 0 # type: ignore

def filter_diram_aliases(regions: Iterable) -> List[MemRegions.Region]:
# To accurately calculate the available diram size, it's essential to eliminate aliases.
# Otherwise, the reported total available diram size will be doubled.
# Alias is region/segment, as defined in map file, which points to the
# same physical memory as other region/segment. Note that segments
# are split according to chip memory types in _get_regions.
regions_filtered: List[MemRegions.Region] = []

for reg in regions:
for reg_filtered in regions_filtered:
if reg.region.type is not reg_filtered.region.type:
# chip regions are different, skip
continue
if reg.len != reg_filtered.len:
# memory regions/segments have different size, skip
continue
if abs(reg.start - reg_filtered.start) != abs(reg.region.primary_addr - reg.region.secondary_addr):
# memory region/segment offset is different from chip region offset, skip
continue
# Alias found, don't add it into the final list
break

else:
# No alias found for region/segment, add it into the final list
regions_filtered.append(reg)

return regions_filtered

r = StructureForSummary()

diram_filter = filter(in_diram, segments)
r.diram_total = get_size(diram_filter)
r.diram_total = get_size(filter_diram_aliases(diram_filter))

dram_filter = filter(in_dram, segments)
r.dram_total = get_size(dram_filter)
iram_filter = filter(in_iram, segments)
r.iram_total = get_size(iram_filter)

# This fixes counting the diram twice if the cache fills the iram entirely
if r.iram_total == 0:
r.diram_total //= 2

def filter_in_section(sections: Iterable[MemRegions.Region], section_to_check: str) -> List[MemRegions.Region]:
return list(filter(lambda x: LinkingSections.in_section(x.section, section_to_check), sections)) # type: ignore

Expand Down
106 changes: 60 additions & 46 deletions test/expected_output
Original file line number Diff line number Diff line change
Expand Up @@ -4187,7 +4187,7 @@ Total image size: 134103 bytes (.bin may be padded larger)
***
Running esp_idf_size on bootloader for esp32s2...
Total sizes:
Used stat D/IRAM: 22569 bytes ( -1065 remain, 105.0% used) Overflow detected! You can run idf.py size-files for more information.
Used stat D/IRAM: 22569 bytes ( 20439 remain, 52.5% used)
.data size: 4 bytes
.bss size: 264 bytes
.text size: 16125 bytes
Expand All @@ -4197,7 +4197,7 @@ Total image size: 22305 bytes (.bin may be padded larger)
***
Running esp_idf_size on bootloader for esp32s2 (target autodetected)...
Total sizes:
Used stat D/IRAM: 22569 bytes ( -1065 remain, 105.0% used) Overflow detected! You can run idf.py size-files for more information.
Used stat D/IRAM: 22569 bytes ( 20439 remain, 52.5% used)
.data size: 4 bytes
.bss size: 264 bytes
.text size: 16125 bytes
Expand Down Expand Up @@ -4947,6 +4947,8 @@ WARNING: Given section not found in any memory region.
Check whether the LD file is compatible with the definitions in get_mem_regions in esp_idf_size
WARNING: Given section not found in any memory region.
Check whether the LD file is compatible with the definitions in get_mem_regions in esp_idf_size
WARNING: Given section not found in any memory region.
Check whether the LD file is compatible with the definitions in get_mem_regions in esp_idf_size
Total sizes:
Used stat D/IRAM: 551174 bytes (-223494 remain, 168.2% used) Overflow detected! You can run idf.py size-files for more information.
.text size: 551174 bytes
Expand Down Expand Up @@ -5308,12 +5310,13 @@ Section total: 0
***
Running esp_idf_size for esp32s3...
Total sizes:
Used static IRAM: 46786 bytes ( 239934 remain, 16.3% used)
.text size: 45759 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 11772 bytes ( 258564 remain, 4.4% used)
Used stat D/IRAM: 42175 bytes ( 228161 remain, 15.6% used)
.data size: 9252 bytes
.bss size: 2520 bytes
.text size: 30403 bytes
Used Flash size : 114851 bytes
.text: 87463 bytes
.rodata: 27132 bytes
Expand All @@ -5335,12 +5338,13 @@ Total image size: 200869 bytes (.bin may be padded larger)
***
Running esp_idf_size for esp32s3 with overflow...
Total sizes:
Used static IRAM: 337906 bytes ( -51186 remain, 117.9% used) Overflow detected! You can run idf.py size-files for more information.
.text size: 336879 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 94049 bytes ( 176287 remain, 34.8% used)
Used stat D/IRAM: 415572 bytes (-145236 remain, 153.7% used) Overflow detected! You can run idf.py size-files for more information.
.data size: 68929 bytes
.bss size: 25120 bytes
.text size: 321523 bytes
Used Flash size : 454371 bytes
.text: 366715 bytes
.rodata: 87400 bytes
Expand All @@ -5349,12 +5353,13 @@ Total image size: 861206 bytes (.bin may be padded larger)
***
Running esp_idf_size for esp32s3 (target autodetected)...
Total sizes:
Used static IRAM: 46786 bytes ( 239934 remain, 16.3% used)
.text size: 45759 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 11772 bytes ( 258564 remain, 4.4% used)
Used stat D/IRAM: 42175 bytes ( 228161 remain, 15.6% used)
.data size: 9252 bytes
.bss size: 2520 bytes
.text size: 30403 bytes
Used Flash size : 114851 bytes
.text: 87463 bytes
.rodata: 27132 bytes
Expand All @@ -5363,12 +5368,13 @@ Total image size: 170889 bytes (.bin may be padded larger)
***
Running esp_idf_size --archives for esp32s3...
Total sizes:
Used static IRAM: 46786 bytes ( 239934 remain, 16.3% used)
.text size: 45759 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 11772 bytes ( 258564 remain, 4.4% used)
Used stat D/IRAM: 42175 bytes ( 228161 remain, 15.6% used)
.data size: 9252 bytes
.bss size: 2520 bytes
.text size: 30403 bytes
Used Flash size : 114851 bytes
.text: 87463 bytes
.rodata: 27132 bytes
Expand Down Expand Up @@ -5407,12 +5413,13 @@ Per-archive contributions to ELF file:
***
Running esp_idf_size --files for esp32s3...
Total sizes:
Used static IRAM: 46786 bytes ( 239934 remain, 16.3% used)
.text size: 45759 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 11772 bytes ( 258564 remain, 4.4% used)
Used stat D/IRAM: 42175 bytes ( 228161 remain, 15.6% used)
.data size: 9252 bytes
.bss size: 2520 bytes
.text size: 30403 bytes
Used Flash size : 114851 bytes
.text: 87463 bytes
.rodata: 27132 bytes
Expand Down Expand Up @@ -5673,12 +5680,13 @@ esp_crypto_shared_gdma.c 0 0 0 0 0
***
Running esp_idf_size --archive_details for esp32s3...
Total sizes:
Used static IRAM: 46786 bytes ( 239934 remain, 16.3% used)
.text size: 45759 bytes
Used static IRAM: 16383 bytes ( 1 remain, 100.0% used)
.text size: 15356 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 11772 bytes ( 258564 remain, 4.4% used)
Used stat D/IRAM: 42175 bytes ( 228161 remain, 15.6% used)
.data size: 9252 bytes
.bss size: 2520 bytes
.text size: 30403 bytes
Used Flash size : 114851 bytes
.text: 87463 bytes
.rodata: 27132 bytes
Expand Down Expand Up @@ -5791,6 +5799,8 @@ Total image size: 102490 bytes (.bin may be padded larger)

***
Running esp_idf_size for esp32c6 with overflow...
WARNING: Given section not found in any memory region.
Check whether the LD file is compatible with the definitions in get_mem_regions in esp_idf_size
Total sizes:
Used stat D/IRAM: 682896 bytes (-158608 remain, 130.3% used) Overflow detected! You can run idf.py size-files for more information.
.data size: 3402 bytes
Expand Down Expand Up @@ -7088,7 +7098,7 @@ Section total: 0
***
Running esp_idf_size for esp32c61...
Total sizes:
Used stat D/IRAM: 34584 bytes ( 125920 remain, 21.5% used)
Used stat D/IRAM: 34584 bytes ( 286424 remain, 10.8% used)
.data size: 2929 bytes
.bss size: 3696 bytes
.text size: 27956 bytes
Expand All @@ -7101,7 +7111,7 @@ Total image size: 117813 bytes (.bin may be padded larger)
***
Running esp_idf_size for esp32c61 (target autodetected)...
Total sizes:
Used stat D/IRAM: 34584 bytes ( 125920 remain, 21.5% used)
Used stat D/IRAM: 34584 bytes ( 286424 remain, 10.8% used)
.data size: 2929 bytes
.bss size: 3696 bytes
.text size: 27956 bytes
Expand All @@ -7114,7 +7124,7 @@ Total image size: 117813 bytes (.bin may be padded larger)
***
Running esp_idf_size --archives for esp32c61...
Total sizes:
Used stat D/IRAM: 34584 bytes ( 125920 remain, 21.5% used)
Used stat D/IRAM: 34584 bytes ( 286424 remain, 10.8% used)
.data size: 2929 bytes
.bss size: 3696 bytes
.text size: 27956 bytes
Expand Down Expand Up @@ -7153,7 +7163,7 @@ Per-archive contributions to ELF file:
***
Running esp_idf_size --files for esp32c61...
Total sizes:
Used stat D/IRAM: 34584 bytes ( 125920 remain, 21.5% used)
Used stat D/IRAM: 34584 bytes ( 286424 remain, 10.8% used)
.data size: 2929 bytes
.bss size: 3696 bytes
.text size: 27956 bytes
Expand Down Expand Up @@ -7329,7 +7339,7 @@ fpga_overrides_rng.c.obj 0 0 0 0 0
***
Running esp_idf_size --archive_details for esp32c61...
Total sizes:
Used stat D/IRAM: 34584 bytes ( 125920 remain, 21.5% used)
Used stat D/IRAM: 34584 bytes ( 286424 remain, 10.8% used)
.data size: 2929 bytes
.bss size: 3696 bytes
.text size: 27956 bytes
Expand Down Expand Up @@ -19083,22 +19093,22 @@ Producing JSON output for esp32s3...
"used_dram_ratio": 0,
"dram_remain": 0,
"iram_vectors": 1027,
"iram_text": 45759,
"iram_text": 15356,
"iram_other": 0,
"used_iram": 46786,
"iram_total": 286720,
"used_iram_ratio": 0.16317661830357144,
"iram_remain": 239934,
"used_iram": 16383,
"iram_total": 16384,
"used_iram_ratio": 0.99993896484375,
"iram_remain": 1,
"diram_data": 9252,
"diram_bss": 2520,
"diram_text": 0,
"diram_text": 30403,
"diram_vectors": 0,
"diram_rodata": 0,
"diram_other": 0,
"diram_total": 270336,
"used_diram": 11772,
"used_diram_ratio": 0.04354580965909091,
"diram_remain": 258564,
"used_diram": 42175,
"used_diram_ratio": 0.15600955847537878,
"diram_remain": 228161,
"flash_code": 87463,
"flash_rodata": 27132,
"flash_other": 256,
Expand Down Expand Up @@ -25185,23 +25195,25 @@ Section total: 0
***
Producing CSV output for esp32s3...
Total sizes:,,,
Used static IRAM,46786 bytes (239934 remain 16.3% used),,,
.text size,45759 bytes,,,
Used static IRAM,16383 bytes (1 remain 100.0% used),,,
.text size,15356 bytes,,,
.vectors size,1027 bytes,,,
Used stat D/IRAM,11772 bytes (258564 remain 4.4% used),,,
Used stat D/IRAM,42175 bytes (228161 remain 15.6% used),,,
.data size,9252 bytes,,,
.bss size,2520 bytes,,,
.text size,30403 bytes,,,
Used Flash size ,114851 bytes,,,
.text,87463 bytes,,,
.rodata,27132 bytes,,,
Total image size,170889 bytes (.bin may be padded larger),,,
Total sizes:,,,
Used static IRAM,46786 bytes (239934 remain 16.3% used),,,
.text size,45759 bytes,,,
Used static IRAM,16383 bytes (1 remain 100.0% used),,,
.text size,15356 bytes,,,
.vectors size,1027 bytes,,,
Used stat D/IRAM,11772 bytes (258564 remain 4.4% used),,,
Used stat D/IRAM,42175 bytes (228161 remain 15.6% used),,,
.data size,9252 bytes,,,
.bss size,2520 bytes,,,
.text size,30403 bytes,,,
Used Flash size ,114851 bytes,,,
.text,87463 bytes,,,
.rodata,27132 bytes,,,
Expand Down Expand Up @@ -25237,12 +25249,13 @@ libcxx.a,0,0,0,0,0,0,5,0,0
libmbedcrypto.a,0,0,0,0,0,0,0,0,0
libsoc.a,0,0,0,0,0,0,0,0,0
Total sizes:,,,
Used static IRAM,46786 bytes (239934 remain 16.3% used),,,
.text size,45759 bytes,,,
Used static IRAM,16383 bytes (1 remain 100.0% used),,,
.text size,15356 bytes,,,
.vectors size,1027 bytes,,,
Used stat D/IRAM,11772 bytes (258564 remain 4.4% used),,,
Used stat D/IRAM,42175 bytes (228161 remain 15.6% used),,,
.data size,9252 bytes,,,
.bss size,2520 bytes,,,
.text size,30403 bytes,,,
Used Flash size ,114851 bytes,,,
.text,87463 bytes,,,
.rodata,27132 bytes,,,
Expand Down Expand Up @@ -25500,12 +25513,13 @@ _fixdfsi.o,0,0,0,0,0,0,0,0,0
_floatsidf.o,0,0,0,0,0,0,0,0,0
_muldf3.o,0,0,0,0,0,0,0,0,0
Total sizes:,,,
Used static IRAM,46786 bytes (239934 remain 16.3% used),,,
.text size,45759 bytes,,,
Used static IRAM,16383 bytes (1 remain 100.0% used),,,
.text size,15356 bytes,,,
.vectors size,1027 bytes,,,
Used stat D/IRAM,11772 bytes (258564 remain 4.4% used),,,
Used stat D/IRAM,42175 bytes (228161 remain 15.6% used),,,
.data size,9252 bytes,,,
.bss size,2520 bytes,,,
.text size,30403 bytes,,,
Used Flash size ,114851 bytes,,,
.text,87463 bytes,,,
.rodata,27132 bytes,,,
Expand Down

0 comments on commit f0ace23

Please sign in to comment.