There is not much documentation on how simulated time works (and the runtime in general). I have gathered these facts from the source code and would like to open a PR with all of these documented. I wanted to ask if they are correct before doing that.
- A task can be spawned either via
madsim::task, madsim::runtime::NodeHandle::spawn, or madsim::runtime::Runtime::block_on.
- tasks spawned via
madsim::runtime::Runtime::block_on all have the same node ID, and it is distinct from node IDs belonging to an actual NodeHandle.
- tasks spawned via
madsim::task belong to the same node from which madsim::task was used. Using it outside the runtime panics.
- Each task belongs to exactly one node (possibly the
block_on one)
- All nodes share a common clock
- within a task,
std::time functions return simulation time. This is achieved by overriding functions usually provided by libc.
- The runtime is single threaded, all task polling happens within the
block_on call.
- If the future passed to
block_on completes while there are still nodes running, those nodes remain in the runtime. They may continue to make progress during a future call to block_on.
- Tasks that are ready are polled in a pseudo-random order
- Time never advances during the polling of a task
- Time advances by a small pseudo-random amount after each time a task is polled (currently 50-100 ns)
- If no tasks are ready, but some tasks are sleeping, time advances until the earliest sleep deadline plus some small amount (currently 50ns)
There is not much documentation on how simulated time works (and the runtime in general). I have gathered these facts from the source code and would like to open a PR with all of these documented. I wanted to ask if they are correct before doing that.
madsim::task,madsim::runtime::NodeHandle::spawn, ormadsim::runtime::Runtime::block_on.madsim::runtime::Runtime::block_onall have the same node ID, and it is distinct from node IDs belonging to an actualNodeHandle.madsim::taskbelong to the same node from whichmadsim::taskwas used. Using it outside the runtime panics.block_onone)std::timefunctions return simulation time. This is achieved by overriding functions usually provided by libc.block_oncall.block_oncompletes while there are still nodes running, those nodes remain in the runtime. They may continue to make progress during a future call toblock_on.