1 /* Copyright (c) 2008-2021. 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/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 #include "src/mc/mc_smx.hpp"
15 #include "smpi_request.hpp"
20 using mcapi = simgrid::mc::mc_api;
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
24 /********** Global variables **********/
26 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
27 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
29 /********** Static functions ***********/
31 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
32 const simgrid::mc::PatternCommunication* comm2)
34 using simgrid::mc::CommPatternDifference;
35 if (comm1->type != comm2->type)
36 return CommPatternDifference::TYPE;
37 if (comm1->rdv != comm2->rdv)
38 return CommPatternDifference::RDV;
39 if (comm1->src_proc != comm2->src_proc)
40 return CommPatternDifference::SRC_PROC;
41 if (comm1->dst_proc != comm2->dst_proc)
42 return CommPatternDifference::DST_PROC;
43 if (comm1->tag != comm2->tag)
44 return CommPatternDifference::TAG;
45 if (comm1->data.size() != comm2->data.size())
46 return CommPatternDifference::DATA_SIZE;
47 if (comm1->data != comm2->data)
48 return CommPatternDifference::DATA;
49 return CommPatternDifference::NONE;
52 static void patterns_copy(std::vector<simgrid::mc::PatternCommunication*>& dest,
53 std::vector<simgrid::mc::PatternCommunication> const& source)
56 for (simgrid::mc::PatternCommunication const& comm : source) {
57 auto* copy_comm = new simgrid::mc::PatternCommunication(comm.dup());
58 dest.push_back(copy_comm);
62 static void restore_communications_pattern(simgrid::mc::State* state)
64 for (size_t i = 0; i < initial_communications_pattern.size(); i++)
65 initial_communications_pattern[i].index_comm = state->communication_indices_[i];
67 for (unsigned long i = 0; i < mcapi::get().get_maxpid(); i++)
68 patterns_copy(incomplete_communications_pattern[i], state->incomplete_comm_pattern_[i]);
71 static char* print_determinism_result(simgrid::mc::CommPatternDifference diff, aid_t process,
72 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
77 if (comm->type == simgrid::mc::PatternCommunicationType::send)
78 type = bprintf("The send communications pattern of the process %ld is different!", process - 1);
80 type = bprintf("The recv communications pattern of the process %ld is different!", process - 1);
82 using simgrid::mc::CommPatternDifference;
84 case CommPatternDifference::TYPE:
85 res = bprintf("%s Different type for communication #%u", type, cursor);
87 case CommPatternDifference::RDV:
88 res = bprintf("%s Different rdv for communication #%u", type, cursor);
90 case CommPatternDifference::TAG:
91 res = bprintf("%s Different tag for communication #%u", type, cursor);
93 case CommPatternDifference::SRC_PROC:
94 res = bprintf("%s Different source for communication #%u", type, cursor);
96 case CommPatternDifference::DST_PROC:
97 res = bprintf("%s Different destination for communication #%u", type, cursor);
99 case CommPatternDifference::DATA_SIZE:
100 res = bprintf("%s Different data size for communication #%u", type, cursor);
102 case CommPatternDifference::DATA:
103 res = bprintf("%s Different data for communication #%u", type, cursor);
113 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
114 const simgrid::kernel::activity::CommImpl* comm_addr)
116 auto src_proc = mcapi::get().get_src_actor(comm_addr);
117 auto dst_proc = mcapi::get().get_dst_actor(comm_addr);
118 comm_pattern->src_proc = src_proc->get_pid();
119 comm_pattern->dst_proc = dst_proc->get_pid();
120 comm_pattern->src_host = mcapi::get().get_actor_host_name(src_proc);
121 comm_pattern->dst_host = mcapi::get().get_actor_host_name(dst_proc);
123 if (comm_pattern->data.empty())
124 comm_pattern->data = mcapi::get().get_pattern_comm_data(comm_addr);
130 void CommunicationDeterminismChecker::deterministic_comm_pattern(aid_t process, const PatternCommunication* comm,
133 if (not backtracking) {
134 PatternCommunicationList& list = initial_communications_pattern[process];
135 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
137 if (diff != CommPatternDifference::NONE) {
138 if (comm->type == PatternCommunicationType::send) {
139 this->send_deterministic = false;
140 if (this->send_diff != nullptr)
141 xbt_free(this->send_diff);
142 this->send_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
144 this->recv_deterministic = false;
145 if (this->recv_diff != nullptr)
146 xbt_free(this->recv_diff);
147 this->recv_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
149 if (_sg_mc_send_determinism && not this->send_deterministic) {
150 XBT_INFO("*********************************************************");
151 XBT_INFO("***** Non-send-deterministic communications pattern *****");
152 XBT_INFO("*********************************************************");
153 XBT_INFO("%s", this->send_diff);
154 xbt_free(this->send_diff);
155 this->send_diff = nullptr;
156 mcapi::get().log_state();
157 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
158 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
159 XBT_INFO("****************************************************");
160 XBT_INFO("***** Non-deterministic communications pattern *****");
161 XBT_INFO("****************************************************");
162 if (this->send_diff) {
163 XBT_INFO("%s", this->send_diff);
164 xbt_free(this->send_diff);
165 this->send_diff = nullptr;
167 if (this->recv_diff) {
168 XBT_INFO("%s", this->recv_diff);
169 xbt_free(this->recv_diff);
170 this->recv_diff = nullptr;
172 mcapi::get().log_state();
173 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
179 /********** Non Static functions ***********/
181 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, CallType call_type, int backtracking)
183 const smx_actor_t issuer = mcapi::get().simcall_get_issuer(request);
184 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
185 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
187 auto pattern = std::make_unique<PatternCommunication>();
188 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
190 if (call_type == CallType::SEND) {
191 /* Create comm pattern */
192 pattern->type = PatternCommunicationType::send;
193 pattern->comm_addr = mcapi::get().get_comm_isend_raw_addr(request);
194 pattern->rdv = mcapi::get().get_pattern_comm_rdv(pattern->comm_addr);
195 pattern->src_proc = mcapi::get().get_pattern_comm_src_proc(pattern->comm_addr);
196 pattern->src_host = mc_api::get().get_actor_host_name(issuer);
199 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
201 pattern->data = mcapi::get().get_pattern_comm_data(pattern->comm_addr);
204 auto send_detached = mcapi::get().check_send_request_detached(request);
206 if (this->initial_communications_pattern_done) {
207 /* Evaluate comm determinism */
208 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
209 initial_communications_pattern[pattern->src_proc].index_comm++;
211 /* Store comm pattern */
212 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
217 } else if (call_type == CallType::RECV) {
218 pattern->type = PatternCommunicationType::receive;
219 pattern->comm_addr = mcapi::get().get_comm_isend_raw_addr(request);
222 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
224 auto comm_addr = pattern->comm_addr;
225 pattern->rdv = mcapi::get().get_pattern_comm_rdv(comm_addr);
226 pattern->dst_proc = mcapi::get().get_pattern_comm_dst_proc(comm_addr);
227 pattern->dst_host = mcapi::get().get_actor_host_name(issuer);
229 xbt_die("Unexpected call_type %i", (int)call_type);
231 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
232 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
235 void CommunicationDeterminismChecker::complete_comm_pattern(const kernel::activity::CommImpl* comm_addr, aid_t issuer,
238 /* Complete comm pattern */
239 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
240 auto current_comm_pattern =
241 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
242 [&comm_addr](const PatternCommunication* comm) { return mcapi::get().comm_addr_equal(comm->comm_addr, comm_addr); });
243 if (current_comm_pattern == std::end(incomplete_pattern))
244 xbt_die("Corresponding communication not found!");
246 update_comm_pattern(*current_comm_pattern, comm_addr);
247 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
248 XBT_DEBUG("Remove incomplete comm pattern for process %ld at cursor %zd", issuer,
249 std::distance(begin(incomplete_pattern), current_comm_pattern));
250 incomplete_pattern.erase(current_comm_pattern);
252 if (this->initial_communications_pattern_done) {
253 /* Evaluate comm determinism */
254 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
255 initial_communications_pattern[issuer].index_comm++;
257 /* Store comm pattern */
258 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
262 CommunicationDeterminismChecker::CommunicationDeterminismChecker() : Checker() {}
264 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
266 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
269 for (auto const& state : stack_)
270 res.push_back(state->get_transition());
274 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
276 std::vector<std::string> trace;
277 for (auto const& state : stack_) {
278 smx_simcall_t req = &state->executed_req_;
279 trace.push_back(mcapi::get().request_to_string(req, state->transition_.argument_, RequestType::executed));
284 void CommunicationDeterminismChecker::log_state() // override
286 if (_sg_mc_comms_determinism) {
287 if (this->send_deterministic && not this->recv_deterministic) {
288 XBT_INFO("*******************************************************");
289 XBT_INFO("**** Only-send-deterministic communication pattern ****");
290 XBT_INFO("*******************************************************");
291 XBT_INFO("%s", this->recv_diff);
293 if (not this->send_deterministic && this->recv_deterministic) {
294 XBT_INFO("*******************************************************");
295 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
296 XBT_INFO("*******************************************************");
297 XBT_INFO("%s", this->send_diff);
300 XBT_INFO("Expanded states = %lu", expanded_states_count_);
301 XBT_INFO("Visited states = %lu", mcapi::get().mc_get_visited_states());
302 XBT_INFO("Executed transitions = %lu", mcapi::get().mc_get_executed_trans());
303 XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
304 if (_sg_mc_comms_determinism)
305 XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
308 void CommunicationDeterminismChecker::prepare()
310 const auto maxpid = mcapi::get().get_maxpid();
312 initial_communications_pattern.resize(maxpid);
313 incomplete_communications_pattern.resize(maxpid);
315 ++expanded_states_count_;
316 auto initial_state = std::make_unique<State>(expanded_states_count_);
318 XBT_DEBUG("********* Start communication determinism verification *********");
320 /* Get an enabled actor and insert it in the interleave set of the initial state */
321 auto actors = mcapi::get().get_actors();
322 for (auto& actor : actors)
323 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
324 initial_state->add_interleaving_set(actor.copy.get_buffer());
326 stack_.push_back(std::move(initial_state));
329 static inline bool all_communications_are_finished()
331 auto maxpid = mcapi::get().get_maxpid();
332 for (size_t current_actor = 1; current_actor < maxpid; current_actor++) {
333 if (not incomplete_communications_pattern[current_actor].empty()) {
334 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
341 void CommunicationDeterminismChecker::restoreState()
343 /* Intermediate backtracking */
344 State* last_state = stack_.back().get();
345 if (last_state->system_state_) {
346 mc_api::get().restore_state(last_state->system_state_);
347 restore_communications_pattern(last_state);
351 /* Restore the initial state */
352 mcapi::get().restore_initial_state();
354 unsigned long n = mcapi::get().get_maxpid();
355 assert(n == incomplete_communications_pattern.size());
356 assert(n == initial_communications_pattern.size());
357 for (unsigned long j = 0; j < n; j++) {
358 incomplete_communications_pattern[j].clear();
359 initial_communications_pattern[j].index_comm = 0;
362 /* Traverse the stack from the state at position start and re-execute the transitions */
363 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
364 if (state == stack_.back())
367 int req_num = state->transition_.argument_;
368 const s_smx_simcall* saved_req = &state->executed_req_;
369 xbt_assert(saved_req);
371 /* because we got a copy of the executed request, we have to fetch the
372 real one, pointed by the request field of the issuer process */
374 const smx_actor_t issuer = mcapi::get().simcall_get_issuer(saved_req);
375 smx_simcall_t req = &issuer->simcall_;
377 /* TODO : handle test and testany simcalls */
378 CallType call = MC_get_call_type(req);
379 mcapi::get().handle_simcall(state->transition_);
380 handle_comm_pattern(call, req, req_num, 1);
381 mcapi::get().mc_wait_for_requests();
383 /* Update statistics */
384 mcapi::get().mc_inc_visited_states();
385 mcapi::get().mc_inc_executed_trans();
389 void CommunicationDeterminismChecker::handle_comm_pattern(simgrid::mc::CallType call_type, smx_simcall_t req, int value, int backtracking)
391 using simgrid::mc::CallType;
397 get_comm_pattern(req, call_type, backtracking);
400 case CallType::WAITANY: {
401 const simgrid::kernel::activity::CommImpl* comm_addr = nullptr;
402 if (call_type == CallType::WAIT)
403 comm_addr = mcapi::get().get_comm_wait_raw_addr(req);
405 comm_addr = mcapi::get().get_comm_waitany_raw_addr(req, value);
406 auto simcall_issuer = mcapi::get().simcall_get_issuer(req);
407 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
410 xbt_die("Unexpected call type %i", (int)call_type);
414 void CommunicationDeterminismChecker::real_run()
416 std::unique_ptr<VisitedState> visited_state = nullptr;
417 smx_simcall_t req = nullptr;
419 while (not stack_.empty()) {
420 /* Get current state */
421 State* cur_state = stack_.back().get();
423 XBT_DEBUG("**************************************************");
424 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num_,
425 cur_state->interleave_size());
427 /* Update statistics */
428 mcapi::get().mc_inc_visited_states();
430 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
431 req = mcapi::get().mc_state_choose_request(cur_state);
435 if (req != nullptr && visited_state == nullptr) {
436 int req_num = cur_state->transition_.argument_;
438 XBT_DEBUG("Execute: %s", mcapi::get().request_to_string(req, req_num, RequestType::simix).c_str());
441 if (dot_output != nullptr)
442 req_str = mcapi::get().request_get_dot_output(req, req_num);
444 mcapi::get().mc_inc_executed_trans();
446 /* TODO : handle test and testany simcalls */
447 CallType call = CallType::NONE;
448 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
449 call = MC_get_call_type(req);
451 /* Answer the request */
452 mcapi::get().handle_simcall(cur_state->transition_);
453 /* After this call req is no longer useful */
455 handle_comm_pattern(call, req, req_num, 0);
457 /* Wait for requests (schedules processes) */
458 mcapi::get().mc_wait_for_requests();
460 /* Create the new expanded state */
461 ++expanded_states_count_;
462 auto next_state = std::make_unique<State>(expanded_states_count_);
464 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
465 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
466 * with the initial pattern. */
467 bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
469 if (_sg_mc_max_visited_states != 0)
470 visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
472 visited_state = nullptr;
474 if (visited_state == nullptr) {
475 /* Get enabled actors and insert them in the interleave set of the next state */
476 auto actors = mcapi::get().get_actors();
477 for (auto& actor : actors)
478 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
479 next_state->add_interleaving_set(actor.copy.get_buffer());
481 if (dot_output != nullptr)
482 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
484 } else if (dot_output != nullptr)
485 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_,
486 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
488 stack_.push_back(std::move(next_state));
490 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
491 XBT_WARN("/!\\ Max depth reached! /!\\ ");
492 else if (visited_state != nullptr)
493 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
494 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
496 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
498 this->initial_communications_pattern_done = true;
500 /* Trash the current state, no longer needed */
501 XBT_DEBUG("Delete state %d at depth %zu", cur_state->num_, stack_.size());
504 visited_state = nullptr;
506 /* Check for deadlocks */
507 if (mcapi::get().mc_check_deadlock()) {
508 mcapi::get().mc_show_deadlock();
509 throw simgrid::mc::DeadlockError();
512 while (not stack_.empty()) {
513 std::unique_ptr<State> state(std::move(stack_.back()));
515 if (state->interleave_size() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
516 /* We found a back-tracking point, let's loop */
517 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num_, stack_.size() + 1);
518 stack_.push_back(std::move(state));
520 this->restoreState();
522 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num_, stack_.size());
526 XBT_DEBUG("Delete state %d at depth %zu", state->num_, stack_.size() + 1);
532 mcapi::get().log_state();
535 void CommunicationDeterminismChecker::run()
537 XBT_INFO("Check communication determinism");
538 mcapi::get().session_initialize();
544 Checker* createCommunicationDeterminismChecker()
546 return new CommunicationDeterminismChecker();
550 } // namespace simgrid