Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify the library initialization + deprecate 2 XBT functions
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 20 Feb 2023 14:46:02 +0000 (15:46 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 21 Feb 2023 14:18:36 +0000 (15:18 +0100)
Work toward making EngineImpl::initialize() the only entry point of
the library initialization. It's now impossible to initialize xbt
separately of EngineImpl.

It should help reducing the mess in that code, which results from the
many ways of initializing the library. simgrid-mc, unit-tests, smpi
and others each have their own way of initializing everything,
resulting in a spagetthi and fragile code. It's a bit of a waste to
initialize an EngineImpl even when you don't want to actually run a
simulation, but easier code is always better.

This commit also includes another one because I git amended locally by
error, sorry for the mess. The other commit was about the deprecation
of xbt_procname and xbt_getpid that were xbt functions relying on S4U.

23 files changed:
MANIFEST.in
include/simgrid/Exception.hpp
include/simgrid/actor.h
include/simgrid/engine.h
include/xbt.h
include/xbt/module.h
include/xbt/virtu.h
src/kernel/EngineImpl.cpp
src/kernel/resource/profile/StochasticDatedValue.cpp
src/s4u/s4u_Actor.cpp
src/s4u/s4u_Engine.cpp
src/simgrid/sg_config.cpp
src/xbt/backtrace.cpp
src/xbt/exception.cpp
src/xbt/xbt_log_layout_format.cpp
src/xbt/xbt_log_layout_simple.cpp
src/xbt/xbt_main.cpp
src/xbt/xbt_modinter.h
src/xbt/xbt_virtu.cpp [deleted file]
teshsuite/models/lmm_usage/lmm_usage.cpp
teshsuite/models/maxmin_bench/maxmin_bench.cpp
teshsuite/xbt/mmalloc/mmalloc_test.cpp
tools/cmake/DefinePackages.cmake

index 5c50b59..c920f5d 100644 (file)
@@ -2499,7 +2499,6 @@ include src/xbt/xbt_parse_units.cpp
 include src/xbt/xbt_replay.cpp
 include src/xbt/xbt_str.cpp
 include src/xbt/xbt_str_test.cpp
-include src/xbt/xbt_virtu.cpp
 include teshsuite/kernel/CMakeLists.txt
 include teshsuite/mc/CMakeLists.txt
 include teshsuite/models/CMakeLists.txt
index e580ea9..f4d151f 100644 (file)
@@ -51,7 +51,8 @@ public:
 
 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
 #define XBT_THROW_POINT                                                                                                \
-  ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid())
+  ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), sg_actor_self_get_name(),        \
+                             sg_actor_self_get_pid())
 
 class XBT_PUBLIC ImpossibleError : public std::logic_error {
 public:
index dcee507..fb34364 100644 (file)
@@ -72,6 +72,7 @@ XBT_PUBLIC void sg_actor_detach();
 XBT_PUBLIC sg_actor_t sg_actor_self();
 XBT_PUBLIC aid_t sg_actor_self_get_pid();
 XBT_PUBLIC aid_t sg_actor_self_get_ppid();
+/** Returns the name of the current actor (or "maestro" if maestro is running) */
 XBT_PUBLIC const char* sg_actor_self_get_name();
 XBT_PUBLIC void* sg_actor_self_get_data();
 XBT_PUBLIC void sg_actor_self_set_data(void* data);
index b4e8c08..d3ff02b 100644 (file)
@@ -11,9 +11,9 @@
 
 SG_BEGIN_DECL /* C interface */
 
-    /** Initialize the SimGrid engine, taking the command line parameters of your main function. */
-    XBT_PUBLIC void
-    simgrid_init(int* argc, char** argv);
+/** Initialize the SimGrid engine, taking the command line parameters of your main function. */
+XBT_PUBLIC void
+simgrid_init(int* argc, char** argv);
 
 /** Creates a new platform, including hosts, links, and the routing table.
  *
index deafed8..0f6e5b8 100644 (file)
@@ -17,8 +17,6 @@
 #include <xbt/asserts.h>
 #include <xbt/log.h>
 
-#include <xbt/module.h>
-
 #include <xbt/dynar.h>
 #include <xbt/dict.h>
 
index 43fe15d..ee9d548 100644 (file)
@@ -8,11 +8,20 @@
 #ifndef XBT_MODULE_H
 #define XBT_MODULE_H
 
-#include <xbt/misc.h>           /* XBT_PUBLIC */
+// avoid deprecation warning on include (remove entire file with XBT_ATTRIB_DEPRECATED_v337)
+#ifndef XBT_MODULE_H_NO_DEPRECATED_WARNING
+#warning xbt/module.h is deprecated and will be removed in v3.37.
+#endif
+
+#include <simgrid/engine.h>
+#include <xbt/base.h>
 
 SG_BEGIN_DECL
 
-XBT_PUBLIC void xbt_init(int* argc, char** argv);
+XBT_ATTRIB_DEPRECATED_v337("Please use simgrid_init(&argc, argv) instead") static void xbt_init(int* argc, char** argv)
+{
+  simgrid_init(argc, argv);
+}
 
 SG_END_DECL
 
index b1a6e04..136b305 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef XBT_VIRTU_H
 #define XBT_VIRTU_H
 
+#include <simgrid/actor.h>
 #include <xbt/base.h>
 
 #ifdef __cplusplus
@@ -28,9 +29,15 @@ XBT_PUBLIC_DATA std::vector<std::string> cmdline;
 
 SG_BEGIN_DECL
 
-XBT_PUBLIC const char* xbt_procname(void);
+XBT_ATTRIB_DEPRECATED_v337("Please use sg_actor_self_get_name()") static const char* xbt_procname(void)
+{
+  return sg_actor_self_get_name() ?: "maestro";
+}
 
-XBT_PUBLIC int xbt_getpid(void);
+XBT_ATTRIB_DEPRECATED_v337("Please use sg_actor_self_get_pid()") static int xbt_getpid(void)
+{
+  return sg_actor_self_get_pid();
+};
 
 SG_END_DECL
 
index 1547836..025228a 100644 (file)
@@ -20,7 +20,8 @@
 #include "src/simgrid/sg_config.hpp"
 #include "src/smpi/include/smpi_actor.hpp"
 #include "src/xbt/xbt_modinter.h" /* whether initialization was already done */
-#include "xbt/module.h"
+
+#include "xbt/log.hpp"
 
 #include <boost/algorithm/string/predicate.hpp>
 #include <dlfcn.h>
@@ -139,6 +140,15 @@ static void install_signal_handlers()
   }
 }
 
