Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
get rid of SIMIX_clean
authorSUTER Frederic <frederic.suter@cc.in2p3.fr>
Thu, 27 May 2021 12:35:31 +0000 (14:35 +0200)
committerSUTER Frederic <frederic.suter@cc.in2p3.fr>
Thu, 27 May 2021 12:35:31 +0000 (14:35 +0200)
src/kernel/EngineImpl.cpp
src/kernel/EngineImpl.hpp
src/s4u/s4u_Engine.cpp
src/simix/smx_global.cpp
src/simix/smx_private.hpp

index ab02ceb..2b9f234 100644 (file)
@@ -13,6 +13,7 @@
 #include "simgrid/sg_config.hpp"
 #include "src/include/surf/surf.hpp" //get_clock() and surf_solve()
 #include "src/kernel/resource/DiskImpl.hpp"
+#include "src/kernel/resource/profile/Profile.hpp"
 #include "src/mc/mc_record.hpp"
 #include "src/mc/mc_replay.hpp"
 #include "src/simix/smx_private.hpp"
@@ -24,17 +25,67 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(ker_engine, "Logging specific to Engine (kernel)");
 
 namespace simgrid {
 namespace kernel {
+EngineImpl* EngineImpl::instance_ = nullptr; /* That singleton is awful too. */
 
 config::Flag<double> cfg_breakpoint{"debug/breakpoint",
                                     "When non-negative, raise a SIGTRAP after given (simulated) time", -1.0};
-EngineImpl::~EngineImpl()
+void EngineImpl::shutdown()
 {
+  static bool already_cleaned_up = false;
+  if (already_cleaned_up)
+    return; // to avoid double cleaning by java and C
+  already_cleaned_up = true;
+  if (not EngineImpl::instance_) {
+    simix_global->destroy_maestro();
+    simix_global->destroy_context_factory();
+    return; // Nothing more to shutdown
+  }
+  XBT_DEBUG("EngineImpl::shutdown() called. Simulation's over.");
+
+  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.");
+  }
+
+#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
+
+  /* Kill all actors (but maestro) */
+  simix_global->get_maestro()->kill_all();
+  instance_->run_all_actors();
+  instance_->empty_trash();
+
+  /* Let's free maestro now */
+  simix_global->destroy_maestro();
+
+  /* Finish context module and SURF */
+  simix_global->destroy_context_factory();
 
   while (not timer::kernel_timers().empty()) {
     delete timer::kernel_timers().top().second;
     timer::kernel_timers().pop();
   }
 
+  tmgr_finalize();
+  sg_platf_exit();
+
+  simgrid::s4u::Engine::shutdown();
+
+  simix_global = nullptr;
+}
+
+EngineImpl::~EngineImpl()
+{
   /* Since hosts_ is a std::map, the hosts are destroyed in the lexicographic order, which ensures that the output is
    * reproducible.
    */
index a89924b..3a32bc9 100644 (file)
@@ -67,14 +67,20 @@ class EngineImpl {
   std::vector<xbt::Task<void()>> tasksTemp;
 
   std::mutex mutex_;
+  static EngineImpl* instance_;
+
   friend s4u::Engine;
 
 public:
   EngineImpl() = default;
 
+  /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
+#ifndef DOXYGEN
   EngineImpl(const EngineImpl&) = delete;
   EngineImpl& operator=(const EngineImpl&) = delete;
   virtual ~EngineImpl();
+  static void shutdown();
+#endif
 
   void load_deployment(const std::string& file) const;
   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
@@ -93,6 +99,7 @@ public:
   const std::vector<resource::Model*>& get_all_models() const { return models_; }
 
   static EngineImpl* get_instance() { return simgrid::s4u::Engine::get_instance()->pimpl; }
+
   actor::ActorCodeFactory get_function(const std::string& name)
   {
     auto res = registered_functions.find(name);
index 9898b81..8b07f49 100644 (file)
@@ -35,12 +35,13 @@ xbt::signal<void()> Engine::on_simulation_end;
 xbt::signal<void(double)> Engine::on_time_advance;
 xbt::signal<void(void)> Engine::on_deadlock;
 
-Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
+Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
 
 void Engine::initialize(int* argc, char** argv)
 {
   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
   Engine::instance_ = this;
+  kernel::EngineImpl::instance_ = pimpl;
   instr::init();
   SIMIX_global_init(argc, argv);
 }
index 82e38cc..8c0868c 100644 (file)
@@ -183,56 +183,7 @@ void SIMIX_global_init(int* argc, char** argv)
   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
 
   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
-    atexit(SIMIX_clean);
-}
-
-/**
- * @ingroup SIMIX_API
- * @brief Clean the SIMIX simulation
- *
- * This functions remove the memory used by SIMIX
- */
-void SIMIX_clean()
-{
-  static bool smx_cleaned = false;
-  if (smx_cleaned)
-    return; // to avoid double cleaning by java and C
-
-  smx_cleaned = true;
-  XBT_DEBUG("SIMIX_clean called. Simulation's over.");
-  auto* engine = simgrid::kernel::EngineImpl::get_instance();
-  if (engine->has_actors_to_run() && simgrid::s4u::Engine::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.");
-  }
-
-#if HAVE_SMPI
-  if (not engine->get_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
-
-  /* Kill all processes (but maestro) */
-  simix_global->get_maestro()->kill_all();
-  engine->run_all_actors();
-  engine->empty_trash();
-
-  /* Let's free maestro now */
-  simix_global->destroy_maestro();
-
-  /* Finish context module and SURF */
-  simix_global->destroy_context_factory();
-
-  surf_exit();
-
-  simix_global = nullptr;
+    atexit(simgrid::kernel::EngineImpl::shutdown);
 }
 
 /**
index 6945526..6b9d345 100644 (file)
@@ -43,6 +43,4 @@ public:
 
 XBT_PUBLIC_DATA std::unique_ptr<simgrid::simix::Global> simix_global;
 
-XBT_PUBLIC void SIMIX_clean();
-
 #endif