-void print_memory_analysis(){
- if (simgrid::smpi::F2C::lookup() != nullptr &&
- simgrid::smpi::F2C::lookup()->size() > simgrid::smpi::F2C::get_num_default_handles()) {
- 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++;
- }
+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)
+ 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 (handles.empty())
+ return;
+
+ 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 += "\nHINT : 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";
+ XBT_INFO(message.c_str(), handles.size());
+ if (max == 0)
+ return;
+
+ // 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, std::less<>> count;
+ for (const 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.emplace(key, 1);
+ if (result.second == false)
+ result.first->second++;
+ }
+ if (display_advice)
+ XBT_WARN("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 (allocs.empty())
+ return;
+
+ 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 buffers :";
+ 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(), allocs.size());
+
+ if (max == 0)
+ return;
+
+ // gather by allocation origin (only one group reported in case of no-loc or if trace-call-location is not used)
+ struct buff_leak {
+ int count;
+ size_t total_size;
+ size_t min_size;
+ size_t max_size;
+ };
+ std::map<std::string, struct buff_leak, std::less<>> leaks_aggreg;
+ for (const auto& elem : allocs) {
+ std::string key = "leaked allocations";
+ if (not xbt_log_no_loc)
+ key = elem.second.file + ":" + std::to_string(elem.second.line) + " : " + key;
+ auto result = leaks_aggreg.emplace(key, buff_leak{1, elem.second.size, elem.second.size, elem.second.size});
+ if (result.second == false) {
+ result.first->second.count++;
+ result.first->second.total_size += elem.second.size;
+ if (elem.second.size > result.first->second.max_size)
+ result.first->second.max_size = elem.second.size;
+ else if (elem.second.size < result.first->second.min_size)
+ result.first->second.min_size = elem.second.size;