+static simgrid::config::Flag<bool> cfg_dbg_clean_atexit{
+    "debug/clean-atexit", "Whether to cleanup SimGrid at exit. Disable it if your code segfaults after its end.", true};
+static void xbt_postexit()
+{
+  if (not cfg_dbg_clean_atexit)
+    return;
+  xbt_log_postexit();
+}
+
 namespace simgrid::kernel {
 
 EngineImpl::~EngineImpl()
@@ -175,8 +185,18 @@ void EngineImpl::initialize(int* argc, char** argv)
   simgrid::mc::AppSide::initialize();
 #endif
 
-  if (xbt_initialized == 0) {
-    xbt_init(argc, argv);
+  static bool inited = false;
+  if (not inited) {
+    inited = true;
+    atexit(xbt_postexit);
+    xbt_log_init(argc, argv);
+
+    simgrid::xbt::install_exception_handler();
+
+    if (*argc > 0)
+      simgrid::xbt::binary_name = argv[0];
+    for (int i = 0; i < *argc; i++)
+      simgrid::xbt::cmdline.emplace_back(argv[i]);
 
     sg_config_init(argc, argv);
   }
@@ -188,7 +208,7 @@ void EngineImpl::initialize(int* argc, char** argv)
   /* register a function to be called after the environment creation */
   s4u::Engine::on_platform_created_cb([this]() { this->presolve(); });
 
-  if (config::get_value<bool>("debug/clean-atexit"))
+  if (cfg_dbg_clean_atexit)
     atexit(shutdown);
 }
 
index 4f7208a..c1d383e 100644 (file)
@@ -4,8 +4,10 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
-#include "xbt.h"
+
+#include "xbt/asserts.h"
 #include "xbt/random.hpp"
+
 #include <math.h>
 
 namespace simgrid::kernel::profile {
index ea0ea36..1e022aa 100644 (file)
@@ -413,7 +413,8 @@ ExecPtr exec_async(double flops)
 
 aid_t get_pid()
 {
-  return simgrid::kernel::actor::ActorImpl::self()->get_pid();
+  auto* self = simgrid::kernel::actor::ActorImpl::self();
+  return self ? self->get_pid() : 0;
 }
 
 aid_t get_ppid()
@@ -428,7 +429,8 @@ std::string get_name()
 
 const char* get_cname()
 {
-  return simgrid::kernel::actor::ActorImpl::self()->get_cname();
+  auto* self = simgrid::kernel::actor::ActorImpl::self();
+  return self ? self->get_cname() : nullptr;
 }
 
 Host* get_host()
@@ -753,6 +755,8 @@ aid_t sg_actor_self_get_ppid()
 
 const char* sg_actor_self_get_name()
 {
+  if (simgrid::s4u::Actor::is_maestro())
+    return "maestro";
   return simgrid::s4u::this_actor::get_cname();
 }
 
index c20cd8e..3ed3452 100644 (file)
@@ -587,7 +587,7 @@ Engine* Engine::set_default_comm_data_copy_callback(
 /* **************************** Public C interface *************************** */
 void simgrid_init(int* argc, char** argv)
 {
-  static simgrid::s4u::Engine e(argc, argv);
+  simgrid::s4u::Engine::get_instance(argc, argv);
 }
 void simgrid_load_platform(const char* file)
 {
index ddaec3f..fb1df9d 100644 (file)
@@ -137,6 +137,7 @@ void sg_config_init(int *argc, char **argv)
     XBT_WARN("Call to sg_config_init() after initialization ignored");
     return;
   }
+  _sg_cfg_init_status = 1;
 
   /* Plugins and models configuration */
   simgrid_plugins().create_flag("plugin", "The plugins", "", true);
@@ -234,8 +235,6 @@ void sg_config_init(int *argc, char **argv)
   static simgrid::config::Flag<bool> cfg_execution_cutpath{
       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
 
-  _sg_cfg_init_status = 1;
-
   sg_config_cmd_line(argc, argv);
 
   xbt_mallocator_initialization_is_done(simgrid::kernel::context::Context::is_parallel());
index 24ebc39..4914d2b 100644 (file)
@@ -5,10 +5,11 @@
 
 #include "src/internal_config.h"
 
+#include <simgrid/actor.h>
+#include <simgrid/s4u/Actor.hpp>
 #include <xbt/backtrace.hpp>
 #include <xbt/string.hpp>
 #include <xbt/sysdep.h>
-#include <xbt/virtu.h>
 
 #include <cstdio>
 #include <cstdlib>
@@ -90,7 +91,8 @@ std::string Backtrace::resolve() const
 void Backtrace::display() const
 {
   std::string backtrace = resolve();
-  std::fprintf(stderr, "Backtrace (displayed in actor %s%s):\n%s\n", xbt_procname(),
+  std::fprintf(stderr, "Backtrace (displayed in actor %s%s):\n%s\n",
+               simgrid::s4u::Actor::is_maestro() ? "maestro" : sg_actor_self_get_name(),
                (xbt_log_no_loc ? " -- short trace because of --log=no_loc" : ""),
                backtrace.empty() ? "(backtrace not set -- did you install Boost.Stacktrace?)" : backtrace.c_str());
 }
index 5aa9eae..51e9649 100644 (file)
@@ -17,9 +17,9 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
 
 void _xbt_throw(char* message, const char* file, int line, const char* func)
 {
-  simgrid::Exception e(
-      simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
-      message ? message : "");
+  simgrid::Exception e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), sg_actor_self_get_name(),
+                                                sg_actor_self_get_pid()),
+                       message ? message : "");
   xbt_free(message);
   throw e;
 }
index 61bb830..e09d701 100644 (file)
@@ -9,7 +9,9 @@
 #include "simgrid/host.h"
 #include "src/xbt/log_private.hpp"
 #include "xbt/sysdep.h"
-#include "xbt/virtu.h"
+#include <simgrid/actor.h>
+#include <simgrid/s4u/Actor.hpp>
+
 #include <algorithm>
 #include <cstdio>
 
@@ -67,6 +69,7 @@ static constexpr const char* ERRMSG =
   } else                                                                                                               \
     (void)0
 #define show_int(data) show_it((data), "d")
+#define show_long(data) show_it((data), "ld")
 #define show_double(data) show_it((data), "f")
 
 static bool xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event_t ev, const char* msg_fmt)
