- 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());
- 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);
- }
+ 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->name();
+ if ((not xbt_log_no_loc) && (not elem->call_location().empty()))
+ key += " at " + elem->call_location();
+ else
+ display_advice = true;
+ auto& result = count.try_emplace(key, 0).first->second;
+ result++;
+ }
+ 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& [key, value] : count) {
+ if (value == 1)
+ XBT_INFO("leaked handle of type %s", key.c_str());
+ else
+ XBT_INFO("%d leaked handles of type %s", value, key.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.file + ":" + std::to_string(elem.line) + ": " + key;
+ auto& result = leaks_aggreg.try_emplace(key, buff_leak{0, 0, elem.size, elem.size}).first->second;
+ result.count++;
+ result.total_size += elem.size;
+ if (elem.size > result.max_size)
+ result.max_size = elem.size;
+ else if (elem.size < result.min_size)
+ result.min_size = elem.size;
+ }
+ // now we can order by total size.
+ std::vector<std::pair<std::string, buff_leak>> leaks(leaks_aggreg.begin(), leaks_aggreg.end());
+ std::sort(leaks.begin(), leaks.end(),
+ [](auto const& a, auto const& b) { return a.second.total_size > b.second.total_size; });
+
+ unsigned int i = 0;
+ for (const auto& [key, value] : leaks) {
+ if (value.min_size == value.max_size)
+ XBT_INFO("%s of total size %zu, called %d times, each with size %zu", key.c_str(), value.total_size, value.count,
+ value.min_size);
+ else
+ XBT_INFO("%s of total size %zu, called %d times, with minimum size %zu and maximum size %zu", key.c_str(),
+ value.total_size, value.count, value.min_size, value.max_size);
+ i++;
+ if (i == max)
+ break;