diff --git a/esphome/build_gen/espidf.py b/esphome/build_gen/espidf.py index 2ef89cf595..65ed710ad7 100644 --- a/esphome/build_gen/espidf.py +++ b/esphome/build_gen/espidf.py @@ -92,7 +92,7 @@ def get_project_cmakelists( # esp_idf_size 2.x (bundled with IDF >=6.0) made NG the default and # removed the --ng flag; on 1.x (IDF 5.5) --ng is required to get - # --format=raw because the legacy mode doesn't support it. + # --format=json2 because the legacy mode doesn't support it. size_ng_flag = "--ng" if idf_version() < cv.Version(6, 0, 0) else "" # Project-wide compile options: -D defines and -W warning flags (skip @@ -211,10 +211,13 @@ include($ENV{{IDF_PATH}}/tools/cmake/project.cmake) 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 only summarizes memory regions; the raw format also dumps every +# symbol (multi-MB, ~2s on a large map) and this command runs inside the +# link edge, so everything downstream of the ELF would wait on it. add_custom_command( 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 ${{CMAKE_PROJECT_NAME}}.map WORKING_DIRECTORY ${{CMAKE_BINARY_DIR}} diff --git a/esphome/espidf/size_summary.py b/esphome/espidf/size_summary.py index 2be3634c69..0d39a8b0e8 100644 --- a/esphome/espidf/size_summary.py +++ b/esphome/espidf/size_summary.py @@ -9,16 +9,19 @@ byte-identical to PlatformIO's output: Flash: [=== ] 48.4% (used 888511 bytes from 1835008 bytes) The format matches ``script/ci_memory_impact_extract.py`` so CI memory -analysis works unchanged on native ESP-IDF builds. RAM total is the -DRAM region size from the linker map; Flash total is taken from +analysis works unchanged on native ESP-IDF builds. RAM usage comes from +the DRAM (or unified DIRAM) region of the linker map; Flash used is the +size of the app ``.bin`` on disk, and Flash total is taken from ``partitions.csv`` using PlatformIO's rule (first app partition whose subtype is ``factory`` or ``ota_0``; see ``platform-espressif32/builder/main.py::_update_max_upload_size``). Structured size data is produced at link time by a CMake POST_BUILD custom command (see ``build_gen/espidf.py``) which writes -``esp_idf_size.json`` next to the ELF. We read that file here rather -than re-running ``esp_idf_size`` from Python. +``esp_idf_size.json`` (``--format=json2``: a per-memory-type summary, +``{"version": ..., "layout": [{"name", "total", "used", ...}]}``) next +to the ELF. We read that file here rather than re-running +``esp_idf_size`` from Python. """ from __future__ import annotations @@ -69,7 +72,9 @@ def _find_app_partition_size(partitions_csv: Path) -> int: 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 print_summary( + size_json: Path, partitions_csv: Path | None, firmware_bin: Path | None +) -> None: """Print PlatformIO-shaped RAM and Flash one-liners. Failures are non-fatal: the build has already succeeded, we just couldn't @@ -84,15 +89,23 @@ def print_summary(size_json: Path, partitions_csv: Path | None) -> None: _LOGGER.debug("Skipping size summary: %s", e) return - memory_types = data.get("memory_types", {}) - ram_region = memory_types.get("DRAM") or memory_types.get("DIRAM") or {} + regions = { + entry.get("name"): entry + for entry in data.get("layout", []) + if isinstance(entry, dict) + } + ram_region = regions.get("DRAM") or regions.get("DIRAM") or {} ram_used = ram_region.get("used") - ram_total = ram_region.get("size") + ram_total = ram_region.get("total") 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: + if firmware_bin is None or partitions_csv is None: + return + try: + image_size = firmware_bin.stat().st_size + except OSError as e: + _LOGGER.debug("Skipping Flash summary: %s", e) return try: app_size = _find_app_partition_size(partitions_csv) diff --git a/esphome/espidf/toolchain.py b/esphome/espidf/toolchain.py index 986f9dfb8b..a91659e2c2 100644 --- a/esphome/espidf/toolchain.py +++ b/esphome/espidf/toolchain.py @@ -542,7 +542,12 @@ def run_compile(config, verbose: bool) -> int: if rc == 0: size_json = CORE.relative_build_path("build", "esp_idf_size.json") partitions = CORE.relative_build_path("partitions.csv") - print_summary(size_json, partitions if partitions.is_file() else None) + firmware_bin = get_firmware_path() + print_summary( + size_json, + partitions if partitions.is_file() else None, + firmware_bin if firmware_bin.is_file() else None, + ) return rc diff --git a/tests/unit_tests/test_size_summary.py b/tests/unit_tests/test_size_summary.py index 0c0852a191..1fcdc037a9 100644 --- a/tests/unit_tests/test_size_summary.py +++ b/tests/unit_tests/test_size_summary.py @@ -18,63 +18,71 @@ def _write_size_json(tmp_path: Path, data: dict) -> Path: def _esp32_size_data() -> dict: - """Synthetic esp_idf_size.json for the original ESP32 (split IRAM/DRAM).""" + """Synthetic json2 esp_idf_size.json for the original ESP32 (split IRAM/DRAM).""" return { - "image_size": 827455, - "memory_types": { - "DRAM": { - "size": 180736, + "version": "1.1", + "layout": [ + { + "name": "DRAM", + "total": 180736, "used": 47332, - "sections": { - ".dram0.bss": {"abbrev_name": ".bss", "size": 30616}, - ".dram0.data": {"abbrev_name": ".data", "size": 16716}, + "free": 133404, + "parts": { + ".bss": {"size": 30616}, + ".data": {"size": 16716}, }, }, - "IRAM": { - "size": 131072, + { + "name": "IRAM", + "total": 131072, "used": 80351, - "sections": { - ".iram0.text": {"abbrev_name": ".text", "size": 79323}, - ".iram0.vectors": {"abbrev_name": ".vectors", "size": 1028}, + "free": 50721, + "parts": { + ".text": {"size": 79323}, + ".vectors": {"size": 1028}, }, }, - }, + ], } def _s3_size_data() -> dict: - """Synthetic esp_idf_size.json for ESP32-S3 (unified DIRAM).""" + """Synthetic json2 esp_idf_size.json for ESP32-S3 (unified DIRAM).""" return { - "image_size": 724215, - "memory_types": { - "DIRAM": { - "size": 341760, + "version": "1.1", + "layout": [ + { + "name": "DIRAM", + "total": 341760, "used": 104999, - "sections": { - ".iram0.text": {"abbrev_name": ".text", "size": 58051}, - ".dram0.bss": {"abbrev_name": ".bss", "size": 27088}, - ".dram0.data": {"abbrev_name": ".data", "size": 19708}, - ".noinit": {"abbrev_name": ".noinit", "size": 152}, + "free": 236761, + "parts": { + ".text": {"size": 58051}, + ".bss": {"size": 27088}, + ".data": {"size": 19708}, + ".noinit": {"size": 152}, }, }, - "IRAM": { - "size": 16384, + { + "name": "IRAM", + "total": 16384, "used": 16384, - "sections": { - ".iram0.text": {"abbrev_name": ".text", "size": 15356}, - ".iram0.vectors": {"abbrev_name": ".vectors", "size": 1028}, + "free": 0, + "parts": { + ".text": {"size": 15356}, + ".vectors": {"size": 1028}, }, }, - }, + ], } def test_print_summary_esp32_uses_dram( tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> 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()) - print_summary(size_json, partitions_csv=None) + print_summary(size_json, partitions_csv=None, firmware_bin=None) out = capsys.readouterr().out assert "RAM:" in out assert "used 47332 bytes from 180736 bytes" in out @@ -83,9 +91,9 @@ def test_print_summary_esp32_uses_dram( def test_print_summary_s3_falls_back_to_diram( tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> 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()) - print_summary(size_json, partitions_csv=None) + print_summary(size_json, partitions_csv=None, firmware_bin=None) out = capsys.readouterr().out assert "used 104999 bytes from 341760 bytes" in out @@ -97,16 +105,19 @@ def test_print_summary_skips_when_diram_total_collapses( size_json = _write_size_json( tmp_path, { - "memory_types": { - "DIRAM": { - "size": 0, + "version": "1.1", + "layout": [ + { + "name": "DIRAM", + "total": 0, "used": 0, - "sections": {}, + "free": 0, + "parts": {}, }, - }, + ], }, ) - print_summary(size_json, partitions_csv=None) + print_summary(size_json, partitions_csv=None, firmware_bin=None) out = capsys.readouterr().out assert "RAM:" not in out @@ -115,16 +126,18 @@ def test_print_summary_handles_missing_json( tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> None: """Missing size json is non-fatal and prints nothing.""" - print_summary(tmp_path / "does_not_exist.json", partitions_csv=None) + print_summary( + tmp_path / "does_not_exist.json", partitions_csv=None, firmware_bin=None + ) 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] ) -> None: - """A size json without ``memory_types`` still doesn't crash.""" - size_json = _write_size_json(tmp_path, {"image_size": 0}) - print_summary(size_json, partitions_csv=None) + """A size json without ``layout`` still doesn't crash.""" + size_json = _write_size_json(tmp_path, {"version": "1.1"}) + print_summary(size_json, partitions_csv=None, firmware_bin=None) assert capsys.readouterr().out == "" @@ -139,7 +152,25 @@ def test_print_summary_flash_line( "# name, type, subtype, offset, size, flags\n" "app0, app, ota_0, 0x10000, 0x1C0000,\n" ) - print_summary(size_json, partitions) + firmware_bin = tmp_path / "firmware.bin" + firmware_bin.write_bytes(b"\x00" * 827455) + print_summary(size_json, partitions, firmware_bin) out = capsys.readouterr().out assert "Flash: " in out assert "(used 827455 bytes from 1835008 bytes)" in out + + +def test_print_summary_skips_flash_without_bin( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """No firmware bin means the RAM line prints but the Flash line is skipped.""" + 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, firmware_bin=None) + out = capsys.readouterr().out + assert "RAM:" in out + assert "Flash:" not in out