#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)");
std::string file;
};
MaxMalloc max_malloc;
+F2C* current_handle=0;
std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
{
}
}
-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);
+ 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"
}
}
+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