Logo AND Algorithmique Numérique Distribuée

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