1 /* Copyright (c) 2008-2022. The SimGrid Team. All rights reserved. */
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. */
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"
15 using api = simgrid::mc::Api;
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
22 enum class CallType { NONE, SEND, RECV, WAIT, WAITANY };
23 enum class CommPatternDifference { NONE, TYPE, RDV, TAG, SRC_PROC, DST_PROC, DATA_SIZE, DATA };
24 enum class PatternCommunicationType {
30 class PatternCommunication {
33 RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr{nullptr};
34 PatternCommunicationType type = PatternCommunicationType::send;
35 unsigned long src_proc = 0;
36 unsigned long dst_proc = 0;
38 std::vector<char> data;
42 PatternCommunication dup() const
44 simgrid::mc::PatternCommunication res;
46 res.comm_addr = this->comm_addr;
47 res.type = this->type;
50 res.dst_proc = this->dst_proc;
52 res.data = this->data;
54 res.index = this->index;
59 struct PatternCommunicationList {
60 unsigned int index_comm = 0;
61 std::vector<std::unique_ptr<simgrid::mc::PatternCommunication>> list;
64 static inline simgrid::mc::CallType MC_get_call_type(const s_smx_simcall* req)
66 using simgrid::mc::CallType;
67 using simgrid::simix::Simcall;
69 case Simcall::COMM_ISEND:
70 return CallType::SEND;
71 case Simcall::COMM_IRECV:
72 return CallType::RECV;
73 case Simcall::COMM_WAIT:
74 return CallType::WAIT;
75 case Simcall::COMM_WAITANY:
76 return CallType::WAITANY;
78 return CallType::NONE;
82 /********** Checker extension **********/
84 struct CommDetExtension {
85 static simgrid::xbt::Extension<simgrid::mc::Checker, CommDetExtension> EXTENSION_ID;
89 const unsigned long maxpid = api::get().get_maxpid();
91 initial_communications_pattern.resize(maxpid);
92 incomplete_communications_pattern.resize(maxpid);
95 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
96 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
98 bool initial_communications_pattern_done = false;
99 bool recv_deterministic = true;
100 bool send_deterministic = true;
101 std::string send_diff;
102 std::string recv_diff;
104 void restore_communications_pattern(const simgrid::mc::State* state);
105 void deterministic_comm_pattern(aid_t process, const PatternCommunication* comm, bool backtracking);
106 void get_comm_pattern(smx_simcall_t request, CallType call_type, bool backtracking);
107 void complete_comm_pattern(RemotePtr<kernel::activity::CommImpl> const& comm_addr, aid_t issuer, bool backtracking);
108 void handle_comm_pattern(const Transition* transition, smx_simcall_t req, int value, bool backtracking);
110 simgrid::xbt::Extension<simgrid::mc::Checker, CommDetExtension> CommDetExtension::EXTENSION_ID;
111 /********** State Extension ***********/
114 CommDetExtension* checker_;
117 std::vector<std::vector<simgrid::mc::PatternCommunication>> incomplete_comm_pattern_;
118 std::vector<unsigned> communication_indices_;
120 static simgrid::xbt::Extension<simgrid::mc::State, StateCommDet> EXTENSION_ID;
121 explicit StateCommDet(CommDetExtension* checker) : checker_(checker)
123 copy_incomplete_comm_pattern();
124 copy_index_comm_pattern();
127 void copy_incomplete_comm_pattern()
129 incomplete_comm_pattern_.clear();
130 const unsigned long maxpid = api::get().get_maxpid();
131 for (unsigned long i = 0; i < maxpid; i++) {
132 std::vector<simgrid::mc::PatternCommunication> res;
133 for (auto const& comm : checker_->incomplete_communications_pattern[i])
134 res.push_back(comm->dup());
135 incomplete_comm_pattern_.push_back(std::move(res));
139 void copy_index_comm_pattern()
141 communication_indices_.clear();
142 for (auto const& list_process_comm : checker_->initial_communications_pattern)
143 this->communication_indices_.push_back(list_process_comm.index_comm);
146 simgrid::xbt::Extension<simgrid::mc::State, StateCommDet> StateCommDet::EXTENSION_ID;
148 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
149 const simgrid::mc::PatternCommunication* comm2)
151 using simgrid::mc::CommPatternDifference;
152 if (comm1->type != comm2->type)
153 return CommPatternDifference::TYPE;
154 if (comm1->rdv != comm2->rdv)
155 return CommPatternDifference::RDV;
156 if (comm1->src_proc != comm2->src_proc)
157 return CommPatternDifference::SRC_PROC;
158 if (comm1->dst_proc != comm2->dst_proc)
159 return CommPatternDifference::DST_PROC;
160 if (comm1->tag != comm2->tag)
161 return CommPatternDifference::TAG;
162 if (comm1->data.size() != comm2->data.size())
163 return CommPatternDifference::DATA_SIZE;
164 if (comm1->data != comm2->data)
165 return CommPatternDifference::DATA;
166 return CommPatternDifference::NONE;
169 void CommDetExtension::restore_communications_pattern(const simgrid::mc::State* state)
171 for (size_t i = 0; i < initial_communications_pattern.size(); i++)
172 initial_communications_pattern[i].index_comm =
173 state->extension<simgrid::mc::StateCommDet>()->communication_indices_[i];
175 const unsigned long maxpid = api::get().get_maxpid();
176 for (unsigned long i = 0; i < maxpid; i++) {
177 incomplete_communications_pattern[i].clear();
178 for (simgrid::mc::PatternCommunication const& comm :
179 state->extension<simgrid::mc::StateCommDet>()->incomplete_comm_pattern_[i])
180 incomplete_communications_pattern[i].push_back(new simgrid::mc::PatternCommunication(comm.dup()));
184 static std::string print_determinism_result(simgrid::mc::CommPatternDifference diff, aid_t process,
185 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
190 if (comm->type == simgrid::mc::PatternCommunicationType::send)
191 type = xbt::string_printf("The send communications pattern of the process %ld is different!", process - 1);
193 type = xbt::string_printf("The recv communications pattern of the process %ld is different!", process - 1);
195 using simgrid::mc::CommPatternDifference;
197 case CommPatternDifference::TYPE:
198 res = xbt::string_printf("%s Different type for communication #%u", type.c_str(), cursor);
200 case CommPatternDifference::RDV:
201 res = xbt::string_printf("%s Different rdv for communication #%u", type.c_str(), cursor);
203 case CommPatternDifference::TAG:
204 res = xbt::string_printf("%s Different tag for communication #%u", type.c_str(), cursor);
206 case CommPatternDifference::SRC_PROC:
207 res = xbt::string_printf("%s Different source for communication #%u", type.c_str(), cursor);
209 case CommPatternDifference::DST_PROC:
210 res = xbt::string_printf("%s Different destination for communication #%u", type.c_str(), cursor);
212 case CommPatternDifference::DATA_SIZE:
213 res = xbt::string_printf("%s Different data size for communication #%u", type.c_str(), cursor);
215 case CommPatternDifference::DATA:
216 res = xbt::string_printf("%s Different data for communication #%u", type.c_str(), cursor);
226 void CommDetExtension::deterministic_comm_pattern(aid_t actor, const PatternCommunication* comm, bool backtracking)
228 if (not backtracking) {
229 PatternCommunicationList& list = initial_communications_pattern[actor];
230 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
232 if (diff != CommPatternDifference::NONE) {
233 if (comm->type == PatternCommunicationType::send) {
234 send_deterministic = false;
235 send_diff = print_determinism_result(diff, actor, comm, list.index_comm + 1);
237 recv_deterministic = false;
238 recv_diff = print_determinism_result(diff, actor, comm, list.index_comm + 1);
240 if (_sg_mc_send_determinism && not send_deterministic) {
241 XBT_INFO("*********************************************************");
242 XBT_INFO("***** Non-send-deterministic communications pattern *****");
243 XBT_INFO("*********************************************************");
244 XBT_INFO("%s", send_diff.c_str());
245 api::get().log_state();
246 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
247 } else if (_sg_mc_comms_determinism && (not send_deterministic && not recv_deterministic)) {
248 XBT_INFO("****************************************************");
249 XBT_INFO("***** Non-deterministic communications pattern *****");
250 XBT_INFO("****************************************************");
251 if (not send_diff.empty())
252 XBT_INFO("%s", send_diff.c_str());
253 if (not recv_diff.empty())
254 XBT_INFO("%s", recv_diff.c_str());
255 api::get().log_state();
256 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
262 /********** Non Static functions ***********/
264 void CommDetExtension::get_comm_pattern(smx_simcall_t request, CallType call_type, bool backtracking)
266 const smx_actor_t issuer = api::get().simcall_get_issuer(request);
267 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
268 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
270 auto pattern = std::make_unique<PatternCommunication>();
271 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
273 if (call_type == CallType::SEND) {
274 /* Create comm pattern */
275 pattern->type = PatternCommunicationType::send;
276 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
277 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
278 pattern->src_proc = api::get().get_pattern_comm_src_proc(pattern->comm_addr);
281 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
283 pattern->data = api::get().get_pattern_comm_data(pattern->comm_addr);
286 auto send_detached = api::get().check_send_request_detached(request);
288 if (initial_communications_pattern_done) {
289 /* Evaluate comm determinism */
290 deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
291 initial_communications_pattern[pattern->src_proc].index_comm++;
293 /* Store comm pattern */
294 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
299 } else if (call_type == CallType::RECV) {
300 pattern->type = PatternCommunicationType::receive;
301 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
304 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
306 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
307 pattern->dst_proc = api::get().get_pattern_comm_dst_proc(pattern->comm_addr);
309 xbt_die("Unexpected call_type %i", (int)call_type);
311 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
312 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
315 void CommDetExtension::complete_comm_pattern(RemotePtr<kernel::activity::CommImpl> const& comm_addr, aid_t issuer,
318 /* Complete comm pattern */
319 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
320 auto current_comm_pattern =
321 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
322 [&comm_addr](const PatternCommunication* comm) { return (comm->comm_addr == comm_addr); });
323 xbt_assert(current_comm_pattern != std::end(incomplete_pattern), "Corresponding communication not found!");
324 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
326 comm_pattern->src_proc = api::get().get_src_actor(comm_addr)->get_pid();
327 comm_pattern->dst_proc = api::get().get_dst_actor(comm_addr)->get_pid();
328 if (comm_pattern->data.empty())
329 comm_pattern->data = api::get().get_pattern_comm_data(comm_addr);
331 XBT_DEBUG("Remove incomplete comm pattern for process %ld at cursor %zd", issuer,
332 std::distance(begin(incomplete_pattern), current_comm_pattern));
333 incomplete_pattern.erase(current_comm_pattern);
335 if (initial_communications_pattern_done) {
336 /* Evaluate comm determinism */
337 deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
338 initial_communications_pattern[issuer].index_comm++;
340 /* Store comm pattern */
341 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
345 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session* session) : Checker(session)
347 CommDetExtension::EXTENSION_ID = simgrid::mc::Checker::extension_create<CommDetExtension>();
348 StateCommDet::EXTENSION_ID = simgrid::mc::State::extension_create<StateCommDet>();
351 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
353 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
356 for (auto const& state : stack_)
357 res.push_back(state->get_transition());
361 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
363 std::vector<std::string> trace;
364 for (auto const& state : stack_)
365 trace.push_back(state->get_transition()->to_string());
369 void CommunicationDeterminismChecker::log_state() // override
371 if (_sg_mc_comms_determinism) {
372 if (extension<CommDetExtension>()->send_deterministic && not extension<CommDetExtension>()->recv_deterministic) {
373 XBT_INFO("*******************************************************");
374 XBT_INFO("**** Only-send-deterministic communication pattern ****");
375 XBT_INFO("*******************************************************");
376 XBT_INFO("%s", extension<CommDetExtension>()->recv_diff.c_str());
378 if (not extension<CommDetExtension>()->send_deterministic && extension<CommDetExtension>()->recv_deterministic) {
379 XBT_INFO("*******************************************************");
380 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
381 XBT_INFO("*******************************************************");
382 XBT_INFO("%s", extension<CommDetExtension>()->send_diff.c_str());
385 XBT_INFO("Expanded states = %ld", State::get_expanded_states());
386 XBT_INFO("Visited states = %lu", api::get().mc_get_visited_states());
387 XBT_INFO("Executed transitions = %lu", Transition::get_executed_transitions());
388 XBT_INFO("Send-deterministic : %s", extension<CommDetExtension>()->send_deterministic ? "Yes" : "No");
389 if (_sg_mc_comms_determinism)
390 XBT_INFO("Recv-deterministic : %s", extension<CommDetExtension>()->recv_deterministic ? "Yes" : "No");
393 void CommunicationDeterminismChecker::prepare()
395 auto initial_state = std::make_unique<State>();
396 extension_set(new CommDetExtension());
397 initial_state->extension_set(new StateCommDet(extension<CommDetExtension>()));
399 XBT_DEBUG("********* Start communication determinism verification *********");
401 /* Add all enabled actors to the interleave set of the initial state */
402 for (auto& act : api::get().get_actors()) {
403 auto actor = act.copy.get_buffer();
404 if (get_session().actor_is_enabled(actor->get_pid()))
405 initial_state->mark_todo(actor->get_pid());
408 stack_.push_back(std::move(initial_state));
411 void CommunicationDeterminismChecker::restoreState()
413 auto extension = this->extension<CommDetExtension>();
415 /* If asked to rollback on a state that has a snapshot, restore it */
416 State* last_state = stack_.back().get();
417 if (last_state->system_state_) {
418 api::get().restore_state(last_state->system_state_);
419 extension->restore_communications_pattern(last_state);
423 /* if no snapshot, we need to restore the initial state and replay the transitions */
424 get_session().restore_initial_state();
426 const unsigned long maxpid = api::get().get_maxpid();
427 assert(maxpid == extension->incomplete_communications_pattern.size());
428 assert(maxpid == extension->initial_communications_pattern.size());
429 for (unsigned long j = 0; j < maxpid; j++) {
430 extension->incomplete_communications_pattern[j].clear();
431 extension->initial_communications_pattern[j].index_comm = 0;
434 /* Traverse the stack from the state at position start and re-execute the transitions */
435 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
436 if (state == stack_.back())
439 auto* transition = state->get_transition();
441 /* because we got a copy of the executed request, we have to fetch the real one,
442 pointed by the request field of the issuer process */
443 auto* saved_req = &state->executed_req_;
444 const smx_actor_t issuer = api::get().simcall_get_issuer(saved_req);
445 smx_simcall_t req = &issuer->simcall_;
447 /* TODO : handle test and testany simcalls */
448 transition->replay();
449 extension->handle_comm_pattern(transition, req, transition->times_considered_, true);
451 /* Update statistics */
452 api::get().mc_inc_visited_states();
456 void CommDetExtension::handle_comm_pattern(const Transition* transition, smx_simcall_t req, int value,
459 if (not _sg_mc_comms_determinism && not _sg_mc_send_determinism)
462 using simgrid::mc::CallType;
463 switch (MC_get_call_type(req)) {
467 get_comm_pattern(req, CallType::SEND, backtracking);
470 get_comm_pattern(req, CallType::RECV, backtracking);
472 case CallType::WAIT: {
473 auto comm_addr = remote(simcall_comm_wait__getraw__comm(req));
474 auto simcall_issuer = api::get().simcall_get_issuer(req);
475 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
478 case CallType::WAITANY: {
479 auto comm_addr = api::get().get_comm_waitany_raw_addr(req, value);
480 auto simcall_issuer = api::get().simcall_get_issuer(req);
481 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
484 xbt_die("Unexpected call type %i", (int)MC_get_call_type(req));
488 void CommunicationDeterminismChecker::real_run()
490 auto extension = this->extension<CommDetExtension>();
492 std::unique_ptr<VisitedState> visited_state = nullptr;
493 const unsigned long maxpid = api::get().get_maxpid();
495 while (not stack_.empty()) {
496 /* Get current state */
497 State* cur_state = stack_.back().get();
499 XBT_DEBUG("**************************************************");
500 XBT_DEBUG("Exploration depth = %zu (state = %ld, interleaved processes = %zu)", stack_.size(), cur_state->num_,
501 cur_state->count_todo());
503 /* Update statistics */
504 api::get().mc_inc_visited_states();
506 int next_transition = -1;
507 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
508 next_transition = cur_state->next_transition();
510 if (next_transition >= 0 && visited_state == nullptr) {
511 cur_state->execute_next(next_transition);
513 auto* transition = cur_state->get_transition();
514 smx_simcall_t req = &cur_state->executed_req_;
516 XBT_DEBUG("Execute: %s", transition->to_string().c_str());
519 if (dot_output != nullptr)
520 req_str = api::get().request_get_dot_output(transition);
522 extension->handle_comm_pattern(transition, req, transition->times_considered_, false);
524 /* Create the new expanded state */
525 auto next_state = std::make_unique<State>();
526 next_state->extension_set(new StateCommDet(extension));
528 bool all_communications_are_finished = true;
529 for (size_t current_actor = 1; all_communications_are_finished && current_actor < maxpid; current_actor++) {
530 if (not extension->incomplete_communications_pattern[current_actor].empty()) {
531 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
532 all_communications_are_finished = false;
536 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
537 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
538 * with the initial pattern. */
539 bool compare_snapshots = extension->initial_communications_pattern_done && all_communications_are_finished;
541 if (_sg_mc_max_visited_states != 0)
542 visited_state = visited_states_.addVisitedState(next_state->num_, next_state.get(), compare_snapshots);
544 visited_state = nullptr;
546 if (visited_state == nullptr) {
547 /* Add all enabled actors to the interleave set of the next state */
548 for (auto& act : api::get().get_actors()) {
549 auto actor = act.copy.get_buffer();
550 if (get_session().actor_is_enabled(actor->get_pid()))
551 next_state->mark_todo(actor->get_pid());
554 if (dot_output != nullptr)
555 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
557 } else if (dot_output != nullptr)
558 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_,
559 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
561 stack_.push_back(std::move(next_state));
563 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
564 XBT_WARN("/!\\ Max depth reached! /!\\ ");
565 else if (visited_state != nullptr)
566 XBT_DEBUG("State already visited (equal to state %ld), exploration stopped on this path.",
567 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
569 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
571 extension->initial_communications_pattern_done = true;
573 /* Trash the current state, no longer needed */
574 XBT_DEBUG("Delete state %ld at depth %zu", cur_state->num_, stack_.size());
577 visited_state = nullptr;
579 api::get().mc_check_deadlock();
581 while (not stack_.empty()) {
582 std::unique_ptr<State> state(std::move(stack_.back()));
584 if (state->count_todo() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
585 /* We found a back-tracking point, let's loop */
586 XBT_DEBUG("Back-tracking to state %ld at depth %zu", state->num_, stack_.size() + 1);
587 stack_.push_back(std::move(state));
589 this->restoreState();
591 XBT_DEBUG("Back-tracking to state %ld at depth %zu done", stack_.back()->num_, stack_.size());
595 XBT_DEBUG("Delete state %ld at depth %zu", state->num_, stack_.size() + 1);
601 api::get().log_state();
604 void CommunicationDeterminismChecker::run()
606 XBT_INFO("Check communication determinism");
607 get_session().take_initial_snapshot();
613 Checker* create_communication_determinism_checker(Session* session)
615 return new CommunicationDeterminismChecker(session);
619 } // namespace simgrid