Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to disable mmap priv on ancient Mac systems
[simgrid.git] / src / smpi / internals / smpi_global.cpp
index 98a0b81..7544bbd 100644 (file)
 #include <cfloat> /* DBL_MAX */
 #include <dlfcn.h>
 #include <fcntl.h>
+#include <fstream>
+
 #if not defined(__APPLE__)
 #include <link.h>
 #endif
-#include <fstream>
+
+#if defined(__APPLE__)
+# include <AvailabilityMacros.h>
+# ifndef MAC_OS_X_VERSION_10_12
+#   define MAC_OS_X_VERSION_10_12 101200
+# endif
+# define HAVE_WORKING_MMAP (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12)
+#elif defined(__FreeBSD__)
+# define HAVE_WORKING_MMAP 0
+#else
+# define HAVE_WORKING_MMAP 1
+#endif
 
 #if HAVE_SENDFILE
 #include <sys/sendfile.h>
@@ -376,7 +389,7 @@ static void smpi_init_options(){
     XBT_DEBUG("Running without smpi_main(); disable smpi/privatization.");
     smpi_privatize_global_variables = SmpiPrivStrategies::NONE;
   }
-#if defined(__FreeBSD__)
+#if HAVE_WORKING_MMAP
   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
     XBT_INFO("mmap privatization is broken on FreeBSD, switching to dlopen privatization instead.");
     smpi_privatize_global_variables = SmpiPrivStrategies::DLOPEN;
@@ -494,11 +507,9 @@ static int visit_libs(struct dl_phdr_info* info, size_t, void* data)
 
 static void smpi_init_privatization_dlopen(std::string executable)
 {
-  std::string executable_copy = executable;
-
   // Prepare the copy of the binary (get its size)
   struct stat fdin_stat;
-  stat(executable_copy.c_str(), &fdin_stat);
+  stat(executable.c_str(), &fdin_stat);
   off_t fdin_size         = fdin_stat.st_size;
   static std::size_t rank = 0;
 
@@ -528,14 +539,14 @@ static void smpi_init_privatization_dlopen(std::string executable)
     }
   }
 
-  simix_global->default_function = [executable_copy, fdin_size](std::vector<std::string> args) {
-    return std::function<void()>([executable_copy, fdin_size, args] {
+  simix_global->default_function = [executable, fdin_size](std::vector<std::string> args) {
+    return std::function<void()>([executable, fdin_size, args] {
 
       // Copy the dynamic library:
       std::string target_executable =
-          executable_copy + "_" + std::to_string(getpid()) + "_" + std::to_string(rank) + ".so";
+          executable + "_" + std::to_string(getpid()) + "_" + std::to_string(rank) + ".so";
 
-      smpi_copy_file(executable_copy, target_executable, fdin_size);
+      smpi_copy_file(executable, target_executable, fdin_size);
       // if smpi/privatize-libs is set, duplicate pointed lib and link each executable copy to a different one.
       std::string target_lib;
       for (auto const& libpath : privatize_libs_paths) {
@@ -587,17 +598,17 @@ static void smpi_init_privatization_dlopen(std::string executable)
   };
 }
 
-static void smpi_init_privatization_no_dlopen(const char* executable)
+static void smpi_init_privatization_no_dlopen(std::string executable)
 {
   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP)
     smpi_prepare_global_memory_segment();
   // Load the dynamic library and resolve the entry point:
-  void* handle = dlopen(executable, RTLD_LAZY | RTLD_LOCAL);
+  void* handle = dlopen(executable.c_str(), RTLD_LAZY | RTLD_LOCAL);
   if (handle == nullptr)
-    xbt_die("dlopen failed for %s: %s (errno: %d -- %s)", executable, dlerror(), errno, strerror(errno));
+    xbt_die("dlopen failed for %s: %s (errno: %d -- %s)", executable.c_str(), dlerror(), errno, strerror(errno));
   smpi_entry_point_type entry_point = smpi_resolve_function(handle);
   if (not entry_point)
-    xbt_die("main not found in %s", executable);
+    xbt_die("main not found in %s", executable.c_str());
   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP)
     smpi_backup_global_memory_segment();
 
@@ -620,7 +631,7 @@ int smpi_main(const char* executable, int argc, char* argv[])
   TRACE_global_init();
 
   SIMIX_global_init(&argc, argv);
-  MSG_init(&argc, argv);
+  MSG_init(&argc, argv); // FIXME Remove this MSG call. Once it's removed, we can remove the msg header include as well
 
   SMPI_switch_data_segment = &smpi_switch_data_segment;