esp32: Update tools/metrics_esp32.py to use JSON output.

Much cleaner. Also add deltas to the output table for easier
interpretation.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2025-10-15 09:58:41 +11:00
parent 00a926e99e
commit 2f88c1e795
2 changed files with 42 additions and 32 deletions

View File

@@ -98,10 +98,10 @@ monitor:
$(call RUN_IDF_PY,$(DEVICE) monitor) $(call RUN_IDF_PY,$(DEVICE) monitor)
size: size:
$(call RUN_IDF_PY,size) $(call RUN_IDF_PY,size $(SIZE_FLAGS))
size-components: size-components:
$(call RUN_IDF_PY,size-components) $(call RUN_IDF_PY,size-components $(SIZE_FLAGS))
size-files: size-files:
$(call RUN_IDF_PY,size-files) $(call RUN_IDF_PY,size-files)

View File

@@ -27,9 +27,7 @@
# #
# 5) If all goes well, it will run for a while and then print a Markdown # 5) If all goes well, it will run for a while and then print a Markdown
# formatted table of binary sizes, sorted by board+variant. # formatted table of binary sizes, sorted by board+variant.
# import json
# Note that for ESP32-S3 and C3, IRAM and DRAM are exchangeable so the IRAM size
# column of the table is really D/IRAM.
import os import os
import re import re
import shutil import shutil
@@ -37,12 +35,14 @@ import sys
import subprocess import subprocess
from dataclasses import dataclass from dataclasses import dataclass
IDF_VERS = ("v5.4.2",) IDF_VERS = ("v5.5.1", "v5.4.2")
BUILDS = ( BUILDS = (
("ESP32_GENERIC", ""), ("ESP32_GENERIC", ""),
("ESP32_GENERIC", "D2WD"), ("ESP32_GENERIC", "D2WD"),
("ESP32_GENERIC", "SPIRAM"), ("ESP32_GENERIC", "SPIRAM"),
("ESP32_GENERIC_C2", ""),
("ESP32_GENERIC_C6", ""),
("ESP32_GENERIC_S3", ""), ("ESP32_GENERIC_S3", ""),
("ESP32_GENERIC_S3", "SPIRAM_OCT"), ("ESP32_GENERIC_S3", "SPIRAM_OCT"),
) )
@@ -60,9 +60,9 @@ class BuildSizes:
idf_ver: str idf_ver: str
board: str board: str
variant: str variant: str
bin_size: str = "" bin_size: int | str = ""
dram_size: str = "" dram_size: int = 0
iram_size: str = "" iram_size: int = 0
def print_summary(self, include_ver=False): def print_summary(self, include_ver=False):
print(f"BOARD={self.board} BOARD_VARIANT={self.variant}") print(f"BOARD={self.board} BOARD_VARIANT={self.variant}")
@@ -80,7 +80,18 @@ class BuildSizes:
"|-------|---------------|-------------|-------------|------------------|------------------|" "|-------|---------------|-------------|-------------|------------------|------------------|"
) )
def print_table_row(self, print_board): def print_table_row(self, print_board, baseline=None):
def compare(field):
this = getattr(self, field)
if baseline and isinstance(this, int):
base = getattr(baseline, field)
delta = this - base
pct = ((this / base) * 100) - 100
plus = "+" if delta >= 0 else ""
return f"{this} ({plus}{delta}/{pct:.1f})"
else:
return str(this)
print( print(
"| " "| "
+ " | ".join( + " | ".join(
@@ -88,9 +99,9 @@ class BuildSizes:
self.board if print_board else "", self.board if print_board else "",
self.variant if print_board else "", self.variant if print_board else "",
self.idf_ver, self.idf_ver,
self.bin_size, compare("bin_size"),
self.iram_size, compare("iram_size"),
self.dram_size, compare("dram_size"),
) )
) )
+ " |" + " |"
@@ -131,25 +142,19 @@ class BuildSizes:
def make_size(self): def make_size(self):
try: try:
size_out = self.run_make("size") size_out = self.run_make("size SIZE_FLAGS='--format json'")
try: size_out = size_out[size_out.rindex("{") :]
# pre IDF v5.4 size output size_out = json.loads(size_out)
# "Used static DRAM:" or "Used stat D/IRAM:"
RE_DRAM = r"Used stat(?:ic)? D.*: *(\d+) bytes"
RE_IRAM = r"Used static IRAM: *(\d+) bytes"
self.dram_size = re.search(RE_DRAM, size_out).group(1)
self.iram_size = re.search(RE_IRAM, size_out).group(1)
except AttributeError:
# IDF v5.4 size output is much nicer formatted
# Note the pipes in these expressions are not the ASCII/RE |
RE_DRAM = r"│ *DI?RAM *│ *(\d+)"
RE_IRAM = r"│ *IRAM *│ *(\d+)"
self.dram_size = re.search(RE_DRAM, size_out).group(1)
self.iram_size = re.search(RE_IRAM, size_out).group(1)
# This line is the same on before/after versions def sum_sizes(*keys):
RE_BIN = r"Total image size: *(\d+) bytes" return sum(size_out.get(k, 0) for k in keys)
self.bin_size = re.search(RE_BIN, size_out).group(1)
# Different targets report DRAM, IRAM, and/or D/IRAM separately
self.dram_size = sum_sizes(
"used_dram", "diram_data", "diram_bss", "diram_rodata", "diram_other"
)
self.iram_size = sum_sizes("used_iram", "diram_text", "diram_vectors")
self.bin_size = size_out["total_size"]
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
self.bin_size = "build failed" self.bin_size = "build failed"
@@ -184,11 +189,16 @@ def main(do_clean):
# print everything again as a table sorted by board+variant # print everything again as a table sorted by board+variant
last_bv = "" last_bv = ""
baseline_sizes = None
BuildSizes.print_table_heading() BuildSizes.print_table_heading()
for build_sizes in sorted(sizes): for build_sizes in sorted(sizes):
bv = (build_sizes.board, build_sizes.variant) bv = (build_sizes.board, build_sizes.variant)
build_sizes.print_table_row(last_bv != bv) new_board = last_bv != bv
if new_board:
baseline_sizes = None
build_sizes.print_table_row(last_bv != bv, baseline_sizes)
last_bv = bv last_bv = bv
baseline_sizes = build_sizes
def idf_git(*commands): def idf_git(*commands):