@@ -134,10 +137,10 @@ static bool xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_even
         case 't': /* thread/process name; LOG4J compliant */
         case 'P': /* Used before SimGrid 3.26 and kept for compatiblity. Should not hurt. */
         case 'a': /* actor name; SimGrid extension */
-          show_string(xbt_procname());
+          show_string(sg_actor_self_get_name());
           break;
         case 'i': /* actor ID; SimGrid extension */
-          show_int(xbt_getpid());
+          show_long(sg_actor_self_get_pid());
           break;
         case 'F': /* file name; LOG4J compliant */
           show_string(ev->fileName);
index a689123..da43e17 100644 (file)
@@ -7,7 +7,8 @@
 
 #include "src/xbt/log_private.hpp"
 #include "xbt/sysdep.h"
-#include "xbt/virtu.h"
+#include <simgrid/actor.h>
+#include <simgrid/s4u/Actor.hpp>
 
 #include "simgrid/engine.h" /* simgrid_get_clock */
 #include "simgrid/host.h"   /* sg_host_self_get_name */
@@ -34,12 +35,12 @@ static bool xbt_log_layout_simple_doit(const s_xbt_log_layout_t*, xbt_log_event_
   check_overflow(1);
 
   /* Display the proc info if available */
-  procname = xbt_procname();
+  procname = sg_actor_self_get_name();
   if (procname && strcmp(procname,"maestro")) {
-    len = snprintf(p, rem_size, "%s:%s:(%d) ", sg_host_self_get_name(), procname, xbt_getpid());
+    len = snprintf(p, rem_size, "%s:%s:(%ld) ", sg_host_self_get_name(), procname, sg_actor_self_get_pid());
     check_overflow(len);
   } else if (not procname) {
-    len = snprintf(p, rem_size, "%s::(%d) ", sg_host_self_get_name(), xbt_getpid());
+    len = snprintf(p, rem_size, "%s::(%ld) ", sg_host_self_get_name(), sg_actor_self_get_pid());
     check_overflow(len);
   }
 
index d8a906e..48e5edf 100644 (file)
@@ -18,7 +18,6 @@
 #include "xbt/log.h"
 #include "xbt/log.hpp"
 #include "xbt/misc.h"
-#include "xbt/module.h" /* this module */
 #include "xbt/sysdep.h"
 
 #include <cmath>
@@ -38,13 +37,6 @@ std::string binary_name;          /* Name of the system process containing us (m
 std::vector<std::string> cmdline; /* all we got in argv */
 } // namespace simgrid::xbt
 
