mirror of
https://github.com/esphome/esphome.git
synced 2026-09-07 05:26:01 +00:00
Compare commits
16
Commits
dev
...
espidf-size-json2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5632aa20e | ||
|
|
c75e3898e4 | ||
|
|
b66990fbff | ||
|
|
05ec260ff3 | ||
|
|
ad40853283 | ||
|
|
52ce6668d6 | ||
|
|
2808837743 | ||
|
|
4caf7bafb8 | ||
|
|
22e29df396 | ||
|
|
cafc09bdca | ||
|
|
e632661adf | ||
|
|
8d0f3ea2bb | ||
|
|
8ee2f4247e | ||
|
|
edf2f7c62a | ||
|
|
1fc7a0798d | ||
|
|
39092a791a |
@@ -90,9 +90,10 @@ def get_project_cmakelists(
|
|||||||
"""
|
"""
|
||||||
idf_target = variant_to_idf_target(get_esp32_variant())
|
idf_target = variant_to_idf_target(get_esp32_variant())
|
||||||
|
|
||||||
# esp_idf_size 2.x (bundled with IDF >=6.0) made NG the default and
|
# esp_idf_size 2.x (IDF >=6.0) made NG the default and removed --ng;
|
||||||
# removed the --ng flag; on 1.x (IDF 5.5) --ng is required to get
|
# 1.x (IDF 5.5) needs --ng for --format=json2. 1.x json2 also lacks
|
||||||
# --format=raw because the legacy mode doesn't support it.
|
# total_size, hence the ELF fallback in espidf/size_summary.py; both
|
||||||
|
# go away together when 1.x support is dropped.
|
||||||
size_ng_flag = "--ng" if idf_version() < cv.Version(6, 0, 0) else ""
|
size_ng_flag = "--ng" if idf_version() < cv.Version(6, 0, 0) else ""
|
||||||
|
|
||||||
# Project-wide compile options: -D defines and -W warning flags (skip
|
# Project-wide compile options: -D defines and -W warning flags (skip
|
||||||
@@ -211,10 +212,12 @@ include($ENV{{IDF_PATH}}/tools/cmake/project.cmake)
|
|||||||
|
|
||||||
project({CORE.name})
|
project({CORE.name})
|
||||||
|
|
||||||
# Emit raw JSON size data for ESPHome to read post-build.
|
# Emit per-memory-type JSON size data for ESPHome to read post-build.
|
||||||
|
# json2 stays small; raw dumps every symbol (~2s on a large map) and
|
||||||
|
# this command runs inside the link edge, blocking everything downstream.
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET ${{CMAKE_PROJECT_NAME}}.elf POST_BUILD
|
TARGET ${{CMAKE_PROJECT_NAME}}.elf POST_BUILD
|
||||||
COMMAND ${{PYTHON}} -m esp_idf_size {size_ng_flag} --format=raw
|
COMMAND ${{PYTHON}} -m esp_idf_size {size_ng_flag} --format=json2
|
||||||
-o ${{CMAKE_BINARY_DIR}}/esp_idf_size.json
|
-o ${{CMAKE_BINARY_DIR}}/esp_idf_size.json
|
||||||
${{CMAKE_PROJECT_NAME}}.map
|
${{CMAKE_PROJECT_NAME}}.map
|
||||||
WORKING_DIRECTORY ${{CMAKE_BINARY_DIR}}
|
WORKING_DIRECTORY ${{CMAKE_BINARY_DIR}}
|
||||||
|
|||||||
@@ -9,16 +9,19 @@ byte-identical to PlatformIO's output:
|
|||||||
Flash: [=== ] 48.4% (used 888511 bytes from 1835008 bytes)
|
Flash: [=== ] 48.4% (used 888511 bytes from 1835008 bytes)
|
||||||
|
|
||||||
The format matches ``script/ci_memory_impact_extract.py`` so CI memory
|
The format matches ``script/ci_memory_impact_extract.py`` so CI memory
|
||||||
analysis works unchanged on native ESP-IDF builds. RAM total is the
|
analysis works unchanged on native ESP-IDF builds. RAM usage comes from
|
||||||
DRAM region size from the linker map; Flash total is taken from
|
the DRAM (or unified DIRAM) region of the linker map. Flash used is the
|
||||||
|
exact image size matching the ``Total image size`` line: json2
|
||||||
|
``total_size`` when present, otherwise derived from the ELF (see
|
||||||
|
``_image_size_from_elf``). Flash total is taken from
|
||||||
``partitions.csv`` using PlatformIO's rule (first app partition whose
|
``partitions.csv`` using PlatformIO's rule (first app partition whose
|
||||||
subtype is ``factory`` or ``ota_0``; see
|
subtype is ``factory`` or ``ota_0``; see
|
||||||
``platform-espressif32/builder/main.py::_update_max_upload_size``).
|
``platform-espressif32/builder/main.py::_update_max_upload_size``).
|
||||||
|
|
||||||
Structured size data is produced at link time by a CMake POST_BUILD
|
Structured size data is produced at link time by a CMake POST_BUILD
|
||||||
custom command (see ``build_gen/espidf.py``) which writes
|
custom command (see ``build_gen/espidf.py``) which writes
|
||||||
``esp_idf_size.json`` next to the ELF. We read that file here rather
|
``esp_idf_size.json`` (``--format=json2``, a per-memory-type summary)
|
||||||
than re-running ``esp_idf_size`` from Python.
|
next to the ELF; we read that rather than re-running ``esp_idf_size``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -27,6 +30,7 @@ import csv
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import struct
|
||||||
|
|
||||||
from esphome.build_helpers.size_summary import print_size_line
|
from esphome.build_helpers.size_summary import print_size_line
|
||||||
|
|
||||||
@@ -69,11 +73,43 @@ def _find_app_partition_size(partitions_csv: Path) -> int:
|
|||||||
raise ValueError(f"No app+factory or app+ota_0 partition in {partitions_csv}")
|
raise ValueError(f"No app+factory or app+ota_0 partition in {partitions_csv}")
|
||||||
|
|
||||||
|
|
||||||
def print_summary(size_json: Path, partitions_csv: Path | None) -> None:
|
def _image_size_from_elf(elf: Path) -> int:
|
||||||
|
"""Sum the allocated PROGBITS section sizes from an ELF32 file.
|
||||||
|
|
||||||
|
Matches ``esp_idf_size.ng.memorymap._get_image_size`` byte for byte;
|
||||||
|
esptool's ``ELFFile`` filters sections differently and would not.
|
||||||
|
Raises ``ValueError`` for anything but a well-formed ELF32 LE file.
|
||||||
|
"""
|
||||||
|
with elf.open("rb") as f:
|
||||||
|
header = f.read(52) # ELF32 header
|
||||||
|
if len(header) < 52 or header[:6] != b"\x7fELF\x01\x01":
|
||||||
|
raise ValueError(f"{elf} is not a 32-bit little-endian ELF")
|
||||||
|
(e_shoff,) = struct.unpack_from("<I", header, 0x20) # e_shoff
|
||||||
|
e_shentsize, e_shnum = struct.unpack_from("<HH", header, 0x2E)
|
||||||
|
if e_shentsize < 40: # sizeof(Elf32_Shdr)
|
||||||
|
raise ValueError(f"{elf} has an invalid section header size")
|
||||||
|
f.seek(e_shoff)
|
||||||
|
table = f.read(e_shnum * e_shentsize)
|
||||||
|
if len(table) < e_shnum * e_shentsize:
|
||||||
|
raise ValueError(f"{elf} has a truncated section header table")
|
||||||
|
total = 0
|
||||||
|
for off in range(0, e_shnum * e_shentsize, e_shentsize):
|
||||||
|
sh_type, sh_flags = struct.unpack_from("<II", table, off + 4)
|
||||||
|
(sh_size,) = struct.unpack_from("<I", table, off + 20)
|
||||||
|
if sh_type == 1 and sh_flags & 0x2: # SHT_PROGBITS with SHF_ALLOC
|
||||||
|
total += sh_size
|
||||||
|
if total == 0:
|
||||||
|
# A used-0-bytes Flash line would read as a real measurement
|
||||||
|
raise ValueError(f"{elf} has no allocated PROGBITS sections")
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
def print_summary(size_json: Path, partitions_csv: Path, firmware_elf: Path) -> None:
|
||||||
"""Print PlatformIO-shaped RAM and Flash one-liners.
|
"""Print PlatformIO-shaped RAM and Flash one-liners.
|
||||||
|
|
||||||
Failures are non-fatal: the build has already succeeded, we just couldn't
|
Failures are non-fatal: the build has already succeeded, we just couldn't
|
||||||
summarize. Logs the cause at debug level.
|
summarize. Anomalies (missing region, unreadable ELF) warn; expected
|
||||||
|
optional inputs (no size json, no partitions.csv) log at debug.
|
||||||
"""
|
"""
|
||||||
if not size_json.is_file():
|
if not size_json.is_file():
|
||||||
_LOGGER.debug("Skipping size summary: %s not found", size_json)
|
_LOGGER.debug("Skipping size summary: %s not found", size_json)
|
||||||
@@ -83,20 +119,49 @@ def print_summary(size_json: Path, partitions_csv: Path | None) -> None:
|
|||||||
except (OSError, json.JSONDecodeError) as e:
|
except (OSError, json.JSONDecodeError) as e:
|
||||||
_LOGGER.debug("Skipping size summary: %s", e)
|
_LOGGER.debug("Skipping size summary: %s", e)
|
||||||
return
|
return
|
||||||
|
if not isinstance(data, dict):
|
||||||
memory_types = data.get("memory_types", {})
|
_LOGGER.warning("Skipping size summary: unexpected json shape in %s", size_json)
|
||||||
ram_region = memory_types.get("DRAM") or memory_types.get("DIRAM") or {}
|
|
||||||
ram_used = ram_region.get("used")
|
|
||||||
ram_total = ram_region.get("size")
|
|
||||||
if ram_total and ram_used is not None:
|
|
||||||
print_size_line("RAM", ram_used, ram_total)
|
|
||||||
|
|
||||||
image_size = data.get("image_size")
|
|
||||||
if image_size is None or partitions_csv is None:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
layout = data.get("layout")
|
||||||
|
regions = {
|
||||||
|
entry.get("name"): entry
|
||||||
|
for entry in (layout if isinstance(layout, list) else [])
|
||||||
|
if isinstance(entry, dict)
|
||||||
|
}
|
||||||
|
# Every chip has a DRAM or DIRAM region, so a warning here usually
|
||||||
|
# means the esp_idf_size json schema changed
|
||||||
|
ram_region = regions.get("DRAM") or regions.get("DIRAM")
|
||||||
|
if ram_region is None:
|
||||||
|
_LOGGER.warning("Skipping RAM summary: no DRAM/DIRAM region in %s", size_json)
|
||||||
|
elif (
|
||||||
|
isinstance(ram_total := ram_region.get("total"), int)
|
||||||
|
and ram_total > 0
|
||||||
|
and isinstance(ram_used := ram_region.get("used"), int)
|
||||||
|
):
|
||||||
|
print_size_line("RAM", ram_used, ram_total)
|
||||||
|
else:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Skipping RAM summary: unusable region %s in %s", ram_region, size_json
|
||||||
|
)
|
||||||
|
|
||||||
|
# esp-idf-size >= 2.1 (IDF >= 6.0) reports the exact image size in
|
||||||
|
# json2; older 1.x omits it, so derive the same figure from the ELF.
|
||||||
|
flash_used = data.get("total_size")
|
||||||
|
if not (isinstance(flash_used, int) and flash_used > 0):
|
||||||
|
_LOGGER.debug("No total_size in %s, deriving from %s", size_json, firmware_elf)
|
||||||
|
try:
|
||||||
|
flash_used = _image_size_from_elf(firmware_elf)
|
||||||
|
except (OSError, ValueError) as e:
|
||||||
|
# The ELF must be present and well formed after a successful build
|
||||||
|
_LOGGER.warning("Skipping Flash summary: %s", e)
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
app_size = _find_app_partition_size(partitions_csv)
|
app_size = _find_app_partition_size(partitions_csv)
|
||||||
except ValueError as e:
|
except (OSError, ValueError) as e:
|
||||||
_LOGGER.debug("Skipping Flash summary: %s", e)
|
_LOGGER.debug("Skipping Flash summary: %s", e)
|
||||||
return
|
return
|
||||||
print_size_line("Flash", image_size, app_size)
|
if app_size <= 0:
|
||||||
|
_LOGGER.debug("Skipping Flash summary: app partition size is 0")
|
||||||
|
return
|
||||||
|
print_size_line("Flash", flash_used, app_size)
|
||||||
|
|||||||
@@ -542,7 +542,7 @@ def run_compile(config, verbose: bool) -> int:
|
|||||||
if rc == 0:
|
if rc == 0:
|
||||||
size_json = CORE.relative_build_path("build", "esp_idf_size.json")
|
size_json = CORE.relative_build_path("build", "esp_idf_size.json")
|
||||||
partitions = CORE.relative_build_path("partitions.csv")
|
partitions = CORE.relative_build_path("partitions.csv")
|
||||||
print_summary(size_json, partitions if partitions.is_file() else None)
|
print_summary(size_json, partitions, get_built_elf_path())
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
|
||||||
@@ -579,6 +579,16 @@ def get_ota_firmware_path() -> Path:
|
|||||||
return build_dir / "firmware.ota.bin"
|
return build_dir / "firmware.ota.bin"
|
||||||
|
|
||||||
|
|
||||||
|
def get_built_elf_path() -> Path:
|
||||||
|
"""Path to the ELF idf.py writes directly, ``<build>/<name>.elf``.
|
||||||
|
|
||||||
|
Exists as soon as the build finishes, unlike the ``firmware.elf``
|
||||||
|
copy that ``create_elf_copy`` makes later.
|
||||||
|
"""
|
||||||
|
build_dir = CORE.relative_build_path("build")
|
||||||
|
return build_dir / f"{CORE.name}.elf"
|
||||||
|
|
||||||
|
|
||||||
def get_elf_path() -> Path:
|
def get_elf_path() -> Path:
|
||||||
"""Get the path to the firmware ELF file.
|
"""Get the path to the firmware ELF file.
|
||||||
|
|
||||||
@@ -706,8 +716,7 @@ def create_elf_copy() -> bool:
|
|||||||
"download ELF" link requests the literal filename ``firmware.elf``
|
"download ELF" link requests the literal filename ``firmware.elf``
|
||||||
(PlatformIO convention), so copy it to that name.
|
(PlatformIO convention), so copy it to that name.
|
||||||
"""
|
"""
|
||||||
build_dir = CORE.relative_build_path("build")
|
src_elf = get_built_elf_path()
|
||||||
src_elf = build_dir / f"{CORE.name}.elf"
|
|
||||||
dst_elf = get_elf_path()
|
dst_elf = get_elf_path()
|
||||||
|
|
||||||
if not src_elf.is_file():
|
if not src_elf.is_file():
|
||||||
|
|||||||
@@ -163,6 +163,18 @@ def test_has_discovered_components_after_configure(tmp_path: Path) -> None:
|
|||||||
assert has_discovered_components()
|
assert has_discovered_components()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_project_cmakelists_size_command_uses_json2() -> None:
|
||||||
|
"""The POST_BUILD size command uses the cheap json2 format, with --ng
|
||||||
|
only on the 1.x tool bundled with IDF < 6."""
|
||||||
|
content = _render()
|
||||||
|
assert "-m esp_idf_size --ng --format=json2" in content
|
||||||
|
|
||||||
|
CORE.data[KEY_ESP32][KEY_IDF_VERSION] = cv.Version(6, 0, 0)
|
||||||
|
content = _render()
|
||||||
|
assert "--ng" not in content
|
||||||
|
assert "--format=json2" in content
|
||||||
|
|
||||||
|
|
||||||
def test_get_project_cmakelists_uses_supplied_builtin_components() -> None:
|
def test_get_project_cmakelists_uses_supplied_builtin_components() -> None:
|
||||||
"""A cached list replaces project_description.json and is still filtered
|
"""A cached list replaces project_description.json and is still filtered
|
||||||
by EXCLUDE_COMPONENTS."""
|
by EXCLUDE_COMPONENTS."""
|
||||||
|
|||||||
@@ -638,6 +638,43 @@ def test_run_compile_passes_compile_process_limit(setup_core: Path) -> None:
|
|||||||
mock_run.assert_called_once_with("build", "size", jobs=1)
|
mock_run.assert_called_once_with("build", "size", jobs=1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_compile_passes_size_summary_paths(setup_core: Path) -> None:
|
||||||
|
"""print_summary receives the size json, partitions.csv, and the built
|
||||||
|
ELF from get_built_elf_path, which must stay in lockstep with the
|
||||||
|
project() name in the generated CMakeLists."""
|
||||||
|
_setup_build(setup_core)
|
||||||
|
config = {CONF_ESPHOME: {}}
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch.object(toolchain, "need_reconfigure", return_value=False),
|
||||||
|
patch.object(toolchain, "run_idf_py", return_value=0),
|
||||||
|
patch.object(toolchain, "print_summary") as mock_summary,
|
||||||
|
):
|
||||||
|
assert toolchain.run_compile(config, verbose=False) == 0
|
||||||
|
|
||||||
|
mock_summary.assert_called_once_with(
|
||||||
|
CORE.relative_build_path("build", "esp_idf_size.json"),
|
||||||
|
CORE.relative_build_path("partitions.csv"),
|
||||||
|
CORE.relative_build_path("build", f"{CORE.name}.elf"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_elf_copy(setup_core: Path) -> None:
|
||||||
|
"""The built <name>.elf is copied to the firmware.elf dashboard name."""
|
||||||
|
_setup_build(setup_core)
|
||||||
|
src = toolchain.get_built_elf_path()
|
||||||
|
src.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
src.write_bytes(b"elf")
|
||||||
|
assert toolchain.create_elf_copy() is True
|
||||||
|
assert toolchain.get_elf_path().read_bytes() == b"elf"
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_elf_copy_missing_source(setup_core: Path) -> None:
|
||||||
|
"""A missing built ELF is a warning and False, not a crash."""
|
||||||
|
_setup_build(setup_core)
|
||||||
|
assert toolchain.create_elf_copy() is False
|
||||||
|
|
||||||
|
|
||||||
def test_run_compile_without_compile_process_limit(setup_core: Path) -> None:
|
def test_run_compile_without_compile_process_limit(setup_core: Path) -> None:
|
||||||
"""When no compile_process_limit is set, no job limit is passed to idf.py."""
|
"""When no compile_process_limit is set, no job limit is passed to idf.py."""
|
||||||
_setup_build(setup_core)
|
_setup_build(setup_core)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import struct
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -17,64 +19,106 @@ def _write_size_json(tmp_path: Path, data: dict) -> Path:
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _write_partitions(tmp_path: Path) -> Path:
|
||||||
|
"""Drop a partitions.csv with a 0x1C0000 (1835008 byte) app slot."""
|
||||||
|
out = tmp_path / "partitions.csv"
|
||||||
|
out.write_text(
|
||||||
|
"# name, type, subtype, offset, size, flags\n"
|
||||||
|
"app0, app, ota_0, 0x10000, 0x1C0000,\n"
|
||||||
|
)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _elf_bytes(sections: list[tuple[int, int, int]], shentsize: int = 40) -> bytes:
|
||||||
|
"""Build a minimal ELF32 LE whose section headers carry the given
|
||||||
|
(sh_type, sh_flags, sh_size) triples."""
|
||||||
|
out = bytearray(52)
|
||||||
|
out[0:4] = b"\x7fELF"
|
||||||
|
out[4] = out[5] = 1 # 32-bit, little-endian
|
||||||
|
struct.pack_into("<I", out, 0x20, 52) # e_shoff
|
||||||
|
struct.pack_into("<HH", out, 0x2E, shentsize, len(sections))
|
||||||
|
for sh_type, sh_flags, sh_size in sections:
|
||||||
|
shdr = bytearray(40)
|
||||||
|
struct.pack_into("<II", shdr, 4, sh_type, sh_flags)
|
||||||
|
struct.pack_into("<I", shdr, 20, sh_size)
|
||||||
|
out += shdr
|
||||||
|
return bytes(out)
|
||||||
|
|
||||||
|
|
||||||
def _esp32_size_data() -> dict:
|
def _esp32_size_data() -> dict:
|
||||||
"""Synthetic esp_idf_size.json for the original ESP32 (split IRAM/DRAM)."""
|
"""Synthetic json2 for the original ESP32 (split IRAM/DRAM), in the
|
||||||
|
esp-idf-size >= 2.1 shape that carries ``total_size``."""
|
||||||
return {
|
return {
|
||||||
"image_size": 827455,
|
"version": "1.1",
|
||||||
"memory_types": {
|
"total_size": 827455,
|
||||||
"DRAM": {
|
"layout": [
|
||||||
"size": 180736,
|
{
|
||||||
|
"name": "DRAM",
|
||||||
|
"total": 180736,
|
||||||
"used": 47332,
|
"used": 47332,
|
||||||
"sections": {
|
"free": 133404,
|
||||||
".dram0.bss": {"abbrev_name": ".bss", "size": 30616},
|
"parts": {
|
||||||
".dram0.data": {"abbrev_name": ".data", "size": 16716},
|
".bss": {"size": 30616},
|
||||||
|
".data": {"size": 16716},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"IRAM": {
|
{
|
||||||
"size": 131072,
|
"name": "IRAM",
|
||||||
|
"total": 131072,
|
||||||
"used": 80351,
|
"used": 80351,
|
||||||
"sections": {
|
"free": 50721,
|
||||||
".iram0.text": {"abbrev_name": ".text", "size": 79323},
|
"parts": {
|
||||||
".iram0.vectors": {"abbrev_name": ".vectors", "size": 1028},
|
".text": {"size": 79323},
|
||||||
|
".vectors": {"size": 1028},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _s3_size_data() -> dict:
|
def _s3_size_data() -> dict:
|
||||||
"""Synthetic esp_idf_size.json for ESP32-S3 (unified DIRAM)."""
|
"""Synthetic json2 for ESP32-S3 (unified DIRAM), in the esp-idf-size 1.x
|
||||||
|
shape without ``total_size``."""
|
||||||
return {
|
return {
|
||||||
"image_size": 724215,
|
"version": "1.1",
|
||||||
"memory_types": {
|
"layout": [
|
||||||
"DIRAM": {
|
{
|
||||||
"size": 341760,
|
"name": "DIRAM",
|
||||||
|
"total": 341760,
|
||||||
"used": 104999,
|
"used": 104999,
|
||||||
"sections": {
|
"free": 236761,
|
||||||
".iram0.text": {"abbrev_name": ".text", "size": 58051},
|
"parts": {
|
||||||
".dram0.bss": {"abbrev_name": ".bss", "size": 27088},
|
".text": {"size": 58051},
|
||||||
".dram0.data": {"abbrev_name": ".data", "size": 19708},
|
".bss": {"size": 27088},
|
||||||
".noinit": {"abbrev_name": ".noinit", "size": 152},
|
".data": {"size": 19708},
|
||||||
|
".noinit": {"size": 152},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"IRAM": {
|
{
|
||||||
"size": 16384,
|
"name": "IRAM",
|
||||||
|
"total": 16384,
|
||||||
"used": 16384,
|
"used": 16384,
|
||||||
"sections": {
|
"free": 0,
|
||||||
".iram0.text": {"abbrev_name": ".text", "size": 15356},
|
"parts": {
|
||||||
".iram0.vectors": {"abbrev_name": ".vectors", "size": 1028},
|
".text": {"size": 15356},
|
||||||
|
".vectors": {"size": 1028},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _print_summary_ram_only(tmp_path: Path, size_json: Path) -> None:
|
||||||
|
"""Call print_summary with no partitions.csv or ELF on disk."""
|
||||||
|
print_summary(size_json, tmp_path / "partitions.csv", tmp_path / "firmware.elf")
|
||||||
|
|
||||||
|
|
||||||
def test_print_summary_esp32_uses_dram(
|
def test_print_summary_esp32_uses_dram(
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Original ESP32: DRAM has no ``.text``, so RAM = DRAM.used / DRAM.size unchanged."""
|
"""Original ESP32: RAM = DRAM.used / DRAM.total."""
|
||||||
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
||||||
print_summary(size_json, partitions_csv=None)
|
_print_summary_ram_only(tmp_path, size_json)
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
assert "RAM:" in out
|
assert "RAM:" in out
|
||||||
assert "used 47332 bytes from 180736 bytes" in out
|
assert "used 47332 bytes from 180736 bytes" in out
|
||||||
@@ -83,63 +127,193 @@ def test_print_summary_esp32_uses_dram(
|
|||||||
def test_print_summary_s3_falls_back_to_diram(
|
def test_print_summary_s3_falls_back_to_diram(
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""ESP32-S3 with no DRAM key falls back to DIRAM and reports raw region usage."""
|
"""ESP32-S3 with no DRAM entry falls back to DIRAM and reports raw region usage."""
|
||||||
size_json = _write_size_json(tmp_path, _s3_size_data())
|
size_json = _write_size_json(tmp_path, _s3_size_data())
|
||||||
print_summary(size_json, partitions_csv=None)
|
_print_summary_ram_only(tmp_path, size_json)
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
assert "used 104999 bytes from 341760 bytes" in out
|
assert "used 104999 bytes from 341760 bytes" in out
|
||||||
|
|
||||||
|
|
||||||
def test_print_summary_skips_when_diram_total_collapses(
|
def test_print_summary_skips_when_diram_total_collapses(
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
tmp_path: Path,
|
||||||
|
capsys: pytest.CaptureFixture[str],
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""A zero-size region drops the RAM line rather than divide by zero."""
|
"""A zero-size region drops the RAM line rather than divide by zero."""
|
||||||
size_json = _write_size_json(
|
size_json = _write_size_json(
|
||||||
tmp_path,
|
tmp_path,
|
||||||
{
|
{
|
||||||
"memory_types": {
|
"version": "1.1",
|
||||||
"DIRAM": {
|
"layout": [{"name": "DIRAM", "total": 0, "used": 0}],
|
||||||
"size": 0,
|
|
||||||
"used": 0,
|
|
||||||
"sections": {},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
print_summary(size_json, partitions_csv=None)
|
_print_summary_ram_only(tmp_path, size_json)
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
assert "RAM:" not in out
|
assert "RAM:" not in out
|
||||||
|
assert "unusable region" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_print_summary_handles_missing_json(
|
def test_print_summary_handles_missing_json(
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Missing size json is non-fatal and prints nothing."""
|
"""Missing size json is non-fatal and prints nothing."""
|
||||||
print_summary(tmp_path / "does_not_exist.json", partitions_csv=None)
|
_print_summary_ram_only(tmp_path, tmp_path / "does_not_exist.json")
|
||||||
assert capsys.readouterr().out == ""
|
assert capsys.readouterr().out == ""
|
||||||
|
|
||||||
|
|
||||||
def test_print_summary_handles_no_memory_types(
|
def test_print_summary_handles_no_layout(
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
tmp_path: Path,
|
||||||
|
capsys: pytest.CaptureFixture[str],
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""A size json without ``memory_types`` still doesn't crash."""
|
"""A size json without ``layout`` warns so schema drift is visible."""
|
||||||
size_json = _write_size_json(tmp_path, {"image_size": 0})
|
size_json = _write_size_json(tmp_path, {"version": "1.1"})
|
||||||
print_summary(size_json, partitions_csv=None)
|
_print_summary_ram_only(tmp_path, size_json)
|
||||||
assert capsys.readouterr().out == ""
|
assert capsys.readouterr().out == ""
|
||||||
|
assert any(
|
||||||
|
r.levelname == "WARNING" and "no DRAM/DIRAM region" in r.message
|
||||||
def test_print_summary_flash_line(
|
for r in caplog.records
|
||||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
|
||||||
) -> None:
|
|
||||||
"""A partition table with an app row yields the Flash line in the exact
|
|
||||||
padded shape script/ci_memory_impact_extract.py greps."""
|
|
||||||
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
|
||||||
partitions = tmp_path / "partitions.csv"
|
|
||||||
partitions.write_text(
|
|
||||||
"# name, type, subtype, offset, size, flags\n"
|
|
||||||
"app0, app, ota_0, 0x10000, 0x1C0000,\n"
|
|
||||||
)
|
)
|
||||||
print_summary(size_json, partitions)
|
|
||||||
|
|
||||||
|
def test_print_summary_flash_line_prefers_total_size(
|
||||||
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""With ``total_size`` in the json, that figure wins without reading the
|
||||||
|
ELF, in the exact shape script/ci_memory_impact_extract.py greps."""
|
||||||
|
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
||||||
|
partitions = _write_partitions(tmp_path)
|
||||||
|
print_summary(size_json, partitions, tmp_path / "firmware.elf")
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
assert "Flash: " in out
|
assert "Flash: " in out
|
||||||
assert "(used 827455 bytes from 1835008 bytes)" in out
|
assert "(used 827455 bytes from 1835008 bytes)" in out
|
||||||
|
|
||||||
|
|
||||||
|
def test_print_summary_flash_line_derives_from_elf(
|
||||||
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""A 1.x json without ``total_size`` sums the ELF's loadable PROGBITS
|
||||||
|
sections; NOBITS and non-alloc sections are excluded."""
|
||||||
|
size_json = _write_size_json(tmp_path, _s3_size_data())
|
||||||
|
partitions = _write_partitions(tmp_path)
|
||||||
|
firmware_elf = tmp_path / "firmware.elf"
|
||||||
|
firmware_elf.write_bytes(
|
||||||
|
_elf_bytes(
|
||||||
|
[
|
||||||
|
(1, 0x6, 700000), # PROGBITS, alloc+exec: counted
|
||||||
|
(1, 0x2, 24215), # PROGBITS, alloc: counted
|
||||||
|
(8, 0x2, 50000), # NOBITS (.bss): excluded
|
||||||
|
(1, 0x0, 12345), # PROGBITS, no alloc (.debug_*): excluded
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
print_summary(size_json, partitions, firmware_elf)
|
||||||
|
out = capsys.readouterr().out
|
||||||
|
assert "(used 724215 bytes from 1835008 bytes)" in out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"data",
|
||||||
|
[
|
||||||
|
pytest.param([1, 2], id="top_level_list"),
|
||||||
|
pytest.param({"version": "1.1", "layout": None}, id="layout_null"),
|
||||||
|
pytest.param({"version": "1.1", "layout": 7}, id="layout_scalar"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_print_summary_handles_unexpected_shapes(
|
||||||
|
data: object, tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""A foreign-schema size json degrades to a warning, never a traceback."""
|
||||||
|
size_json = _write_size_json(tmp_path, data)
|
||||||
|
_print_summary_ram_only(tmp_path, size_json)
|
||||||
|
assert capsys.readouterr().out == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_print_summary_skips_flash_on_zero_app_partition(
|
||||||
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""A zero-size app partition skips the Flash line rather than printing
|
||||||
|
a from-0-bytes figure CI would record."""
|
||||||
|
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
||||||
|
partitions = tmp_path / "partitions.csv"
|
||||||
|
partitions.write_text(
|
||||||
|
"# name, type, subtype, offset, size, flags\napp0, app, ota_0, 0x10000, 0x0,\n"
|
||||||
|
)
|
||||||
|
print_summary(size_json, partitions, tmp_path / "firmware.elf")
|
||||||
|
out = capsys.readouterr().out
|
||||||
|
assert "Flash:" not in out
|
||||||
|
|
||||||
|
|
||||||
|
def test_print_summary_skips_flash_on_unreadable_partitions(
|
||||||
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""An unreadable partitions.csv is non-fatal (chmod tricks don't work
|
||||||
|
for root in CI containers, so simulate the OSError instead)."""
|
||||||
|
size_json = _write_size_json(tmp_path, _esp32_size_data())
|
||||||
|
partitions = _write_partitions(tmp_path)
|
||||||
|
with patch(
|
||||||
|
"esphome.espidf.size_summary._find_app_partition_size",
|
||||||
|
side_effect=PermissionError("denied"),
|
||||||
|
):
|
||||||
|
print_summary(size_json, partitions, tmp_path / "firmware.elf")
|
||||||
|
assert "Flash:" not in capsys.readouterr().out
|
||||||
|
|
||||||
|
|
||||||
|
def test_print_summary_flash_falls_back_on_bad_total_size(
|
||||||
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
"""A zero or non-int total_size falls back to the ELF instead of
|
||||||
|
printing a used-0-bytes line CI would read as a real measurement."""
|
||||||
|
data = _s3_size_data()
|
||||||
|
data["total_size"] = 0
|
||||||
|
size_json = _write_size_json(tmp_path, data)
|
||||||
|
partitions = _write_partitions(tmp_path)
|
||||||
|
firmware_elf = tmp_path / "firmware.elf"
|
||||||
|
firmware_elf.write_bytes(_elf_bytes([(1, 0x2, 4096)]))
|
||||||
|
print_summary(size_json, partitions, firmware_elf)
|
||||||
|
out = capsys.readouterr().out
|
||||||
|
assert "(used 4096 bytes from 1835008 bytes)" in out
|
||||||
|
|
||||||
|
|
||||||
|
_GOOD_ELF = _elf_bytes([(1, 0x2, 1024)])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("elf_bytes", "with_partitions"),
|
||||||
|
[
|
||||||
|
pytest.param(None, True, id="missing_elf"),
|
||||||
|
pytest.param(b"junk", True, id="not_an_elf"),
|
||||||
|
pytest.param(
|
||||||
|
_elf_bytes([(1, 0x2, 1024)], shentsize=0), True, id="bad_shentsize"
|
||||||
|
),
|
||||||
|
pytest.param(_GOOD_ELF[:60], True, id="truncated_table"),
|
||||||
|
pytest.param(_elf_bytes([]), True, id="no_sections"),
|
||||||
|
pytest.param(_elf_bytes([(8, 0x2, 50000)]), True, id="no_progbits"),
|
||||||
|
pytest.param(_GOOD_ELF, False, id="missing_partitions"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_print_summary_skips_flash_on_bad_input(
|
||||||
|
elf_bytes: bytes | None,
|
||||||
|
with_partitions: bool,
|
||||||
|
tmp_path: Path,
|
||||||
|
capsys: pytest.CaptureFixture[str],
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""An unusable ELF or missing partitions.csv skips the Flash line, not the RAM line."""
|
||||||
|
size_json = _write_size_json(tmp_path, _s3_size_data())
|
||||||
|
firmware_elf = tmp_path / "firmware.elf"
|
||||||
|
if elf_bytes is not None:
|
||||||
|
firmware_elf.write_bytes(elf_bytes)
|
||||||
|
if with_partitions:
|
||||||
|
_write_partitions(tmp_path)
|
||||||
|
print_summary(size_json, tmp_path / "partitions.csv", firmware_elf)
|
||||||
|
out = capsys.readouterr().out
|
||||||
|
assert "RAM:" in out
|
||||||
|
assert "Flash:" not in out
|
||||||
|
# ELF problems warn (anomaly after a successful build); a missing
|
||||||
|
# partitions.csv stays at debug
|
||||||
|
warned = any(
|
||||||
|
r.levelname == "WARNING" and "Skipping Flash summary" in r.message
|
||||||
|
for r in caplog.records
|
||||||
|
)
|
||||||
|
assert warned == with_partitions
|
||||||
|
|||||||
Reference in New Issue
Block a user