Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display leak origin in message, if -trace-call-location is used.
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
index d280569..90ef155 100644 (file)
@@ -13,9 +13,7 @@
 #include "xbt/file.hpp"
 #include <boost/tokenizer.hpp>
 #include "smpi_config.hpp"
-#include "smpi_f2c.hpp"
 #include "src/simix/smx_private.hpp"
-
 #include <algorithm>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
@@ -38,6 +36,7 @@ struct MaxMalloc {
   std::string file;
 };
 MaxMalloc max_malloc;
+F2C* current_handle=0;
 
 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
 {
@@ -133,27 +132,42 @@ void print_time_analysis(double global_time){
 
 void print_memory_analysis()
 {
-  size_t leak_count = 0;
+  // 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) {
+    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",
-             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("Leak %p of type %s", p.second, boost::core::demangle(typeid(*(p.second)).name()).c_str());
-        printed++;
+             handles.size());
+    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;
+          }
+          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 (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"
@@ -169,6 +183,21 @@ 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()));
+  }
+}
+
 }
 }
 } // namespace simgrid