Skip to content

Add ability to run hosts in random order #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2024
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
8 changes: 8 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl Builder {
self
}

/// Enables running of nodes in random order. This allows exploration
/// of extra state space in multi-node simulations where race conditions
/// may arise based on message send/receive order.
pub fn enable_random_order(&mut self) -> &mut Self {
self.config.random_node_order = true;
self
}

/// Build a simulation with the settings from the builder.
///
/// This will use default rng with entropy from the device running.
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub(crate) struct Config {

/// Enables tokio IO driver
pub(crate) enable_tokio_io: bool,

/// Enables running of host/client code in random order at each
/// simulation step
pub(crate) random_node_order: bool,
}

/// Configures link behavior.
Expand Down Expand Up @@ -91,6 +95,7 @@ impl Default for Config {
tcp_capacity: 64,
udp_capacity: 64,
enable_tokio_io: false,
random_node_order: false,
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/sim.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rand::seq::SliceRandom;
use std::cell::RefCell;
use std::future::Future;
use std::net::IpAddr;
Expand Down Expand Up @@ -347,13 +348,18 @@ impl<'a> Sim<'a> {
// Tick each host runtimes with running software. If the software
// completes, extract the result and return early if an error is
// encountered.
for (&addr, rt) in self

let mut running: Vec<_> = self
.rts
.iter_mut()
.filter(|(_, rt)| rt.is_software_running())
{
let _span_guard = tracing::span!(Level::INFO, "node", name = &*rt.nodename).entered();
.collect();
if self.config.random_node_order {
running.shuffle(&mut self.world.borrow_mut().rng);
}

for (&addr, rt) in running {
let _span_guard = tracing::span!(Level::INFO, "node", name = &*rt.nodename,).entered();
{
let mut world = self.world.borrow_mut();
// We need to move deliverable messages off the network and
Expand Down