Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a name() to all F2C objects.
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
index 8cbd011e9bcf0378885f1943f5a9427fd9aeb821..791bf56f6974d8b2159421a3ae678d5af4eecd5b 100644 (file)
@@ -16,6 +16,8 @@
 #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)");
 
 extern std::string surf_parsed_filename;
@@ -58,9 +60,8 @@ std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
     XBT_DEBUG("token : %s", token_iter->c_str());
     Tokenizer factor_values(*token_iter, factor_separator);
     s_smpi_factor_t fact;
-    if (factor_values.begin() == factor_values.end()) {
-      xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string.c_str());
-    }
+    xbt_assert(factor_values.begin() != factor_values.end(), "Malformed radical for smpi factor: '%s'",
+               smpi_coef_string.c_str());
     unsigned int iteration = 0;
     for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
       iteration++;
@@ -130,36 +131,54 @@ void print_time_analysis(double global_time){
   }
 }
 
-void print_memory_analysis(){
-  if (simgrid::smpi::F2C::lookup() != nullptr &&
-      simgrid::smpi::F2C::lookup()->size() > simgrid::smpi::F2C::get_num_default_handles()) {
+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",
-             simgrid::smpi::F2C::lookup()->size() - simgrid::smpi::F2C::get_num_default_handles());
-    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);
+      for (const auto& p : handles) {
+        if (xbt_log_no_loc) {
+          XBT_WARN("Leaked handle of type %s", boost::core::demangle(typeid(*p.second).name()).c_str());
+        } else {
+          XBT_WARN("Leaked handle of type %s at %p", p.second->name().c_str(), p.second);
+        }
       }
+      if (truncate)
+        XBT_WARN("(more handle leaks hidden as you wanted to see only %lu of them)", max);
     }
   }
-  if (simgrid::config::get_value<bool>("smpi/display-analysis")) {
-    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" 
-           "If this is too much, consider sharing allocations for computation buffers.\n"
-           "This can be done automatically by setting --cfg=smpi/auto-shared-malloc-thresh to the minimum size wanted size (this can alter execution if data content is necessary)\n", 
-           total_malloc_size, max_malloc.size, simgrid::xbt::Path(max_malloc.file).get_base_name().c_str(), max_malloc.line, max_malloc.numcall
-    );
+
+  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"
+             "If this is too much, consider sharing allocations for computation buffers.\n"
+             "This can be done automatically by setting --cfg=smpi/auto-shared-malloc-thresh to the minimum size wanted size (this can alter execution if data content is necessary)\n",
+             total_malloc_size, max_malloc.size, simgrid::xbt::Path(max_malloc.file).get_base_name().c_str(), max_malloc.line, max_malloc.numcall
+      );
+    else
+      XBT_INFO("Allocations analysis asked, but 0 bytes were allocated through malloc/calloc calls intercepted by SMPI.\n"
+               "Either code is using other ways of allocating memory, or it was built with SMPI_NO_OVERRIDE_MALLOC");
     if(total_shared_size != 0)
       XBT_INFO("%lu bytes were automatically shared between processes, in %u calls\n", total_shared_size, total_shared_calls);
   }
 }
-
 }
 }
 } // namespace simgrid