1 /* Copyright (c) 2008-2019. 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"
12 #include "src/mc/mc_request.hpp"
13 #include "src/mc/mc_smx.hpp"
16 #include "smpi_request.hpp"
21 using simgrid::mc::remote;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
25 /********** Global variables **********/
27 std::vector<simgrid::mc::PatternCommunicationList> initial_communications_pattern;
28 std::vector<std::vector<simgrid::mc::PatternCommunication*>> incomplete_communications_pattern;
30 /********** Static functions ***********/
32 static e_mc_comm_pattern_difference_t compare_comm_pattern(simgrid::mc::PatternCommunication* comm1,
33 simgrid::mc::PatternCommunication* comm2)
35 if(comm1->type != comm2->type)
37 if (comm1->rdv != comm2->rdv)
39 if (comm1->src_proc != comm2->src_proc)
41 if (comm1->dst_proc != comm2->dst_proc)
43 if (comm1->tag != comm2->tag)
45 if (comm1->data.size() != comm2->data.size())
46 return DATA_SIZE_DIFF;
47 if (comm1->data != comm2->data)
52 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process,
53 simgrid::mc::PatternCommunication* comm, unsigned int cursor)
58 if (comm->type == simgrid::mc::PatternCommunicationType::send)
59 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
61 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
65 res = bprintf("%s Different type for communication #%u", type, cursor);
68 res = bprintf("%s Different rdv for communication #%u", type, cursor);
71 res = bprintf("%s Different tag for communication #%u", type, cursor);
74 res = bprintf("%s Different source for communication #%u", type, cursor);
77 res = bprintf("%s Different destination for communication #%u", type, cursor);
80 res = bprintf("%s\n Different data size for communication #%u", type, cursor);
83 res = bprintf("%s\n Different data for communication #%u", type, cursor);
93 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
94 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr)
97 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
98 mc_model_checker->process().read(temp_comm, comm_addr);
99 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
101 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_actor_.get()));
102 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()));
103 comm_pattern->src_proc = src_proc->get_pid();
104 comm_pattern->dst_proc = dst_proc->get_pid();
105 comm_pattern->src_host = MC_smx_actor_get_host_name(src_proc);
106 comm_pattern->dst_host = MC_smx_actor_get_host_name(dst_proc);
107 if (comm_pattern->data.size() == 0 && comm->src_buff_ != nullptr) {
109 mc_model_checker->process().read(&buff_size, remote(comm->dst_buff_size_));
110 comm_pattern->data.resize(buff_size);
111 mc_model_checker->process().read_bytes(comm_pattern->data.data(), comm_pattern->data.size(),
112 remote(comm->src_buff_));
119 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, simgrid::mc::PatternCommunication* comm,
122 simgrid::mc::PatternCommunicationList& list = initial_communications_pattern[process];
124 if (not backtracking) {
125 e_mc_comm_pattern_difference_t diff = compare_comm_pattern(list.list[list.index_comm].get(), comm);
127 if (diff != NONE_DIFF) {
128 if (comm->type == simgrid::mc::PatternCommunicationType::send) {
129 this->send_deterministic = 0;
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 = 0;
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 simgrid::mc::session->log_state();
147 mc_model_checker->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 XBT_INFO("%s", this->send_diff);
153 XBT_INFO("%s", this->recv_diff);
154 xbt_free(this->send_diff);
155 this->send_diff = nullptr;
156 xbt_free(this->recv_diff);
157 this->recv_diff = nullptr;
158 simgrid::mc::session->log_state();
159 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
165 /********** Non Static functions ***********/
167 void CommunicationDeterminismChecker::get_comm_pattern(smx_simcall_t request, e_mc_call_type_t call_type,
170 const smx_actor_t issuer = MC_smx_simcall_get_issuer(request);
171 const simgrid::mc::PatternCommunicationList& initial_pattern = initial_communications_pattern[issuer->get_pid()];
172 const std::vector<simgrid::mc::PatternCommunication*>& incomplete_pattern =
173 incomplete_communications_pattern[issuer->get_pid()];
175 std::unique_ptr<simgrid::mc::PatternCommunication> pattern(new simgrid::mc::PatternCommunication());
176 pattern->index = initial_pattern.index_comm + incomplete_pattern.size();
178 if (call_type == MC_CALL_TYPE_SEND) {
179 /* Create comm pattern */
180 pattern->type = simgrid::mc::PatternCommunicationType::send;
181 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request));
183 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
184 mc_model_checker->process().read(temp_synchro,
185 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
186 simgrid::kernel::activity::CommImpl* synchro =
187 static_cast<simgrid::kernel::activity::CommImpl*>(temp_synchro.getBuffer());
189 char* remote_name = mc_model_checker->process().read<char*>(RemotePtr<char*>(
190 (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->name_ : &synchro->mbox_cpy->name_)));
191 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
193 mc_model_checker->process().resolveActor(simgrid::mc::remote(synchro->src_actor_.get()))->get_pid();
194 pattern->src_host = MC_smx_actor_get_host_name(issuer);
197 simgrid::smpi::Request mpi_request;
198 mc_model_checker->process().read(
199 &mpi_request, remote(static_cast<simgrid::smpi::Request*>(simcall_comm_isend__get__data(request))));
200 pattern->tag = mpi_request.tag();
203 if (synchro->src_buff_ != nullptr) {
204 pattern->data.resize(synchro->src_buff_size_);
205 mc_model_checker->process().read_bytes(pattern->data.data(), pattern->data.size(), remote(synchro->src_buff_));
208 if(mpi_request.detached()){
209 if (not this->initial_communications_pattern_done) {
210 /* Store comm pattern */
211 initial_communications_pattern[pattern->src_proc].list.push_back(std::move(pattern));
213 /* Evaluate comm determinism */
214 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
215 initial_communications_pattern[pattern->src_proc].index_comm++;
220 } else if (call_type == MC_CALL_TYPE_RECV) {
221 pattern->type = simgrid::mc::PatternCommunicationType::receive;
222 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_irecv__getraw__result(request));
225 simgrid::smpi::Request mpi_request;
226 mc_model_checker->process().read(
227 &mpi_request, remote(static_cast<simgrid::smpi::Request*>(simcall_comm_irecv__get__data(request))));
228 pattern->tag = mpi_request.tag();
231 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
232 mc_model_checker->process().read(temp_comm,
233 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
234 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
237 mc_model_checker->process().read(&remote_name,
238 remote(comm->get_mailbox()
239 ? &simgrid::xbt::string::to_string_data(comm->get_mailbox()->name_).data
240 : &simgrid::xbt::string::to_string_data(comm->mbox_cpy->name_).data));
241 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
243 mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()))->get_pid();
244 pattern->dst_host = MC_smx_actor_get_host_name(issuer);
246 xbt_die("Unexpected call_type %i", (int) call_type);
248 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
249 incomplete_communications_pattern[issuer->get_pid()].push_back(pattern.release());
252 void CommunicationDeterminismChecker::complete_comm_pattern(
253 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer, int backtracking)
255 /* Complete comm pattern */
256 std::vector<simgrid::mc::PatternCommunication*>& incomplete_pattern = incomplete_communications_pattern[issuer];
257 auto current_comm_pattern = std::find_if(
258 begin(incomplete_pattern), end(incomplete_pattern),
259 [&comm_addr](simgrid::mc::PatternCommunication* comm) { return remote(comm->comm_addr) == comm_addr; });
260 if (current_comm_pattern == std::end(incomplete_pattern))
261 xbt_die("Corresponding communication not found!");
263 update_comm_pattern(*current_comm_pattern, comm_addr);
264 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern(*current_comm_pattern);
265 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %zd", issuer,
266 std::distance(begin(incomplete_pattern), current_comm_pattern));
267 incomplete_pattern.erase(current_comm_pattern);
269 if (not this->initial_communications_pattern_done)
270 /* Store comm pattern */
271 initial_communications_pattern[issuer].list.push_back(std::move(comm_pattern));
273 /* Evaluate comm determinism */
274 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
275 initial_communications_pattern[issuer].index_comm++;
279 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& s) : Checker(s)
283 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
285 RecordTrace CommunicationDeterminismChecker::get_record_trace() // override
288 for (auto const& state : stack_)
289 res.push_back(state->getTransition());
293 std::vector<std::string> CommunicationDeterminismChecker::get_textual_trace() // override
295 std::vector<std::string> trace;
296 for (auto const& state : stack_) {
297 smx_simcall_t req = &state->executed_req;
300 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
305 void CommunicationDeterminismChecker::log_state() // override
307 if (_sg_mc_comms_determinism && not this->recv_deterministic && this->send_deterministic) {
308 XBT_INFO("******************************************************");
309 XBT_INFO("**** Only-send-deterministic communication pattern ****");
310 XBT_INFO("******************************************************");
311 XBT_INFO("%s", this->recv_diff);
312 } else if (_sg_mc_comms_determinism && not this->send_deterministic && this->recv_deterministic) {
313 XBT_INFO("******************************************************");
314 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
315 XBT_INFO("******************************************************");
316 XBT_INFO("%s", this->send_diff);
318 XBT_INFO("Expanded states = %lu", expanded_states_count_);
319 XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
320 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
321 XBT_INFO("Send-deterministic : %s", not this->send_deterministic ? "No" : "Yes");
322 if (_sg_mc_comms_determinism)
323 XBT_INFO("Recv-deterministic : %s", not this->recv_deterministic ? "No" : "Yes");
326 void CommunicationDeterminismChecker::prepare()
328 const int maxpid = MC_smx_get_maxpid();
330 initial_communications_pattern.resize(maxpid);
331 incomplete_communications_pattern.resize(maxpid);
333 std::unique_ptr<simgrid::mc::State> initial_state =
334 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expanded_states_count_));
336 XBT_DEBUG("********* Start communication determinism verification *********");
338 /* Get an enabled actor and insert it in the interleave set of the initial state */
339 for (auto& actor : mc_model_checker->process().actors())
340 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
341 initial_state->addInterleavingSet(actor.copy.getBuffer());
343 stack_.push_back(std::move(initial_state));
346 static inline bool all_communications_are_finished()
348 for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
349 if (not incomplete_communications_pattern[current_actor].empty()) {
350 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
357 void CommunicationDeterminismChecker::restoreState()
359 /* Intermediate backtracking */
360 simgrid::mc::State* last_state = stack_.back().get();
361 if (last_state->system_state) {
362 last_state->system_state->restore(&mc_model_checker->process());
363 MC_restore_communications_pattern(last_state);
367 /* Restore the initial state */
368 simgrid::mc::session->restore_initial_state();
370 unsigned n = MC_smx_get_maxpid();
371 assert(n == incomplete_communications_pattern.size());
372 assert(n == initial_communications_pattern.size());
373 for (unsigned j=0; j < n ; j++) {
374 incomplete_communications_pattern[j].clear();
375 initial_communications_pattern[j].index_comm = 0;
378 /* Traverse the stack from the state at position start and re-execute the transitions */
379 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
380 if (state == stack_.back())
383 int req_num = state->transition.argument;
384 smx_simcall_t saved_req = &state->executed_req;
385 xbt_assert(saved_req);
387 /* because we got a copy of the executed request, we have to fetch the
388 real one, pointed by the request field of the issuer process */
390 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
391 smx_simcall_t req = &issuer->simcall;
393 /* TODO : handle test and testany simcalls */
394 e_mc_call_type_t call = MC_get_call_type(req);
395 mc_model_checker->handle_simcall(state->transition);
396 MC_handle_comm_pattern(call, req, req_num, 1);
397 mc_model_checker->wait_for_requests();
399 /* Update statistics */
400 mc_model_checker->visited_states++;
401 mc_model_checker->executed_transitions++;
405 void CommunicationDeterminismChecker::real_run()
407 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
408 smx_simcall_t req = nullptr;
410 while (not stack_.empty()) {
411 /* Get current state */
412 simgrid::mc::State* cur_state = stack_.back().get();
414 XBT_DEBUG("**************************************************");
415 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), cur_state->num,
416 cur_state->interleaveSize());
418 /* Update statistics */
419 mc_model_checker->visited_states++;
421 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
422 req = MC_state_get_request(cur_state);
426 if (req != nullptr && visited_state == nullptr) {
428 int req_num = cur_state->transition.argument;
430 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
433 if (dot_output != nullptr)
434 req_str = simgrid::mc::request_get_dot_output(req, req_num);
436 mc_model_checker->executed_transitions++;
438 /* TODO : handle test and testany simcalls */
439 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
440 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
441 call = MC_get_call_type(req);
443 /* Answer the request */
444 mc_model_checker->handle_simcall(cur_state->transition);
445 /* After this call req is no longer useful */
447 MC_handle_comm_pattern(call, req, req_num, 0);
449 /* Wait for requests (schedules processes) */
450 mc_model_checker->wait_for_requests();
452 /* Create the new expanded state */
453 std::unique_ptr<simgrid::mc::State> next_state =
454 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expanded_states_count_));
456 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
457 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
458 * with the initial pattern. */
459 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
461 if (_sg_mc_max_visited_states != 0)
462 visited_state = visited_states_.addVisitedState(expanded_states_count_, next_state.get(), compare_snapshots);
464 visited_state = nullptr;
466 if (visited_state == nullptr) {
468 /* Get enabled actors and insert them in the interleave set of the next state */
469 for (auto& actor : mc_model_checker->process().actors())
470 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
471 next_state->addInterleavingSet(actor.copy.getBuffer());
473 if (dot_output != nullptr)
474 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num, next_state->num, req_str.c_str());
476 } else if (dot_output != nullptr)
477 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", cur_state->num,
478 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
480 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 if (not this->initial_communications_pattern_done)
493 this->initial_communications_pattern_done = 1;
495 /* Trash the current state, no longer needed */
496 XBT_DEBUG("Delete state %d at depth %zu", cur_state->num, stack_.size());
499 visited_state = nullptr;
501 /* Check for deadlocks */
502 if (mc_model_checker->checkDeadlock()) {
504 throw simgrid::mc::DeadlockError();
507 while (not stack_.empty()) {
508 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
510 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
511 /* We found a back-tracking point, let's loop */
512 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num, stack_.size() + 1);
513 stack_.push_back(std::move(state));
515 this->restoreState();
517 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num, stack_.size());
521 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size() + 1);
527 simgrid::mc::session->log_state();
530 void CommunicationDeterminismChecker::run()
532 XBT_INFO("Check communication determinism");
533 simgrid::mc::session->initialize();
540 Checker* createCommunicationDeterminismChecker(Session& s)
542 return new CommunicationDeterminismChecker(s);