Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
small comments improvements around a complex code
[simgrid.git] / src / mc / checker / CommunicationDeterminismChecker.cpp
1 /* Copyright (c) 2008-2019. 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
15 #if HAVE_SMPI
16 #include "smpi_request.hpp"
17 #endif
18
19 #include <cstdint>
20
21 using simgrid::mc::remote;
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
24
25 /********** Global variables **********/
26
27 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
28 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
29
30 /********** Static functions ***********/
31
32 static e_mc_comm_pattern_difference_t compare_comm_pattern(simgrid::mc::PatternCommunication* comm1,
33                                                            simgrid::mc::PatternCommunication* comm2)
34 {
35   if(comm1->type != comm2->type)
36     return TYPE_DIFF;
37   if (comm1->rdv != comm2->rdv)
38     return RDV_DIFF;
39   if (comm1->src_proc != comm2->src_proc)
40     return SRC_PROC_DIFF;
41   if (comm1->dst_proc != comm2->dst_proc)
42     return DST_PROC_DIFF;
43   if (comm1->tag != comm2->tag)
44     return TAG_DIFF;
45   if (comm1->data.size() != comm2->data.size())
46     return DATA_SIZE_DIFF;
47   if (comm1->data != comm2->data)
48     return DATA_DIFF;
49   return NONE_DIFF;
50 }
51
52 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process,
53                                       simgrid::mc::PatternCommunication* comm, unsigned int cursor)
54 {
55   char* type;
56   char* res;
57
58   if (comm->type == simgrid::mc::PatternCommunicationType::send)
59     type = bprintf("The send communications pattern of the process %d is different!", process - 1);
60   else
61     type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
62
63   switch(diff) {
64   case TYPE_DIFF:
65     res = bprintf("%s Different type for communication #%u", type, cursor);
66     break;
67   case RDV_DIFF:
68     res = bprintf("%s Different rdv for communication #%u", type, cursor);
69     break;
70   case TAG_DIFF:
71     res = bprintf("%s Different tag for communication #%u", type, cursor);
72     break;
73   case SRC_PROC_DIFF:
74     res = bprintf("%s Different source for communication #%u", type, cursor);
75     break;
76   case DST_PROC_DIFF:
77     res = bprintf("%s Different destination for communication #%u", type, cursor);
78     break;
79   case DATA_SIZE_DIFF:
80     res = bprintf("%s Different data size for communication #%u", type, cursor);
81     break;
82   case DATA_DIFF:
83     res = bprintf("%s Different data for communication #%u", type, cursor);
84     break;
85   default:
86     res = nullptr;
87     break;
88   }
89
90   return res;
91 }
92
93 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
94                                 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr)
95 {
96   // HACK, type punning
97   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
98   mc_model_checker->process().read(temp_comm, comm_addr);
99   simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
100
101   smx_actor_t src_proc   = mc_model_checker->process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
102   smx_actor_t dst_proc   = mc_model_checker->process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
103   comm_pattern->src_proc = src_proc->get_pid();
104   comm_pattern->dst_proc = dst_proc->get_pid();
105   comm_pattern->src_host = MC_smx_actor_get_host_name(src_proc);
106   comm_pattern->dst_host = MC_smx_actor_get_host_name(dst_proc);
107   if (comm_pattern->data.size() == 0 && comm->src_buff_ != nullptr) {
108     size_t buff_size;
109     mc_model_checker->process().read(&buff_size, remote(comm->dst_buff_size_));
110     comm_pattern->data.resize(buff_size);
111     mc_model_checker->process().read_bytes(comm_pattern->data.data(), comm_pattern->data.size(),
112                                            remote(comm->src_buff_));
113   }
114 }
115
116 namespace simgrid {
117 namespace mc {
118
119 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, simgrid::mc::PatternCommunication* comm,
120                                                                  int backtracking)
121 {
122   simgrid::mc::PatternCommunicationList& list = initial_communications_pattern[process];
123
124   if (not backtracking) {
125     e_mc_comm_pattern_difference_t diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
126
127     if (diff != NONE_DIFF) {
128       if (comm->type == simgrid::mc::PatternCommunicationType::send) {
129         this->send_deterministic = 0;
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 = 0;
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         simgrid::mc::session->log_state();
147         mc_model_checker->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         simgrid::mc::session->log_state();
163         mc_model_checker->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, e_mc_call_type_t call_type,
172                                                        int backtracking)
173 {
174   const smx_actor_t issuer = MC_smx_simcall_get_issuer(request);
175   const simgrid::mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
176   const std::vector<simgrid::mc::PatternCommunication*>& incomplete_pattern =
177       incomplete_communications_pattern[issuer->get_pid()];
178
179   std::unique_ptr<simgrid::mc::PatternCommunication> pattern(new simgrid::mc::PatternCommunication());
180   pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
181
182   if (call_type == MC_CALL_TYPE_SEND) {
183     /* Create comm pattern */
184     pattern->type = simgrid::mc::PatternCommunicationType::send;
185     pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request));
186
187     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
188     mc_model_checker->process().read(temp_synchro,
189                                      remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
190     simgrid::kernel::activity::CommImpl* synchro =
191         static_cast<simgrid::kernel::activity::CommImpl*>(temp_synchro.get_buffer());
192
193     char* remote_name = mc_model_checker->process().read<char*>(RemotePtr<char*>(
194         (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->name_ : &synchro->mbox_cpy->name_)));
195     pattern->rdv      = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
196     pattern->src_proc =
197         mc_model_checker->process().resolve_actor(simgrid::mc::remote(synchro->src_actor_.get()))->get_pid();
198     pattern->src_host = MC_smx_actor_get_host_name(issuer);
199
200 #if HAVE_SMPI
201     simgrid::smpi::Request mpi_request;
202     mc_model_checker->process().read(
203         &mpi_request, remote(static_cast<simgrid::smpi::Request*>(simcall_comm_isend__get__data(request))));
204     pattern->tag = mpi_request.tag();
205 #endif
206
207     if (synchro->src_buff_ != nullptr) {
208       pattern->data.resize(synchro->src_buff_size_);
209       mc_model_checker->process().read_bytes(pattern->data.data(), pattern->data.size(), remote(synchro->src_buff_));
210     }
211 #if HAVE_SMPI
212     if(mpi_request.detached()){
213       if (this->initial_communications_pattern_done) {
214         /* Evaluate comm determinism */
215         this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
216         initial_communications_pattern[pattern->src_proc].index_comm++;
217       } else {
218         /* Store comm pattern */
219         initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
220       }
221       return;
222     }
223 #endif
224   } else if (call_type == MC_CALL_TYPE_RECV) {
225     pattern->type = simgrid::mc::PatternCommunicationType::receive;
226     pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_irecv__getraw__result(request));
227
228 #if HAVE_SMPI
229     simgrid::smpi::Request mpi_request;
230     mc_model_checker->process().read(
231         &mpi_request, remote(static_cast<simgrid::smpi::Request*>(simcall_comm_irecv__get__data(request))));
232     pattern->tag = mpi_request.tag();
233 #endif
234
235     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
236     mc_model_checker->process().read(temp_comm,
237                                      remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
238     simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
239
240     char* remote_name;
241     mc_model_checker->process().read(&remote_name,
242                                      remote(comm->get_mailbox()
243                                                 ? &simgrid::xbt::string::to_string_data(comm->get_mailbox()->name_).data
244                                                 : &simgrid::xbt::string::to_string_data(comm->mbox_cpy->name_).data));
245     pattern->rdv      = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
246     pattern->dst_proc =
247         mc_model_checker->process().resolve_actor(simgrid::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(
257     simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer, int backtracking)
258 {
259   /* Complete comm pattern */
260   std::vector<simgrid::mc::PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
261   auto current_comm_pattern = std::find_if(
262       begin(incomplete_pattern), end(incomplete_pattern),
263       [&comm_addr](simgrid::mc::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<simgrid::mc::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(
304           simgrid::mc::request_to_string(req, state->transition_.argument_, simgrid::mc::RequestType::executed));
305   }
306   return trace;
307 }
308
309 void CommunicationDeterminismChecker::log_state() // override
310 {
311   if (_sg_mc_comms_determinism) {
312     if (this->send_deterministic && not this->recv_deterministic) {
313       XBT_INFO("*******************************************************");
314       XBT_INFO("**** Only-send-deterministic communication pattern ****");
315       XBT_INFO("*******************************************************");
316       XBT_INFO("%s", this->recv_diff);
317     }
318     if (not this->send_deterministic && this->recv_deterministic) {
319       XBT_INFO("*******************************************************");
320       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
321       XBT_INFO("*******************************************************");
322       XBT_INFO("%s", this->send_diff);
323     }
324   }
325   XBT_INFO("Expanded states = %lu", expanded_states_count_);
326   XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
327   XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
328   XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
329   if (_sg_mc_comms_determinism)
330     XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
331 }
332
333 void CommunicationDeterminismChecker::prepare()
334 {
335   const int maxpid = MC_smx_get_maxpid();
336
337   initial_communications_pattern.resize(maxpid);
338   incomplete_communications_pattern.resize(maxpid);
339
340   std::unique_ptr<simgrid::mc::State> initial_state(new simgrid::mc::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   for (auto& actor : mc_model_checker->process().actors())
346     if (simgrid::mc::actor_is_enabled(actor.copy.get_buffer()))
347       initial_state->add_interleaving_set(actor.copy.get_buffer());
348
349   stack_.push_back(std::move(initial_state));
350 }
351
352 static inline bool all_communications_are_finished()
353 {
354   for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
355     if (not incomplete_communications_pattern[current_actor].empty()) {
356       XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
357       return false;
358     }
359   }
360   return true;
361 }
362
363 void CommunicationDeterminismChecker::restoreState()
364 {
365   /* Intermediate backtracking */
366   simgrid::mc::State* last_state = stack_.back().get();
367   if (last_state->system_state) {
368     last_state->system_state->restore(&mc_model_checker->process());
369     MC_restore_communications_pattern(last_state);
370     return;
371   }
372
373   /* Restore the initial state */
374   simgrid::mc::session->restore_initial_state();
375
376   unsigned n = MC_smx_get_maxpid();
377   assert(n == incomplete_communications_pattern.size());
378   assert(n == initial_communications_pattern.size());
379   for (unsigned j=0; j < n ; j++) {
380     incomplete_communications_pattern[j].clear();
381     initial_communications_pattern[j].index_comm = 0;
382   }
383
384   /* Traverse the stack from the state at position start and re-execute the transitions */
385   for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
386     if (state == stack_.back())
387       break;
388
389     int req_num             = state->transition_.argument_;
390     smx_simcall_t saved_req = &state->executed_req_;
391     xbt_assert(saved_req);
392
393     /* because we got a copy of the executed request, we have to fetch the
394        real one, pointed by the request field of the issuer process */
395
396     const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
397     smx_simcall_t req = &issuer->simcall;
398
399     /* TODO : handle test and testany simcalls */
400     e_mc_call_type_t call = MC_get_call_type(req);
401     mc_model_checker->handle_simcall(state->transition_);
402     MC_handle_comm_pattern(call, req, req_num, 1);
403     mc_model_checker->wait_for_requests();
404
405     /* Update statistics */
406     mc_model_checker->visited_states++;
407     mc_model_checker->executed_transitions++;
408   }
409 }
410
411 void CommunicationDeterminismChecker::real_run()
412 {
413   std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
414   smx_simcall_t req = nullptr;
415
416   while (not stack_.empty()) {
417     /* Get current state */
418     simgrid::mc::State* cur_state = stack_.back().get();
419
420     XBT_DEBUG("**************************************************");
421     XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num_,
422               cur_state->interleave_size());
423
424     /* Update statistics */
425     mc_model_checker->visited_states++;
426
427     if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
428       req = MC_state_get_request(cur_state);
429     else
430       req = nullptr;
431
432     if (req != nullptr && visited_state == nullptr) {
433
434       int req_num = cur_state->transition_.argument_;
435
436       XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
437
438       std::string req_str;
439       if (dot_output != nullptr)
440         req_str = simgrid::mc::request_get_dot_output(req, req_num);
441
442       mc_model_checker->executed_transitions++;
443
444       /* TODO : handle test and testany simcalls */
445       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
446       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
447         call = MC_get_call_type(req);
448
449       /* Answer the request */
450       mc_model_checker->handle_simcall(cur_state->transition_);
451       /* After this call req is no longer useful */
452
453       MC_handle_comm_pattern(call, req, req_num, 0);
454
455       /* Wait for requests (schedules processes) */
456       mc_model_checker->wait_for_requests();
457
458       /* Create the new expanded state */
459       std::unique_ptr<simgrid::mc::State> next_state(new simgrid::mc::State(++expanded_states_count_));
460
461       /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
462        * least, data are transferred). These communications  are incomplete and they cannot be analyzed and compared
463        * with the initial pattern. */
464       bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
465
466       if (_sg_mc_max_visited_states != 0)
467         visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
468       else
469         visited_state = nullptr;
470
471       if (visited_state == nullptr) {
472
473         /* Get enabled actors and insert them in the interleave set of the next state */
474         for (auto& actor : mc_model_checker->process().actors())
475           if (simgrid::mc::actor_is_enabled(actor.copy.get_buffer()))
476             next_state->add_interleaving_set(actor.copy.get_buffer());
477
478         if (dot_output != nullptr)
479           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
480
481       } else if (dot_output != nullptr)
482         fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_,
483                 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
484
485       stack_.push_back(std::move(next_state));
486
487     } else {
488
489       if (stack_.size() > (std::size_t) _sg_mc_max_depth)
490         XBT_WARN("/!\\ Max depth reached! /!\\ ");
491       else if (visited_state != nullptr)
492         XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
493             visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
494       else
495         XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
496
497       this->initial_communications_pattern_done = true;
498
499       /* Trash the current state, no longer needed */
500       XBT_DEBUG("Delete state %d at depth %zu", cur_state->num_, stack_.size());
501       stack_.pop_back();
502
503       visited_state = nullptr;
504
505       /* Check for deadlocks */
506       if (mc_model_checker->checkDeadlock()) {
507         MC_show_deadlock();
508         throw simgrid::mc::DeadlockError();
509       }
510
511       while (not stack_.empty()) {
512         std::unique_ptr<simgrid::mc::State> state(std::move(stack_.back()));
513         stack_.pop_back();
514         if (state->interleave_size() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
515           /* We found a back-tracking point, let's loop */
516           XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num_, stack_.size() + 1);
517           stack_.push_back(std::move(state));
518
519           this->restoreState();
520
521           XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num_, stack_.size());
522
523           break;
524         } else {
525           XBT_DEBUG("Delete state %d at depth %zu", state->num_, stack_.size() + 1);
526         }
527       }
528     }
529   }
530
531   simgrid::mc::session->log_state();
532 }
533
534 void CommunicationDeterminismChecker::run()
535 {
536   XBT_INFO("Check communication determinism");
537   simgrid::mc::session->initialize();
538
539   this->prepare();
540
541   this->real_run();
542 }
543
544 Checker* createCommunicationDeterminismChecker(Session& s)
545 {
546   return new CommunicationDeterminismChecker(s);
547 }
548
549 }
550 }