Files
JIANT/tests/target_budget.py
T
Claude 7180629b0b 0.9.12 (draft): safe by default on FM-1s that crash; the sound's flash layout pinned (0.9.10 crackle candidate)
Stability (users' random "FELUCCA/JIANT CRASH" screens since the dual-core 0.6.x builds):
- MENU > SYSTEM > CPU 2 CORES / 1 CORE and CPU CLOCK 360 / 320 MHZ (settings byte favorites.factory[15][17],
  editor ids 23, 24), from the next start. A crash or a watchdog hang while both cores ran, or the second core
  given up (misread / no answer / fault), turns 2 CORES off for good (was: until power-off) and says
  "2 CORES OFF AFTER A CRASH - MENU > SYSTEM > CPU" / "CORE 2 FAILED: 1 CORE"; ABOUT: CORES 1 (AFTER A CRASH).
- 320 MHz: curist's fm1-clk320-probe switch (MIT, hal/fm1_clk320.h, unchanged; LICENSES/MIT-fm1-clk320.txt), at
  boot before the scan, core 1, audio and USB; a reset during it: no failed boot, no retry until power-off.
- Crash screen: version, the faulting core and how many ran, a short code (cpu_safe.c crash_code) and the five
  numbers labelled (VEC PC EMU DBG RETS, same order as every older screen). The last crash (or watchdog hang) is
  logged as 32 characters in the settings record (SLICE's retired favourites row 13): MENU > ABOUT > LAST CRASH,
  the editor's settings backup, console `crash`. tools/crash_decode.py maps a photo's numbers to functions (ELF).
- Fixed race: every flash erase/write/read turns XIP off with IRQs off; the 48 kHz USB resampler job (0.8.9)
  runs on core 1 past the audio ISR, so a core-1 fault then would fetch the vectors from a disabled flash.
  irq_save now parks core 1 first (fm1_mc_park, FL_PARK hook).
- One core: the load governor's budget at 55 % (was 65 %) so peaks have room without core 1.
- MENU > AUDIO > LOAD (was SYSTEM's; a bottom tile holds 7 rows), RAMTEXT 1 KiB larger (from the pool's spare).

Crackle since 0.9.10 (owner's unit: 0.9.9 clean, 0.9.10/0.9.11 up to 94 % load on every output, host counts equal):
- 0.9.9 -> 0.9.10 changed no sound code and left the RAM code byte for byte, but the +6 KB of new drawings came
  before the engines in the image: every engine render (flash/XIP, shared cache) moved +6178 B (analog +9414)
  while the effects, master and ISR stayed put. Now everything an interrupt reaches (347 functions, 244 tables)
  is placed first in name order (build.py audio_sections, app.ld .text.a.* / .rodata.a.*): UI changes can no
  longer move it (verified: this commit's +2.3 KB of UI moved the renders by 48 B, sound-side edits). Same size.
- TIMER5's USB refill (0.9.10, every 10 kHz tick, also nested in the render): no SIE poll for 0.6 ms after a
  packet is queued (it cannot have left yet): ~4 polls a ms instead of ~10. tests/target_budget.py watches
  ep4_tx / ep4_fill.

Image 578540 B (+2348 over 0.9.11). ./build.sh && tests/run_tests.sh: ALL HOST TESTS PASSED (new
tests/cpu_safe_test.c; settings, ui, editor tests extended).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-10-18 20:40:33 +00:00

139 lines
8.0 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
"""Target-side cost estimate of the hot DSP code, from the pi32v2 objdump listing tools/build.py writes
(build/felucca.dis). For each engine's render function and the audio ISR: the instructions
inside loops (every address range closed by a backward branch: the per-sample loops and what they
contain), and the hardware divides and calls in them. cost = the loop instructions, each weighted 4 per
level of nesting (an inner loop runs several times per sample) and 1 + 8 for a divide (many cycles). A static count, not a cycle count: it changes only when the compiled code changes,
so it is exact run to run, and it catches a render loop that grew (more work per sample, a new divide,
something not inlined any more). Compared with tests/target_budget.txt at +10 %. The host
CPU check (regress.c) is the primary one; this one sees the target compiler's code.
tests/target_budget.py [DIS [BUDGET]] BUDGET_UPDATE=1 rewrites BUDGET"""
import os
import re
import sys
FUNCS = ["analog_render", "digital_render", "digital_render_legacy", "digital_render_custom", "phase_render", "lofi_render", "formant_render",
"wheel_render", "wheel_block", "analog_ext", "analog_morph", "lofi_byte_render", # (JIANT 0.4: SYNC RING SAW3, BYTE)
"drum_render", "noise_render", "fm6_render", "fm6_op_run", "fm6_op_fb",
"dx_run", "dx_layer", # DRUM-X (drumx_voice.c): a hit, osc + noise; (0.6.49) LAYER
"drum_side_add", "drum_mix", "drum_bus", # (JIANT 0.6.48) DRUM-X IMAGE and SENDs; (0.7.9) the kit's curve
"slicer_bus", # (JIANT 0.6.3) the SLICER, one on a bus
"fm1_alnk0_irq", "fm1_timer5_irq", # the audio ISR; TIMER5: the key / LED scan (hal/fm1_input.h)
"ep4_tx", "ep4_fill", # (0.9.12) TIMER5's USB audio refill, nested in the render (usb.c:
# the poll, and the packet once a ms: its copy loops)
"uac_rs_run", # the USB audio input at 48 kHz: the 44.1 -> 48 resampler (usb.c;
# 0.8.9: in RAM, on core 1 with CORES 2; was uac_tap48)
"click_render", # the metronome's click (click.c), while it sounds
"mod_begin", "mod_voice", "mod_end", # the modulation matrix (mod.c), called when active
"perf_begin", "perf_mute", "perf_pre", "perf_block", "perf_master", # the FX layer (perform.c), when busy
"rev_room", "rev_spring", "dist_amp", "dx_hands_step", "drw_dust", # the reverb bus (fx.c): REVERB TYPE ROOM / SPRING
"dly_lo", # (0.8.4) the delay at half / a quarter rate (fx.c)
"animal_render", # (JIANT 0.8.3) ANIMAL (eng_animal.c): its five sources (one loop a voice)
"formant_word_render", "formant_block", # (JIANT 0.8.5) VOICE singing words (eng_formant.c, sing.c)
"djf_block"] # (0.9.1) the master DJ FILTER (fx.c), while it is off the middle
# built only with FELUCCA_FM4=1 (DIGITAL, src/eng_digital.c; not in the default build, so not in BUDGET): absent,
# they are skipped; present, checked against these (their budget lines until the engine was retired in 1.0)
OPTIONAL = {"digital_render": 12, "digital_render_legacy": 333, "digital_render_custom": 558,
"uac_rs_run": 558} # (absent from a FELUCCA_UAC_48K=0 or FELUCCA_UAC=0 build; 0.8.9: four taps a pass, so
# the static count is up, ~185 instructions an output frame run against ~230)
TOL = 0.10 # exact (no noise): small edits pass, a grown render loop does not
DIV_W = 8 # a divide weighs 1 + 8 instructions
NEST = 4 # an instruction in a loop inside a loop weighs 4, two deep 16, ...
MAXD = 4
dis = sys.argv[1] if len(sys.argv) > 1 else "build/felucca.dis"
budget = sys.argv[2] if len(sys.argv) > 2 else "tests/target_budget.txt"
LABEL = re.compile(r"^([A-Za-z_][A-Za-z_0-9.]*):$")
INSN = re.compile(r"^\s*([0-9a-f]+):\s+((?:[0-9a-f]{2} )+)\s*(.*)$")
TARGET = re.compile(r"goto -?\d+ <[^>]*: ([0-9a-f]+) >")
def functions(path):
"""{name: [(addr, text)]}: a function runs to the next label not starting with '.' (the compiler's
jump-table labels are inside functions); jump-table data ('< n : 0x.. >') is left out"""
out, cur = {}, None
with open(path) as f:
for line in f:
m = LABEL.match(line.strip())
if m:
name = m.group(1)
if not name.startswith("."):
cur = out.setdefault(name, []) if name in FUNCS else None
continue
m = INSN.match(line)
if cur is not None and m and not m.group(3).lstrip().startswith("<"):
cur.append((int(m.group(1), 16), m.group(3).strip()))
return out
def cost(insns):
"""loops: one span per loop head (the farthest backward branch to it); an instruction inside d
spans weighs NEST ** (d - 1) (an inner loop runs several times per pass of the outer one)"""
lo = insns[0][0]
heads = {}
for a, t in insns:
m = TARGET.search(t)
if m and lo <= int(m.group(1), 16) < a:
h = int(m.group(1), 16)
heads[h] = max(heads.get(h, a), a)
n = divs = calls = 0
w = 0
for a, t in insns:
d = min(sum(1 for h, e in heads.items() if h <= a <= e), MAXD)
if not d:
continue
k = NEST ** (d - 1)
n += 1
if re.search(r"= r\d+ / r\d+", t):
divs += 1
k *= 1 + DIV_W
calls += t.startswith("call")
w += k
return {"insns": len(insns), "loop": n, "div": divs, "call": calls, "cost": w}
def main():
if not os.path.exists(dis):
print(f"target: skip ({dis} missing: run ./build.sh)")
return 0
fns = functions(dis)
res = {n: cost(fns[n]) for n in FUNCS if fns.get(n)}
missing = [n for n in FUNCS if n not in res and n not in OPTIONAL]
base = {}
if os.path.exists(budget):
for line in open(budget):
p = line.split()
if len(p) >= 2 and not line.startswith("#"):
base[p[0]] = int(p[1])
if os.environ.get("BUDGET_UPDATE"):
with open(budget, "w") as f:
f.write("# FELUCCA target cost budget (tests/target_budget.py): instructions in the loops of each\n"
f"# function in build/felucca.dis, x{NEST} per nesting level, divides x{1 + DIV_W}. The check allows "
f"+{TOL * 100:.0f} %.\n# Rewritten by BUDGET_UPDATE=1.\n")
for n, r in res.items():
if n not in OPTIONAL:
f.write(f"{n} {r['cost']}\n")
print(f"target: budget {budget} rewritten ({len(res)} functions)")
fail = 0
for n in missing:
print(f"target: FAIL {n} not found in {dis} (renamed? inlined? update FUNCS)")
fail += 1
for n, r in res.items():
b = base.get(n, OPTIONAL.get(n))
state = "no budget (BUDGET_UPDATE=1 adds it)" if b is None else "ok"
if b is not None and not os.environ.get("BUDGET_UPDATE"):
if r["cost"] > b * (1 + TOL):
state = f"OVER BUDGET (+{(r['cost'] / b - 1) * 100:.0f} %, limit +{TOL * 100:.0f} %)"
fail += 1
elif r["cost"] < b * (1 - TOL):
state = f"note: {(r['cost'] / b - 1) * 100:.0f} % (BUDGET_UPDATE=1 to keep it)"
print(f"target: {n:15s} {r['insns']:5d} instructions, {r['loop']:4d} in loops, {r['div']} divides, "
f"{r['call']:2d} calls there: cost {r['cost']:5d} (budget {b if b is not None else '-'}) {state}")
return 1 if fail else 0
if __name__ == "__main__":
sys.exit(main())