Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Use std::function in timer and add overloads
[simgrid.git] / src / simix / smx_global.cpp
index e7759b9..bc31f71 100644 (file)
@@ -4,6 +4,9 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include <functional>
+
+#include <signal.h> /* Signal handling */
 #include <stdlib.h>
 #include "src/internal_config.h"
 
 #include "src/surf/storage_interface.hpp"
 #include "src/surf/xml/platf.hpp"
 #include "smx_private.h"
-#include "smx_private.hpp"
 #include "xbt/str.h"
 #include "xbt/ex.h"             /* ex_backtrace_display */
 #include "mc/mc.h"
 #include "src/mc/mc_replay.h"
 #include "simgrid/sg_config.h"
 
+#include "src/simix/SynchroExec.hpp"
+#include "src/simix/SynchroComm.hpp"
+#include "src/simix/SynchroSleep.hpp"
+#include "src/simix/SynchroIo.hpp"
+#include "src/simix/SynchroRaw.hpp"
+
 #if HAVE_MC
 #include "src/mc/mc_private.h"
 #include "src/mc/mc_protocol.h"
@@ -41,20 +49,16 @@ static xbt_heap_t simix_timers = NULL;
 
 /** @brief Timer datatype */
 typedef struct s_smx_timer {
-  double date;
-  void(* func)(void*);
-  void* args;
+  double date = 0.0;
+  std::function<void()> callback;
+
+  s_smx_timer() {}
+  s_smx_timer(double date, std::function<void()> callback)
+    : date(date), callback(std::move(callback)) {}
 } s_smx_timer_t;
 
 void (*SMPI_switch_data_segment)(int) = NULL;
 
-static void* SIMIX_synchro_mallocator_new_f(void);
-static void SIMIX_synchro_mallocator_free_f(void* synchro);
-static void SIMIX_synchro_mallocator_reset_f(void* synchro);
-
-/* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
-#include <signal.h>
-
 int _sg_do_verbose_exit = 1;
 static void inthandler(int ignored)
 {
@@ -144,8 +148,8 @@ static void install_segvhandler(void)
 }
 
 #endif /* _WIN32 */
-/********************************* SIMIX **************************************/
 
+/********************************* SIMIX **************************************/
 double SIMIX_timer_next(void)
 {
   return xbt_heap_size(simix_timers) > 0 ? xbt_heap_maxkey(simix_timers) : -1.0;
@@ -156,12 +160,6 @@ static void kill_process(smx_process_t process)
   SIMIX_process_kill(process, NULL);
 }
 
-static void SIMIX_storage_create_(smx_storage_t storage)
-{
-  const char* key = xbt_dict_get_elm_key(storage);
-  SIMIX_storage_create(key, storage, NULL);
-}
-
 static std::function<void()> maestro_code;
 
 namespace simgrid {
@@ -190,19 +188,15 @@ void SIMIX_set_maestro(void (*code)(void*), void* data)
 void SIMIX_global_init(int *argc, char **argv)
 {
 #if HAVE_MC
-  _sg_do_model_check = getenv(MC_ENV_VARIABLE) != NULL;
+  // The communication initialization is done ASAP.
+  // We need to communicate  initialization of the different layers to the model-checker.
+  simgrid::mc::Client::initialize();
 #endif
 
-  s_smx_process_t proc;
-
   if (!simix_global) {
     simix_global = xbt_new0(s_smx_global_t, 1);
 
-#ifdef TIME_BENCH_AMDAHL
-    simix_global->timer_seq = xbt_os_timer_new();
-    simix_global->timer_par = xbt_os_timer_new();
-    xbt_os_cputimer_start(simix_global->timer_seq);
-#endif
+    simgrid::simix::Process proc;
     simix_global->process_to_run = xbt_dynar_new(sizeof(smx_process_t), NULL);
     simix_global->process_that_ran = xbt_dynar_new(sizeof(smx_process_t), NULL);
     simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc, process_hookup));
@@ -214,9 +208,6 @@ void SIMIX_global_init(int *argc, char **argv)
     simix_global->create_process_function = SIMIX_process_create;
     simix_global->kill_process_function = kill_process;
     simix_global->cleanup_process_function = SIMIX_process_cleanup;
-    simix_global->synchro_mallocator = xbt_mallocator_new(65536,
-        SIMIX_synchro_mallocator_new_f, SIMIX_synchro_mallocator_free_f,
-        SIMIX_synchro_mallocator_reset_f);
     simix_global->mutex = xbt_os_mutex_init();
 
     surf_init(argc, argv);      /* Initialize SURF structures */
@@ -245,28 +236,24 @@ void SIMIX_global_init(int *argc, char **argv)
     SIMIX_HOST_LEVEL = simgrid::s4u::Host::extension_create(SIMIX_host_destroy);
 
     simgrid::surf::storageCreatedCallbacks.connect([](simgrid::surf::Storage* storage) {
-      const char* id = storage->getName();
-        // TODO, create sg_storage_by_name
-        sg_storage_t s = xbt_lib_get_elm_or_null(storage_lib, id);
-        xbt_assert(s != NULL, "Storage not found for name %s", id);
-        SIMIX_storage_create_(s);
-      });
+      const char* name = storage->getName();
+      // TODO, create sg_storage_by_name
+      sg_storage_t s = xbt_lib_get_elm_or_null(storage_lib, name);
+      xbt_assert(s != NULL, "Storage not found for name %s", name);
+
+      SIMIX_storage_create(name, s, NULL);
+    });
 
     SIMIX_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, SIMIX_storage_destroy);
   }
