Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Count all intercepted allocations/deallocations in order to build a small leak-checker.
authorAugustin Degomme <adegomme@users.noreply.github.com>
Fri, 9 Apr 2021 01:28:02 +0000 (03:28 +0200)
committerAugustin Degomme <adegomme@users.noreply.github.com>
Fri, 9 Apr 2021 09:10:25 +0000 (11:10 +0200)
This allows to check for leaks, but also to provide debug information on the involved buffers (file, line, size) when a crash happens
todo: evaluate memory cost and slowdown for real apps (disabled by default, needs --cfg=smpi/display-allocs or -analyze flag to be activated)

src/smpi/bindings/smpi_mpi.cpp
src/smpi/include/smpi_config.hpp
src/smpi/include/smpi_utils.hpp
src/smpi/internals/smpi_config.cpp
src/smpi/internals/smpi_shared.cpp
src/smpi/internals/smpi_utils.cpp

index ca63a1a..c1072a9 100644 (file)
@@ -56,6 +56,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi, "Logging specific to SMPI ,(mpi)
           xbt_backtrace_display_current();                                                                             \
         }                                                                                                              \
         simgrid::smpi::utils::print_current_handle();                                                                  \
+        simgrid::smpi::utils::print_buffer_info();                                                                     \
         xbt_die("%s - returned %.*s instead of MPI_SUCCESS", __func__, error_size, error_string);                      \
       } else                                                                                                           \
         err->call((errhan), ret);                                                                                      \
index 955d943..903ebdb 100644 (file)
@@ -27,4 +27,5 @@ extern XBT_PRIVATE simgrid::config::Flag<std::string> _smpi_cfg_comp_adjustment_
 extern XBT_PRIVATE simgrid::config::Flag<std::string> _smpi_cfg_papi_events_file;
 #endif
 extern XBT_PRIVATE simgrid::config::Flag<double> _smpi_cfg_auto_shared_malloc_thresh;
+extern XBT_PRIVATE simgrid::config::Flag<bool> _smpi_cfg_display_alloc;
 #endif
index 4d2b9c5..8d7b73c 100644 (file)
@@ -23,12 +23,17 @@ namespace smpi {
 namespace utils {
   XBT_PUBLIC std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string);
   XBT_PUBLIC void add_benched_time(double time);
-  XBT_PUBLIC void account_malloc_size(size_t size, const char* file, int line);
+  XBT_PUBLIC void account_malloc_size(size_t size, std::string file, int line, void* ptr);
   XBT_PUBLIC void account_shared_size(size_t size);
   XBT_PUBLIC void print_time_analysis(double time);
+  XBT_PUBLIC void print_buffer_info();
   XBT_PUBLIC void print_memory_analysis();
   XBT_PUBLIC void print_current_handle();
   XBT_PUBLIC void set_current_handle(F2C* handle);
+  XBT_PUBLIC void set_current_buffer(int i, const char* name, const void* handle);
+  XBT_PUBLIC size_t get_buffer_size(const void* ptr);
+  XBT_PUBLIC void account_free(const void* ptr);
+
 }
 }
 }
