1 /* Copyright (c) 2008-2020. 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_api.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/mc_request.hpp"
14 #include "src/mc/mc_smx.hpp"
17 #include "smpi_request.hpp"
22 using mcapi = simgrid::mc::mc_api;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
26 /********** Global variables **********/
28 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
29 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
31 /********** Static functions ***********/
33 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
34 const simgrid::mc::PatternCommunication* comm2)
36 using simgrid::mc::CommPatternDifference;
37 if (comm1->type != comm2->type)
38 return CommPatternDifference::TYPE;
39 if (comm1->rdv != comm2->rdv)
40 return CommPatternDifference::RDV;
41 if (comm1->src_proc != comm2->src_proc)
42 return CommPatternDifference::SRC_PROC;
43 if (comm1->dst_proc != comm2->dst_proc)
44 return CommPatternDifference::DST_PROC;
45 if (comm1->tag != comm2->tag)
46 return CommPatternDifference::TAG;
47 if (comm1->data.size() != comm2->data.size())
48 return CommPatternDifference::DATA_SIZE;
49 if (comm1->data != comm2->data)
50 return CommPatternDifference::DATA;
51 return CommPatternDifference::NONE;
54 static char* print_determinism_result(simgrid::mc::CommPatternDifference diff, int process,
55 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
60 if (comm->type == simgrid::mc::PatternCommunicationType::send)
61 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
63 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
65 using simgrid::mc::CommPatternDifference;
67 case CommPatternDifference::TYPE:
68 res = bprintf("%s Different type for communication #%u", type, cursor);
70 case CommPatternDifference::RDV:
71 res = bprintf("%s Different rdv for communication #%u", type, cursor);
73 case CommPatternDifference::TAG:
74 res = bprintf("%s Different tag for communication #%u", type, cursor);
76 case CommPatternDifference::SRC_PROC:
77 res = bprintf("%s Different source for communication #%u", type, cursor);
79 case CommPatternDifference::DST_PROC:
80 res = bprintf("%s Different destination for communication #%u", type, cursor);
82 case CommPatternDifference::DATA_SIZE:
83 res = bprintf("%s Different data size for communication #%u", type, cursor);
85 case CommPatternDifference::DATA:
86 res = bprintf("%s Different data for communication #%u", type, cursor);
96 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
97 const simgrid::kernel::activity::CommImpl* comm_addr)
99 auto src_proc = mcapi::get().get_src_actor(comm_addr);
100 auto dst_proc = mcapi::get().get_dst_actor(comm_addr);
101 comm_pattern->src_proc = src_proc->get_pid();
102 comm_pattern->dst_proc = dst_proc->get_pid();
103 comm_pattern->src_host = mcapi::get().get_actor_host_name(src_proc);
104 comm_pattern->dst_host = mcapi::get().get_actor_host_name(dst_proc);
106 if (comm_pattern->data.empty()) {
107 auto pattern_data = mcapi::get().get_pattern_comm_data(comm_addr);
108 if (pattern_data.data() != nullptr) {
109 auto data_size = pattern_data.size();
110 comm_pattern->data.resize(data_size);
111 memcpy(comm_pattern->data.data(), pattern_data.data(), data_size);
119 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, const PatternCommunication* comm,
122 if (not backtracking) {
123 PatternCommunicationList& list = initial_communications_pattern[process];
124 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
126 if (diff != CommPatternDifference::NONE) {
127 if (comm->type == PatternCommunicationType::send) {
128 this->send_deterministic = false;
129 if (this->send_diff != nullptr)
130 xbt_free(this->send_diff);
131 this->send_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
133 this->recv_deterministic = false;
134 if (this->recv_diff != nullptr)
135 xbt_free(this->recv_diff);
136 this->recv_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
138 if (_sg_mc_send_determinism && not this->send_deterministic) {
139 XBT_INFO("*********************************************************");
140 XBT_INFO("***** Non-send-deterministic communications pattern *****");
141 XBT_INFO("*********************************************************");
142 XBT_INFO("%s", this->send_diff);
143 xbt_free(this->send_diff);
144 this->send_diff = nullptr;
145 mcapi::get().log_state();
146 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
147 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
148 XBT_INFO("****************************************************");
149 XBT_INFO("***** Non-deterministic communications pattern *****");
150 XBT_INFO("****************************************************");
151 if (this->send_diff) {
152 XBT_INFO("%s", this->send_diff);
153 xbt_free(this->send_diff);
154 this->send_diff = nullptr;
156 if (this->recv_diff) {
157 XBT_INFO("%s", this->recv_diff);
158 xbt_free(this->recv_diff);
159 this->recv_diff = nullptr;
161 mcapi::get().log_state();
162 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
168 /********** Non Static functions ***********/
170 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, CallType call_type, int backtracking)
172 const smx_actor_t issuer = mcapi::get().simcall_get_issuer(request);
173 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
174 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
176 auto pattern = std::make_unique<PatternCommunication>();
177 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
179 if (call_type == CallType::SEND) {
180 /* Create comm pattern */
181 pattern->type = PatternCommunicationType::send;
182 pattern->comm_addr = mcapi::get().get_comm_isend_raw_addr(request);
183 pattern->rdv = mcapi::get().get_pattern_comm_rdv(pattern->comm_addr);
184 pattern->src_proc = mcapi::get().get_pattern_comm_src_proc(pattern->comm_addr);
185 pattern->src_host = mc_api::get().get_actor_host_name(issuer);
188 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
190 auto pattern_data = mcapi::get().get_pattern_comm_data(pattern->comm_addr);
191 if (pattern_data.data() != nullptr) {
192 auto data_size = pattern_data.size();
193 pattern->data.resize(data_size);
194 memcpy(pattern->data.data(), pattern_data.data(), data_size);
198 auto send_detached = mcapi::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 = mcapi::get().get_comm_isend_raw_addr(request);
216 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
218 auto comm_addr = pattern->comm_addr;
219 pattern->rdv = mcapi::get().get_pattern_comm_rdv(comm_addr);
220 pattern->dst_proc = mcapi::get().get_pattern_comm_dst_proc(comm_addr);
221 pattern->dst_host = mcapi::get().get_actor_host_name(issuer);
223 xbt_die("Unexpected call_type %i", (int)call_type);
225 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
226 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
229 void CommunicationDeterminismChecker::complete_comm_pattern(const kernel::activity::CommImpl* comm_addr,
230 unsigned int issuer, int backtracking)
232 /* Complete comm pattern */
233 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
234 auto current_comm_pattern =
235 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
236 [&comm_addr](const PatternCommunication* comm) { return mcapi::get().comm_addr_equal(comm->comm_addr, comm_addr); });
237 if (current_comm_pattern == std::end(incomplete_pattern))
238 xbt_die("Corresponding communication not found!");
240 update_comm_pattern(*current_comm_pattern, comm_addr);
241 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
242 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %zd", issuer,
243 std::distance(begin(incomplete_pattern), current_comm_pattern));
244 incomplete_pattern.erase(current_comm_pattern);
246 if (this->initial_communications_pattern_done) {
247 /* Evaluate comm determinism */
248 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
249 initial_communications_pattern[issuer].index_comm++;
251 /* Store comm pattern */
252 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
256 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& s) : Checker(s) {}
258 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
260 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
263 for (auto const& state : stack_)
264 res.push_back(state->get_transition());
268 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
270 std::vector<std::string> trace;
271 for (auto const& state : stack_) {
272 smx_simcall_t req = &state->executed_req_;
273 trace.push_back(mcapi::get().request_to_string(req, state->transition_.argument_, RequestType::executed));
278 void CommunicationDeterminismChecker::log_state() // override
280 if (_sg_mc_comms_determinism) {
281 if (this->send_deterministic && not this->recv_deterministic) {
282 XBT_INFO("*******************************************************");
283 XBT_INFO("**** Only-send-deterministic communication pattern ****");
284 XBT_INFO("*******************************************************");
285 XBT_INFO("%s", this->recv_diff);
287 if (not this->send_deterministic && this->recv_deterministic) {
288 XBT_INFO("*******************************************************");
289 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
290 XBT_INFO("*******************************************************");
291 XBT_INFO("%s", this->send_diff);
294 XBT_INFO("Expanded states = %lu", expanded_states_count_);
295 XBT_INFO("Visited states = %lu", mcapi::get().mc_get_visited_states());
296 XBT_INFO("Executed transitions = %lu", mcapi::get().mc_get_executed_trans());
297 XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
298 if (_sg_mc_comms_determinism)
299 XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
302 void CommunicationDeterminismChecker::prepare()
304 const int maxpid = mcapi::get().get_maxpid();
306 initial_communications_pattern.resize(maxpid);
307 incomplete_communications_pattern.resize(maxpid);
309 ++expanded_states_count_;
310 auto initial_state = std::make_unique<State>(expanded_states_count_);
312 XBT_DEBUG("********* Start communication determinism verification *********");
314 /* Get an enabled actor and insert it in the interleave set of the initial state */
315 auto actors = mcapi::get().get_actors();
316 for (auto& actor : actors)
317 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
318 initial_state->add_interleaving_set(actor.copy.get_buffer());
320 stack_.push_back(std::move(initial_state));
323 static inline bool all_communications_are_finished()
325 auto maxpid = mcapi::get().get_maxpid();
326 for (size_t current_actor = 1; current_actor < maxpid; current_actor++) {
327 if (not incomplete_communications_pattern[current_actor].empty()) {
328 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
335 void CommunicationDeterminismChecker::restoreState()
337 /* Intermediate backtracking */
338 State* last_state = stack_.back().get();
339 if (last_state->system_state_) {
340 mc_api::get().restore_state(last_state->system_state_);
341 MC_restore_communications_pattern(last_state);
345 /* Restore the initial state */
346 mcapi::get().restore_initial_state();
348 unsigned n = mcapi::get().get_maxpid();
349 assert(n == incomplete_communications_pattern.size());
350 assert(n == initial_communications_pattern.size());
351 for (unsigned j = 0; j < n; j++) {
352 incomplete_communications_pattern[j].clear();
353 initial_communications_pattern[j].index_comm = 0;
356 /* Traverse the stack from the state at position start and re-execute the transitions */
357 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
358 if (state == stack_.back())
361 int req_num = state->transition_.argument_;
362 const s_smx_simcall* saved_req = &state->executed_req_;
363 xbt_assert(saved_req);
365 /* because we got a copy of the executed request, we have to fetch the
366 real one, pointed by the request field of the issuer process */
368 const smx_actor_t issuer = mcapi::get().simcall_get_issuer(saved_req);
369 smx_simcall_t req = &issuer->simcall_;
371 /* TODO : handle test and testany simcalls */
372 CallType call = MC_get_call_type(req);
373 mcapi::get().handle_simcall(state->transition_);
374 handle_comm_pattern(call, req, req_num, 1);
375 mcapi::get().mc_wait_for_requests();
377 /* Update statistics */
378 mcapi::get().mc_inc_visited_states();
379 mcapi::get().mc_inc_executed_trans();
383 void CommunicationDeterminismChecker::handle_comm_pattern(simgrid::mc::CallType call_type, smx_simcall_t req, int value, int backtracking)
385 using simgrid::mc::CallType;
391 get_comm_pattern(req, call_type, backtracking);
394 case CallType::WAITANY: {
395 simgrid::kernel::activity::CommImpl* comm_addr = nullptr;
396 if (call_type == CallType::WAIT)
397 comm_addr = mcapi::get().get_comm_wait_raw_addr(req);
399 comm_addr = mcapi::get().get_comm_waitany_raw_addr(req, value);
400 auto simcall_issuer = mcapi::get().simcall_get_issuer(req);
401 complete_comm_pattern(comm_addr, simcall_issuer->get_pid(), backtracking);
404 xbt_die("Unexpected call type %i", (int)call_type);
408 void CommunicationDeterminismChecker::real_run()
410 std::unique_ptr<VisitedState> visited_state = nullptr;
411 smx_simcall_t req = nullptr;
413 while (not stack_.empty()) {
414 /* Get current state */
415 State* cur_state = stack_.back().get();
417 XBT_DEBUG("**************************************************");
418 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num_,
419 cur_state->interleave_size());
421 /* Update statistics */
422 mcapi::get().mc_inc_visited_states();
424 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
425 req = mcapi::get().mc_state_choose_request(cur_state);
429 if (req != nullptr && visited_state == nullptr) {
430 int req_num = cur_state->transition_.argument_;
432 XBT_DEBUG("Execute: %s", mcapi::get().request_to_string(req, req_num, RequestType::simix).c_str());
435 if (dot_output != nullptr)
436 req_str = mcapi::get().request_get_dot_output(req, req_num);
438 mcapi::get().mc_inc_executed_trans();
440 /* TODO : handle test and testany simcalls */
441 CallType call = CallType::NONE;
442 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
443 call = MC_get_call_type(req);
445 /* Answer the request */
446 mcapi::get().handle_simcall(cur_state->transition_);
447 /* After this call req is no longer useful */
449 handle_comm_pattern(call, req, req_num, 0);
451 /* Wait for requests (schedules processes) */
452 mcapi::get().mc_wait_for_requests();
454 /* Create the new expanded state */
455 ++expanded_states_count_;
456 auto next_state = std::make_unique<State>(expanded_states_count_);
458 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
459 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
460 * with the initial pattern. */
461 bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
463 if (_sg_mc_max_visited_states != 0)
464 visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
466 visited_state = nullptr;
468 if (visited_state == nullptr) {
469 /* Get enabled actors and insert them in the interleave set of the next state */
470 auto actors = mcapi::get().get_actors();
471 for (auto& actor : actors)
472 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
473 next_state->add_interleaving_set(actor.copy.get_buffer());
475 if (dot_output != nullptr)
476 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
478 } else if (dot_output != nullptr)
479 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_,
480 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
482 stack_.push_back(std::move(next_state));
484 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
485 XBT_WARN("/!\\ Max depth reached! /!\\ ");
486 else if (visited_state != nullptr)
487 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
488 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
490 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
492 this->initial_communications_pattern_done = true;
494 /* Trash the current state, no longer needed */
495 XBT_DEBUG("Delete state %d at depth %zu", cur_state->num_, stack_.size());
498 visited_state = nullptr;
500 /* Check for deadlocks */
501 if (mcapi::get().mc_check_deadlock()) {
502 mcapi::get().mc_show_deadlock();
503 throw simgrid::mc::DeadlockError();
506 while (not stack_.empty()) {
507 std::unique_ptr<State> state(std::move(stack_.back()));
509 if (state->interleave_size() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
510 /* We found a back-tracking point, let's loop */
511 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num_, stack_.size() + 1);
512 stack_.push_back(std::move(state));
514 this->restoreState();
516 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num_, stack_.size());
520 XBT_DEBUG("Delete state %d at depth %zu", state->num_, stack_.size() + 1);
526 mcapi::get().log_state();
529 void CommunicationDeterminismChecker::run()
531 XBT_INFO("Check communication determinism");
532 mcapi::get().s_initialize();
538 Checker* createCommunicationDeterminismChecker(Session& s)
540 return new CommunicationDeterminismChecker(s);
544 } // namespace simgrid