1 /* Copyright (c) 2016-2021. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smpi_utils.hpp"
9 #include "src/surf/xml/platf_private.hpp"
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 "src/simix/smx_private.hpp"
18 #include "private.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
22 extern std::string surf_parsed_filename;
23 extern int surf_parse_lineno;
29 double total_benched_time=0;
30 unsigned long total_malloc_size=0;
31 unsigned long total_shared_size=0;
32 unsigned int total_shared_calls=0;
33 struct alloc_metadata_t {
35 unsigned int numcall = 0;
40 struct current_buffer_metadata_t {
41 alloc_metadata_t alloc;
45 alloc_metadata_t max_malloc;
46 F2C* current_handle = nullptr;
47 current_buffer_metadata_t current_buffer1;
48 current_buffer_metadata_t current_buffer2;
50 std::unordered_map<const void*, alloc_metadata_t> allocs;
52 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
54 std::vector<s_smpi_factor_t> smpi_factor;
56 /** Setup the tokenizer that parses the string **/
57 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
58 boost::char_separator<char> sep(";");
59 boost::char_separator<char> factor_separator(":");
60 Tokenizer tokens(smpi_coef_string, sep);
63 * Iterate over patterns like A:B:C:D;E:F;G:H
64 * These will be broken down into:
69 for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
70 XBT_DEBUG("token : %s", token_iter->c_str());
71 Tokenizer factor_values(*token_iter, factor_separator);
73 xbt_assert(factor_values.begin() != factor_values.end(), "Malformed radical for smpi factor: '%s'",
74 smpi_coef_string.c_str());
75 unsigned int iteration = 0;
76 for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
79 if (factor_iter == factor_values.begin()) { /* first element */
81 fact.factor = std::stoi(*factor_iter);
82 } catch (const std::invalid_argument&) {
83 throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
88 fact.values.push_back(
89 xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, (*factor_iter).c_str(), "smpi factor", ""));
90 } catch (const std::invalid_argument&) {
91 throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
92 std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
97 smpi_factor.push_back(fact);
98 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
100 std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
101 return (pa.factor < pb.factor);
103 for (auto const& fact : smpi_factor) {
104 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
106 smpi_factor.shrink_to_fit();
111 void add_benched_time(double time){
112 total_benched_time += time;
115 void account_malloc_size(size_t size, const std::string& file, int line, void* ptr)
117 if (smpi_cfg_display_alloc()) {
118 alloc_metadata_t metadata;
119 metadata.size = size;
120 metadata.line = line;
121 metadata.numcall = 1;
122 metadata.file = file;
123 allocs.emplace(ptr, metadata);
125 total_malloc_size += size;
126 if(size > max_malloc.size){
127 max_malloc.size = size;
128 max_malloc.line = line;
129 max_malloc.numcall = 1;
130 max_malloc.file = file;
131 } else if (size == max_malloc.size && max_malloc.line == line && max_malloc.file == file) {
132 max_malloc.numcall++;
137 void account_shared_size(size_t size){
138 if (smpi_cfg_display_alloc()) {
139 total_shared_size += size;
140 total_shared_calls++;
144 void print_time_analysis(double global_time){
145 if (simgrid::config::get_value<bool>("smpi/display-timing")) {
146 XBT_INFO("Simulated time: %g seconds. \n\n"
147 "The simulation took %g seconds (after parsing and platform setup)\n"
148 "%g seconds were actual computation of the application",
149 SIMIX_get_clock(), global_time , total_benched_time);
150 if (total_benched_time/global_time>=0.75)
151 XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
152 "You may want to use sampling functions or trace replay to reduce this.");
156 static void print_leaked_handles(){
157 // Put the leaked non-default handles in a vector to sort them by id
158 std::vector<std::pair<unsigned int, smpi::F2C*>> handles;
159 if (simgrid::smpi::F2C::lookup() != nullptr)
160 std::copy_if(simgrid::smpi::F2C::lookup()->begin(), simgrid::smpi::F2C::lookup()->end(),
161 std::back_inserter(handles),
162 [](auto const& entry) { return entry.first >= simgrid::smpi::F2C::get_num_default_handles(); });
163 if (not handles.empty()) {
164 auto max = static_cast<unsigned long>(simgrid::config::get_value<int>("smpi/list-leaks"));
165 std::string message = "Probable memory leaks in your code: SMPI detected %zu unfreed MPI handles :";
167 message +="\nHINT : Display types and addresses (n max) with --cfg=smpi/list-leaks:n.\n"\
168 "Running smpirun with -wrapper \"valgrind --leak-check=full\" can provide more information";
169 XBT_INFO(message.c_str(), handles.size());
170 if (max > 0) { // we cannot trust F2C::lookup()->size() > F2C::get_num_default_handles() because some default
171 // handles are already freed at this point
172 bool display_advice = false;
173 std::map<std::string, int, std::less<>> count;
174 for (const auto& elem : handles) {
175 std::string key = elem.second->name();
176 if ((not xbt_log_no_loc) && (not elem.second->call_location().empty()))
177 key+=" at "+ elem.second->call_location();
180 auto result = count.emplace(key, 1);
181 if (result.second == false)
182 result.first->second++;
185 XBT_WARN("To get more information (location of allocations), compile your code with -trace-call-location flag of smpicc/f90");
187 for (const auto& p : count) {
189 XBT_INFO("leaked handle of type %s", p.first.c_str());
191 XBT_INFO("%d leaked handles of type %s", p.second, p.first.c_str());
196 if (max < count.size())
197 XBT_INFO("(%lu more handle leaks hidden as you wanted to see only %lu of them)", count.size()-max, max);
202 static void print_leaked_buffers(){
203 if (not allocs.empty()) {
204 auto max = static_cast<unsigned long>(simgrid::config::get_value<int>("smpi/list-leaks"));
205 std::string message = "Probable memory leaks in your code: SMPI detected %zu unfreed buffers :";
207 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";
208 XBT_INFO(message.c_str(), allocs.size());
211 //gather by allocation origin (only one group reported in case of no-loc or if trace-call-location is not used)
218 std::map<std::string, struct buff_leak, std::less<>> leaks_aggreg;
219 for (const auto& elem : allocs) {
220 std::string key = "leaked allocations";
221 if (not xbt_log_no_loc)
222 key=elem.second.file+":"+std::to_string(elem.second.line)+" : "+key;
223 auto result = leaks_aggreg.emplace(key, buff_leak{1, elem.second.size, elem.second.size, elem.second.size});
224 if (result.second == false){
225 result.first->second.count ++;
226 result.first->second.total_size += elem.second.size;
227 if(elem.second.size > result.first->second.max_size)
228 result.first->second.max_size = elem.second.size;
229 else if (elem.second.size < result.first->second.min_size)
230 result.first->second.min_size = elem.second.size;
233 //now we can order by total size.
234 std::vector<std::pair<std::string, buff_leak>> leaks(leaks_aggreg.begin(), leaks_aggreg.end());
235 std::sort(leaks.begin(), leaks.end(), [](auto const& a, auto const& b) { return a.second.total_size > b.second.total_size; });
238 for (const auto& p : leaks) {
239 if(p.second.min_size == p.second.max_size)
240 XBT_INFO("%s of total size %zu, called %d times, each with size %zu",
241 p.first.c_str(),p.second.total_size,p.second.count,p.second.min_size);
243 XBT_INFO("%s of total size %zu, called %d times, with minimum size %zu and maximum size %zu",
244 p.first.c_str(),p.second.total_size,p.second.count,p.second.min_size,p.second.max_size);
249 if (max < leaks_aggreg.size())
250 XBT_INFO("(more buffer leaks hidden as you wanted to see only %lu of them)", max);
255 void print_memory_analysis()
257 if (smpi_cfg_display_alloc()) {
258 print_leaked_handles();
259 print_leaked_buffers();
261 if(total_malloc_size != 0)
262 XBT_INFO("Memory Usage: Simulated application allocated %lu bytes during its lifetime through malloc/calloc calls.\n"
263 "Largest allocation at once from a single process was %zu bytes, at %s:%d. It was called %u times during the whole simulation.\n"
264 "If this is too much, consider sharing allocations for computation buffers.\n"
265 "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",
266 total_malloc_size, max_malloc.size, simgrid::xbt::Path(max_malloc.file).get_base_name().c_str(), max_malloc.line, max_malloc.numcall
269 XBT_INFO("Allocations analysis asked, but 0 bytes were allocated through malloc/calloc calls intercepted by SMPI.\n"
270 "Either code is using other ways of allocating memory, or it was built with SMPI_NO_OVERRIDE_MALLOC");
271 if(total_shared_size != 0)
272 XBT_INFO("%lu bytes were automatically shared between processes, in %u calls\n", total_shared_size, total_shared_calls);
276 void set_current_handle(F2C* handle){
277 current_handle=handle;
280 void print_current_handle(){
282 if(current_handle->call_location().empty())
283 XBT_INFO("To get handle location information, pass -trace-call-location flag to smpicc/f90 as well");
285 XBT_INFO("Handle %s was allocated by a call at %s", current_handle->name().c_str(),
286 (char*)(current_handle->call_location().c_str()));
290 void set_current_buffer(int i, const char* name, const void* buf){
293 if(not current_buffer1.name.empty()){
294 current_buffer1.name="";
296 if(not current_buffer2.name.empty()){
297 current_buffer2.name="";
300 auto meta = allocs.find(buf);
301 if (meta == allocs.end()) {
302 XBT_DEBUG("Buffer %p was not allocated with malloc/calloc", buf);
306 current_buffer1.alloc = meta->second;
307 current_buffer1.name = name;
309 current_buffer2.alloc=meta->second;
310 current_buffer2.name=name;
314 void print_buffer_info(){
315 if(not current_buffer1.name.empty())
316 XBT_INFO("Buffer %s was allocated from %s line %d, with size %zu", current_buffer1.name.c_str(), current_buffer1.alloc.file.c_str(), current_buffer1.alloc.line, current_buffer1.alloc.size);
317 if(not current_buffer2.name.empty())
318 XBT_INFO("Buffer %s was allocated from %s line %d, with size %zu", current_buffer2.name.c_str(), current_buffer2.alloc.file.c_str(), current_buffer2.alloc.line, current_buffer2.alloc.size);
321 size_t get_buffer_size(const void* buf){
322 auto meta = allocs.find(buf);
323 if (meta == allocs.end()) {
324 //we don't know this buffer (on stack or feature disabled), assume it's fine.
325 return std::numeric_limits<std::size_t>::max();
327 return meta->second.size;
330 void account_free(const void* ptr){
331 if (smpi_cfg_display_alloc()) {
338 } // namespace simgrid