Logo AND Algorithmique Numérique Distribuée

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