index 425ca50..13e2afd 100644 (file)
@@ -118,9 +118,13 @@ simgrid::config::Flag<std::string> _smpi_cfg_comp_adjustment_file{"smpi/comp-adj
 #endif
 
 simgrid::config::Flag<double> _smpi_cfg_auto_shared_malloc_thresh("smpi/auto-shared-malloc-thresh",
-                                                                  "Threshold size for the automatic sharing of memory", 
+                                                                  "Threshold size for the automatic sharing of memory",
                                                                   0);
 
+simgrid::config::Flag<bool> _smpi_cfg_display_alloc("smpi/display-allocs",
+                                                    "Whether we should display a memory allocations analysis after simulation.",
+                                                     false);
+
 double smpi_cfg_host_speed(){
   return _smpi_cfg_host_speed;
 }
@@ -165,6 +169,10 @@ bool smpi_cfg_trace_call_use_absolute_path(){
   return _smpi_cfg_trace_call_use_absolute_path;
 }
 
+bool smpi_cfg_display_alloc(){
+  return _smpi_cfg_display_alloc;
+}
+
 std::string smpi_cfg_comp_adjustment_file(){
   return _smpi_cfg_comp_adjustment_file;
 }
@@ -181,7 +189,6 @@ void smpi_init_options(){
   // return if already called
   if(_smpi_options_initialized)
     return;
-  simgrid::config::declare_flag<bool>("smpi/display-allocs", "Whether we should display a memory allocations analysis after simulation.", false);
   simgrid::config::declare_flag<bool>("smpi/display-timing", "Whether we should display the timing after simulation.", false);
   simgrid::config::declare_flag<int>("smpi/list-leaks", "Whether we should display the n first MPI handle leaks (addresses and type only) after simulation", 0);
   simgrid::config::declare_flag<bool>("smpi/keep-temps", "Whether we should keep the generated temporary files.", false);
index b495246..dfc99b8 100644 (file)
@@ -39,6 +39,7 @@
 
 #include "private.hpp"
 #include "xbt/config.hpp"
+#include "xbt/file.hpp"
 
 #include <cerrno>
 
@@ -304,8 +305,12 @@ void* smpi_shared_malloc_partial(size_t size, const size_t* shared_block_offsets
 void* smpi_shared_malloc_intercept(size_t size, const char* file, int line)
 {
   if( smpi_cfg_auto_shared_malloc_thresh() == 0 || size < smpi_cfg_auto_shared_malloc_thresh()){
-    simgrid::smpi::utils::account_malloc_size(size, file, line);
-    return ::operator new(size);
+    void* ptr = ::operator new(size);
+    if(not smpi_cfg_trace_call_use_absolute_path())
+      simgrid::smpi::utils::account_malloc_size(size, simgrid::xbt::Path(file).get_base_name(), line, ptr);
+    else
+      simgrid::smpi::utils::account_malloc_size(size, file, line, ptr);
+    return ptr;
   } else {
     simgrid::smpi::utils::account_shared_size(size);
     return smpi_shared_malloc(size, file, line);
@@ -315,8 +320,11 @@ void* smpi_shared_malloc_intercept(size_t size, const char* file, int line)
 void* smpi_shared_calloc_intercept(size_t num_elm, size_t elem_size, const char* file, int line)
 {
   if( smpi_cfg_auto_shared_malloc_thresh() == 0 || elem_size*num_elm < smpi_cfg_auto_shared_malloc_thresh()){
-    simgrid::smpi::utils::account_malloc_size(elem_size*num_elm, file, line);
     void* ptr = ::operator new(elem_size*num_elm);
+    if(not smpi_cfg_trace_call_use_absolute_path())
+      simgrid::smpi::utils::account_malloc_size(elem_size*num_elm, simgrid::xbt::Path(file).get_base_name(), line, ptr);
+    else
+      simgrid::smpi::utils::account_malloc_size(elem_size*num_elm, file, line, ptr);
     memset(ptr, 0, elem_size*num_elm);
     return ptr;
   } else {
@@ -406,6 +414,7 @@ std::vector<std::pair<size_t, size_t>> merge_private_blocks(const std::vector<st
 
 void smpi_shared_free(void *ptr)
 {
+  simgrid::smpi::utils::account_free(ptr);
   if (smpi_cfg_shared_malloc() == SharedMallocType::LOCAL) {
     auto meta = allocs_metadata.find(ptr);
     if (meta == allocs_metadata.end()) {
index 78a50c4..cec9084 100644 (file)
@@ -15,6 +15,7 @@
 #include "smpi_config.hpp"
 #include "src/simix/smx_private.hpp"
 #include <algorithm>
+#include "private.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
 
@@ -29,14 +30,24 @@ double total_benched_time=0;
 unsigned long total_malloc_size=0;
 unsigned long total_shared_size=0;
 unsigned int total_shared_calls=0;
-struct MaxMalloc {
+struct alloc_metadata_t {
   size_t size          = 0;
   unsigned int numcall = 0;
   int line             = 0;
   std::string file;
 };
-MaxMalloc max_malloc;
+
+struct current_buffer_metadata_t {
+  alloc_metadata_t alloc;
+  std::string name;
+};
+
+alloc_metadata_t max_malloc;
 F2C* current_handle = nullptr;
+current_buffer_metadata_t current_buffer1;
+current_buffer_metadata_t current_buffer2;
+
+std::unordered_map<const void*, alloc_metadata_t> allocs;
 
 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
 {
@@ -101,21 +112,32 @@ void add_benched_time(double time){
   total_benched_time += time;
 }
 
-void account_malloc_size(size_t size, const char* file, int line){
-  total_malloc_size += size;
-  if(size > max_malloc.size){
-    max_malloc.size = size;
-    max_malloc.line = line;
-    max_malloc.numcall = 1;
-    max_malloc.file = std::string(file);
-  }else if(size == max_malloc.size && max_malloc.line == line && not max_malloc.file.compare(file)){
-    max_malloc.numcall++;
+void account_malloc_size(size_t size, std::string file, int line, void* ptr){
+  if (smpi_cfg_display_alloc()) {
+    alloc_metadata_t metadata;
+    metadata.size = size;
+    metadata.line = line;
+    metadata.numcall = 1;
+    metadata.file = std::string(file);
+    allocs.insert(std::make_pair(ptr, metadata));
+
+    total_malloc_size += size;
+    if(size > max_malloc.size){
+      max_malloc.size = size;
+      max_malloc.line = line;
+      max_malloc.numcall = 1;
+      max_malloc.file = std::string(file);
+    }else if(size == max_malloc.size && max_malloc.line == line && not max_malloc.file.compare(file)){
+      max_malloc.numcall++;
+    }
   }
 }
 
 void account_shared_size(size_t size){
-  total_shared_size += size;
-  total_shared_calls++;
+  if (smpi_cfg_display_alloc()) {
+    total_shared_size += size;
+    total_shared_calls++;
+  }
 }
 
 void print_time_analysis(double global_time){
@@ -132,43 +154,69 @@ void print_time_analysis(double global_time){
 
 void print_memory_analysis()
 {
-  // Put the leaked non-default handles in a vector to sort them by id
-  std::vector<std::pair<unsigned int, smpi::F2C*>> handles;
-  if (simgrid::smpi::F2C::lookup() != nullptr)
-    std::copy_if(simgrid::smpi::F2C::lookup()->begin(), simgrid::smpi::F2C::lookup()->end(),
-                 std::back_inserter(handles),
-                 [](auto const& entry) { return entry.first >= simgrid::smpi::F2C::get_num_default_handles(); });
-
-  if (not handles.empty()) {
-    XBT_INFO("Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles : "
-             "display types and addresses (n max) with --cfg=smpi/list-leaks:n.\n"
-             "Running smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information",
-             handles.size());
+  if (smpi_cfg_display_alloc()) {
+    // Put the leaked non-default handles in a vector to sort them by id
+    std::vector<std::pair<unsigned int, smpi::F2C*>> handles;
+    if (simgrid::smpi::F2C::lookup() != nullptr)
+      std::copy_if(simgrid::smpi::F2C::lookup()->begin(), simgrid::smpi::F2C::lookup()->end(),
+                   std::back_inserter(handles),
+                   [](auto const& entry) { return entry.first >= simgrid::smpi::F2C::get_num_default_handles(); });
+
     auto max = static_cast<unsigned long>(simgrid::config::get_value<int>("smpi/list-leaks"));
-    if (max > 0) { // we cannot trust F2C::lookup()->size() > F2C::get_num_default_handles() because some default
-                   // handles are already freed at this point
-      std::sort(handles.begin(), handles.end(), [](auto const& a, auto const& b) { return a.first < b.first; });
-      bool truncate = max < handles.size();
-      if (truncate)
-        handles.resize(max);
-      bool printed_advice=false;
-      for (const auto& p : handles) {
-        if (xbt_log_no_loc || p.second->call_location().empty()) {
-          if (!printed_advice){
-            XBT_INFO("To get more information (location of allocations), compile your code with -trace-call-location flag of smpicc/f90");
-            printed_advice=true;
+    if (not handles.empty()) {
+      XBT_INFO("Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles : "
+               "display types and addresses (n max) with --cfg=smpi/list-leaks:n.\n"
+               "Running smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information",
+               handles.size());
+      if (max > 0) { // we cannot trust F2C::lookup()->size() > F2C::get_num_default_handles() because some default
+                     // handles are already freed at this point
+        std::sort(handles.begin(), handles.end(), [](auto const& a, auto const& b) { return a.first < b.first; });
+        bool truncate = max < handles.size();
+        if (truncate)
+          handles.resize(max);
+        bool printed_advice=false;
+        for (const auto& p : handles) {
+          if (xbt_log_no_loc || p.second->call_location().empty()) {
+            if (!printed_advice){
+              XBT_INFO("To get more information (location of allocations), compile your code with -trace-call-location flag of smpicc/f90");
+              printed_advice=true;
+            }
+            XBT_INFO("Leaked handle of type %s", p.second->name().c_str());
+          } else {
+            XBT_INFO("Leaked handle of type %s at %s", p.second->name().c_str(), p.second->call_location().c_str());
           }
-          XBT_INFO("Leaked handle of type %s", p.second->name().c_str());
-        } else {
-          XBT_INFO("Leaked handle of type %s at %s", p.second->name().c_str(), p.second->call_location().c_str());
         }
+        if (truncate)
+          XBT_INFO("(more handle leaks hidden as you wanted to see only %lu of them)", max);
+      }
+    }
+
+    if (not allocs.empty()) {
+      std::vector<std::pair<const void*, alloc_metadata_t>> leaks;
+      std::copy(allocs.begin(),
+              allocs.end(),
+              std::back_inserter<std::vector<std::pair<const void*, alloc_metadata_t>>>(leaks));
+      XBT_INFO("Probable memory leaks in your code: SMPI detected %zu unfreed buffers : "
+               "display types and addresses (n max) with --cfg=smpi/list-leaks:n.\n"
+               "Running smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information",
+               leaks.size());
+      if (max > 0) {
+        std::sort(leaks.begin(), leaks.end(), [](auto const& a, auto const& b) { return a.second.size < b.second.size; });
+        bool truncate = max < leaks.size();
+        if (truncate)
+          leaks.resize(max);
+        for (const auto& p : leaks) {
+          if (xbt_log_no_loc) {
+            XBT_INFO("Leaked buffer of size %zu", p.second.size);
+          } else {
+            XBT_INFO("Leaked buffer of size %zu, allocated in file %s at line %d", p.second.size, p.second.file.c_str(), p.second.line);
+          }
+        }
+        if (truncate)
+          XBT_INFO("(more buffer leaks hidden as you wanted to see only %lu of them)", max);
       }
-      if (truncate)
-        XBT_INFO("(more handle leaks hidden as you wanted to see only %lu of them)", max);
     }
-  }
 
-  if (simgrid::config::get_value<bool>("smpi/display-allocs")) {
     if(total_malloc_size != 0)
       XBT_INFO("Memory Usage: Simulated application allocated %lu bytes during its lifetime through malloc/calloc calls.\n"
              "Largest allocation at once from a single process was %zu bytes, at %s:%d. It was called %u times during the whole simulation.\n"
@@ -198,6 +246,52 @@ void print_current_handle(){
   }
 }
 
+void set_current_buffer(int i, const char* name, const void* buf){
+  //clear previous one
+  if(i==1){
+    if(not current_buffer1.name.empty()){
+      current_buffer1.name="";
+    }
+    if(not current_buffer2.name.empty()){
+      current_buffer2.name="";
+    }
+  }
+  auto meta = allocs.find(buf);
+  if (meta == allocs.end()) {
+    XBT_DEBUG("Buffer %p was not allocated with malloc/calloc", buf);
+    return;
+  }
+  if(i==1){
+    current_buffer1.alloc = meta->second;
+    current_buffer1.name = name;
+  }else{
+    current_buffer2.alloc=meta->second;
+    current_buffer2.name=name;
+  }
+}
+
+void print_buffer_info(){
+    if(not current_buffer1.name.empty())
+      XBT_INFO("Buffer %s was allocated from %s line %d, with size %zu", current_buffer1.name.c_str(), current_buffer1.alloc.file.c_str(), current_buffer1.alloc.line, current_buffer1.alloc.size);
+    if(not current_buffer2.name.empty())
+      XBT_INFO("Buffer %s was allocated from %s line %d, with size %zu", current_buffer2.name.c_str(), current_buffer2.alloc.file.c_str(), current_buffer2.alloc.line, current_buffer2.alloc.size);    
+}
+
+size_t get_buffer_size(const void* buf){
+  auto meta = allocs.find(buf);
+  if (meta == allocs.end()) {
+    //we don't know this buffer (on stack or feature disabled), assume it's fine.
+    return  std::numeric_limits<std::size_t>::max();
+  }
+  return meta->second.size;
+}
+
+void account_free(const void* ptr){
+  if (smpi_cfg_display_alloc()) {
+    allocs.erase(ptr);
+  }
+}
+
 }
 }
 } // namespace simgrid