Logo AND Algorithmique Numérique Distribuée

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