Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / simix / smx_global.cpp
index 618ee85..5e14abb 100644 (file)
 
 #include "simgrid/sg_config.hpp"
 #include "src/kernel/EngineImpl.hpp"
-#include "src/kernel/activity/ExecImpl.hpp"
-#include "src/kernel/activity/IoImpl.hpp"
-#include "src/kernel/activity/MailboxImpl.hpp"
-#include "src/kernel/activity/SleepImpl.hpp"
-#include "src/kernel/activity/SynchroRaw.hpp"
 #include "src/mc/mc_record.hpp"
 #include "src/mc/mc_replay.hpp"
 #include "src/simix/smx_private.hpp"
@@ -34,8 +29,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, "Logging specific to SIMIX
 
 std::unique_ptr<simgrid::simix::Global> simix_global;
 
-void (*SMPI_switch_data_segment)(simgrid::s4u::ActorPtr) = nullptr;
-
 namespace simgrid {
 namespace simix {
 config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor status at exit", true};
@@ -43,7 +36,7 @@ config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor sta
 xbt_dynar_t simix_global_get_actors_addr()
 {
 #if SIMGRID_HAVE_MC
-  return simix_global->actors_vector;
+  return kernel::EngineImpl::get_instance()->get_actors_vector();
 #else
   xbt_die("This function is intended to be used when compiling with MC");
 #endif
@@ -51,7 +44,7 @@ xbt_dynar_t simix_global_get_actors_addr()
 xbt_dynar_t simix_global_get_dead_actors_addr()
 {
 #if SIMGRID_HAVE_MC
-  return simix_global->dead_actors_vector;
+  return kernel::EngineImpl::get_instance()->get_dead_actors_vector();
 #else
   xbt_die("This function is intended to be used when compiling with MC");
 #endif
@@ -65,7 +58,7 @@ XBT_ATTRIB_NORETURN static void inthandler(int)
   if (simgrid::simix::cfg_verbose_exit) {
     XBT_INFO("CTRL-C pressed. The current status will be displayed before exit (disable that behavior with option "
              "'debug/verbose-exit').");
-    simix_global->display_all_actor_status();
+    simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
   } else {
     XBT_INFO("CTRL-C pressed, exiting. Hiding the current process status since 'debug/verbose-exit' is set to false.");
   }
@@ -80,7 +73,7 @@ static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
             "Access violation or Bus error detected.\n"
             "This probably comes from a programming error in your code, or from a stack\n"
             "overflow. If you are certain of your code, try increasing the stack size\n"
-            "   --cfg=contexts/stack-size=XXX (current size is %u KiB).\n"
+            "   --cfg=contexts/stack-size:XXX (current size is %u KiB).\n"
             "\n"
             "If it does not help, this may have one of the following causes:\n"
             "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n"
@@ -145,120 +138,6 @@ static void install_segvhandler()
 
 #endif /* _WIN32 */
 
-/********************************* SIMIX **************************************/
-namespace simgrid {
-namespace simix {
-
-/** Execute all the tasks that are queued, e.g. `.then()` callbacks of futures. */
-bool Global::execute_tasks()
-{
-  xbt_assert(tasksTemp.empty());
-
-  if (tasks.empty())
-    return false;
-
-  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 Global::empty_trash()
-{
-  while (not actors_to_destroy.empty()) {
-    kernel::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
-}
-/**
- * @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 Global::run_all_actors()
-{
-  simix_global->context_factory->run_all();
-
-  actors_to_run.swap(actors_that_ran);
-  actors_to_run.clear();
-}
-
-/** Wake up all actors waiting for a Surf action to finish */
-void Global::wake_all_waiting_actors() const
-{
-  for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models()) {
-    XBT_DEBUG("Handling the failed actions (if any)");
-    while (auto* action = model->extract_failed_action()) {
-      XBT_DEBUG("   Handling Action %p", action);
-      if (action->get_activity() != nullptr)
-        kernel::activity::ActivityImplPtr(action->get_activity())->post();
-    }
-    XBT_DEBUG("Handling the terminated actions (if any)");
-    while (auto* action = model->extract_done_action()) {
-      XBT_DEBUG("   Handling Action %p", action);
-      if (action->get_activity() == nullptr)
-        XBT_DEBUG("probably vcpu's action %p, skip", action);
-      else
-        kernel::activity::ActivityImplPtr(action->get_activity())->post();
-    }
-  }
-}
-
-void Global::display_all_actor_status() const
-{
-  XBT_INFO("%zu actors are still running, waiting for something.", process_list.size());
-  /*  List the actors and their state */
-  XBT_INFO("Legend of the following listing: \"Actor <pid> (<name>@<host>): <status>\"");
-  for (auto const& kv : process_list) {
-    kernel::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 %d 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(), (int)actor->waiting_synchro_->state_);
-    } 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_));
-    }
-  }
-}
-
-} // namespace simix
-} // namespace simgrid
-
 static simgrid::kernel::actor::ActorCode maestro_code;
 void SIMIX_set_maestro(void (*code)(void*), void* data)
 {
@@ -271,104 +150,38 @@ void SIMIX_set_maestro(void (*code)(void*), void* data)
 
 void SIMIX_global_init(int* argc, char** argv)
 {
-  if (simix_global == nullptr) {
-    simix_global = std::make_unique<simgrid::simix::Global>();
+  if (simix_global != nullptr)
+    return;
+
+  simix_global = std::make_unique<simgrid::simix::Global>();
 
 #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();
+  // 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
 
-    surf_init(argc, argv); /* Initialize SURF structures */
+  surf_init(argc, argv); /* Initialize SURF structures */
 
-    simix_global->maestro_ = nullptr;
-    SIMIX_context_mod_init();
+  SIMIX_context_mod_init();
 
-    // Either create a new context with maestro or create
-    // a context object with the current context maestro):
-    simgrid::kernel::actor::create_maestro(maestro_code);
+  // Either create a new context with maestro or create
+  // a context object with the current context maestro):
+  simgrid::kernel::actor::create_maestro(maestro_code);
 
-    /* Prepare to display some more info when dying on Ctrl-C pressing */
-    std::signal(SIGINT, inthandler);
+  /* Prepare to display some more info when dying on Ctrl-C pressing */
+  std::signal(SIGINT, inthandler);
 
 #ifndef _WIN32
-    install_segvhandler();
+  install_segvhandler();
 #endif
-    /* register a function to be called by SURF after the environment creation */
-    sg_platf_init();
-    simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
-  }
+  /* register a function to be called by SURF after the environment creation */
+  sg_platf_init();
+  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.");
-  if (not simix_global->actors_to_run.empty() && SIMIX_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 simix_global->process_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->maestro_->kill_all();
-  simix_global->run_all_actors();
-  simix_global->empty_trash();
-
-  /* Exit the SIMIX network module */
-  SIMIX_mailbox_exit();
-
-  while (not simgrid::kernel::timer::kernel_timers().empty()) {
-    delete simgrid::kernel::timer::kernel_timers().top().second;
-    simgrid::kernel::timer::kernel_timers().pop();
-  }
-  /* Free the remaining data structures */
-  simix_global->actors_to_run.clear();
-  simix_global->actors_that_ran.clear();
-  simix_global->actors_to_destroy.clear();
-  simix_global->process_list.clear();
-
-#if SIMGRID_HAVE_MC
-  xbt_dynar_free(&simix_global->actors_vector);
-  xbt_dynar_free(&simix_global->dead_actors_vector);
-#endif
-
-  /* Let's free maestro now */
-  delete simix_global->maestro_;
-  simix_global->maestro_ = nullptr;
-
-  /* Finish context module and SURF */
-  SIMIX_context_mod_exit();
-
-  surf_exit();
-
-  simix_global = nullptr;
+    atexit(simgrid::kernel::EngineImpl::shutdown);
 }
 
 /**
@@ -377,13 +190,9 @@ void SIMIX_clean()
  *
  * @return Return the clock.
  */
-double SIMIX_get_clock()
+double SIMIX_get_clock() // XBT_ATTRIB_DEPRECATED_v332
 {
-  if (MC_is_active() || MC_record_replay_is_active()) {
-    return MC_process_clock_get(SIMIX_process_self());
-  } else {
-    return surf_get_clock();
-  }
+  return simgrid::s4u::Engine::get_clock();
 }
 
 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
@@ -391,37 +200,10 @@ void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
   simgrid::kernel::EngineImpl::get_instance()->run();
 }
 
-double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
-{
-  return simgrid::kernel::timer::Timer::next();
-}
-
-smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
-{
-  return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
-}
-
-/** @brief cancels a timer that was added earlier */
-void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
-{
-  timer->remove();
-}
-
-/** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
-double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
-{
-  return timer ? timer->get_date() : 0.0;
-}
-
-void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
-{
-  simix_global->display_all_actor_status();
-}
-
 int SIMIX_is_maestro()
 {
   if (simix_global == nullptr) // SimDag
     return true;
   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
-  return self == nullptr || self == simix_global->maestro_;
+  return self == nullptr || simix_global->is_maestro(self);
 }