Skip to content

Commit

Permalink
Add ability to run hosts in random order (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
progwriter authored Apr 24, 2024
1 parent 8b99772 commit 766108f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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

0 comments on commit 766108f

Please sign in to comment.