From 7f25b4336c762b14554681051ebfc3215ca46b70 Mon Sep 17 00:00:00 2001 From: R0ck Date: Sun, 25 Oct 2026 01:27:27 +0100 Subject: [PATCH 1/2] renode: run a board in lockstep with the engine, as an option With MESHBENCH_RENODE_LOCKSTEP=1 a clock entry in the machine's virtual time ticks the chip each millisecond and holds the CPU until the engine grants the next one; idle time is skipped and Renode's random numbers are seeded from the node. Two idle runs of two RAK_4631 repeaters gave identical event logs. Console input still reaches the UART on the wall clock, so emulated runs stay reported as not reproducible. For #774. --- CHANGELOG.md | 6 + docs/shortcomings.md | 18 +++ internal/firmware/emulated/gdbstub.go | 20 +++- internal/firmware/emulated/gdbstub_test.go | 19 ++++ internal/firmware/emulated/renode.go | 5 + tools/renode/peripherals/VirtualSX1262.cs | 9 ++ .../renode/peripherals/VirtualSX1262Engine.cs | 106 ++++++++++++++++-- 7 files changed, 170 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f0f1bc9..d1fe3ddf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,12 @@ had changed in them - which is the gap this file exists to close. ### Added +- **Renode boards can run in lockstep with the engine.** With + `MESHBENCH_RENODE_LOCKSTEP=1`, an nRF52 board's CPU and chip advance one + millisecond of virtual time per millisecond the engine asks for, with the + machine's random numbers seeded from the node. Console input still arrives + on the wall clock, so runs with emulated boards are still reported as not + reproducible; see `docs/shortcomings.md` ยง3.2a. - **Debug a node's firmware while the run goes on.** `node.debug`, and "Debug this node" on a node's row menu, say how to attach gdb or VS Code to a running node. A native node gets its process, binary and whether it has diff --git a/docs/shortcomings.md b/docs/shortcomings.md index 93222782..0b33adc8 100644 --- a/docs/shortcomings.md +++ b/docs/shortcomings.md @@ -475,6 +475,24 @@ GPS feed emits a sentence a real second rather than a simulated one, and a native node is handed a seeded identity on its command line while an emulated one generates its own on first boot. +**Renode lockstep, a first step (#774).** With `MESHBENCH_RENODE_LOCKSTEP=1` +an nRF52 board under Renode no longer runs free. A clock entry in the +machine's own virtual time ticks the chip once a millisecond and holds the CPU +there until the engine asks for the next one. Renode's virtual time comes from +the instructions executed and idle time is skipped, so the CPU and the chip +step with the engine. The machine's random numbers, which the RNG peripheral +answers and MeshCore draws its jitter from, are seeded from the node. +Measured: two RAK_4631 repeaters, two idle runs, gave byte-identical event logs +and identical chip counters. + +It does not yet make the run reproducible, and `sim.state` still says so. +Console input reaches Renode's UART over a socket, on the wall clock, so the +lines provisioning types at boot and any `console.type` land wherever the +machine happens to be. A third run with every core busy diverged for that +reason. It also costs about five times real time for two boards on this +machine. Gating the console by virtual time, and the QEMU half (`-icount` and +a hold in the fork's SX1262 device), are what remain. + **Consequence.** A run containing an emulated node can be compared with nothing but itself, so a firmware A/B with an emulated arm is not measuring what it appears to. That is now said rather than left to be discovered: `sim.state` diff --git a/internal/firmware/emulated/gdbstub.go b/internal/firmware/emulated/gdbstub.go index bb3bac2a..fb713913 100644 --- a/internal/firmware/emulated/gdbstub.go +++ b/internal/firmware/emulated/gdbstub.go @@ -1,6 +1,9 @@ package emulated -import "fmt" +import ( + "fmt" + "os" +) // qemuGDBArgs starts QEMU's GDB stub, on loopback only: a stub is the whole // machine, memory and registers, to anything that reaches the port. @@ -22,6 +25,21 @@ func (e *EmulatedNode) qemuGDBArgs() []string { // reset, the machine is not started here at all: the stub's autostart starts // it when a debugger connects and continues. func (e *EmulatedNode) renodeStart() string { + if os.Getenv(EnvRenodeLockstep) == "1" { + // Idle time skipped rather than waited out on the host clock: in + // lockstep the engine is the clock, and a sleeping CPU waiting for + // wall time would hold every tick to real time. And Renode's own + // random numbers - what the nRF52's RNG peripheral answers, which + // MeshCore draws its jitter from - seeded from the node rather than + // afresh each run. + seed := noiseSeedFor(e.RunSeed, e.NodeName, e.BoardName, e.Position) + return fmt.Sprintf("emulation SetSeed %d\nemulation SetAdvanceImmediately true\n%s", + int32(seed), e.renodeStartFree()) + } + return e.renodeStartFree() +} + +func (e *EmulatedNode) renodeStartFree() string { if e.GDBPort <= 0 { return "start\n" } diff --git a/internal/firmware/emulated/gdbstub_test.go b/internal/firmware/emulated/gdbstub_test.go index be71a6c3..2a6cbb59 100644 --- a/internal/firmware/emulated/gdbstub_test.go +++ b/internal/firmware/emulated/gdbstub_test.go @@ -45,3 +45,22 @@ func TestTheDebuggerFollowsTheArchitecture(t *testing.T) { } } } + +// In lockstep the machine's random numbers are seeded from the node and idle +// time is skipped, or the same tick would not run the same firmware (#774). +func TestALocksteppedRenodeScriptIsSeeded(t *testing.T) { + t.Setenv(EnvRenodeLockstep, "1") + a := (&EmulatedNode{NodeName: "a", RunSeed: 1}).renodeStart() + b := (&EmulatedNode{NodeName: "b", RunSeed: 1}).renodeStart() + for _, want := range []string{"emulation SetSeed ", "emulation SetAdvanceImmediately true\n", "start\n"} { + if !strings.Contains(a, want) { + t.Errorf("missing %q in:\n%s", want, a) + } + } + if a == b { + t.Error("two nodes share one random stream") + } + if a != (&EmulatedNode{NodeName: "a", RunSeed: 1}).renodeStart() { + t.Error("the same node is seeded differently twice") + } +} diff --git a/internal/firmware/emulated/renode.go b/internal/firmware/emulated/renode.go index cddf4308..350620ac 100644 --- a/internal/firmware/emulated/renode.go +++ b/internal/firmware/emulated/renode.go @@ -124,6 +124,11 @@ machine LoadPlatformDescription @%[1]s/usbd.repl return script, nil } +// EnvRenodeLockstep runs a Renode board from the engine's ticks, a millisecond +// of virtual time per millisecond asked for, rather than letting it run free +// on the host clock (#774). +const EnvRenodeLockstep = "MESHBENCH_RENODE_LOCKSTEP" + // startRenode boots this node's script, and holds its console open. func (e *EmulatedNode) startRenode(ctx context.Context, bridge string) error { renodeBin, err := lookupTool("renode") diff --git a/tools/renode/peripherals/VirtualSX1262.cs b/tools/renode/peripherals/VirtualSX1262.cs index 9982b26d..f4337db5 100644 --- a/tools/renode/peripherals/VirtualSX1262.cs +++ b/tools/renode/peripherals/VirtualSX1262.cs @@ -25,6 +25,7 @@ using Antmicro.Renode.Core; using Antmicro.Renode.Logging; using Antmicro.Renode.Peripherals.SPI; +using Antmicro.Renode.Time; namespace Antmicro.Renode.Peripherals.Radio { @@ -108,6 +109,14 @@ public void Connect() engine = new VirtualSX1262Engine(lib, chip, chipLock, SettleIrq, () => femLevel, m => this.Log(LogLevel.Warning, m)); + if(VirtualSX1262Engine.Lockstep) + { + // Once a millisecond of the machine's own virtual time; see + // VirtualSX1262Engine.OnVirtualMillisecond. + var tick = engine; + machine.ClockSource.AddClockEntry(new ClockEntry(1, 1000, + () => tick.OnVirtualMillisecond(), this, "meshbench-lockstep")); + } if(engine.Connect(engineHost, enginePort)) { this.Log(LogLevel.Info, "joined the engine at {0}:{1}", diff --git a/tools/renode/peripherals/VirtualSX1262Engine.cs b/tools/renode/peripherals/VirtualSX1262Engine.cs index 59490817..db2dae70 100644 --- a/tools/renode/peripherals/VirtualSX1262Engine.cs +++ b/tools/renode/peripherals/VirtualSX1262Engine.cs @@ -98,6 +98,7 @@ private void Serve(object opaque) } } stream = null; + Release(); } private void Handle(byte kind, byte[] payload) @@ -156,29 +157,97 @@ private void Handle(byte kind, byte[] payload) private void Advance(uint toMs) { - lock(chipLock) + if(Lockstep) + { + AdvanceInLockstep(toMs); + } + else { - // A millisecond at a time, as a native node is stepped. - // Stepping rather than jumping is what keeps the chip's own - // timeouts behaving: a preamble flag that should clear after - // 66 ms does not, if time arrives in 500 ms lumps. - while(simMs < toMs) + lock(chipLock) { - simMs++; + // A millisecond at a time, as a native node is stepped. + // Stepping rather than jumping is what keeps the chip's own + // timeouts behaving: a preamble flag that should clear after + // 66 ms does not, if time arrives in 500 ms lumps. + while(simMs < toMs) + { + simMs++; + lib.Tick(chip, simMs); + DrainTx(); + } lib.Tick(chip, simMs); DrainTx(); + SendStats(); } - lib.Tick(chip, simMs); - DrainTx(); - SendStats(); + settle(); } - settle(); var ack = new byte[4]; Put32(ack, 0, toMs); Send(Ack, ack, ack.Length); } + // Lockstep (#774). The machine runs, but a clock entry in its own + // virtual time calls OnVirtualMillisecond once a millisecond, on the + // CPU's thread, and that is where the chip is ticked and where the CPU + // waits until the engine has asked for the next millisecond. So the + // CPU and the chip never run ahead of the engine, and since Renode's + // virtual time comes from the instructions executed rather than the + // host clock, the same tick runs the same firmware on an idle machine + // and a loaded one. Advance only grants time and waits for the CPU to + // reach it. + private void AdvanceInLockstep(uint toMs) + { + lock(gate) + { + granted = Math.Max(granted, toMs); + Monitor.PulseAll(gate); + while(simMs < toMs && !released) + { + Monitor.Wait(gate, 1000); + } + } + lock(chipLock) + { + SendStats(); + } + } + + // OnVirtualMillisecond is the clock entry's handler; see + // AdvanceInLockstep. + public void OnVirtualMillisecond() + { + lock(chipLock) + { + simMs++; + lib.Tick(chip, simMs); + DrainTx(); + } + settle(); + lock(gate) + { + Monitor.PulseAll(gate); + // A timeout and a re-check rather than a bare wait, so a + // machine being shut down is not held for ever by an engine + // that has gone. + while(simMs >= granted && !released) + { + Monitor.Wait(gate, 1000); + } + } + } + + // Release lets the machine run free: the engine has gone, or the + // machine is shutting down. + private void Release() + { + lock(gate) + { + released = true; + Monitor.PulseAll(gate); + } + } + // Anything the firmware handed its radio goes out to the engine now. // // A transmission reaches the channel immediately and is not immediately @@ -328,6 +397,7 @@ private static void Put16(byte[] b, int at, ushort v) public void Dispose() { + Release(); var wire = stream; stream = null; if(wire != null) @@ -337,6 +407,12 @@ public void Dispose() client = null; } + // Lockstep runs the machine from the tick instead of letting it run + // free; see Advance. Set from MESHBENCH_RENODE_LOCKSTEP, and the + // script then never starts the machine itself. + public static readonly bool Lockstep = + Environment.GetEnvironmentVariable("MESHBENCH_RENODE_LOCKSTEP") == "1"; + private const byte Frame = 0x01; private const byte Tick = 0x02; private const byte Ack = 0x03; @@ -349,6 +425,12 @@ public void Dispose() // capabilities, is left off - this host sends no failed frames. private const int StatsLength = 47; + // gate guards granted, how far the engine has asked the machine to + // run in lockstep, and released, set once nothing will ask again. + private readonly object gate = new object(); + private uint granted; + private bool released; + private readonly VirtualSX1262Lib lib; private readonly IntPtr chip; private readonly object chipLock; @@ -364,6 +446,6 @@ public void Dispose() private TcpClient client; private NetworkStream stream; private Thread thread; - private uint simMs; + private volatile uint simMs; } } From 43d92058379fc16604e4b9fe346a8dedf92643dd Mon Sep 17 00:00:00 2001 From: R0ck Date: Fri, 30 Oct 2026 17:00:37 +0000 Subject: [PATCH 2/2] renode lockstep: only hold the CPU once the engine is joined, so a board with no engine still runs --- internal/firmware/emulated/gdbstub_test.go | 2 +- internal/firmware/emulated/renode.go | 2 +- tools/renode/peripherals/VirtualSX1262.cs | 18 ++++++++++-------- .../renode/peripherals/VirtualSX1262Engine.cs | 6 +++--- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/firmware/emulated/gdbstub_test.go b/internal/firmware/emulated/gdbstub_test.go index 2a6cbb59..e288562c 100644 --- a/internal/firmware/emulated/gdbstub_test.go +++ b/internal/firmware/emulated/gdbstub_test.go @@ -47,7 +47,7 @@ func TestTheDebuggerFollowsTheArchitecture(t *testing.T) { } // In lockstep the machine's random numbers are seeded from the node and idle -// time is skipped, or the same tick would not run the same firmware (#774). +// time is skipped, or the same tick would not run the same firmware. func TestALocksteppedRenodeScriptIsSeeded(t *testing.T) { t.Setenv(EnvRenodeLockstep, "1") a := (&EmulatedNode{NodeName: "a", RunSeed: 1}).renodeStart() diff --git a/internal/firmware/emulated/renode.go b/internal/firmware/emulated/renode.go index 350620ac..d95cffe2 100644 --- a/internal/firmware/emulated/renode.go +++ b/internal/firmware/emulated/renode.go @@ -126,7 +126,7 @@ machine LoadPlatformDescription @%[1]s/usbd.repl // EnvRenodeLockstep runs a Renode board from the engine's ticks, a millisecond // of virtual time per millisecond asked for, rather than letting it run free -// on the host clock (#774). +// on the host clock. const EnvRenodeLockstep = "MESHBENCH_RENODE_LOCKSTEP" // startRenode boots this node's script, and holds its console open. diff --git a/tools/renode/peripherals/VirtualSX1262.cs b/tools/renode/peripherals/VirtualSX1262.cs index f4337db5..a932470d 100644 --- a/tools/renode/peripherals/VirtualSX1262.cs +++ b/tools/renode/peripherals/VirtualSX1262.cs @@ -109,18 +109,20 @@ public void Connect() engine = new VirtualSX1262Engine(lib, chip, chipLock, SettleIrq, () => femLevel, m => this.Log(LogLevel.Warning, m)); - if(VirtualSX1262Engine.Lockstep) - { - // Once a millisecond of the machine's own virtual time; see - // VirtualSX1262Engine.OnVirtualMillisecond. - var tick = engine; - machine.ClockSource.AddClockEntry(new ClockEntry(1, 1000, - () => tick.OnVirtualMillisecond(), this, "meshbench-lockstep")); - } if(engine.Connect(engineHost, enginePort)) { this.Log(LogLevel.Info, "joined the engine at {0}:{1}", engineHost, enginePort); + // Only once joined: with no engine to grant time, the CPU + // would wait at its first virtual millisecond for ever. + if(VirtualSX1262Engine.Lockstep) + { + // Once a millisecond of the machine's own virtual time; see + // VirtualSX1262Engine.OnVirtualMillisecond. + var tick = engine; + machine.ClockSource.AddClockEntry(new ClockEntry(1, 1000, + () => tick.OnVirtualMillisecond(), this, "meshbench-lockstep")); + } } } diff --git a/tools/renode/peripherals/VirtualSX1262Engine.cs b/tools/renode/peripherals/VirtualSX1262Engine.cs index db2dae70..1625c132 100644 --- a/tools/renode/peripherals/VirtualSX1262Engine.cs +++ b/tools/renode/peripherals/VirtualSX1262Engine.cs @@ -187,7 +187,7 @@ private void Advance(uint toMs) Send(Ack, ack, ack.Length); } - // Lockstep (#774). The machine runs, but a clock entry in its own + // Lockstep. The machine runs, but a clock entry in its own // virtual time calls OnVirtualMillisecond once a millisecond, on the // CPU's thread, and that is where the chip is ticked and where the CPU // waits until the engine has asked for the next millisecond. So the @@ -408,8 +408,8 @@ public void Dispose() } // Lockstep runs the machine from the tick instead of letting it run - // free; see Advance. Set from MESHBENCH_RENODE_LOCKSTEP, and the - // script then never starts the machine itself. + // free; see Advance. Set from MESHBENCH_RENODE_LOCKSTEP, which also + // decides the script: a seeded machine, its idle time skipped. public static readonly bool Lockstep = Environment.GetEnvironmentVariable("MESHBENCH_RENODE_LOCKSTEP") == "1";