Logo AND Algorithmique Numérique Distribuée

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