Logo AND Algorithmique Numérique Distribuée

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