chore(embedded): add memory-budget guardrail script and docs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
electron-rare
2026-03-11 03:52:54 +01:00
parent 68b60dbc89
commit 97d531cf26
3 changed files with 117 additions and 2 deletions
+3 -2
View File
@@ -47,6 +47,7 @@ python -m platformio device monitor -b 115200 --port <PORT>
```bash ```bash
./scripts/doctor_repo.sh ./scripts/doctor_repo.sh
./scripts/check_memory_budget.sh --env freenove_esp32s3_full_with_ui --max-ram 75 --max-flash 80 --yes
python tests/sprint1_utility_contract.py --mode serial --port <PORT> python tests/sprint1_utility_contract.py --mode serial --port <PORT>
python tests/sprint2_capture_contract.py --mode serial --port <PORT> python tests/sprint2_capture_contract.py --mode serial --port <PORT>
python tests/sprint3_audio_contract.py --mode serial --port <PORT> python tests/sprint3_audio_contract.py --mode serial --port <PORT>
@@ -87,8 +88,8 @@ python tests/phase9_ui_validation.py --port <PORT>
- Source files: `258` - Source files: `258`
### Corrections Prioritaires ### Corrections Prioritaires
- [ ] Vérifier target PlatformIO et budget mémoire - [x] Vérifier target PlatformIO et budget mémoire
- [ ] Ajouter/fiabiliser les commandes de vérification automatiques. - [x] Ajouter/fiabiliser les commandes de vérification automatiques.
- [ ] Clore les points bloquants avant optimisation avancée. - [ ] Clore les points bloquants avant optimisation avancée.
### Optimisation ### Optimisation
+9
View File
@@ -23,6 +23,15 @@ python -m platformio run -e freenove_esp32s3_full_with_ui
python -m platformio run -e freenove_esp32s3_full_with_ui -t buildfs python -m platformio run -e freenove_esp32s3_full_with_ui -t buildfs
``` ```
## Budget memoire
```bash
./scripts/check_memory_budget.sh --env freenove_esp32s3_full_with_ui --max-ram 75 --max-flash 80 --yes
```
Le script force un build `platformio run`, lit les lignes `RAM:` et `Flash:`,
et retourne un code non-zero si les seuils sont depasses.
## Flash ## Flash
```bash ```bash
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage:
check_memory_budget.sh [--env <name>] [--max-ram <pct>] [--max-flash <pct>] [--yes]
Options:
--env <name> PlatformIO env (default: freenove_esp32s3_full_with_ui)
--max-ram <pct> RAM usage threshold in percent (default: 75)
--max-flash <pct> Flash usage threshold in percent (default: 80)
--yes Non-interactive mode (skip optional TUI prompts)
-h, --help
Examples:
./scripts/check_memory_budget.sh
./scripts/check_memory_budget.sh --max-ram 70 --max-flash 78
./scripts/check_memory_budget.sh --env freenove_esp32s3_full_with_ui --yes
EOF
}
env_name="freenove_esp32s3_full_with_ui"
max_ram="75"
max_flash="80"
non_interactive=0
while [[ $# -gt 0 ]]; do
case "$1" in
--env)
env_name="${2:-}"
shift 2
;;
--max-ram)
max_ram="${2:-}"
shift 2
;;
--max-flash)
max_flash="${2:-}"
shift 2
;;
--yes|--non-interactive)
non_interactive=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage
exit 2
;;
esac
done
if [[ "$non_interactive" -eq 0 && -t 0 && -t 1 && "$(command -v gum >/dev/null 2>&1; echo $?)" -eq 0 ]]; then
env_name="$(gum input --value "$env_name" --placeholder "PlatformIO env")"
max_ram="$(gum input --value "$max_ram" --placeholder "RAM threshold %")"
max_flash="$(gum input --value "$max_flash" --placeholder "Flash threshold %")"
fi
tmp_log="$(mktemp -t zacus-mem-budget.XXXXXX.log)"
cleanup() { rm -f "$tmp_log"; }
trap cleanup EXIT
echo "[RUN] python3 -m platformio run -e $env_name"
python3 -m platformio run -e "$env_name" | tee "$tmp_log"
ram_line="$(grep -E '^RAM:' "$tmp_log" | tail -n1 || true)"
flash_line="$(grep -E '^Flash:' "$tmp_log" | tail -n1 || true)"
if [[ -z "$ram_line" || -z "$flash_line" ]]; then
echo "[ERR] cannot parse RAM/Flash lines from PlatformIO output" >&2
exit 1
fi
ram_pct="$(echo "$ram_line" | sed -E 's/.* ([0-9]+([.][0-9]+)?)% .*/\1/')"
flash_pct="$(echo "$flash_line" | sed -E 's/.* ([0-9]+([.][0-9]+)?)% .*/\1/')"
ram_used="$(echo "$ram_line" | sed -E 's/.*[(]used ([0-9]+) bytes from ([0-9]+) bytes[)].*/\1\/\2/')"
flash_used="$(echo "$flash_line" | sed -E 's/.*[(]used ([0-9]+) bytes from ([0-9]+) bytes[)].*/\1\/\2/')"
echo "[INFO] env=$env_name"
echo "[INFO] RAM : ${ram_pct}% ($ram_used), threshold=${max_ram}%"
echo "[INFO] Flash : ${flash_pct}% ($flash_used), threshold=${max_flash}%"
ram_over="$(awk "BEGIN {print ($ram_pct > $max_ram) ? 1 : 0}")"
flash_over="$(awk "BEGIN {print ($flash_pct > $max_flash) ? 1 : 0}")"
status=0
if [[ "$ram_over" -eq 1 ]]; then
echo "[FAIL] RAM usage is above threshold"
status=1
fi
if [[ "$flash_over" -eq 1 ]]; then
echo "[FAIL] Flash usage is above threshold"
status=1
fi
if [[ "$status" -eq 0 ]]; then
echo "[PASS] memory budget is within thresholds"
fi
exit "$status"