Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docs/shortcomings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
20 changes: 19 additions & 1 deletion internal/firmware/emulated/gdbstub.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
}
Expand Down
19 changes: 19 additions & 0 deletions internal/firmware/emulated/gdbstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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")
}
}
5 changes: 5 additions & 0 deletions internal/firmware/emulated/renode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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")
Expand Down
11 changes: 11 additions & 0 deletions tools/renode/peripherals/VirtualSX1262.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -112,6 +113,16 @@ public void Connect()
{
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"));
}
}
}

Expand Down
106 changes: 94 additions & 12 deletions tools/renode/peripherals/VirtualSX1262Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private void Serve(object opaque)
}
}
stream = null;
Release();
}

private void Handle(byte kind, byte[] payload)
Expand Down Expand Up @@ -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. 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
Expand Down Expand Up @@ -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)
Expand All @@ -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, which also
// decides the script: a seeded machine, its idle time skipped.
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;
Expand All @@ -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;
Expand All @@ -364,6 +446,6 @@ public void Dispose()
private TcpClient client;
private NetworkStream stream;
private Thread thread;
private uint simMs;
private volatile uint simMs;
}
}