Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a23e2caee21c209bc641aeba62bf0983f1b8315f
[simgrid.git] / src / mc / checker / CommunicationDeterminismChecker.cpp
1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/checker/CommunicationDeterminismChecker.hpp"
7 #include "src/kernel/activity/MailboxImpl.hpp"
8 #include "src/mc/Session.hpp"
9 #include "src/mc/mc_api.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/mc_request.hpp"
14 #include "src/mc/mc_smx.hpp"
15
16 #if HAVE_SMPI
17 #include "smpi_request.hpp"
18 #endif
19
20 #include <cstdint>
21
22 using mcapi = simgrid::mc::mc_api;
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
25
26 /********** Global variables **********/
27
28 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
29 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
30
31 /********** Static functions ***********/
32
33 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
34                                                                const simgrid::mc::PatternCommunication* comm2)
35 {
36   using simgrid::mc::CommPatternDifference;
37   if (comm1->type != comm2->type)
38     return CommPatternDifference::TYPE;
39   if (comm1->rdv != comm2->rdv)
40     return CommPatternDifference::RDV;
41   if (comm1->src_proc != comm2->src_proc)
42     return CommPatternDifference::SRC_PROC;
43   if (comm1->dst_proc != comm2->dst_proc)
44     return CommPatternDifference::DST_PROC;
45   if (comm1->tag != comm2->tag)
46     return CommPatternDifference::TAG;
47   if (comm1->data.size() != comm2->data.size())
48     return CommPatternDifference::DATA_SIZE;
49   if (comm1->data != comm2->data)
50     return CommPatternDifference::DATA;
51   return CommPatternDifference::NONE;
52 }
53
54 static char* print_determinism_result(simgrid::mc::CommPatternDifference diff, int process,
55                                       const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
56 {
57   char* type;
58   char* res;
59
60   if (comm->type == simgrid::mc::PatternCommunicationType::send)
61     type = bprintf("The send communications pattern of the process %d is different!", process - 1);
62   else
63     type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
64
65   using simgrid::mc::CommPatternDifference;
66   switch (diff) {
67     case CommPatternDifference::TYPE:
68       res = bprintf("%s Different type for communication #%u", type, cursor);
69       break;
70     case CommPatternDifference::RDV:
71       res = bprintf("%s Different rdv for communication #%u", type, cursor);
72       break;
73     case CommPatternDifference::TAG:
74       res = bprintf("%s Different tag for communication #%u", type, cursor);
75       break;
76     case CommPatternDifference::SRC_PROC:
77       res = bprintf("%s Different source for communication #%u", type, cursor);
78       break;
79     case CommPatternDifference::DST_PROC:
80       res = bprintf("%s Different destination for communication #%u", type, cursor);
81       break;
82     case CommPatternDifference::DATA_SIZE:
83       res = bprintf("%s Different data size for communication #%u", type, cursor);
84       break;
85     case CommPatternDifference::DATA:
86       res = bprintf("%s Different data for communication #%u", type, cursor);
87       break;
88     default:
89       res = nullptr;
90       break;
91   }
92
93   return res;
94 }
95
96 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
97                                 const simgrid::kernel::activity::CommImpl* comm_addr)
98 {
99   auto src_proc = mcapi::get().get_src_actor(comm_addr);
100   auto dst_proc = mcapi::get().get_dst_actor(comm_addr);
101   comm_pattern->src_proc = src_proc->get_pid();
102   comm_pattern->dst_proc = dst_proc->get_pid();
103   comm_pattern->src_host = mcapi::get().get_actor_host_name(src_proc);
104   comm_pattern->dst_host = mcapi::get().get_actor_host_name(dst_proc);
105
106   if (comm_pattern->data.empty()) {
107     auto pattern_data = mcapi::get().get_pattern_comm_data(comm_addr);
108     if (pattern_data.data() != nullptr) {
109       auto data_size = pattern_data.size();
110       comm_pattern->data.resize(data_size);
111       memcpy(comm_pattern->data.data(), pattern_data.data(), data_size);
112     }
113    }
114 }
115
116 namespace simgrid {
117 namespace mc {
118
119 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, const PatternCommunication* comm,
120                                                                  int backtracking)
121 {
122   if (not backtracking) {
123     PatternCommunicationList& list = initial_communications_pattern[process];
124     CommPatternDifference diff     = compare_comm_pattern(list.list[list.index_comm].get(), comm);
125
126     if (diff != CommPatternDifference::NONE) {
127       if (comm->type == PatternCommunicationType::send) {
128         this->send_deterministic = false;
129         if (this->send_diff != nullptr)
130           xbt_free(this->send_diff);
131         this->send_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
132       } else {
133         this->recv_deterministic = false;
134         if (this->recv_diff != nullptr)
135           xbt_free(this->recv_diff);
136         this->recv_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
137       }
138       if (_sg_mc_send_determinism && not this->send_deterministic) {
139         XBT_INFO("*********************************************************");
140         XBT_INFO("***** Non-send-deterministic communications pattern *****");
141         XBT_INFO("*********************************************************");
142         XBT_INFO("%s", this->send_diff);
143         xbt_free(this->send_diff);
144         this->send_diff = nullptr;
145         mcapi::get().log_state();
146         mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
147       } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
148         XBT_INFO("****************************************************");
149         XBT_INFO("***** Non-deterministic communications pattern *****");
150         XBT_INFO("****************************************************");
151         if (this->send_diff) {
152           XBT_INFO("%s", this->send_diff);
153           xbt_free(this->send_diff);
154           this->send_diff = nullptr;
155         }
156         if (this->recv_diff) {
157           XBT_INFO("%s", this->recv_diff);
158           xbt_free(this->recv_diff);
159           this->recv_diff = nullptr;
160         }
161         mcapi::get().log_state();
162         mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
163       }
164     }
165   }
166 }
167
168 /********** Non Static functions ***********/
169
170 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, CallType call_type, int backtracking)
171 {
172   const smx_actor_t issuer                                     = mcapi::get().simcall_get_issuer(request);
173   const mc::PatternCommunicationList& initial_pattern          = initial_communications_pattern[issuer->get_pid()];
174   const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
175
176   auto pattern   = std::make_unique<PatternCommunication>();
177   pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
178
179   if (call_type == CallType::SEND) {
180     /* Create comm pattern */
181     pattern->type      = PatternCommunicationType::send;
182     pattern->comm_addr = mcapi::get().get_comm_isend_raw_addr(request);
183     pattern->rdv      = mcapi::get().get_pattern_comm_rdv(pattern->comm_addr);
184     pattern->src_proc = mcapi::get().get_pattern_comm_src_proc(pattern->comm_addr);
185     pattern->src_host = mc_api::get().get_actor_host_name(issuer);
186
187 #if HAVE_SMPI
188     pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
189 #endif
190     auto pattern_data = mcapi::get().get_pattern_comm_data(pattern->comm_addr);
191     if (pattern_data.data() != nullptr) {
192       auto data_size = pattern_data.size();
193       pattern->data.resize(data_size);
194       memcpy(pattern->data.data(), pattern_data.data(), data_size);
195     }
196
197 #if HAVE_SMPI
198     auto send_detached = mcapi::get().check_send_request_detached(request);
199     if (send_detached) {
200       if (this->initial_communications_pattern_done) {
201         /* Evaluate comm determinism */
202         this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
203         initial_communications_pattern[pattern->src_proc].index_comm++;
204       } else {
205         /* Store comm pattern */
206         initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
207       }
208       return;
209     }
210 #endif
211   } else if (call_type == CallType::RECV) {
212     pattern->type = PatternCommunicationType::receive;
213     pattern->comm_addr = mcapi::get().get_comm_isend_raw_addr(request);
214
215 #if HAVE_SMPI
216     pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
217 #endif
218     auto comm_addr = pattern->comm_addr;
219     pattern->rdv = mcapi::get().get_pattern_comm_rdv(comm_addr);
220     pattern->dst_proc = mcapi::get().get_pattern_comm_dst_proc(comm_addr);
221     pattern->dst_host = mcapi::get().get_actor_host_name(issuer);
222   } else
223     xbt_die("Unexpected call_type %i", (int)call_type);
224
225   XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
226   incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
227 }
228
229 void CommunicationDeterminismChecker::complete_comm_pattern(const kernel::activity::CommImpl* comm_addr,
230                                                             unsigned int issuer, int backtracking)
231 {
232   /* Complete comm pattern */
233   std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
234   auto current_comm_pattern =
235       std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
236                    [&comm_addr](const PatternCommunication* comm) { return mcapi::get().comm_addr_equal(comm->comm_addr, comm_addr); });
237   if (current_comm_pattern == std::end(incomplete_pattern))
238     xbt_die("Corresponding communication not found!");
239
240   update_comm_pattern(*current_comm_pattern, comm_addr);
241   std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
242   XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %zd", issuer,
243             std::distance(begin(incomplete_pattern), current_comm_pattern));
244   incomplete_pattern.erase(current_comm_pattern);
245
246   if (this->initial_communications_pattern_done) {
247     /* Evaluate comm determinism */
248     this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
249     initial_communications_pattern[issuer].index_comm++;
250   } else {
251     /* Store comm pattern */
252     initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
253   }
254 }
255
256 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& s) : Checker(s) {}
257
258 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
259
260 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
261 {
262   RecordTrace res;
263   for (auto const& state : stack_)
264     res.push_back(state->get_transition());
265   return res;
266 }
267
268 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
269 {
270   std::vector<std::string> trace;
271   for (auto const& state : stack_) {
272     smx_simcall_t req = &state->executed_req_;
273     trace.push_back(mcapi::get().request_to_string(req, state->transition_.argument_, RequestType::executed));
274   }
275   return trace;
276 }
277
278 void CommunicationDeterminismChecker::log_state() // override
279 {
280   if (_sg_mc_comms_determinism) {
281     if (this->send_deterministic && not this->recv_deterministic) {
282       XBT_INFO("*******************************************************");
283       XBT_INFO("**** Only-send-deterministic communication pattern ****");
284       XBT_INFO("*******************************************************");
285       XBT_INFO("%s", this->recv_diff);
286     }
287     if (not this->send_deterministic && this->recv_deterministic) {
288       XBT_INFO("*******************************************************");
289       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
290       XBT_INFO("*******************************************************");
291       XBT_INFO("%s", this->send_diff);
292     }
293   }
294   XBT_INFO("Expanded states = %lu", expanded_states_count_);
295   XBT_INFO("Visited states = %lu", mcapi::get().mc_get_visited_states());
296   XBT_INFO("Executed transitions = %lu", mcapi::get().mc_get_executed_trans());
297   XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
298   if (_sg_mc_comms_determinism)
299     XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
300 }
301
302 void CommunicationDeterminismChecker::prepare()
303 {
304   const int maxpid = mcapi::get().get_maxpid();
305
306   initial_communications_pattern.resize(maxpid);
307   incomplete_communications_pattern.resize(maxpid);
308
309   ++expanded_states_count_;
310   auto initial_state = std::make_unique<State>(expanded_states_count_);
311
312   XBT_DEBUG("********* Start communication determinism verification *********");
313
314   /* Get an enabled actor and insert it in the interleave set of the initial state */
315   auto actors = mcapi::get().get_actors();
316   for (auto& actor : actors)
317     if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
318       initial_state->add_interleaving_set(actor.copy.get_buffer());
319
320   stack_.push_back(std::move(initial_state));
321 }
322
323 static inline bool all_communications_are_finished()
324 {
325   auto maxpid = mcapi::get().get_maxpid();
326   for (size_t current_actor = 1; current_actor < maxpid; current_actor++) {
327     if (not incomplete_communications_pattern[current_actor].empty()) {
328       XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
329       return false;
330     }
331   }
332   return true;
333 }
334
335 void CommunicationDeterminismChecker::restoreState()
336 {
337   /* Intermediate backtracking */
338   State* last_state = stack_.back().get();
339   if (last_state->system_state_) {
340     mc_api::get().restore_state(last_state->system_state_);
341     MC_restore_communications_pattern(last_state);
342     return;
343   }
344
345   /* Restore the initial state */
346   mcapi::get().restore_initial_state();
347
348   unsigned n = mcapi::get().get_maxpid();
349   assert(n == incomplete_communications_pattern.size());
350   assert(n == initial_communications_pattern.size());
351   for (unsigned j = 0; j < n; j++) {
352     incomplete_communications_pattern[j].clear();
353     initial_communications_pattern[j].index_comm = 0;
354   }
355
356   /* Traverse the stack from the state at position start and re-execute the transitions */
357   for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
358     if (state == stack_.back())
359       break;
360
361     int req_num                    = state->transition_.argument_;
362     const s_smx_simcall* saved_req = &state->executed_req_;
363     xbt_assert(saved_req);
364
365     /* because we got a copy of the executed request, we have to fetch the
366        real one, pointed by the request field of the issuer process */
367
368     const smx_actor_t issuer = mcapi::get().simcall_get_issuer(saved_req);
369     smx_simcall_t req        = &issuer->simcall_;
370
371     /* TODO : handle test and testany simcalls */
372     CallType call = MC_get_call_type(req);
373     mcapi::get().handle_simcall(state->transition_);
374     handle_comm_pattern(call, req, req_num, 1);
375     mcapi::get().mc_wait_for_requests();
376
377     /* Update statistics */
378     mcapi::get().mc_inc_visited_states();
379     mcapi::get().mc_inc_executed_trans();
380   }
381 }
382
383 void CommunicationDeterminismChecker::handle_comm_pattern(simgrid::mc::CallType call_type, smx_simcall_t req, int value, int backtracking)
384 {
385   using simgrid::mc::CallType;
386   switch(call_type) {
387     case CallType::NONE:
388       break;
389     case CallType::SEND:
390     case CallType::RECV:
391       get_comm_pattern(req, call_type, backtracking);
392       break;
393     case CallType::WAIT:
394     case CallType::WAITANY: {
395       simgrid::kernel::activity::CommImpl* comm_addr = nullptr;
396       if (call_type == CallType::WAIT)
397         comm_addr = mcapi::get().get_comm_wait_raw_addr(req);
398       else
399         comm_addr = mcapi::get().get_comm_waitany_raw_addr(req, value);
400       auto simcall_issuer = mcapi::get().simcall_get_issuer(req);
401       complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
402     } break;
403   default:
404     xbt_die("Unexpected call type %i", (int)call_type);
405   }
406 }
407
408 void CommunicationDeterminismChecker::real_run()
409 {
410   std::unique_ptr<VisitedState> visited_state = nullptr;
411   smx_simcall_t req                           = nullptr;
412
413   while (not stack_.empty()) {
414     /* Get current state */
415     State* cur_state = stack_.back().get();
416
417     XBT_DEBUG("**************************************************");
418     XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num_,
419               cur_state->interleave_size());
420
421     /* Update statistics */
422     mcapi::get().mc_inc_visited_states();
423
424     if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
425       req = mcapi::get().mc_state_choose_request(cur_state);
426     else
427       req = nullptr;
428
429     if (req != nullptr && visited_state == nullptr) {
430       int req_num = cur_state->transition_.argument_;
431
432       XBT_DEBUG("Execute: %s", mcapi::get().request_to_string(req, req_num, RequestType::simix).c_str());
433
434       std::string req_str;
435       if (dot_output != nullptr)
436         req_str = mcapi::get().request_get_dot_output(req, req_num);
437
438       mcapi::get().mc_inc_executed_trans();
439
440       /* TODO : handle test and testany simcalls */
441       CallType call = CallType::NONE;
442       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
443         call = MC_get_call_type(req);
444
445       /* Answer the request */
446       mcapi::get().handle_simcall(cur_state->transition_);
447       /* After this call req is no longer useful */
448
449       handle_comm_pattern(call, req, req_num, 0);
450
451       /* Wait for requests (schedules processes) */
452       mcapi::get().mc_wait_for_requests();
453
454       /* Create the new expanded state */
455       ++expanded_states_count_;
456       auto next_state = std::make_unique<State>(expanded_states_count_);
457
458       /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
459        * least, data are transferred). These communications  are incomplete and they cannot be analyzed and compared
460        * with the initial pattern. */
461       bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
462
463       if (_sg_mc_max_visited_states != 0)
464         visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
465       else
466         visited_state = nullptr;
467
468       if (visited_state == nullptr) {
469         /* Get enabled actors and insert them in the interleave set of the next state */
470         auto actors = mcapi::get().get_actors();
471         for (auto& actor : actors)
472           if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
473             next_state->add_interleaving_set(actor.copy.get_buffer());
474
475         if (dot_output != nullptr)
476           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
477
478       } else if (dot_output != nullptr)
479         fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_,
480                 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
481
482       stack_.push_back(std::move(next_state));
483     } else {
484       if (stack_.size() > (std::size_t)_sg_mc_max_depth)
485         XBT_WARN("/!\\ Max depth reached! /!\\ ");
486       else if (visited_state != nullptr)
487         XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
488                   visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
489       else
490         XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
491
492       this->initial_communications_pattern_done = true;
493
494       /* Trash the current state, no longer needed */
495       XBT_DEBUG("Delete state %d at depth %zu", cur_state->num_, stack_.size());
496       stack_.pop_back();
497
498       visited_state = nullptr;
499
500       /* Check for deadlocks */
501       if (mcapi::get().mc_check_deadlock()) {
502         mcapi::get().mc_show_deadlock();
503         throw simgrid::mc::DeadlockError();
504       }
505
506       while (not stack_.empty()) {
507         std::unique_ptr<State> state(std::move(stack_.back()));
508         stack_.pop_back();
509         if (state->interleave_size() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
510           /* We found a back-tracking point, let's loop */
511           XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num_, stack_.size() + 1);
512           stack_.push_back(std::move(state));
513
514           this->restoreState();
515
516           XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num_, stack_.size());
517
518           break;
519         } else {
520           XBT_DEBUG("Delete state %d at depth %zu", state->num_, stack_.size() + 1);
521         }
522       }
523     }
524   }
525
526   mcapi::get().log_state();
527 }
528
529 void CommunicationDeterminismChecker::run()
530 {
531   XBT_INFO("Check communication determinism");
532   mcapi::get().s_initialize();
533
534   this->prepare();
535   this->real_run();
536 }
537
538 Checker* createCommunicationDeterminismChecker(Session& s)
539 {
540   return new CommunicationDeterminismChecker(s);
541 }
542
543 } // namespace mc
544 } // namespace simgrid