-  if (!simix_timers) {
-    simix_timers = xbt_heap_new(8, &free);
-  }
+  if (!simix_timers)
+    simix_timers = xbt_heap_new(8, [](void* p) {
+      delete static_cast<smx_timer_t>(p);
+    });
 
-  if (xbt_cfg_get_boolean("clean_atexit"))
+  if (xbt_cfg_get_boolean("clean-atexit"))
     atexit(SIMIX_clean);
 
-#if HAVE_MC
-  // The communication initialization is done ASAP.
-  // We need to communicate  initialization of the different layers to the model-checker.
-  simgrid::mc::Client::initialize();
-#endif
-
   if (_sg_cfg_exit_asap)
     exit(0);
 }
@@ -280,9 +267,6 @@ int smx_cleaned = 0;
  */
 void SIMIX_clean(void)
 {
-#ifdef TIME_BENCH_PER_SR
-  smx_ctx_raw_new_sr();
-#endif
   if (smx_cleaned) return; // to avoid double cleaning by java and C
   smx_cleaned = 1;
   XBT_DEBUG("SIMIX_clean called. Simulation's over.");
@@ -313,7 +297,7 @@ void SIMIX_clean(void)
   simix_global->mutex = NULL;
 
   /* Let's free maestro now */
-  SIMIX_context_free(simix_global->maestro_process->context);
+  delete simix_global->maestro_process->context;
   xbt_free(simix_global->maestro_process->running_ctx);
   xbt_free(simix_global->maestro_process);
   simix_global->maestro_process = NULL;
@@ -327,16 +311,6 @@ void SIMIX_clean(void)
 
   surf_exit();
 
-#ifdef TIME_BENCH_AMDAHL
-  xbt_os_cputimer_stop(simix_global->timer_seq);
-  XBT_INFO("Amdahl timing informations. Sequential time: %f; Parallel time: %f",
-           xbt_os_timer_elapsed(simix_global->timer_seq),
-           xbt_os_timer_elapsed(simix_global->timer_par));
-  xbt_os_timer_free(simix_global->timer_seq);
-  xbt_os_timer_free(simix_global->timer_par);
-#endif
-
-  xbt_mallocator_free(simix_global->synchro_mallocator);
   xbt_free(simix_global);
   simix_global = NULL;
 
@@ -393,23 +367,12 @@ void SIMIX_run(void)
   do {
     XBT_DEBUG("New Schedule Round; size(queue)=%lu",
         xbt_dynar_length(simix_global->process_to_run));
-#ifdef TIME_BENCH_PER_SR
-    smx_ctx_raw_new_sr();
-#endif
     while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
       XBT_DEBUG("New Sub-Schedule Round; size(queue)=%lu",
               xbt_dynar_length(simix_global->process_to_run));
 
       /* Run all processes that are ready to run, possibly in parallel */
-#ifdef TIME_BENCH_AMDAHL
-      xbt_os_cputimer_stop(simix_global->timer_seq);
-      xbt_os_cputimer_resume(simix_global->timer_par);
-#endif
       SIMIX_process_runall();
-#ifdef TIME_BENCH_AMDAHL
-      xbt_os_cputimer_stop(simix_global->timer_par);
-      xbt_os_cputimer_resume(simix_global->timer_seq);
-#endif
 
       /* Move all killer processes to the end of the list, because killing a process that have an ongoing simcall is a bad idea */
       xbt_dynar_three_way_partition(simix_global->process_that_ran, process_syscall_color);
@@ -507,9 +470,15 @@ void SIMIX_run(void)
        //FIXME: make the timers being real callbacks
        // (i.e. provide dispatchers that read and expand the args)
        timer = (smx_timer_t) xbt_heap_pop(simix_timers);
-       if (timer->func)
-         timer->func(timer->args);
-       xbt_free(timer);
+       if (timer->callback) {
+         try {
+           timer->callback();
+         }
+         catch(...) {
+           xbt_die("Exception throwed ouf of timer callback");
+         }
+       }
+       delete timer;
     }
 
     /* Wake up all processes waiting for a Surf action to finish */
@@ -566,14 +535,18 @@ void SIMIX_run(void)
  */
 smx_timer_t SIMIX_timer_set(double date, void (*function)(void*), void *arg)
 {
-  smx_timer_t timer = xbt_new0(s_smx_timer_t, 1);
+  smx_timer_t timer = new s_smx_timer_t(date, std::bind(function, arg));
+  xbt_heap_push(simix_timers, timer, date);
+  return timer;
+}
 
-  timer->date = date;
-  timer->func = function;
-  timer->args = arg;
+smx_timer_t SIMIX_timer_set(double date, std::function<void()> callback)
+{
+  smx_timer_t timer = new s_smx_timer_t(date, std::move(callback));
   xbt_heap_push(simix_timers, timer, date);
   return timer;
 }
+
 /** @brief cancels a timer that was added earlier */
 void SIMIX_timer_remove(smx_timer_t timer) {
   xbt_heap_rm_elm(simix_timers, timer, timer->date);
@@ -592,8 +565,7 @@ double SIMIX_timer_get_date(smx_timer_t timer) {
  * to call SIMIX_process_create().
  * \param function create process function
  */
-void SIMIX_function_register_process_create(smx_creation_func_t
-                                                       function)
+void SIMIX_function_register_process_create(smx_creation_func_t function)
 {
   simix_global->create_process_function = function;
 }
@@ -645,67 +617,45 @@ void SIMIX_display_process_status(void)
     if (process->waiting_synchro) {
 
       const char* synchro_description = "unknown";
-      switch (process->waiting_synchro->type) {
 
-      case SIMIX_SYNC_EXECUTE:
+      if (dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro) != nullptr)
         synchro_description = "execution";
-        break;
-
-      case SIMIX_SYNC_PARALLEL_EXECUTE:
-        synchro_description = "parallel execution";
-        break;
 
-      case SIMIX_SYNC_COMMUNICATE:
+      if (dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro) != nullptr)
         synchro_description = "communication";
-        break;
 
-      case SIMIX_SYNC_SLEEP:
+      if (dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro) != nullptr)
         synchro_description = "sleeping";
+
+      if (dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro) != nullptr)
+        synchro_description = "synchronization";
+
+      if (dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro) != nullptr)
+        synchro_description = "I/O";
+
+
+      /*
+        switch (process->waiting_synchro->type) {
+      case SIMIX_SYNC_PARALLEL_EXECUTE:
+        synchro_description = "parallel execution";
         break;
 
       case SIMIX_SYNC_JOIN:
         synchro_description = "joining";
         break;
+*/
 
