Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split mmalloc in a separate library
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 21 Jul 2022 21:00:09 +0000 (23:00 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 21 Jul 2022 21:00:13 +0000 (23:00 +0200)
The idea is to allow to preload mmalloc before sthread, that must come
before simgrid itself.

src/mc/api.cpp
src/xbt/mmalloc/mm_interface.c [new file with mode: 0644]
src/xbt/mmalloc/mm_module.c
teshsuite/xbt/CMakeLists.txt
tools/cmake/DefinePackages.cmake
tools/cmake/MakeLib.cmake

index a29dbf2..ce024e6 100644 (file)
@@ -37,9 +37,18 @@ simgrid::mc::Exploration* Api::initialize(char** argv, const std::unordered_map<
     int i = 1;
     while (argv[i] != nullptr && argv[i][0] == '-')
       i++;
+    if (env.find("LD_PRELOAD") == env.end())
+      setenv("LD_PRELOAD", "libsgmalloc.so", 1);
+
     for (auto const& [key, val] : env) {
-      XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
-      setenv(key.c_str(), val.c_str(), 1);
+      if (key == "LD_PRELOAD") {
+        auto v2 = std::string("libsgmalloc.so:") + val;
+        XBT_INFO("setenv '%s'='%s'", key.c_str(), v2.c_str());
+        setenv(key.c_str(), v2.c_str(), 1);
+      } else {
+        XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
+        setenv(key.c_str(), val.c_str(), 1);
+      }
     }
     xbt_assert(argv[i] != nullptr,
                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
diff --git a/src/xbt/mmalloc/mm_interface.c b/src/xbt/mmalloc/mm_interface.c
new file mode 100644 (file)
index 0000000..030a250
--- /dev/null
@@ -0,0 +1,58 @@
+/* External interface to a mmap'd malloc managed region. */
+
+/* Copyright (c) 2012-2022. 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. */
+
+/* Copyright 1992, 2000 Free Software Foundation, Inc.
+
+   Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include "mmprivate.h"
+#include <fcntl.h> /* After sys/types.h, at least for dpx/2.  */
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+// This is the underlying implementation of mmalloc_get_bytes_used_remote.
+// Is it used directly to evaluate the bytes used from a different process.
+size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo)
+{
+  int bytes = 0;
+  for (size_t i = 0; i < heaplimit; ++i) {
+    if (heapinfo[i].type == MMALLOC_TYPE_UNFRAGMENTED) {
+      if (heapinfo[i].busy_block.busy_size > 0)
+        bytes += heapinfo[i].busy_block.busy_size;
+    } else if (heapinfo[i].type > 0) {
+      for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo[i].type); j++) {
+        if (heapinfo[i].busy_frag.frag_size[j] > 0)
+          bytes += heapinfo[i].busy_frag.frag_size[j];
+      }
+    }
+  }
+  return bytes;
+}
+
+__attribute__((weak)) xbt_mheap_t mmalloc_get_current_heap(void)
+{
+  fprintf(stderr, "Fake mmalloc_get_current_heap()\n");
+  return NULL;
+}
\ No newline at end of file
index a113213..d8b5359 100644 (file)
@@ -207,23 +207,3 @@ xbt_mheap_t mmalloc_preinit(void)
 
   return __mmalloc_default_mdp;
 }
-
-// This is the underlying implementation of mmalloc_get_bytes_used_remote.
-// Is it used directly in order to evaluate the bytes used from a different
-// process.
-size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo)
-{
-  int bytes = 0;
-  for (size_t i=0; i < heaplimit; ++i){
-    if (heapinfo[i].type == MMALLOC_TYPE_UNFRAGMENTED){
-      if (heapinfo[i].busy_block.busy_size > 0)
-        bytes += heapinfo[i].busy_block.busy_size;
-    } else if (heapinfo[i].type > 0) {
-      for (size_t j=0; j < (size_t) (BLOCKSIZE >> heapinfo[i].type); j++){
-        if(heapinfo[i].busy_frag.frag_size[j] > 0)
-          bytes += heapinfo[i].busy_frag.frag_size[j];
-      }
-    }
-  }
-  return bytes;
-}
index 1d9a841..fb10524 100644 (file)
@@ -21,7 +21,7 @@ endforeach()
 
 if(HAVE_MMALLOC)
   add_executable       (mmalloc_test EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp)
-  target_link_libraries(mmalloc_test simgrid)
+  target_link_libraries(mmalloc_test simgrid sgmalloc)
   set_target_properties(mmalloc_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mmalloc)
   set_property(TARGET mmalloc_test APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
   add_dependencies(tests mmalloc_test)
index 2fbe539..813d829 100644 (file)
@@ -288,7 +288,7 @@ set(XBT_SRC
   )
 
 if(HAVE_MMALLOC)
-  set(XBT_SRC ${XBT_SRC}  src/xbt/mmalloc/mm.c )
+  set(SGMALLOC_SRC src/xbt/mmalloc/mm.c)
 else()
   set(EXTRA_DIST ${EXTRA_DIST} src/xbt/mmalloc/mm.c)
 endif()
@@ -655,6 +655,8 @@ set(MC_SRC
   src/mc/mc_smx.cpp
   src/mc/udpor_global.cpp
   src/mc/udpor_global.hpp
+
+  src/xbt/mmalloc/mm_interface.c
   )
 
 set(MC_SIMGRID_MC_SRC  src/mc/explo/simgrid_mc.cpp)
index c50bd88..551c48e 100644 (file)
@@ -35,6 +35,12 @@ else()
   set(EXTRA_DIST ${EXTRA_DIST} ${STHREAD_SRC})
 endif()
 
+if(HAVE_MMALLOC)
+  add_library(sgmalloc SHARED ${SGMALLOC_SRC})
+  set_property(TARGET sgmalloc
+                APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
+endif()
+
 if(enable_model-checking)
   add_executable(simgrid-mc ${MC_SIMGRID_MC_SRC})
   target_link_libraries(simgrid-mc simgrid)