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");
19 /********** Global variables **********/
21 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
22 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
24 /********** Static functions ***********/
26 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
27 const simgrid::mc::PatternCommunication* comm2)
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;
47 static void patterns_copy(std::vector<simgrid::mc::PatternCommunication*>& dest,
48 std::vector<simgrid::mc::PatternCommunication> const& source)
51 for (simgrid::mc::PatternCommunication const& comm : source) {
52 auto* copy_comm = new simgrid::mc::PatternCommunication(comm.dup());
53 dest.push_back(copy_comm);
57 static void restore_communications_pattern(simgrid::mc::State* state)
59 for (size_t i = 0; i < initial_communications_pattern.size(); i++)
60 initial_communications_pattern[i].index_comm = state->communication_indices_[i];
62 const unsigned long maxpid = api::get().get_maxpid();
63 for (unsigned long i = 0; i < maxpid; i++)
64 patterns_copy(incomplete_communications_pattern[i], state->incomplete_comm_pattern_[i]);
67 static char* print_determinism_result(simgrid::mc::CommPatternDifference diff, aid_t process,
68 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
73 if (comm->type == simgrid::mc::PatternCommunicationType::send)
74 type = bprintf("The send communications pattern of the process %ld is different!", process - 1);
76 type = bprintf("The recv communications pattern of the process %ld is different!", process - 1);
78 using simgrid::mc::CommPatternDifference;
80 case CommPatternDifference::TYPE:
81 res = bprintf("%s Different type for communication #%u", type, cursor);
83 case CommPatternDifference::RDV:
84 res = bprintf("%s Different rdv for communication #%u", type, cursor);
86 case CommPatternDifference::TAG:
87 res = bprintf("%s Different tag for communication #%u", type, cursor);
89 case CommPatternDifference::SRC_PROC:
90 res = bprintf("%s Different source for communication #%u", type, cursor);
92 case CommPatternDifference::DST_PROC:
93 res = bprintf("%s Different destination for communication #%u", type, cursor);
95 case CommPatternDifference::DATA_SIZE:
96 res = bprintf("%s Different data size for communication #%u", type, cursor);
98 case CommPatternDifference::DATA:
99 res = bprintf("%s Different data for communication #%u", type, cursor);
109 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
110 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> const& comm_addr)
112 auto src_proc = api::get().get_src_actor(comm_addr);
113 auto dst_proc = api::get().get_dst_actor(comm_addr);
114 comm_pattern->src_proc = src_proc->get_pid();
115 comm_pattern->dst_proc = dst_proc->get_pid();
117 if (comm_pattern->data.empty()) {
118 comm_pattern->data = api::get().get_pattern_comm_data(comm_addr);
125 void CommunicationDeterminismChecker::deterministic_comm_pattern(aid_t process, const PatternCommunication* comm,
128 if (not backtracking) {
129 PatternCommunicationList& list = initial_communications_pattern[process];
130 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
132 if (diff != CommPatternDifference::NONE) {
133 if (comm->type == PatternCommunicationType::send) {
134 this->send_deterministic = false;
135 if (this->send_diff != nullptr)
136 xbt_free(this->send_diff);
137 this->send_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
139 this->recv_deterministic = false;
140 if (this->recv_diff != nullptr)
141 xbt_free(this->recv_diff);
142 this->recv_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
144 if (_sg_mc_send_determinism && not this->send_deterministic) {
145 XBT_INFO("*********************************************************");
146 XBT_INFO("***** Non-send-deterministic communications pattern *****");
147 XBT_INFO("*********************************************************");
148 XBT_INFO("%s", this->send_diff);
149 xbt_free(this->send_diff);
150 this->send_diff = nullptr;
151 api::get().log_state();
152 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
153 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
154 XBT_INFO("****************************************************");
155 XBT_INFO("***** Non-deterministic communications pattern *****");
156 XBT_INFO("****************************************************");
157 if (this->send_diff) {
158 XBT_INFO("%s", this->send_diff);
159 xbt_free(this->send_diff);
160 this->send_diff = nullptr;
162 if (this->recv_diff) {
163 XBT_INFO("%s", this->recv_diff);
164 xbt_free(this->recv_diff);
165 this->recv_diff = nullptr;
167 api::get().log_state();
168 api::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
174 /********** Non Static functions ***********/
176 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, CallType call_type, bool backtracking)
178 const smx_actor_t issuer = api::get().simcall_get_issuer(request);
179 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
180 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
182 auto pattern = std::make_unique<PatternCommunication>();
183 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
185 if (call_type == CallType::SEND) {
186 /* Create comm pattern */
187 pattern->type = PatternCommunicationType::send;
188 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
189 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
190 pattern->src_proc = api::get().get_pattern_comm_src_proc(pattern->comm_addr);
193 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
195 pattern->data = api::get().get_pattern_comm_data(pattern->comm_addr);
198 auto send_detached = api::get().check_send_request_detached(request);
200 if (this->initial_communications_pattern_done) {
201 /* Evaluate comm determinism */
202 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
203 initial_communications_pattern[pattern->src_proc].index_comm++;
205 /* Store comm pattern */
206 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
211 } else if (call_type == CallType::RECV) {
212 pattern->type = PatternCommunicationType::receive;
213 pattern->comm_addr = api::get().get_comm_isend_raw_addr(request);
216 pattern->tag = api::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
218 pattern->rdv = api::get().get_pattern_comm_rdv(pattern->comm_addr);
219 pattern->dst_proc = api::get().get_pattern_comm_dst_proc(pattern->comm_addr);
221 xbt_die("Unexpected call_type %i", (int)call_type);
223 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
224 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
227 void CommunicationDeterminismChecker::complete_comm_pattern(RemotePtr<kernel::activity::CommImpl> const& comm_addr,
228 aid_t issuer, bool backtracking)
230 /* Complete comm pattern */
231 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
232 auto current_comm_pattern =
233 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
234 [&comm_addr](const PatternCommunication* comm) { return (comm->comm_addr == comm_addr); });
235 xbt_assert(current_comm_pattern != std::end(incomplete_pattern), "Corresponding communication not found!");
237 update_comm_pattern(*current_comm_pattern, comm_addr);
238 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
239 XBT_DEBUG("Remove incomplete comm pattern for process %ld at cursor %zd", issuer,
240 std::distance(begin(incomplete_pattern), current_comm_pattern));
241 incomplete_pattern.erase(current_comm_pattern);
243 if (this->initial_communications_pattern_done) {
244 /* Evaluate comm determinism */
245 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
246 initial_communications_pattern[issuer].index_comm++;
248 /* Store comm pattern */
249 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
253 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session* session) : Checker(session) {}
255 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
257 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
260 for (auto const& state : stack_)
261 res.push_back(state->get_transition());
265 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
267 std::vector<std::string> trace;
268 for (auto const& state : stack_)
269 trace.push_back(state->get_transition()->to_string());
273 void CommunicationDeterminismChecker::log_state() // override
275 if (_sg_mc_comms_determinism) {
276 if (this->send_deterministic && not this->recv_deterministic) {
277 XBT_INFO("*******************************************************");
278 XBT_INFO("**** Only-send-deterministic communication pattern ****");
279 XBT_INFO("*******************************************************");
280 XBT_INFO("%s", this->recv_diff);
282 if (not this->send_deterministic && this->recv_deterministic) {
283 XBT_INFO("*******************************************************");
284 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
285 XBT_INFO("*******************************************************");
286 XBT_INFO("%s", this->send_diff);
289 XBT_INFO("Expanded states = %ld", State::get_expanded_states());
290 XBT_INFO("Visited states = %lu", api::get().mc_get_visited_states());
291 XBT_INFO("Executed transitions = %lu", Transition::get_executed_transitions());
292 XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
293 if (_sg_mc_comms_determinism)
294 XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
297 void CommunicationDeterminismChecker::prepare()
299 const unsigned long maxpid = api::get().get_maxpid();
301 initial_communications_pattern.resize(maxpid);
302 incomplete_communications_pattern.resize(maxpid);
304 auto initial_state = std::make_unique<State>();
306 XBT_DEBUG("********* Start communication determinism verification *********");
308 /* Add all enabled actors to the interleave set of the initial state */
309 for (auto& act : api::get().get_actors()) {
310 auto actor = act.copy.get_buffer();
311 if (get_session().actor_is_enabled(actor->get_pid()))
312 initial_state->mark_todo(actor->get_pid());
315 stack_.push_back(std::move(initial_state));
318 static inline bool all_communications_are_finished()
320 const unsigned long maxpid = api::get().get_maxpid();
321 for (size_t current_actor = 1; current_actor < maxpid; current_actor++) {
322 if (not incomplete_communications_pattern[current_actor].empty()) {
323 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
330 void CommunicationDeterminismChecker::restoreState()
332 /* Intermediate backtracking */
333 State* last_state = stack_.back().get();
334 if (last_state->system_state_) {
335 api::get().restore_state(last_state->system_state_);
336 restore_communications_pattern(last_state);
340 get_session().restore_initial_state();
342 const unsigned long maxpid = api::get().get_maxpid();
343 assert(maxpid == incomplete_communications_pattern.size());
344 assert(maxpid == initial_communications_pattern.size());
345 for (unsigned long j = 0; j < maxpid; j++) {
346 incomplete_communications_pattern[j].clear();
347 initial_communications_pattern[j].index_comm = 0;
350 /* Traverse the stack from the state at position start and re-execute the transitions */
351 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
352 if (state == stack_.back())
355 int req_num = state->get_transition()->times_considered_;
356 const s_smx_simcall* saved_req = &state->executed_req_;
357 xbt_assert(saved_req);
359 /* because we got a copy of the executed request, we have to fetch the
360 real one, pointed by the request field of the issuer process */
362 const smx_actor_t issuer = api::get().simcall_get_issuer(saved_req);
363 smx_simcall_t req = &issuer->simcall_;
365 /* TODO : handle test and testany simcalls */
366 CallType call = MC_get_call_type(req);
367 state->get_transition()->replay();
368 handle_comm_pattern(call, req, req_num, 1);
370 /* Update statistics */
371 api::get().mc_inc_visited_states();
375 void CommunicationDeterminismChecker::handle_comm_pattern(simgrid::mc::CallType call_type, smx_simcall_t req, int value,
378 using simgrid::mc::CallType;
384 get_comm_pattern(req, call_type, backtracking);
387 case CallType::WAITANY: {
388 RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr;
389 if (call_type == CallType::WAIT)
390 comm_addr = remote(simcall_comm_wait__getraw__comm(req));
392 comm_addr = api::get().get_comm_waitany_raw_addr(req, value);
393 auto simcall_issuer = api::get().simcall_get_issuer(req);
394 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
397 xbt_die("Unexpected call type %i", (int)call_type);
401 void CommunicationDeterminismChecker::real_run()
403 std::unique_ptr<VisitedState> visited_state = nullptr;
405 while (not stack_.empty()) {
406 /* Get current state */
407 State* cur_state = stack_.back().get();
409 XBT_DEBUG("**************************************************");
410 XBT_DEBUG("Exploration depth = %zu (state = %ld, interleaved processes = %zu)", stack_.size(), cur_state->num_,
411 cur_state->count_todo());
413 /* Update statistics */
414 api::get().mc_inc_visited_states();
416 int next_transition = -1;
417 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
418 next_transition = cur_state->next_transition();
420 if (next_transition >= 0 && visited_state == nullptr) {
421 cur_state->execute_next(next_transition);
423 int req_num = cur_state->get_transition()->times_considered_;
424 smx_simcall_t req = &cur_state->executed_req_;
426 XBT_DEBUG("Execute: %s", cur_state->get_transition()->to_string().c_str());
429 if (dot_output != nullptr)
430 req_str = api::get().request_get_dot_output(cur_state->get_transition());
432 /* TODO : handle test and testany simcalls */
433 CallType call = CallType::NONE;
434 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
435 call = MC_get_call_type(req);
437 handle_comm_pattern(call, req, req_num, 0);
439 /* Create the new expanded state */
440 auto next_state = std::make_unique<State>();
442 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
443 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
444 * with the initial pattern. */
445 bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
447 if (_sg_mc_max_visited_states != 0)
448 visited_state = visited_states_.addVisitedState(next_state->num_, next_state.get(), compare_snapshots);
450 visited_state = nullptr;
452 if (visited_state == nullptr) {
453 /* Add all enabled actors to the interleave set of the next state */
454 for (auto& act : api::get().get_actors()) {
455 auto actor = act.copy.get_buffer();
456 if (get_session().actor_is_enabled(actor->get_pid()))
457 next_state->mark_todo(actor->get_pid());
460 if (dot_output != nullptr)
461 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
463 } else if (dot_output != nullptr)
464 fprintf(dot_output, "\"%ld\" -> \"%ld\" [%s];\n", cur_state->num_,
465 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
467 stack_.push_back(std::move(next_state));
469 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
470 XBT_WARN("/!\\ Max depth reached! /!\\ ");
471 else if (visited_state != nullptr)
472 XBT_DEBUG("State already visited (equal to state %ld), exploration stopped on this path.",
473 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
475 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
477 this->initial_communications_pattern_done = true;
479 /* Trash the current state, no longer needed */
480 XBT_DEBUG("Delete state %ld at depth %zu", cur_state->num_, stack_.size());
483 visited_state = nullptr;
485 api::get().mc_check_deadlock();
487 while (not stack_.empty()) {
488 std::unique_ptr<State> state(std::move(stack_.back()));
490 if (state->count_todo() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
491 /* We found a back-tracking point, let's loop */
492 XBT_DEBUG("Back-tracking to state %ld at depth %zu", state->num_, stack_.size() + 1);
493 stack_.push_back(std::move(state));
495 this->restoreState();
497 XBT_DEBUG("Back-tracking to state %ld at depth %zu done", stack_.back()->num_, stack_.size());
501 XBT_DEBUG("Delete state %ld at depth %zu", state->num_, stack_.size() + 1);
507 api::get().log_state();
510 void CommunicationDeterminismChecker::run()
512 XBT_INFO("Check communication determinism");
513 get_session().take_initial_snapshot();
519 Checker* create_communication_determinism_checker(Session* session)
521 return new CommunicationDeterminismChecker(session);
525 } // namespace simgrid