Previously, we only exposed turmoil::elapsed which returned the elapsed virtual time for the currently executing host. If you step your simulation for some duration before registering a host then the host's elapsed time will be less than the overall sim elapsed time.
We didn't want to make a breaking API change for this, so opted to add a new function (turmoil::sim_elapsed). This function returns an Option so that the caller can gracefully handle failure (turmoil::elapsed will crash if called outside of an executing host, for example). This makes it easier to use sim_elapsed for things like logging elapsed time via the std tracing subscriber (there's an example of how to do this in the grpc example).
One sharp edge I ran into is that we sometimes do tracing in this crate itself (e.g. in Tcp::bind) which happens while we're holding a mutable borrow of the thread-local RefCell<World>. Since the tracing event causes us to call tracing::sim_elapsed, we have an attempted double mutable borrow. In these cases I just made sim_elapsed return None. It's a bit of a wart but I think it's okay. We also return None when sim_elapsed is called when there's no executing host, so I think the API returning Option feels right. We can internally fix the double borrow issue in future without a breaking change.
Previously, we only exposed
turmoil::elapsed
which returned the elapsed virtual time for the currently executing host. If you step your simulation for some duration before registering a host then the host's elapsed time will be less than the overall sim elapsed time.We didn't want to make a breaking API change for this, so opted to add a new function (
turmoil::sim_elapsed
). This function returns anOption
so that the caller can gracefully handle failure (turmoil::elapsed
will crash if called outside of an executing host, for example). This makes it easier to usesim_elapsed
for things like logging elapsed time via the std tracing subscriber (there's an example of how to do this in thegrpc
example).One sharp edge I ran into is that we sometimes do tracing in this crate itself (e.g. in
Tcp::bind
) which happens while we're holding a mutable borrow of the thread-localRefCell<World>
. Since the tracing event causes us to calltracing::sim_elapsed
, we have an attempted double mutable borrow. In these cases I just madesim_elapsed
returnNone
. It's a bit of a wart but I think it's okay. We also returnNone
whensim_elapsed
is called when there's no executing host, so I think the API returningOption
feels right. We can internally fix the double borrow issue in future without a breaking change.