Logo AND Algorithmique Numérique Distribuée

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