-
-int xbt_initialized = 0;
-simgrid::config::Flag<bool> cfg_dbg_clean_atexit{
-    "debug/clean-atexit",
-    "Whether to cleanup SimGrid at exit. Disable it if your code segfaults after its end.",
-    true};
-
 const int xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
 const int xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
 
@@ -57,34 +49,6 @@ XBT_ATTRIB_NOINLINE void sthread_disable()
   asm("");
 }
 
-static void xbt_postexit()
-{
-  if (not cfg_dbg_clean_atexit)
-    return;
-  xbt_initialized--;
-  xbt_log_postexit();
-}
-
-/** @brief Initialize the xbt mechanisms. */
-void xbt_init(int *argc, char **argv)
-{
-  xbt_initialized++;
-  if (xbt_initialized > 1) {
-    XBT_DEBUG("XBT has been initialized %d times.", xbt_initialized);
-    return;
-  }
-  atexit(xbt_postexit);
-
-  simgrid::xbt::install_exception_handler();
-
-  if (*argc > 0)
-    simgrid::xbt::binary_name = argv[0];
-  for (int i = 0; i < *argc; i++)
-    simgrid::xbt::cmdline.emplace_back(argv[i]);
-
-  xbt_log_init(argc, argv);
-}
-
 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
 /** @brief like xbt_free, but you can be sure that it is a function  */
 void xbt_free_f(void* p) noexcept(noexcept(::free))
