+ }
+ }
+ }
+}
+/**
+ * @brief Executes the actors in actors_to_run.
+ *
+ * The actors in actors_to_run are run (in parallel if possible). On exit, actors_to_run is empty, and actors_that_ran
+ * contains the list of actors that just ran. The two lists are swapped so, be careful when using them before and after
+ * a call to this function.
+ */
+void EngineImpl::run_all_actors()
+{
+ instance_->get_context_factory()->run_all();
+
+ actors_to_run_.swap(actors_that_ran_);
+ actors_to_run_.clear();
+}
+
+actor::ActorImpl* EngineImpl::get_actor_by_pid(aid_t pid)
+{
+ auto item = actor_list_.find(pid);
+ if (item != actor_list_.end())
+ return item->second;
+
+ // Search the trash
+ for (auto& a : actors_to_destroy_)
+ if (a.get_pid() == pid)
+ return &a;
+ return nullptr; // Not found, even in the trash
+}
+
+/** Execute all the tasks that are queued, e.g. `.then()` callbacks of futures. */
+bool EngineImpl::execute_tasks()
+{
+ if (tasks.empty())
+ return false;
+
+ std::vector<xbt::Task<void()>> tasksTemp;
+ do {
+ // We don't want the callbacks to modify the vector we are iterating over:
+ tasks.swap(tasksTemp);
+
+ // Execute all the queued tasks:
+ for (auto& task : tasksTemp)
+ task();
+
+ tasksTemp.clear();
+ } while (not tasks.empty());
+
+ return true;
+}
+
+void EngineImpl::remove_daemon(actor::ActorImpl* actor)
+{
+ auto it = daemons_.find(actor);
+ xbt_assert(it != daemons_.end(), "The dying daemon is not a daemon after all. Please report that bug.");
+ daemons_.erase(it);
+}
+
+void EngineImpl::add_actor_to_run_list_no_check(actor::ActorImpl* actor)
+{
+ XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), actor->get_host()->get_cname());
+ actors_to_run_.push_back(actor);
+}
+
+void EngineImpl::add_actor_to_run_list(actor::ActorImpl* actor)
+{
+ if (std::find(begin(actors_to_run_), end(actors_to_run_), actor) != end(actors_to_run_)) {
+ XBT_DEBUG("Actor %s is already in the to_run list", actor->get_cname());
+ } else {
+ XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), actor->get_host()->get_cname());
+ actors_to_run_.push_back(actor);
+ }
+}
+void EngineImpl::empty_trash()
+{
+ while (not actors_to_destroy_.empty()) {
+ actor::ActorImpl* actor = &actors_to_destroy_.front();
+ actors_to_destroy_.pop_front();
+ XBT_DEBUG("Getting rid of %s (refcount: %d)", actor->get_cname(), actor->get_refcount());
+ intrusive_ptr_release(actor);
+ }
+#if SIMGRID_HAVE_MC
+ xbt_dynar_reset(dead_actors_vector_);
+#endif
+}
+
+void EngineImpl::display_all_actor_status() const
+{
+ XBT_INFO("%zu actors are still running, waiting for something.", actor_list_.size());
+ /* List the actors and their state */
+ XBT_INFO("Legend of the following listing: \"Actor <pid> (<name>@<host>): <status>\"");
+ for (auto const& kv : actor_list_) {
+ const actor::ActorImpl* actor = kv.second;
+
+ if (actor->waiting_synchro_) {
+ const char* synchro_description = "unknown";
+
+ if (boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(actor->waiting_synchro_) != nullptr)
+ synchro_description = "execution";
+
+ if (boost::dynamic_pointer_cast<kernel::activity::CommImpl>(actor->waiting_synchro_) != nullptr)
+ synchro_description = "communication";
+
+ if (boost::dynamic_pointer_cast<kernel::activity::SleepImpl>(actor->waiting_synchro_) != nullptr)
+ synchro_description = "sleeping";
+
+ if (boost::dynamic_pointer_cast<kernel::activity::RawImpl>(actor->waiting_synchro_) != nullptr)
+ synchro_description = "synchronization";
+
+ if (boost::dynamic_pointer_cast<kernel::activity::IoImpl>(actor->waiting_synchro_) != nullptr)
+ synchro_description = "I/O";
+
+ XBT_INFO("Actor %ld (%s@%s): waiting for %s activity %#zx (%s) in state %s to finish", actor->get_pid(),
+ actor->get_cname(), actor->get_host()->get_cname(), synchro_description,
+ (xbt_log_no_loc ? (size_t)0xDEADBEEF : (size_t)actor->waiting_synchro_.get()),
+ actor->waiting_synchro_->get_cname(), actor->waiting_synchro_->get_state_str());
+ } else {
+ XBT_INFO("Actor %ld (%s@%s) simcall %s", actor->get_pid(), actor->get_cname(), actor->get_host()->get_cname(),
+ SIMIX_simcall_name(actor->simcall_));
+ }
+ }
+}
+
+void EngineImpl::presolve() const
+{
+ XBT_DEBUG("Consume all trace events occurring before the starting time.");
+ double next_event_date;
+ while ((next_event_date = profile::future_evt_set.next_date()) != -1.0) {
+ if (next_event_date > NOW)
+ break;
+
+ double value = -1.0;
+ resource::Resource* resource = nullptr;
+ while (auto* event = profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) {
+ if (value >= 0)
+ resource->apply_event(event, value);
+ }
+ }
+
+ XBT_DEBUG("Set every models in the right state by updating them to 0.");
+ for (auto const& model : models_)
+ model->update_actions_state(NOW, 0.0);
+}
+
+double EngineImpl::solve(double max_date) const
+{
+ double time_delta = -1.0; /* duration */
+ double value = -1.0;
+ resource::Resource* resource = nullptr;
+
+ if (max_date != -1.0) {
+ xbt_assert(max_date >= NOW, "You asked to simulate up to %f, but that's in the past already", max_date);
+
+ time_delta = max_date - NOW;
+ }
+
+ XBT_DEBUG("Looking for next event in all models");
+ for (auto model : models_) {
+ if (not model->next_occurring_event_is_idempotent()) {
+ continue;
+ }
+ double next_event = model->next_occurring_event(NOW);
+ if ((time_delta < 0.0 || next_event < time_delta) && next_event >= 0.0) {
+ time_delta = next_event;
+ }
+ }
+
+ XBT_DEBUG("Min for resources (remember that NS3 don't update that value): %f", time_delta);
+
+ XBT_DEBUG("Looking for next trace event");
+
+ while (true) { // Handle next occurring events until none remains
+ double next_event_date = profile::future_evt_set.next_date();
+ XBT_DEBUG("Next TRACE event: %f", next_event_date);
+
+ for (auto model : models_) {
+ /* Skip all idempotent models, they were already treated above
+ * NS3 is the one to handled here */
+ if (model->next_occurring_event_is_idempotent())
+ continue;
+
+ if (next_event_date != -1.0) {
+ time_delta = std::min(next_event_date - NOW, time_delta);
+ } else {
+ time_delta = std::max(next_event_date - NOW, time_delta); // Get the positive component
+ }
+
+ XBT_DEBUG("Run the NS3 network at most %fs", time_delta);
+ // run until min or next flow
+ double model_next_action_end = model->next_occurring_event(time_delta);
+
+ XBT_DEBUG("Min for network : %f", model_next_action_end);
+ if (model_next_action_end >= 0.0)
+ time_delta = model_next_action_end;
+ }
+
+ if (next_event_date < 0.0 || (next_event_date > NOW + time_delta)) {
+ // next event may have already occurred or will after the next resource change, then bail out
+ XBT_DEBUG("no next usable TRACE event. Stop searching for it");
+ break;