Files
bfe-core1106-sdk/sim/benches/sim_bench.rs
T
BFE EngineeringandClaude Opus 4.8 4ee4dfcf88 review: fix review findings (correctness, hardening, doc-accuracy)
First recursive code-review-harness pass over the authored SDK code (sim/,
config-lint, flowgen, drivers/{relays,freshness}, build/, ci.yml). Four parallel
dimension reviewers; all findings at every severity corrected per workspace rule.

Correctness / reliability:
- freshness.c min_budget_ms: use a `seen` flag, not `best==0`, as the empty
  sentinel — a zero-tolerance (max_stale_ms==0) binding was silently widened to a
  looser neighbour's budget. Regression test added; still 66/66 MC/DC.
- config-lint parse_reserved_ranges: match `reg` as a whole property token (ident
  boundary before, `=` after) so `reg-names` / a `region-*` label no longer
  mis-parses into a bogus reserved range.
- config-lint loader check: fail closed — flag any loader with a LOAD_ADDR that is
  not a known-safe boot component, instead of only known MCU names, so a future
  coprocessor ("Rtos"/"Bl32") can't slip past the 0x40000-brick gate.
- build-kernel.sh: sha256 verification is now mandatory (refuse to build if the pin
  is missing) and the mktemp scratch tree is removed on exit (trap), while a
  caller-provided WORK is left intact for CI artifact upload.

Test quality:
- freshness: added the age==max_stale boundary case and a clock-wraparound
  (now < last_ok) fail-safe-to-UNKNOWN test.
- relays: unsetenv(WARDEN_GPIO_ROOT) at main() so the NULL-env arm is hermetic.

Security / CI:
- ci.yml: top-level `permissions: contents: read` (badges overrides to write);
  pin taiki-e/install-action to commit SHA (v2.86.7).

Maintainability / docs:
- drivers/enforce-mcdc.sh: one shared, name-derived gate replaces the two
  copy-pasted per-driver scripts; Makefiles call ../../enforce-mcdc.sh.
- docs/architecture.md: §3/§4/§6/§7 rewritten to match reality — NPU/RGA models,
  config-lint, and the relays+freshness MC/DC harnesses are done; kernel §6 now
  reflects the 5.10->6.18.46 forward-port (ADR-0001), not the superseded plan44/6.6.
- README: status blurb + layout table corrected (kernel/, .github/; stale ci/ and
  patches/ descriptions fixed). hpmcu "8 tests" -> 7 in docs.
- freshness.{c,h}: ADR reference points at flare-edge ADR-0004 (warden-sdk's
  ADR-0004 is the CI runner — number collision).
- normalize rustfmt drift across sim/ + config-lint.

All green: sim 37 tests, config-lint 8 tests, both drivers 100% MC/DC (relays
40/40, freshness 66/66), clippy clean under -D warnings, gitleaks clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017wB8KB3MMQztRDXCMCkPrf
2026-08-25 15:48:52 -06:00

88 lines
2.5 KiB
Rust

//! Micro-benchmarks for the warden-sim hardware models.
//!
//! Dependency-free (`harness = false`): a fixed-iteration timing loop, so the sim
//! crate keeps zero deps and CI can capture a stable ns/op number per model with no
//! criterion tree to compile. Human-readable timings go to stdout; one JSON line per
//! benchmark goes to stderr for CI trend capture (`bench: … ns_per_op …`).
//!
//! Run: `cargo bench` (or `cargo run --release --bench sim_bench`).
use std::time::Instant;
use warden_sim::{CruSim, HpmcuSim, MemBus, ModbusSlave, Rect, RgaSim, SimBus, Surface};
fn bench<F: FnMut()>(name: &str, iters: u64, mut f: F) {
for _ in 0..(iters / 10).max(1) {
f(); // warm up
}
let t = Instant::now();
for _ in 0..iters {
f();
}
let ns = t.elapsed().as_nanos() as f64 / iters as f64;
println!("{name:<24} {ns:>9.1} ns/op ({iters} iters)");
eprintln!("{{\"bench\":\"{name}\",\"ns_per_op\":{ns:.1},\"iters\":{iters}}}");
}
fn main() {
const N: u64 = 1_000_000;
// HPMCU watchdog: one tick (mailbox poke + two peeks + state machine).
{
let mut h = HpmcuSim::new(SimBus::new(), 0, 0);
let mut now = 0u64;
bench("hpmcu_tick", N, || {
now += 1;
h.tick(now);
});
}
// CRU reset ladder: one poll.
{
let mut c = CruSim::new(SimBus::new());
let mut now = 0u64;
bench("cru_poll", N, || {
now += 1;
let _ = c.poll(now);
});
}
// Modbus RTU: a read-holding-registers request -> response round trip.
{
let mut s = ModbusSlave::new(1, 16, 16);
s.set_holding(0, 0x1234);
let req = warden_sim::modbus::request(1, &[0x03, 0x00, 0x00, 0x00, 0x04]);
bench("modbus_read_holding", N, || {
let _ = s.handle_frame(&req);
});
}
// RGA: one improcess dispatch record (+ clear, so the log stays bounded).
{
let mut r = RgaSim::new();
let sf = Surface {
width: 720,
height: 720,
format: 0,
};
let rc = Rect {
x: 0,
y: 0,
w: 720,
h: 720,
};
bench("rga_improcess", N, || {
let _ = r.improcess(sf, sf, rc, rc);
r.clear();
});
}
// MemBus: a poke + peek round trip (the /dev/mem seam's hot path).
{
let bus = SimBus::new();
bench("membus_poke_peek", N, || {
bus.poke32(0x100, 0xdead_beef);
let _ = bus.peek32(0x100);
});
}
}