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/VisitedState.hpp"
9 #include "src/mc/mc_exit.hpp"
10 #include "src/mc/mc_private.hpp"
11 #include "src/mc/mc_record.hpp"
12 #include "src/mc/mc_request.hpp"
13 #include "src/mc/mc_smx.hpp"
14 #include "src/mc/mc_state.hpp"
15 #include "src/mc/remote/Client.hpp"
18 #include "smpi_request.hpp"
23 using simgrid::mc::remote;
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
27 /********** Global variables **********/
29 xbt_dynar_t initial_communications_pattern;
30 xbt_dynar_t incomplete_communications_pattern;
32 /********** Static functions ***********/
34 static e_mc_comm_pattern_difference_t compare_comm_pattern(simgrid::mc::PatternCommunication* comm1,
35 simgrid::mc::PatternCommunication* comm2)
37 if(comm1->type != comm2->type)
39 if (comm1->rdv != comm2->rdv)
41 if (comm1->src_proc != comm2->src_proc)
43 if (comm1->dst_proc != comm2->dst_proc)
45 if (comm1->tag != comm2->tag)
47 if (comm1->data.size() != comm2->data.size())
48 return DATA_SIZE_DIFF;
49 if (comm1->data != comm2->data)
54 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process,
55 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);
67 res = bprintf("%s Different type for communication #%u", type, cursor);
70 res = bprintf("%s Different rdv for communication #%u", type, cursor);
73 res = bprintf("%s Different tag for communication #%u", type, cursor);
76 res = bprintf("%s Different source for communication #%u", type, cursor);
79 res = bprintf("%s Different destination for communication #%u", type, cursor);
82 res = bprintf("%s\n Different data size for communication #%u", type, cursor);
85 res = bprintf("%s\n Different data for communication #%u", type, cursor);
95 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
96 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr)
99 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
100 mc_model_checker->process().read(temp_comm, comm_addr);
101 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
103 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_actor_.get()));
104 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()));
105 comm_pattern->src_proc = src_proc->get_pid();
106 comm_pattern->dst_proc = dst_proc->get_pid();
107 comm_pattern->src_host = MC_smx_actor_get_host_name(src_proc);
108 comm_pattern->dst_host = MC_smx_actor_get_host_name(dst_proc);
109 if (comm_pattern->data.size() == 0 && comm->src_buff_ != nullptr) {
111 mc_model_checker->process().read(&buff_size, remote(comm->dst_buff_size_));
112 comm_pattern->data.resize(buff_size);
113 mc_model_checker->process().read_bytes(comm_pattern->data.data(), comm_pattern->data.size(),
114 remote(comm->src_buff_));
121 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, simgrid::mc::PatternCommunication* comm,
124 simgrid::mc::PatternCommunicationList* list =
125 xbt_dynar_get_as(initial_communications_pattern, process, simgrid::mc::PatternCommunicationList*);
127 if (not backtracking) {
128 e_mc_comm_pattern_difference_t diff = compare_comm_pattern(list->list[list->index_comm].get(), comm);
130 if (diff != NONE_DIFF) {
131 if (comm->type == simgrid::mc::PatternCommunicationType::send) {
132 this->send_deterministic = 0;
133 if (this->send_diff != nullptr)
134 xbt_free(this->send_diff);
135 this->send_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
137 this->recv_deterministic = 0;
138 if (this->recv_diff != nullptr)
139 xbt_free(this->recv_diff);
140 this->recv_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
142 if (_sg_mc_send_determinism && not this->send_deterministic) {
143 XBT_INFO("*********************************************************");
144 XBT_INFO("***** Non-send-deterministic communications pattern *****");
145 XBT_INFO("*********************************************************");
146 XBT_INFO("%s", this->send_diff);
147 xbt_free(this->send_diff);
148 this->send_diff = nullptr;
149 simgrid::mc::session->logState();
150 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
151 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
152 XBT_INFO("****************************************************");
153 XBT_INFO("***** Non-deterministic communications pattern *****");
154 XBT_INFO("****************************************************");
155 XBT_INFO("%s", this->send_diff);
156 XBT_INFO("%s", this->recv_diff);
157 xbt_free(this->send_diff);
158 this->send_diff = nullptr;
159 xbt_free(this->recv_diff);
160 this->recv_diff = nullptr;
161 simgrid::mc::session->logState();
162 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
168 /********** Non Static functions ***********/
170 void CommunicationDeterminismChecker::get_comm_pattern(xbt_dynar_t list, smx_simcall_t request,
171 e_mc_call_type_t call_type, int backtracking)
173 const smx_actor_t issuer = MC_smx_simcall_get_issuer(request);
174 simgrid::mc::PatternCommunicationList* initial_pattern =
175 xbt_dynar_get_as(initial_communications_pattern, issuer->get_pid(), simgrid::mc::PatternCommunicationList*);
176 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(incomplete_communications_pattern, issuer->get_pid(), xbt_dynar_t);
178 std::unique_ptr<simgrid::mc::PatternCommunication> pattern =
179 std::unique_ptr<simgrid::mc::PatternCommunication>(new simgrid::mc::PatternCommunication());
180 pattern->index = initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
182 if (call_type == MC_CALL_TYPE_SEND) {
183 /* Create comm pattern */
184 pattern->type = simgrid::mc::PatternCommunicationType::send;
185 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request));
187 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
188 mc_model_checker->process().read(temp_synchro,
189 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
190 simgrid::kernel::activity::CommImpl* synchro =
191 static_cast<simgrid::kernel::activity::CommImpl*>(temp_synchro.getBuffer());
193 char* remote_name = mc_model_checker->process().read<char*>(
194 RemotePtr<char*>((uint64_t)(synchro->mbox ? &synchro->mbox->name_ : &synchro->mbox_cpy->name_)));
195 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
197 mc_model_checker->process().resolveActor(simgrid::mc::remote(synchro->src_actor_.get()))->get_pid();
198 pattern->src_host = MC_smx_actor_get_host_name(issuer);
201 simgrid::smpi::Request mpi_request = mc_model_checker->process().read<simgrid::smpi::Request>(
202 RemotePtr<simgrid::smpi::Request>((std::uint64_t)simcall_comm_isend__get__data(request)));
203 pattern->tag = mpi_request.tag();
206 if (synchro->src_buff_ != nullptr) {
207 pattern->data.resize(synchro->src_buff_size_);
208 mc_model_checker->process().read_bytes(pattern->data.data(), pattern->data.size(), remote(synchro->src_buff_));
211 if(mpi_request.detached()){
212 if (not this->initial_communications_pattern_done) {
213 /* Store comm pattern */
214 simgrid::mc::PatternCommunicationList* list =
215 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*);
216 list->list.push_back(std::move(pattern));
218 /* Evaluate comm determinism */
219 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
220 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*)
226 } else if (call_type == MC_CALL_TYPE_RECV) {
227 pattern->type = simgrid::mc::PatternCommunicationType::receive;
228 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_irecv__getraw__result(request));
231 simgrid::smpi::Request mpi_request;
232 mc_model_checker->process().read(&mpi_request,
233 remote((simgrid::smpi::Request*)simcall_comm_irecv__get__data(request)));
234 pattern->tag = mpi_request.tag();
237 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
238 mc_model_checker->process().read(temp_comm,
239 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
240 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
243 mc_model_checker->process().read(
244 &remote_name, remote(comm->mbox ? &simgrid::xbt::string::to_string_data(comm->mbox->name_).data
245 : &simgrid::xbt::string::to_string_data(comm->mbox_cpy->name_).data));
246 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
248 mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()))->get_pid();
249 pattern->dst_host = MC_smx_actor_get_host_name(issuer);
251 xbt_die("Unexpected call_type %i", (int) call_type);
253 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->get_pid());
254 xbt_dynar_t dynar = xbt_dynar_get_as(incomplete_communications_pattern, issuer->get_pid(), xbt_dynar_t);
255 simgrid::mc::PatternCommunication* pattern2 = pattern.release();
256 xbt_dynar_push(dynar, &pattern2);
259 void CommunicationDeterminismChecker::complete_comm_pattern(
260 xbt_dynar_t list, simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer,
263 simgrid::mc::PatternCommunication* current_comm_pattern;
264 unsigned int cursor = 0;
265 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern;
268 /* Complete comm pattern */
269 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern)
270 if (remote(current_comm_pattern->comm_addr) == comm_addr) {
271 update_comm_pattern(current_comm_pattern, comm_addr);
273 simgrid::mc::PatternCommunication* temp;
274 xbt_dynar_remove_at(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &temp);
275 comm_pattern = std::unique_ptr<simgrid::mc::PatternCommunication>(temp);
276 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
281 xbt_die("Corresponding communication not found!");
283 simgrid::mc::PatternCommunicationList* pattern =
284 xbt_dynar_get_as(initial_communications_pattern, issuer, simgrid::mc::PatternCommunicationList*);
286 if (not this->initial_communications_pattern_done)
287 /* Store comm pattern */
288 pattern->list.push_back(std::move(comm_pattern));
290 /* Evaluate comm determinism */
291 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
292 pattern->index_comm++;
296 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& session) : Checker(session)
300 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
302 RecordTrace CommunicationDeterminismChecker::getRecordTrace() // override
305 for (auto const& state : stack_)
306 res.push_back(state->getTransition());
310 std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // override
312 std::vector<std::string> trace;
313 for (auto const& state : stack_) {
314 smx_simcall_t req = &state->executed_req;
317 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
322 void CommunicationDeterminismChecker::logState() // override
324 if (_sg_mc_comms_determinism && not this->recv_deterministic && this->send_deterministic) {
325 XBT_INFO("******************************************************");
326 XBT_INFO("**** Only-send-deterministic communication pattern ****");
327 XBT_INFO("******************************************************");
328 XBT_INFO("%s", this->recv_diff);
329 } else if (_sg_mc_comms_determinism && not this->send_deterministic && this->recv_deterministic) {
330 XBT_INFO("******************************************************");
331 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
332 XBT_INFO("******************************************************");
333 XBT_INFO("%s", this->send_diff);
335 XBT_INFO("Expanded states = %lu", expandedStatesCount_);
336 XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
337 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
338 XBT_INFO("Send-deterministic : %s", not this->send_deterministic ? "No" : "Yes");
339 if (_sg_mc_comms_determinism)
340 XBT_INFO("Recv-deterministic : %s", not this->recv_deterministic ? "No" : "Yes");
343 void CommunicationDeterminismChecker::prepare()
345 const int maxpid = MC_smx_get_maxpid();
347 // Create initial_communications_pattern elements:
348 initial_communications_pattern = simgrid::xbt::newDeleteDynar<simgrid::mc::PatternCommunicationList*>();
349 for (int i = 0; i < maxpid; i++) {
350 simgrid::mc::PatternCommunicationList* process_list_pattern = new simgrid::mc::PatternCommunicationList();
351 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
354 // Create incomplete_communications_pattern elements:
355 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
356 for (int i = 0; i < maxpid; i++) {
357 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(simgrid::mc::PatternCommunication*), nullptr);
358 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
361 std::unique_ptr<simgrid::mc::State> initial_state =
362 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
364 XBT_DEBUG("********* Start communication determinism verification *********");
366 /* Get an enabled actor and insert it in the interleave set of the initial state */
367 for (auto& actor : mc_model_checker->process().actors())
368 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
369 initial_state->addInterleavingSet(actor.copy.getBuffer());
371 stack_.push_back(std::move(initial_state));
374 static inline bool all_communications_are_finished()
376 for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
377 xbt_dynar_t pattern = xbt_dynar_get_as(incomplete_communications_pattern, current_actor, xbt_dynar_t);
378 if (not xbt_dynar_is_empty(pattern)) {
379 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
386 void CommunicationDeterminismChecker::restoreState()
388 /* Intermediate backtracking */
389 simgrid::mc::State* state = stack_.back().get();
390 if (state->system_state) {
391 simgrid::mc::restore_snapshot(state->system_state);
392 MC_restore_communications_pattern(state);
396 /* Restore the initial state */
397 simgrid::mc::session->restoreInitialState();
399 unsigned n = MC_smx_get_maxpid();
400 assert(n == xbt_dynar_length(incomplete_communications_pattern));
401 assert(n == xbt_dynar_length(initial_communications_pattern));
402 for (unsigned j=0; j < n ; j++) {
403 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
404 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
407 /* Traverse the stack from the state at position start and re-execute the transitions */
408 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
409 if (state == stack_.back())
412 int req_num = state->transition.argument;
413 smx_simcall_t saved_req = &state->executed_req;
414 xbt_assert(saved_req);
416 /* because we got a copy of the executed request, we have to fetch the
417 real one, pointed by the request field of the issuer process */
419 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
420 smx_simcall_t req = &issuer->simcall;
422 /* TODO : handle test and testany simcalls */
423 e_mc_call_type_t call = MC_get_call_type(req);
424 mc_model_checker->handle_simcall(state->transition);
425 MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
426 mc_model_checker->wait_for_requests();
428 /* Update statistics */
429 mc_model_checker->visited_states++;
430 mc_model_checker->executed_transitions++;
434 void CommunicationDeterminismChecker::real_run()
436 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
437 smx_simcall_t req = nullptr;
439 while (not stack_.empty()) {
440 /* Get current state */
441 simgrid::mc::State* state = stack_.back().get();
443 XBT_DEBUG("**************************************************");
444 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), state->num,
445 state->interleaveSize());
447 /* Update statistics */
448 mc_model_checker->visited_states++;
450 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
451 req = MC_state_get_request(state);
455 if (req != nullptr && visited_state == nullptr) {
457 int req_num = state->transition.argument;
459 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
462 if (dot_output != nullptr)
463 req_str = simgrid::mc::request_get_dot_output(req, req_num);
465 mc_model_checker->executed_transitions++;
467 /* TODO : handle test and testany simcalls */
468 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
469 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
470 call = MC_get_call_type(req);
472 /* Answer the request */
473 mc_model_checker->handle_simcall(state->transition);
474 /* After this call req is no longer useful */
476 if (not this->initial_communications_pattern_done)
477 MC_handle_comm_pattern(call, req, req_num, initial_communications_pattern, 0);
479 MC_handle_comm_pattern(call, req, req_num, nullptr, 0);
481 /* Wait for requests (schedules processes) */
482 mc_model_checker->wait_for_requests();
484 /* Create the new expanded state */
485 std::unique_ptr<simgrid::mc::State> next_state =
486 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
488 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
489 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
490 * with the initial pattern. */
491 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
493 if (_sg_mc_max_visited_states != 0)
494 visited_state = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), compare_snapshots);
496 visited_state = nullptr;
498 if (visited_state == nullptr) {
500 /* Get enabled actors and insert them in the interleave set of the next state */
501 for (auto& actor : mc_model_checker->process().actors())
502 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
503 next_state->addInterleavingSet(actor.copy.getBuffer());
505 if (dot_output != nullptr)
506 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
507 state->num, next_state->num, req_str.c_str());
509 } else if (dot_output != nullptr)
510 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
511 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
513 stack_.push_back(std::move(next_state));
517 if (stack_.size() > (std::size_t) _sg_mc_max_depth)
518 XBT_WARN("/!\\ Max depth reached! /!\\ ");
519 else if (visited_state != nullptr)
520 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
521 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
523 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
525 if (not this->initial_communications_pattern_done)
526 this->initial_communications_pattern_done = 1;
528 /* Trash the current state, no longer needed */
529 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size());
532 visited_state = nullptr;
534 /* Check for deadlocks */
535 if (mc_model_checker->checkDeadlock()) {
537 throw simgrid::mc::DeadlockError();
540 while (not stack_.empty()) {
541 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
543 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
544 /* We found a back-tracking point, let's loop */
545 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num, stack_.size() + 1);
546 stack_.push_back(std::move(state));
548 this->restoreState();
550 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num, stack_.size());
554 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size() + 1);
560 simgrid::mc::session->logState();
563 void CommunicationDeterminismChecker::run()
565 XBT_INFO("Check communication determinism");
566 simgrid::mc::session->initialize();
573 Checker* createCommunicationDeterminismChecker(Session& session)
575 return new CommunicationDeterminismChecker(session);