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(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(simgrid::mc::CallType call_type, 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 static void patterns_copy(std::vector<simgrid::mc::PatternCommunication*>& dest,
170 std::vector<simgrid::mc::PatternCommunication> const& source)
173 for (simgrid::mc::PatternCommunication const& comm : source) {
174 auto* copy_comm = new simgrid::mc::PatternCommunication(comm.dup());
175 dest.push_back(copy_comm);
179 void CommDetExtension::restore_communications_pattern(simgrid::mc::State* state)
181 for (size_t i = 0; i < initial_communications_pattern.size(); i++)
182 initial_communications_pattern[i].index_comm =
183 state->extension<simgrid::mc::StateCommDet>()->communication_indices_[i];
185 const unsigned long maxpid = api::get().get_maxpid();
186 for (unsigned long i = 0; i < maxpid; i++)
187 patterns_copy(incomplete_communications_pattern[i],
188 state->extension<simgrid::mc::StateCommDet>()->incomplete_comm_pattern_[i]);
191 static std::string print_determinism_result(simgrid::mc::CommPatternDifference diff, aid_t process,
192 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
197 if (comm->type == simgrid::mc::PatternCommunicationType::send)
198 type = xbt::string_printf("The send communications pattern of the process %ld is different!", process - 1);
200 type = xbt::string_printf("The recv communications pattern of the process %ld is different!", process - 1);
202 using simgrid::mc::CommPatternDifference;
204 case CommPatternDifference::TYPE:
205 res = xbt::string_printf("%s Different type for communication #%u", type.c_str(), cursor);
207 case CommPatternDifference::RDV:
208 res = xbt::string_printf("%s Different rdv for communication #%u", type.c_str(), cursor);
210 case CommPatternDifference::TAG:
211 res = xbt::string_printf("%s Different tag for communication #%u", type.c_str(), cursor);
213 case CommPatternDifference::SRC_PROC:
214 res = xbt::string_printf("%s Different source for communication #%u", type.c_str(), cursor);
216 case CommPatternDifference::DST_PROC:
217 res = xbt::string_printf("%s Different destination for communication #%u", type.c_str(), cursor);
219 case CommPatternDifference::DATA_SIZE:
220 res = xbt::string_printf("%s Different data size for communication #%u", type.c_str(), cursor);
222 case CommPatternDifference::DATA:
223 res = xbt::string_printf("%s Different data for communication #%u", type.c_str(), cursor);
233 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
234 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> const& comm_addr)
236 auto src_proc = api::get().get_src_actor(comm_addr);
237 auto dst_proc = api::get().get_dst_actor(comm_addr);
238 comm_pattern->src_proc = src_proc->get_pid();
239 comm_pattern->dst_proc = dst_proc->get_pid();
241 if (comm_pattern->data.empty()) {
242 comm_pattern->data = api::get().get_pattern_comm_data(comm_addr);
246 void CommDetExtension::deterministic_comm_pattern(aid_t actor, const PatternCommunication* comm, bool backtracking)
248 if (not backtracking) {
249 PatternCommunicationList& list = initial_communications_pattern[actor];
250 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
252 if (diff != CommPatternDifference::NONE) {
253 if (comm->type == PatternCommunicationType::send) {
254 send_deterministic = false;
255 send_diff = print_determinism_result(diff, actor, comm, list.index_comm + 1);
257 recv_deterministic = false;
258 recv_diff = print_determinism_result(diff, actor, comm, list.index_comm + 1);
260 if (_sg_mc_send_determinism && not send_deterministic) {
261 XBT_INFO("*********************************************************");
262 XBT_INFO("***** Non-send-deterministic communications pattern *****");
263 XBT_INFO("*********************************************************");
264 XBT_INFO("%s", send_diff.c_str());
265 api::get().log_state();
266 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
267 } else if (_sg_mc_comms_determinism && (not send_deterministic && not recv_deterministic)) {
268 XBT_INFO("****************************************************");
269 XBT_INFO("***** Non-deterministic communications pattern *****");
270 XBT_INFO("****************************************************");
271 if (not send_diff.empty())
272 XBT_INFO("%s", send_diff.c_str());
273 if (not recv_diff.empty())
274 XBT_INFO("%s", recv_diff.c_str());
275 api::get().log_state();
276 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
282 /********** Non Static functions ***********/
284 void CommDetExtension::get_comm_pattern(smx_simcall_t request, CallType call_type, bool backtracking)
286 const smx_actor_t issuer = api::get().simcall_get_issuer(request);
287 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
288 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
290 auto pattern = std::make_unique<PatternCommunication>();
291 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
293 if (call_type == CallType::SEND) {
294 /* Create comm pattern */
295 pattern->type = PatternCommunicationType::send;
296 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
297 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
298 pattern->src_proc = api::get().get_pattern_comm_src_proc(pattern->comm_addr);
301 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
303 pattern->data = api::get().get_pattern_comm_data(pattern->comm_addr);
306 auto send_detached = api::get().check_send_request_detached(request);
308 if (initial_communications_pattern_done) {
309 /* Evaluate comm determinism */
310 deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
311 initial_communications_pattern[pattern->src_proc].index_comm++;
313 /* Store comm pattern */
314 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
319 } else if (call_type == CallType::RECV) {
320 pattern->type = PatternCommunicationType::receive;
321 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
324 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
326 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
327 pattern->dst_proc = api::get().get_pattern_comm_dst_proc(pattern->comm_addr);
329 xbt_die("Unexpected call_type %i", (int)call_type);
331 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
332 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
335 void CommDetExtension::complete_comm_pattern(RemotePtr<kernel::activity::CommImpl> const& comm_addr, aid_t issuer,
338 /* Complete comm pattern */
339 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
340 auto current_comm_pattern =
341 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
342 [&comm_addr](const PatternCommunication* comm) { return (comm->comm_addr == comm_addr); });
343 xbt_assert(current_comm_pattern != std::end(incomplete_pattern), "Corresponding communication not found!");
345 update_comm_pattern(*current_comm_pattern, comm_addr);
346 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
347 XBT_DEBUG("Remove incomplete comm pattern for process %ld at cursor %zd", issuer,
348 std::distance(begin(incomplete_pattern), current_comm_pattern));
349 incomplete_pattern.erase(current_comm_pattern);
351 if (initial_communications_pattern_done) {
352 /* Evaluate comm determinism */
353 deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
354 initial_communications_pattern[issuer].index_comm++;
356 /* Store comm pattern */
357 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
361 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session* session) : Checker(session)
363 CommDetExtension::EXTENSION_ID = simgrid::mc::Checker::extension_create<CommDetExtension>();
364 StateCommDet::EXTENSION_ID = simgrid::mc::State::extension_create<StateCommDet>();
367 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
369 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
372 for (auto const& state : stack_)
373 res.push_back(state->get_transition());
377 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
379 std::vector<std::string> trace;
380 for (auto const& state : stack_)
381 trace.push_back(state->get_transition()->to_string());
385 void CommunicationDeterminismChecker::log_state() // override
387 if (_sg_mc_comms_determinism) {
388 if (extension<CommDetExtension>()->send_deterministic && not extension<CommDetExtension>()->recv_deterministic) {
389 XBT_INFO("*******************************************************");
390 XBT_INFO("**** Only-send-deterministic communication pattern ****");
391 XBT_INFO("*******************************************************");
392 XBT_INFO("%s", extension<CommDetExtension>()->recv_diff.c_str());
394 if (not extension<CommDetExtension>()->send_deterministic && extension<CommDetExtension>()->recv_deterministic) {
395 XBT_INFO("*******************************************************");
396 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
397 XBT_INFO("*******************************************************");
398 XBT_INFO("%s", extension<CommDetExtension>()->send_diff.c_str());
401 XBT_INFO("Expanded states = %ld", State::get_expanded_states());
402 XBT_INFO("Visited states = %lu", api::get().mc_get_visited_states());
403 XBT_INFO("Executed transitions = %lu", Transition::get_executed_transitions());
404 XBT_INFO("Send-deterministic : %s", extension<CommDetExtension>()->send_deterministic ? "Yes" : "No");
405 if (_sg_mc_comms_determinism)
406 XBT_INFO("Recv-deterministic : %s", extension<CommDetExtension>()->recv_deterministic ? "Yes" : "No");
409 void CommunicationDeterminismChecker::prepare()
411 auto initial_state = std::make_unique<State>();
412 extension_set(new CommDetExtension());
413 initial_state->extension_set(new StateCommDet(extension<CommDetExtension>()));
415 XBT_DEBUG("********* Start communication determinism verification *********");
417 /* Add all enabled actors to the interleave set of the initial state */
418 for (auto& act : api::get().get_actors()) {
419 auto actor = act.copy.get_buffer();
420 if (get_session().actor_is_enabled(actor->get_pid()))
421 initial_state->mark_todo(actor->get_pid());
424 stack_.push_back(std::move(initial_state));
427 void CommunicationDeterminismChecker::restoreState()
429 auto extension = this->extension<CommDetExtension>();
431 /* Intermediate backtracking */
432 State* last_state = stack_.back().get();
433 if (last_state->system_state_) {
434 api::get().restore_state(last_state->system_state_);
435 extension->restore_communications_pattern(last_state);
439 get_session().restore_initial_state();
441 const unsigned long maxpid = api::get().get_maxpid();
442 assert(maxpid == extension->incomplete_communications_pattern.size());
443 assert(maxpid == extension->initial_communications_pattern.size());
444 for (unsigned long j = 0; j < maxpid; j++) {
445 extension->incomplete_communications_pattern[j].clear();
446 extension->initial_communications_pattern[j].index_comm = 0;
449 /* Traverse the stack from the state at position start and re-execute the transitions */
450 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
451 if (state == stack_.back())
454 int req_num = state->get_transition()->times_considered_;
455 const s_smx_simcall* saved_req = &state->executed_req_;
456 xbt_assert(saved_req);
458 /* because we got a copy of the executed request, we have to fetch the
459 real one, pointed by the request field of the issuer process */
461 const smx_actor_t issuer = api::get().simcall_get_issuer(saved_req);
462 smx_simcall_t req = &issuer->simcall_;
464 /* TODO : handle test and testany simcalls */
465 CallType call = MC_get_call_type(req);
466 state->get_transition()->replay();
467 extension->handle_comm_pattern(call, req, req_num, true);
469 /* Update statistics */
470 api::get().mc_inc_visited_states();
474 void CommDetExtension::handle_comm_pattern(simgrid::mc::CallType call_type, smx_simcall_t req, int value,
477 using simgrid::mc::CallType;
483 get_comm_pattern(req, call_type, backtracking);
486 case CallType::WAITANY: {
487 RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr;
488 if (call_type == CallType::WAIT)
489 comm_addr = remote(simcall_comm_wait__getraw__comm(req));
491 comm_addr = api::get().get_comm_waitany_raw_addr(req, value);
492 auto simcall_issuer = api::get().simcall_get_issuer(req);
493 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
496 xbt_die("Unexpected call type %i", (int)call_type);
500 void CommunicationDeterminismChecker::real_run()
502 auto extension = this->extension<CommDetExtension>();
504 std::unique_ptr<VisitedState> visited_state = nullptr;
505 const unsigned long maxpid = api::get().get_maxpid();
507 while (not stack_.empty()) {
508 /* Get current state */
509 State* cur_state = stack_.back().get();
511 XBT_DEBUG("**************************************************");
512 XBT_DEBUG("Exploration depth = %zu (state = %ld, interleaved processes = %zu)", stack_.size(), cur_state->num_,
513 cur_state->count_todo());
515 /* Update statistics */
516 api::get().mc_inc_visited_states();
518 int next_transition = -1;
519 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
520 next_transition = cur_state->next_transition();
522 if (next_transition >= 0 && visited_state == nullptr) {
523 cur_state->execute_next(next_transition);
525 int req_num = cur_state->get_transition()->times_considered_;
526 smx_simcall_t req = &cur_state->executed_req_;
528 XBT_DEBUG("Execute: %s", cur_state->get_transition()->to_string().c_str());
531 if (dot_output != nullptr)
532 req_str = api::get().request_get_dot_output(cur_state->get_transition());
534 /* TODO : handle test and testany simcalls */
535 CallType call = CallType::NONE;
536 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
537 call = MC_get_call_type(req);
539 extension->handle_comm_pattern(call, req, req_num, false);
541 /* Create the new expanded state */
542 auto next_state = std::make_unique<State>();
543 next_state->extension_set(new StateCommDet(extension));
545 bool all_communications_are_finished = true;
546 for (size_t current_actor = 1; all_communications_are_finished && current_actor < maxpid; current_actor++) {
547 if (not extension->incomplete_communications_pattern[current_actor].empty()) {
548 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
549 all_communications_are_finished = false;
553 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
554 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
555 * with the initial pattern. */
556 bool compare_snapshots = extension->initial_communications_pattern_done && all_communications_are_finished;
558 if (_sg_mc_max_visited_states != 0)
559 visited_state = visited_states_.addVisitedState(next_state->num_, next_state.get(), compare_snapshots);
561 visited_state = nullptr;
563 if (visited_state == nullptr) {
564 /* Add all enabled actors to the interleave set of the next state */
565 for (auto& act : api::get().get_actors()) {
566 auto actor = act.copy.get_buffer();
567 if (get_session().actor_is_enabled(actor->get_pid()))
568 next_state->mark_todo(actor->get_pid());
571 if (dot_output != nullptr)
572 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
574 } else if (dot_output != nullptr)
575 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_,
576 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
578 stack_.push_back(std::move(next_state));
580 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
581 XBT_WARN("/!\\ Max depth reached! /!\\ ");
582 else if (visited_state != nullptr)
583 XBT_DEBUG("State already visited (equal to state %ld), exploration stopped on this path.",
584 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
586 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
588 extension->initial_communications_pattern_done = true;
590 /* Trash the current state, no longer needed */
591 XBT_DEBUG("Delete state %ld at depth %zu", cur_state->num_, stack_.size());
594 visited_state = nullptr;
596 api::get().mc_check_deadlock();
598 while (not stack_.empty()) {
599 std::unique_ptr<State> state(std::move(stack_.back()));
601 if (state->count_todo() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
602 /* We found a back-tracking point, let's loop */
603 XBT_DEBUG("Back-tracking to state %ld at depth %zu", state->num_, stack_.size() + 1);
604 stack_.push_back(std::move(state));
606 this->restoreState();
608 XBT_DEBUG("Back-tracking to state %ld at depth %zu done", stack_.back()->num_, stack_.size());
612 XBT_DEBUG("Delete state %ld at depth %zu", state->num_, stack_.size() + 1);
618 api::get().log_state();
621 void CommunicationDeterminismChecker::run()
623 XBT_INFO("Check communication determinism");
624 get_session().take_initial_snapshot();
630 Checker* create_communication_determinism_checker(Session* session)
632 return new CommunicationDeterminismChecker(session);
636 } // namespace simgrid