Logo AND Algorithmique Numérique Distribuée

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