Logo AND Algorithmique Numérique Distribuée

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