Logo AND Algorithmique Numérique Distribuée

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