+ /* clear models before freeing handle, network models can use external callback defined in the handle */
+ models_prio_.clear();
+}
+
+void EngineImpl::initialize(int* argc, char** argv)
+{
+ xbt_assert(EngineImpl::instance_ == nullptr,
+ "It is currently forbidden to create more than one instance of kernel::EngineImpl");
+ EngineImpl::instance_ = this;
+#if SIMGRID_HAVE_MC
+ // The communication initialization is done ASAP, as we need to get some init parameters from the MC for different
+ // layers. But simix_global needs to be created, as we send the address of some of its fields to the MC that wants to
+ // read them directly.
+ simgrid::mc::AppSide::initialize();
+#endif
+
+ if (xbt_initialized == 0) {
+ xbt_init(argc, argv);
+
+ sg_config_init(argc, argv);
+ }
+
+ instance_->context_mod_init();
+
+ /* Prepare to display some more info when dying on Ctrl-C pressing */
+ std::signal(SIGINT, inthandler);
+
+#ifndef _WIN32
+ install_segvhandler();
+#endif
+
+ /* register a function to be called by SURF after the environment creation */
+ sg_platf_init();
+ s4u::Engine::on_platform_created_cb([this]() { this->presolve(); });
+
+ if (config::get_value<bool>("debug/clean-atexit"))
+ atexit(shutdown);
+}
+
+void EngineImpl::context_mod_init() const
+{
+ xbt_assert(not instance_->has_context_factory());
+
+#if HAVE_SMPI && defined(__NetBSD__)
+ smpi_init_options_internal(false);
+ std::string priv = config::get_value<std::string>("smpi/privatization");
+ if (context_factory_name == "thread" && (priv == "dlopen" || priv == "yes" || priv == "default" || priv == "1")) {
+ XBT_WARN("dlopen+thread broken on Apple and BSD. Switching to raw contexts.");
+ context_factory_name = "raw";
+ }
+#endif
+
+#if HAVE_SMPI && defined(__FreeBSD__)
+ smpi_init_options_internal(false);
+ if (context_factory_name == "thread" && config::get_value<std::string>("smpi/privatization") != "no") {
+ XBT_WARN("mmap broken on FreeBSD, but dlopen+thread broken too. Switching to dlopen+raw contexts.");
+ context_factory_name = "raw";
+ }
+#endif
+
+ /* select the context factory to use to create the contexts */
+ if (context::factory_initializer != nullptr) { // Give Java a chance to hijack the factory mechanism
+ instance_->set_context_factory(context::factory_initializer());
+ return;
+ }
+ /* use the factory specified by --cfg=contexts/factory:value */
+ for (auto const& factory : context_factories)
+ if (context_factory_name == factory.first) {
+ instance_->set_context_factory(factory.second());
+ break;
+ }
+
+ if (not instance_->has_context_factory()) {
+ XBT_ERROR("Invalid context factory specified. Valid factories on this machine:");
+#if HAVE_RAW_CONTEXTS
+ XBT_ERROR(" raw: high performance context factory implemented specifically for SimGrid");
+#else
+ XBT_ERROR(" (raw contexts were disabled at compilation time on this machine -- check configure logs for details)");
+#endif
+#if HAVE_UCONTEXT_CONTEXTS
+ XBT_ERROR(" ucontext: classical system V contexts (implemented with makecontext, swapcontext and friends)");
+#else
+ XBT_ERROR(" (ucontext was disabled at compilation time on this machine -- check configure logs for details)");
+#endif
+#if HAVE_BOOST_CONTEXTS
+ XBT_ERROR(" boost: this uses the boost libraries context implementation");
+#else
+ XBT_ERROR(" (boost was disabled at compilation time on this machine -- check configure logs for details. Did you "
+ "install the libboost-context-dev package?)");
+#endif
+ XBT_ERROR(" thread: slow portability layer using pthreads as provided by gcc");
+ xbt_die("Please use a valid factory.");
+ }
+}
+
+void EngineImpl::shutdown()
+{
+ if (EngineImpl::instance_ == nullptr)
+ return;
+ XBT_DEBUG("EngineImpl::shutdown() called. Simulation's over.");
+#if HAVE_SMPI
+ if (not instance_->actor_list_.empty()) {
+ if (smpi_process()->initialized()) {
+ xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
+ } else {
+ XBT_WARN("Process called exit when leaving - Skipping cleanups");
+ return;
+ }
+ }
+#endif
+
+ if (instance_->has_actors_to_run() && simgrid_get_clock() <= 0.0) {
+ XBT_CRITICAL(" ");
+ XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
+ XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
+ xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
+ }
+
+ /* Kill all actors (but maestro) */
+ instance_->maestro_->kill_all();
+ instance_->run_all_actors();
+ instance_->empty_trash();
+
+ /* Let's free maestro now */
+ delete instance_->maestro_;
+ instance_->maestro_ = nullptr;
+
+ /* Finish context module and SURF */
+ instance_->destroy_context_factory();
+
+ while (not timer::kernel_timers().empty()) {
+ delete timer::kernel_timers().top().second;
+ timer::kernel_timers().pop();
+ }
+
+ tmgr_finalize();
+ sg_platf_exit();
+
+ delete instance_;
+ instance_ = nullptr;
+}
+
+void EngineImpl::seal_platform() const
+{
+ /* sealing resources before run: links */
+ for (auto const& kv : links_)
+ kv.second->get_iface()->seal();
+ /* seal netzone root, recursively seal children netzones, hosts and disks */
+ netzone_root_->seal();
+}
+
+void EngineImpl::load_platform(const std::string& platf)
+{
+ double start = xbt_os_time();
+ if (boost::algorithm::ends_with(platf, ".so") or boost::algorithm::ends_with(platf, ".dylib")) {
+#ifdef _WIN32
+ xbt_die("loading platform through shared library isn't supported on windows");
+#else
+ void* handle = dlopen(platf.c_str(), RTLD_LAZY);
+ xbt_assert(handle, "Impossible to open platform file: %s", platf.c_str());
+ platf_handle_ = std::unique_ptr<void, std::function<int(void*)>>(handle, dlclose);
+ using load_fct_t = void (*)(const simgrid::s4u::Engine&);
+ auto callable = (load_fct_t)dlsym(platf_handle_.get(), "load_platform");
+ const char* dlsym_error = dlerror();
+ xbt_assert(not dlsym_error, "Error: %s", dlsym_error);
+ callable(*simgrid::s4u::Engine::get_instance());
+#endif /* _WIN32 */
+ } else {
+ parse_platform_file(platf);
+ }
+
+ double end = xbt_os_time();
+ XBT_DEBUG("PARSE TIME: %g", (end - start));