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 simgrid::mc::remote;
23 using mcapi = simgrid::mc::mc_api;
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
27 /********** Global variables **********/
29 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
30 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
32 /********** Static functions ***********/
34 static simgrid::mc::CommPatternDifference compare_comm_pattern(const simgrid::mc::PatternCommunication* comm1,
35 const simgrid::mc::PatternCommunication* comm2)
37 using simgrid::mc::CommPatternDifference;
38 if (comm1->type != comm2->type)
39 return CommPatternDifference::TYPE;
40 if (comm1->rdv != comm2->rdv)
41 return CommPatternDifference::RDV;
42 if (comm1->src_proc != comm2->src_proc)
43 return CommPatternDifference::SRC_PROC;
44 if (comm1->dst_proc != comm2->dst_proc)
45 return CommPatternDifference::DST_PROC;
46 if (comm1->tag != comm2->tag)
47 return CommPatternDifference::TAG;
48 if (comm1->data.size() != comm2->data.size())
49 return CommPatternDifference::DATA_SIZE;
50 if (comm1->data != comm2->data)
51 return CommPatternDifference::DATA;
52 return CommPatternDifference::NONE;
55 static char* print_determinism_result(simgrid::mc::CommPatternDifference diff, int process,
56 const simgrid::mc::PatternCommunication* comm, unsigned int cursor)
61 if (comm->type == simgrid::mc::PatternCommunicationType::send)
62 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
64 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
66 using simgrid::mc::CommPatternDifference;
68 case CommPatternDifference::TYPE:
69 res = bprintf("%s Different type for communication #%u", type, cursor);
71 case CommPatternDifference::RDV:
72 res = bprintf("%s Different rdv for communication #%u", type, cursor);
74 case CommPatternDifference::TAG:
75 res = bprintf("%s Different tag for communication #%u", type, cursor);
77 case CommPatternDifference::SRC_PROC:
78 res = bprintf("%s Different source for communication #%u", type, cursor);
80 case CommPatternDifference::DST_PROC:
81 res = bprintf("%s Different destination for communication #%u", type, cursor);
83 case CommPatternDifference::DATA_SIZE:
84 res = bprintf("%s Different data size for communication #%u", type, cursor);
86 case CommPatternDifference::DATA:
87 res = bprintf("%s Different data for communication #%u", type, cursor);
97 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
98 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr)
100 auto src_proc = mcapi::get().get_src_actor(comm_addr);
101 auto dst_proc = mcapi::get().get_dst_actor(comm_addr);
102 comm_pattern->src_proc = src_proc->get_pid();
103 comm_pattern->dst_proc = dst_proc->get_pid();
104 comm_pattern->src_host = mcapi::get().get_actor_host_name(src_proc);
105 comm_pattern->dst_host = mcapi::get().get_actor_host_name(dst_proc);
107 if (comm_pattern->data.empty()) {
108 auto pattern_data = mcapi::get().get_pattern_comm_data(comm_addr);
109 if (pattern_data.data() != nullptr) {
110 auto data_size = pattern_data.size();
111 comm_pattern->data.resize(data_size);
112 memcpy(comm_pattern->data.data(), pattern_data.data(), data_size);
120 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, const PatternCommunication* comm,
123 if (not backtracking) {
124 PatternCommunicationList& list = initial_communications_pattern[process];
125 CommPatternDifference diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
127 if (diff != CommPatternDifference::NONE) {
128 if (comm->type == PatternCommunicationType::send) {
129 this->send_deterministic = false;
130 if (this->send_diff != nullptr)
131 xbt_free(this->send_diff);
132 this->send_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
134 this->recv_deterministic = false;
135 if (this->recv_diff != nullptr)
136 xbt_free(this->recv_diff);
137 this->recv_diff = print_determinism_result(diff, process, comm, list.index_comm + 1);
139 if (_sg_mc_send_determinism && not this->send_deterministic) {
140 XBT_INFO("*********************************************************");
141 XBT_INFO("***** Non-send-deterministic communications pattern *****");
142 XBT_INFO("*********************************************************");
143 XBT_INFO("%s", this->send_diff);
144 xbt_free(this->send_diff);
145 this->send_diff = nullptr;
146 mcapi::get().log_state();
147 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
148 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
149 XBT_INFO("****************************************************");
150 XBT_INFO("***** Non-deterministic communications pattern *****");
151 XBT_INFO("****************************************************");
152 if (this->send_diff) {
153 XBT_INFO("%s", this->send_diff);
154 xbt_free(this->send_diff);
155 this->send_diff = nullptr;
157 if (this->recv_diff) {
158 XBT_INFO("%s", this->recv_diff);
159 xbt_free(this->recv_diff);
160 this->recv_diff = nullptr;
162 mcapi::get().log_state();
163 mcapi::get().mc_exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
169 /********** Non Static functions ***********/
171 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, CallType call_type, int backtracking)
173 const smx_actor_t issuer = mcapi::get().mc_smx_simcall_get_issuer(request);
174 const mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
175 const std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer->get_pid()];
177 auto pattern = std::make_unique<PatternCommunication>();
178 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
180 if (call_type == CallType::SEND) {
181 /* Create comm pattern */
182 pattern->type = PatternCommunicationType::send;
183 pattern->comm_addr = mcapi::get().get_pattern_comm_addr(request);
184 pattern->rdv = mcapi::get().get_pattern_comm_rdv(pattern->comm_addr);
185 pattern->src_proc = mcapi::get().get_pattern_comm_src_proc(pattern->comm_addr);
186 pattern->src_host = mc_api::get().get_actor_host_name(issuer);
189 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_ISEND);
191 auto pattern_data = mcapi::get().get_pattern_comm_data(pattern->comm_addr);
192 if (pattern_data.data() != nullptr) {
193 auto data_size = pattern_data.size();
194 pattern->data.resize(data_size);
195 memcpy(pattern->data.data(), pattern_data.data(), data_size);
199 auto send_detached = mcapi::get().check_send_request_detached(request);
201 if (this->initial_communications_pattern_done) {
202 /* Evaluate comm determinism */
203 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
204 initial_communications_pattern[pattern->src_proc].index_comm++;
206 /* Store comm pattern */
207 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
212 } else if (call_type == CallType::RECV) {
213 pattern->type = PatternCommunicationType::receive;
214 pattern->comm_addr = mcapi::get().get_pattern_comm_addr(request);
217 pattern->tag = mcapi::get().get_smpi_request_tag(request, simgrid::simix::Simcall::COMM_IRECV);
219 auto comm_addr = pattern->comm_addr;
220 pattern->rdv = mcapi::get().get_pattern_comm_rdv(comm_addr);
221 pattern->dst_proc = mcapi::get().get_pattern_comm_dst_proc(comm_addr);
222 pattern->dst_host = mcapi::get().get_actor_host_name(issuer);
224 xbt_die("Unexpected call_type %i", (int)call_type);
226 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
227 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
230 void CommunicationDeterminismChecker::complete_comm_pattern(RemotePtr<kernel::activity::CommImpl> comm_addr,
231 unsigned int issuer, int backtracking)
233 /* Complete comm pattern */
234 std::vector<PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
235 auto current_comm_pattern =
236 std::find_if(begin(incomplete_pattern), end(incomplete_pattern),
237 [&comm_addr](const PatternCommunication* comm) { return remote(comm->comm_addr) == comm_addr; });
238 if (current_comm_pattern == std::end(incomplete_pattern))
239 xbt_die("Corresponding communication not found!");
241 update_comm_pattern(*current_comm_pattern, comm_addr);
242 std::unique_ptr<PatternCommunication> comm_pattern(*current_comm_pattern);
243 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %zd", issuer,
244 std::distance(begin(incomplete_pattern), current_comm_pattern));
245 incomplete_pattern.erase(current_comm_pattern);
247 if (this->initial_communications_pattern_done) {
248 /* Evaluate comm determinism */
249 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
250 initial_communications_pattern[issuer].index_comm++;
252 /* Store comm pattern */
253 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
257 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& s) : Checker(s) {}
259 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
261 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
264 for (auto const& state : stack_)
265 res.push_back(state->get_transition());
269 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
271 std::vector<std::string> trace;
272 for (auto const& state : stack_) {
273 smx_simcall_t req = &state->executed_req_;
274 trace.push_back(mcapi::get().request_to_string(req, state->transition_.argument_, RequestType::executed));
279 void CommunicationDeterminismChecker::log_state() // override
281 if (_sg_mc_comms_determinism) {
282 if (this->send_deterministic && not this->recv_deterministic) {
283 XBT_INFO("*******************************************************");
284 XBT_INFO("**** Only-send-deterministic communication pattern ****");
285 XBT_INFO("*******************************************************");
286 XBT_INFO("%s", this->recv_diff);
288 if (not this->send_deterministic && this->recv_deterministic) {
289 XBT_INFO("*******************************************************");
290 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
291 XBT_INFO("*******************************************************");
292 XBT_INFO("%s", this->send_diff);
295 XBT_INFO("Expanded states = %lu", expanded_states_count_);
296 XBT_INFO("Visited states = %lu", mcapi::get().mc_get_visited_states());
297 XBT_INFO("Executed transitions = %lu", mcapi::get().mc_get_executed_trans());
298 XBT_INFO("Send-deterministic : %s", this->send_deterministic ? "Yes" : "No");
299 if (_sg_mc_comms_determinism)
300 XBT_INFO("Recv-deterministic : %s", this->recv_deterministic ? "Yes" : "No");
303 void CommunicationDeterminismChecker::prepare()
305 const int maxpid = mcapi::get().get_maxpid();
307 initial_communications_pattern.resize(maxpid);
308 incomplete_communications_pattern.resize(maxpid);
310 ++expanded_states_count_;
311 auto initial_state = std::make_unique<State>(expanded_states_count_);
313 XBT_DEBUG("********* Start communication determinism verification *********");
315 /* Get an enabled actor and insert it in the interleave set of the initial state */
316 auto actors = mcapi::get().get_actors();
317 for (auto& actor : actors)
318 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
319 initial_state->add_interleaving_set(actor.copy.get_buffer());
321 stack_.push_back(std::move(initial_state));
324 static inline bool all_communications_are_finished()
326 auto maxpid = mcapi::get().get_maxpid();
327 for (size_t current_actor = 1; current_actor < maxpid; current_actor++) {
328 if (not incomplete_communications_pattern[current_actor].empty()) {
329 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
336 void CommunicationDeterminismChecker::restoreState()
338 /* Intermediate backtracking */
339 State* last_state = stack_.back().get();
340 if (last_state->system_state_) {
341 mc_api::get().restore_state(last_state->system_state_);
342 MC_restore_communications_pattern(last_state);
346 /* Restore the initial state */
347 mcapi::get().restore_initial_state();
349 unsigned n = mcapi::get().get_maxpid();
350 assert(n == incomplete_communications_pattern.size());
351 assert(n == initial_communications_pattern.size());
352 for (unsigned j = 0; j < n; j++) {
353 incomplete_communications_pattern[j].clear();
354 initial_communications_pattern[j].index_comm = 0;
357 /* Traverse the stack from the state at position start and re-execute the transitions */
358 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
359 if (state == stack_.back())
362 int req_num = state->transition_.argument_;
363 const s_smx_simcall* saved_req = &state->executed_req_;
364 xbt_assert(saved_req);
366 /* because we got a copy of the executed request, we have to fetch the
367 real one, pointed by the request field of the issuer process */
369 const smx_actor_t issuer = mcapi::get().mc_smx_simcall_get_issuer(saved_req);
370 smx_simcall_t req = &issuer->simcall_;
372 /* TODO : handle test and testany simcalls */
373 CallType call = MC_get_call_type(req);
374 mcapi::get().handle_simcall(state->transition_);
375 MC_handle_comm_pattern(call, req, req_num, 1);
376 mcapi::get().mc_wait_for_requests();
378 /* Update statistics */
379 mcapi::get().mc_inc_visited_states();
380 mcapi::get().mc_inc_executed_trans();
384 void CommunicationDeterminismChecker::real_run()
386 std::unique_ptr<VisitedState> visited_state = nullptr;
387 smx_simcall_t req = nullptr;
389 while (not stack_.empty()) {
390 /* Get current state */
391 State* cur_state = stack_.back().get();
393 XBT_DEBUG("**************************************************");
394 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num_,
395 cur_state->interleave_size());
397 /* Update statistics */
398 mcapi::get().mc_inc_visited_states();
400 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
401 req = mcapi::get().mc_state_choose_request(cur_state);
405 if (req != nullptr && visited_state == nullptr) {
406 int req_num = cur_state->transition_.argument_;
408 XBT_DEBUG("Execute: %s", mcapi::get().request_to_string(req, req_num, RequestType::simix).c_str());
411 if (dot_output != nullptr)
412 req_str = mcapi::get().request_get_dot_output(req, req_num);
414 mcapi::get().mc_inc_executed_trans();
416 /* TODO : handle test and testany simcalls */
417 CallType call = CallType::NONE;
418 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
419 call = MC_get_call_type(req);
421 /* Answer the request */
422 mcapi::get().handle_simcall(cur_state->transition_);
423 /* After this call req is no longer useful */
425 MC_handle_comm_pattern(call, req, req_num, 0);
427 /* Wait for requests (schedules processes) */
428 mcapi::get().mc_wait_for_requests();
430 /* Create the new expanded state */
431 ++expanded_states_count_;
432 auto next_state = std::make_unique<State>(expanded_states_count_);
434 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
435 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
436 * with the initial pattern. */
437 bool compare_snapshots = this->initial_communications_pattern_done && all_communications_are_finished();
439 if (_sg_mc_max_visited_states != 0)
440 visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
442 visited_state = nullptr;
444 if (visited_state == nullptr) {
445 /* Get enabled actors and insert them in the interleave set of the next state */
446 auto actors = mcapi::get().get_actors();
447 for (auto& actor : actors)
448 if (mcapi::get().actor_is_enabled(actor.copy.get_buffer()->get_pid()))
449 next_state->add_interleaving_set(actor.copy.get_buffer());
451 if (dot_output != nullptr)
452 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_, next_state->num_, req_str.c_str());
454 } else if (dot_output != nullptr)
455 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num_,
456 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
458 stack_.push_back(std::move(next_state));
460 if (stack_.size() > (std::size_t)_sg_mc_max_depth)
461 XBT_WARN("/!\\ Max depth reached! /!\\ ");
462 else if (visited_state != nullptr)
463 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
464 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
466 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
468 this->initial_communications_pattern_done = true;
470 /* Trash the current state, no longer needed */
471 XBT_DEBUG("Delete state %d at depth %zu", cur_state->num_, stack_.size());
474 visited_state = nullptr;
476 /* Check for deadlocks */
477 if (mcapi::get().mc_check_deadlock()) {
478 mcapi::get().mc_show_deadlock();
479 throw simgrid::mc::DeadlockError();
482 while (not stack_.empty()) {
483 std::unique_ptr<State> state(std::move(stack_.back()));
485 if (state->interleave_size() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
486 /* We found a back-tracking point, let's loop */
487 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num_, stack_.size() + 1);
488 stack_.push_back(std::move(state));
490 this->restoreState();
492 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num_, stack_.size());
496 XBT_DEBUG("Delete state %d at depth %zu", state->num_, stack_.size() + 1);
502 mcapi::get().log_state();
505 void CommunicationDeterminismChecker::run()
507 XBT_INFO("Check communication determinism");
508 mcapi::get().s_initialize();
514 Checker* createCommunicationDeterminismChecker(Session& s)
516 return new CommunicationDeterminismChecker(s);
520 } // namespace simgrid