Logo AND Algorithmique Numérique Distribuée

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