-      case SIMIX_SYNC_SYNCHRO:
-        synchro_description = "synchronization";
-        break;
-
-      case SIMIX_SYNC_IO:
-        synchro_description = "I/O";
-        break;
-      }
       XBT_INFO("Process %lu (%s@%s): waiting for %s synchro %p (%s) in state %d to finish",
-          process->pid, process->name, sg_host_get_name(process->host),
+          process->pid, process->name.c_str(), sg_host_get_name(process->host),
           synchro_description, process->waiting_synchro,
           process->waiting_synchro->name, (int)process->waiting_synchro->state);
     }
     else {
-      XBT_INFO("Process %lu (%s@%s)", process->pid, process->name, sg_host_get_name(process->host));
+      XBT_INFO("Process %lu (%s@%s)", process->pid, process->name.c_str(), sg_host_get_name(process->host));
     }
   }
 }
 
-static void* SIMIX_synchro_mallocator_new_f(void) {
-  smx_synchro_t synchro = xbt_new(s_smx_synchro_t, 1);
-  synchro->simcalls = xbt_fifo_new();
-  return synchro;
-}
-
-static void SIMIX_synchro_mallocator_free_f(void* synchro) {
-  xbt_fifo_free(((smx_synchro_t) synchro)->simcalls);
-  xbt_free(synchro);
-}
-
-static void SIMIX_synchro_mallocator_reset_f(void* synchro) {
-
-  // we also recycle the simcall list
-  xbt_fifo_t fifo = ((smx_synchro_t) synchro)->simcalls;
-  xbt_fifo_reset(fifo);
-  memset(synchro, 0, sizeof(s_smx_synchro_t));
-  ((smx_synchro_t) synchro)->simcalls = fifo;
-}
-
 xbt_dict_t simcall_HANDLER_asr_get_properties(smx_simcall_t simcall, const char *name){
   return SIMIX_asr_get_properties(name);
 }