index bd3625e..b715ae3 100644 (file)
@@ -15,8 +15,6 @@ SG_BEGIN_DECL
 
 void xbt_log_postexit(void);
 
-extern int xbt_initialized;
-
 SG_END_DECL
 
 #endif
diff --git a/src/xbt/xbt_virtu.cpp b/src/xbt/xbt_virtu.cpp
deleted file mode 100644 (file)
index 56b2616..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-/* virtualization layer for XBT */
-
-/* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
-
-/* 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 <simgrid/s4u/Actor.hpp>
-#include <xbt/virtu.h>
-
-#include "src/kernel/actor/ActorImpl.hpp"
-
-int xbt_getpid()
-{
-  const auto* self = simgrid::kernel::actor::ActorImpl::self();
-  return self == nullptr ? 0 : static_cast<int>(self->get_pid());
-}
-
-const char* xbt_procname(void)
-{
-  return simgrid::s4u::Actor::is_maestro() ? "maestro" : simgrid::kernel::actor::ActorImpl::self()->get_cname();
-}
index c37cfdc..190d995 100644 (file)
@@ -8,7 +8,6 @@
 #include "simgrid/s4u/Engine.hpp"
 #include "src/kernel/lmm/maxmin.hpp"
 #include "xbt/log.h"
-#include "xbt/module.h"
 #include "xbt/sysdep.h"
 #include <algorithm>
 #include <array>
index d905956..b43d234 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "simgrid/s4u/Engine.hpp"
 #include "src/kernel/lmm/maxmin.hpp"
-#include "xbt/module.h"
 #include "xbt/random.hpp"
 #include "xbt/sysdep.h" /* time manipulation for benchmarking */
 #include "xbt/xbt_os_time.h"
index 3b03ea9..c76ea46 100644 (file)
@@ -4,6 +4,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "simgrid/Exception.hpp"
+#include "simgrid/engine.h"
 #include "src/xbt/mmalloc/mmalloc.h"
 #include "xbt.h"
 
@@ -34,7 +35,7 @@ int main(int argc, char**argv)
 {
   xbt_mheap_t heapA = nullptr;
   std::array<void*, TESTSIZE> pointers;
-  xbt_init(&argc,argv);
+  simgrid_init(&argc, argv);
 
   XBT_INFO("Allocating a new heap");
   unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
index c30d4cd..874ea7d 100644 (file)
@@ -284,7 +284,6 @@ set(XBT_SRC
   src/xbt/xbt_parse_units.cpp
   src/xbt/xbt_replay.cpp
   src/xbt/xbt_str.cpp
-  src/xbt/xbt_virtu.cpp
   )
 
 if(HAVE_MMALLOC)