Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Group leak reports by type/file/line (for handles, will see for buffer later).
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
index 788dac5..3a3633a 100644 (file)
 #include "xbt/file.hpp"
 #include <boost/tokenizer.hpp>
 #include "smpi_config.hpp"
-#include "smpi_f2c.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)");
 
@@ -31,13 +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)
 {
@@ -102,21 +112,33 @@ 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, const 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    = 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    = file;
+    } else if (size == max_malloc.size && max_malloc.line == line && max_malloc.file == 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){
@@ -131,30 +153,86 @@ void print_time_analysis(double global_time){
   }
 }
 
-void print_memory_analysis()
-{
-  size_t leak_count = 0;
+static void print_leaked_handles(){
+  // 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)
-    leak_count =
-        std::count_if(simgrid::smpi::F2C::lookup()->cbegin(), simgrid::smpi::F2C::lookup()->cend(),
-                      [](auto const& entry) { return entry.first >= simgrid::smpi::F2C::get_num_default_handles(); });
-  if (leak_count > 0) {
-    XBT_INFO("Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles : "
+    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()) {
+    auto max = static_cast<unsigned long>(simgrid::config::get_value<int>("smpi/list-leaks"));
+    std::string message = "Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles :";
+    if(max==0)
+      message +="display types and addresses (n max) with --cfg=smpi/list-leaks:n.\nRunning smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information";
+    XBT_INFO(message.c_str(), 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
+      bool display_advice = false;
+      std::map<std::string, int> count;
+      for (auto & elem : handles){
+        std::string key = elem.second->name();
+        if ((not xbt_log_no_loc) && (not elem.second->call_location().empty()))
+          key+=" at "+ elem.second->call_location();
+        else
+          display_advice=true;
+        auto result = count.insert(std::pair<std::string, int>(key, 1));
+        if (result.second == false)
+          result.first->second++;
+      }
+      if (display_advice)
+        XBT_INFO("To get more information (location of allocations), compile your code with -trace-call-location flag of smpicc/f90");
+      unsigned int i = 0;
+      for (const auto& p : count) {
+        if(p.second == 1)
+          XBT_INFO("leaked handle of type %s", p.first.c_str());
+        else
+          XBT_INFO("%d leaked handles of type %s", p.second, p.first.c_str());
+        i++;
+        if(i == max)
+          break;
+      }
+      if (max < count.size())
+        XBT_INFO("(%lu more handle leaks hidden as you wanted to see only %lu of them)", count.size()-max, max);
+    }
+  }
+}
+
+static void print_leaked_buffers(){
+  if (not allocs.empty()) {
+    auto max = static_cast<unsigned long>(simgrid::config::get_value<int>("smpi/list-leaks"));
+    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",
-             leak_count);
-    int n = simgrid::config::get_value<int>("smpi/list-leaks");
-    for (auto const& p : *simgrid::smpi::F2C::lookup()) {
-      static int printed = 0;
-      if (printed >= n)
-        break;
-      if (p.first >= simgrid::smpi::F2C::get_num_default_handles()) {
-        XBT_WARN("Leaked handle of type %s at %p", boost::core::demangle(typeid(*(p.second)).name()).c_str(), p.second);
-        printed++;
+             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 (simgrid::config::get_value<bool>("smpi/display-allocs")) {
+}
+
+void print_memory_analysis()
+{
+  if (smpi_cfg_display_alloc()) {
+    print_leaked_handles();
+    print_leaked_buffers();
+
     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"
@@ -169,6 +247,67 @@ void print_memory_analysis()
       XBT_INFO("%lu bytes were automatically shared between processes, in %u calls\n", total_shared_size, total_shared_calls);
   }
 }
+
+void set_current_handle(F2C* handle){
+  current_handle=handle;
+}
+
+void print_current_handle(){
+  if(current_handle){
+    if(current_handle->call_location().empty())
+      XBT_INFO("To get handle location information, pass -trace-call-location flag to smpicc/f90 as well");
+    else
+      XBT_INFO("Handle %s was allocated by a call at %s", current_handle->name().c_str(),
+               (char*)(current_handle->call_location().c_str()));
+  }
+}
+
+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