diff --git a/.github/workflows/flash_analysis.yml b/.github/workflows/flash_analysis.yml index 9e57e814640..5fb14a34a30 100644 --- a/.github/workflows/flash_analysis.yml +++ b/.github/workflows/flash_analysis.yml @@ -34,6 +34,19 @@ jobs: strategy: matrix: target: [px4_fmu-v2, px4_fmu-v5x, px4_fmu-v6x] + # The flash-analysis variant links against an oversized flash region, + # so a change that overflows the real one still gets a report. + variant: [flash-analysis] + include: + # The px4io firmware is analysed as it ships. Its image holds no + # commit hash, so a change of its bytes is a change of what every + # FMU with an IO flashes to it at boot. + - target: px4_io-v2 + variant: default + reporter_args: --reproducible-image + - target: cubepilot_io-v2 + variant: default + reporter_args: --reproducible-image outputs: px4_fmu-v2-bloaty-output: ${{ steps.gen-output.outputs.px4_fmu-v2-bloaty-output }} px4_fmu-v2-memory-summary: ${{ steps.gen-output.outputs.px4_fmu-v2-memory-summary }} @@ -41,6 +54,10 @@ jobs: px4_fmu-v5x-memory-summary: ${{ steps.gen-output.outputs.px4_fmu-v5x-memory-summary }} px4_fmu-v6x-bloaty-output: ${{ steps.gen-output.outputs.px4_fmu-v6x-bloaty-output }} px4_fmu-v6x-memory-summary: ${{ steps.gen-output.outputs.px4_fmu-v6x-memory-summary }} + px4_io-v2-bloaty-output: ${{ steps.gen-output.outputs.px4_io-v2-bloaty-output }} + px4_io-v2-memory-summary: ${{ steps.gen-output.outputs.px4_io-v2-memory-summary }} + cubepilot_io-v2-bloaty-output: ${{ steps.gen-output.outputs.cubepilot_io-v2-bloaty-output }} + cubepilot_io-v2-memory-summary: ${{ steps.gen-output.outputs.cubepilot_io-v2-memory-summary }} steps: - uses: runs-on/action@v2 - uses: actions/checkout@v6 @@ -79,7 +96,7 @@ jobs: ccache -z - name: Build Target - run: make ${{ matrix.target }}_flash-analysis + run: make ${{ matrix.target }}_${{ matrix.variant }} - name: Store the ELF and linker script with the change run: | @@ -133,7 +150,7 @@ jobs: run: ccache -z - name: Build - run: make ${{ matrix.target }}_flash-analysis + run: make ${{ matrix.target }}_${{ matrix.variant }} - name: Store the ELF before the change run: cp ./build/**/*.elf ./before-change.elf @@ -162,7 +179,8 @@ jobs: BLOATY_OUTPUT: ${{ steps.bloaty-step.outputs.bloaty-output }} run: | summary=$(python3 "$RUNNER_TEMP/firmware_size.py" \ - --before before-change.elf --after with-change.elf --linker-script with-change.ld) + --before before-change.elf --after with-change.elf --linker-script with-change.ld \ + ${{ matrix.reporter_args }}) EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) { echo "${{ matrix.target }}-memory-summary=$summary" @@ -179,6 +197,8 @@ jobs: V2: ${{ needs.analyze_flash.outputs.px4_fmu-v2-memory-summary }} V5X: ${{ needs.analyze_flash.outputs.px4_fmu-v5x-memory-summary }} V6X: ${{ needs.analyze_flash.outputs.px4_fmu-v6x-memory-summary }} + IO: ${{ needs.analyze_flash.outputs.px4_io-v2-memory-summary }} + CUBEPILOT_IO: ${{ needs.analyze_flash.outputs.cubepilot_io-v2-memory-summary }} if: github.event.pull_request steps: - name: Find Comment @@ -201,7 +221,9 @@ jobs: steps.fc.outputs.comment-id != '' || fromJSON(env.V2).changed || fromJSON(env.V5X).changed || - fromJSON(env.V6X).changed + fromJSON(env.V6X).changed || + fromJSON(env.IO).changed || + fromJSON(env.CUBEPILOT_IO).changed run: | mkdir -p pr-comment cat > pr-comment/manifest.json < px4_fmu-v2 — Bloaty section/symbol breakdown @@ -245,6 +271,22 @@ jobs: ``` +
+ px4_io-v2 — Bloaty section/symbol breakdown + + ``` + ${{ needs.analyze_flash.outputs.px4_io-v2-bloaty-output }} + ``` +
+ +
+ cubepilot_io-v2 — Bloaty section/symbol breakdown + + ``` + ${{ needs.analyze_flash.outputs.cubepilot_io-v2-bloaty-output }} + ``` +
+ **Updated: _${{ steps.bt.outputs.timestamp }}_** PR_COMMENT_BODY_EOF @@ -253,7 +295,9 @@ jobs: steps.fc.outputs.comment-id != '' || fromJSON(env.V2).changed || fromJSON(env.V5X).changed || - fromJSON(env.V6X).changed + fromJSON(env.V6X).changed || + fromJSON(env.IO).changed || + fromJSON(env.CUBEPILOT_IO).changed uses: actions/upload-artifact@v7 with: name: pr-comment diff --git a/Tools/ci/firmware_size.py b/Tools/ci/firmware_size.py index a1482aaf178..fcc8a935af9 100644 --- a/Tools/ci/firmware_size.py +++ b/Tools/ci/firmware_size.py @@ -2,11 +2,13 @@ """Compare flash image and static RAM usage in bare-metal firmware ELFs.""" import argparse +import hashlib import json import os from pathlib import Path import re import subprocess +import tempfile # The commit hash compiled into px4_firmware_version_binary() changes its code # size, so identical sources on two commits can differ by up to 16 B. @@ -60,6 +62,14 @@ def memory_usage(elf: Path) -> dict[str, int]: return {"flash": image_end - image_start, "ram": ram, "image_start": image_start} +def image_digest(elf: Path) -> str: + """SHA-256 of the flash image, the bytes objcopy -O binary writes.""" + with tempfile.TemporaryDirectory() as directory: + binary = Path(directory) / "image.bin" + subprocess.run(["arm-none-eabi-objcopy", "-O", "binary", str(elf), str(binary)], check=True) + return hashlib.sha256(binary.read_bytes()).hexdigest() + + def ld_number(text: str) -> int: scale = {"K": 1 << 10, "M": 1 << 20}.get(text[-1].upper(), 1) return int(text[:-1] if scale > 1 else text, 0) * scale @@ -108,8 +118,9 @@ def format_usage(used: int, capacity: int) -> str: return f"{used / capacity:.2%}" -def summarize(before: dict[str, int], after: dict[str, int], capacity: dict[str, int]) -> dict: - return { +def summarize(before: dict[str, int], after: dict[str, int], capacity: dict[str, int], + image_changed: bool | None = None) -> dict: + summary = { "flash": format_change(before["flash"], after["flash"]), "flash_used": format_usage(after["flash"], capacity["flash"]), "ram": format_change(before["ram"], after["ram"]), @@ -117,6 +128,12 @@ def summarize(before: dict[str, int], after: dict[str, int], capacity: dict[str, "changed": any(abs(after[key] - before[key]) >= MIN_REPORTED_DELTA for key in ("flash", "ram")), } + if image_changed is not None: + # A reproducible image is worth a comment whenever its bytes change, + # whatever that does to its size. + summary["image"] = "🔴 changed" if image_changed else "unchanged" + summary["changed"] = summary["changed"] or image_changed + return summary def main(): @@ -124,11 +141,17 @@ def main(): parser.add_argument("--before", type=Path, required=True) parser.add_argument("--after", type=Path, required=True) parser.add_argument("--linker-script", type=Path, required=True) + parser.add_argument("--reproducible-image", action="store_true", + help="the image holds nothing that varies between builds of the same " + "sources, such as a commit hash: also report whether its bytes changed") args = parser.parse_args() before = memory_usage(args.before) after = memory_usage(args.after) capacity = memory_capacity(args.linker_script, after["image_start"]) - print(json.dumps(summarize(before, after, capacity))) + image_changed = None + if args.reproducible_image: + image_changed = image_digest(args.before) != image_digest(args.after) + print(json.dumps(summarize(before, after, capacity, image_changed))) if __name__ == "__main__": diff --git a/Tools/ci/test_firmware_size.py b/Tools/ci/test_firmware_size.py index efde280cb91..ab9d318916d 100644 --- a/Tools/ci/test_firmware_size.py +++ b/Tools/ci/test_firmware_size.py @@ -37,13 +37,13 @@ SECTIONS """) def build(self, name="firmware", *, text=32, data=16, bss=32, - ramfunc=16, reserved=16, debug=16, linker_flags=()): + ramfunc=16, reserved=16, debug=16, fill=1, linker_flags=()): source = self.root / f"{name}.s" obj = source.with_suffix(".o") elf = source.with_suffix(".elf") source.write_text(f""" .section .text,"ax" -.space {text}, 1 +.space {text}, {fill} .section .ramfunc,"ax" .space {ramfunc}, 2 .section .data,"aw" @@ -144,18 +144,33 @@ SECTIONS self.assertTrue(format_change(10000, 10000 + delta).startswith( f"{expected}{delta:+,} B")) + def report(self, before, after, *flags): + output = subprocess.check_output([ + sys.executable, str(Path(__file__).with_name("firmware_size.py").resolve()), + "--before", str(before), "--after", str(after), "--linker-script", str(self.linker), *flags, + ], cwd=self.root, text=True) + return json.loads(output) + def test_cli_from_outside_checkout(self): before = self.build("before") after = self.build("after", bss=96) - output = subprocess.check_output([ - sys.executable, str(Path(__file__).with_name("firmware_size.py").resolve()), - "--before", str(before), "--after", str(after), "--linker-script", str(self.linker), - ], cwd=self.root, text=True) - self.assertEqual(json.loads(output), { + self.assertEqual(self.report(before, after), { "flash": "+0 B (+0.00%)", "flash_used": "0.00%", "ram": "+64 B (+80.00%)", "ram_used": "0.03%", "changed": True, }) + def test_reproducible_image_reports_changed_bytes_of_the_same_size(self): + # The px4io firmware is flashed to the IO by every FMU that carries it, + # so a change of its bytes has to surface even when no size moves. + # Debug information is not part of the image and must not count. + before = self.build("before") + for name, change, image, changed in (("same", {"debug": 4096}, "unchanged", False), + ("patched", {"fill": 9}, "🔴 changed", True)): + with self.subTest(name): + result = self.report(before, self.build(name, **change), "--reproducible-image") + self.assertEqual((result["image"], result["changed"], result["flash"]), + (image, changed, "+0 B (+0.00%)")) + if __name__ == "__main__": unittest.main()