Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tell when we have more leaked handles to display
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
1 /* Copyright (c) 2016-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smpi_utils.hpp"
8
9 #include "src/surf/xml/platf_private.hpp"
10 #include "xbt/log.h"
11 #include "xbt/parse_units.hpp"
12 #include "xbt/sysdep.h"
13 #include "xbt/file.hpp"
14 #include <boost/tokenizer.hpp>
15 #include "smpi_config.hpp"
16 #include "smpi_f2c.hpp"
17 #include "src/simix/smx_private.hpp"
18
19 #include <algorithm>
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
22
23 extern std::string surf_parsed_filename;
24 extern int surf_parse_lineno;
25
26 namespace simgrid {
27 namespace smpi {
28 namespace utils {
29
30 double total_benched_time=0;
31 unsigned long total_malloc_size=0;
32 unsigned long total_shared_size=0;
33 unsigned int total_shared_calls=0;
34 struct MaxMalloc {
35   size_t size          = 0;
36   unsigned int numcall = 0;
37   int line             = 0;
38   std::string file;
39 };
40 MaxMalloc max_malloc;
41
42 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
43 {
44   std::vector<s_smpi_factor_t> smpi_factor;
45
46   /** Setup the tokenizer that parses the string **/
47   using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
48   boost::char_separator<char> sep(";");
49   boost::char_separator<char> factor_separator(":");
50   Tokenizer tokens(smpi_coef_string, sep);
51
52   /**
53    * Iterate over patterns like A:B:C:D;E:F;G:H
54    * These will be broken down into:
55    * A --> B, C, D
56    * E --> F
57    * G --> H
58    */
59   for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
60     XBT_DEBUG("token : %s", token_iter->c_str());
61     Tokenizer factor_values(*token_iter, factor_separator);
62     s_smpi_factor_t fact;
63     xbt_assert(factor_values.begin() != factor_values.end(), "Malformed radical for smpi factor: '%s'",
64                smpi_coef_string.c_str());
65     unsigned int iteration = 0;
66     for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
67       iteration++;
68
69       if (factor_iter == factor_values.begin()) { /* first element */
70         try {
71           fact.factor = std::stoi(*factor_iter);
72         } catch (const std::invalid_argument&) {
73           throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
74                                       ": " + *factor_iter);
75         }
76       } else {
77         try {
78           fact.values.push_back(
79               xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, (*factor_iter).c_str(), "smpi factor", ""));
80         } catch (const std::invalid_argument&) {
81           throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
82                                       std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
83         }
84       }
85     }
86
87     smpi_factor.push_back(fact);
88     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
89   }
90   std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
91     return (pa.factor < pb.factor);
92   });
93   for (auto const& fact : smpi_factor) {
94     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
95   }
96   smpi_factor.shrink_to_fit();
97
98   return smpi_factor;
99 }
100
101 void add_benched_time(double time){
102   total_benched_time += time;
103 }
104
105 void account_malloc_size(size_t size, const char* file, int line){
106   total_malloc_size += size;
107   if(size > max_malloc.size){
108     max_malloc.size = size;
109     max_malloc.line = line;
110     max_malloc.numcall = 1;
111     max_malloc.file = std::string(file);
112   }else if(size == max_malloc.size && max_malloc.line == line && not max_malloc.file.compare(file)){
113     max_malloc.numcall++;
114   }
115 }
116
117 void account_shared_size(size_t size){
118   total_shared_size += size;
119   total_shared_calls++;
120 }
121
122 void print_time_analysis(double global_time){
123   if (simgrid::config::get_value<bool>("smpi/display-timing")) {
124     XBT_INFO("Simulated time: %g seconds. \n\n"
125         "The simulation took %g seconds (after parsing and platform setup)\n"
126         "%g seconds were actual computation of the application",
127         SIMIX_get_clock(), global_time , total_benched_time);
128     if (total_benched_time/global_time>=0.75)
129       XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
130     "You may want to use sampling functions or trace replay to reduce this.");
131   }
132 }
133
134 void print_memory_analysis()
135 {
136   size_t leak_count = 0;
137   if (simgrid::smpi::F2C::lookup() != nullptr)
138     leak_count =
139         std::count_if(simgrid::smpi::F2C::lookup()->cbegin(), simgrid::smpi::F2C::lookup()->cend(),
140                       [](auto const& entry) { return entry.first >= simgrid::smpi::F2C::get_num_default_handles(); });
141   if (leak_count > 0) {
142     XBT_INFO("Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles : "
143              "display types and addresses (n max) with --cfg=smpi/list-leaks:n.\n"
144              "Running smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information",
145              leak_count);
146     int n = simgrid::config::get_value<int>("smpi/list-leaks");
147     for (auto const& p : *simgrid::smpi::F2C::lookup()) {
148       static int printed = 0;
149       if (printed >= n) {
150         if (n > 0)
151           XBT_WARN("(more handle leaks hidden as you wanted to see only %d of them)", n);
152         break;
153       }
154       if (p.first >= simgrid::smpi::F2C::get_num_default_handles()) {
155         if (xbt_log_no_loc) {
156           XBT_WARN("Leaked handle of type %s", boost::core::demangle(typeid(*(p.second)).name()).c_str());
157         } else {
158           XBT_WARN("Leaked handle of type %s at %p", boost::core::demangle(typeid(*(p.second)).name()).c_str(),
159                    p.second);
160         }
161         printed++;
162       }
163     }
164   }
165   if (simgrid::config::get_value<bool>("smpi/display-allocs")) {
166     if(total_malloc_size != 0)
167       XBT_INFO("Memory Usage: Simulated application allocated %lu bytes during its lifetime through malloc/calloc calls.\n"
168              "Largest allocation at once from a single process was %zu bytes, at %s:%d. It was called %u times during the whole simulation.\n"
169              "If this is too much, consider sharing allocations for computation buffers.\n"
170              "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",
171              total_malloc_size, max_malloc.size, simgrid::xbt::Path(max_malloc.file).get_base_name().c_str(), max_malloc.line, max_malloc.numcall
172       );
173     else
174       XBT_INFO("Allocations analysis asked, but 0 bytes were allocated through malloc/calloc calls intercepted by SMPI.\n"
175                "Either code is using other ways of allocating memory, or it was built with SMPI_NO_OVERRIDE_MALLOC");
176     if(total_shared_size != 0)
177       XBT_INFO("%lu bytes were automatically shared between processes, in %u calls\n", total_shared_size, total_shared_calls);
178   }
179 }
180 }
181 }
182 } // namespace simgrid