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(smx_simcall_t request, e_mc_call_type_t call_type,
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 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer, int backtracking)
262 simgrid::mc::PatternCommunication* current_comm_pattern;
263 unsigned int cursor = 0;
264 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern;
267 /* Complete comm pattern */
268 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern)
269 if (remote(current_comm_pattern->comm_addr) == comm_addr) {
270 update_comm_pattern(current_comm_pattern, comm_addr);
272 simgrid::mc::PatternCommunication* temp;
273 xbt_dynar_remove_at(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &temp);
274 comm_pattern = std::unique_ptr<simgrid::mc::PatternCommunication>(temp);
275 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
280 xbt_die("Corresponding communication not found!");
282 simgrid::mc::PatternCommunicationList* pattern =
283 xbt_dynar_get_as(initial_communications_pattern, issuer, simgrid::mc::PatternCommunicationList*);
285 if (not this->initial_communications_pattern_done)
286 /* Store comm pattern */
287 pattern->list.push_back(std::move(comm_pattern));
289 /* Evaluate comm determinism */
290 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
291 pattern->index_comm++;
295 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& session) : Checker(session)
299 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
301 RecordTrace CommunicationDeterminismChecker::getRecordTrace() // override
304 for (auto const& state : stack_)
305 res.push_back(state->getTransition());
309 std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // override
311 std::vector<std::string> trace;
312 for (auto const& state : stack_) {
313 smx_simcall_t req = &state->executed_req;
316 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
321 void CommunicationDeterminismChecker::logState() // override
323 if (_sg_mc_comms_determinism && not this->recv_deterministic && this->send_deterministic) {
324 XBT_INFO("******************************************************");
325 XBT_INFO("**** Only-send-deterministic communication pattern ****");
326 XBT_INFO("******************************************************");
327 XBT_INFO("%s", this->recv_diff);
328 } else if (_sg_mc_comms_determinism && not this->send_deterministic && this->recv_deterministic) {
329 XBT_INFO("******************************************************");
330 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
331 XBT_INFO("******************************************************");
332 XBT_INFO("%s", this->send_diff);
334 XBT_INFO("Expanded states = %lu", expandedStatesCount_);
335 XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
336 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
337 XBT_INFO("Send-deterministic : %s", not this->send_deterministic ? "No" : "Yes");
338 if (_sg_mc_comms_determinism)
339 XBT_INFO("Recv-deterministic : %s", not this->recv_deterministic ? "No" : "Yes");
342 void CommunicationDeterminismChecker::prepare()
344 const int maxpid = MC_smx_get_maxpid();
346 // Create initial_communications_pattern elements:
347 initial_communications_pattern = simgrid::xbt::newDeleteDynar<simgrid::mc::PatternCommunicationList*>();
348 for (int i = 0; i < maxpid; i++) {
349 simgrid::mc::PatternCommunicationList* process_list_pattern = new simgrid::mc::PatternCommunicationList();
350 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
353 // Create incomplete_communications_pattern elements:
354 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
355 for (int i = 0; i < maxpid; i++) {
356 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(simgrid::mc::PatternCommunication*), nullptr);
357 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
360 std::unique_ptr<simgrid::mc::State> initial_state =
361 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
363 XBT_DEBUG("********* Start communication determinism verification *********");
365 /* Get an enabled actor and insert it in the interleave set of the initial state */
366 for (auto& actor : mc_model_checker->process().actors())
367 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
368 initial_state->addInterleavingSet(actor.copy.getBuffer());
370 stack_.push_back(std::move(initial_state));
373 static inline bool all_communications_are_finished()
375 for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
376 xbt_dynar_t pattern = xbt_dynar_get_as(incomplete_communications_pattern, current_actor, xbt_dynar_t);
377 if (not xbt_dynar_is_empty(pattern)) {
378 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
385 void CommunicationDeterminismChecker::restoreState()
387 /* Intermediate backtracking */
388 simgrid::mc::State* state = stack_.back().get();
389 if (state->system_state) {
390 simgrid::mc::restore_snapshot(state->system_state);
391 MC_restore_communications_pattern(state);
395 /* Restore the initial state */
396 simgrid::mc::session->restoreInitialState();
398 unsigned n = MC_smx_get_maxpid();
399 assert(n == xbt_dynar_length(incomplete_communications_pattern));
400 assert(n == xbt_dynar_length(initial_communications_pattern));
401 for (unsigned j=0; j < n ; j++) {
402 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
403 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
406 /* Traverse the stack from the state at position start and re-execute the transitions */
407 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
408 if (state == stack_.back())
411 int req_num = state->transition.argument;
412 smx_simcall_t saved_req = &state->executed_req;
413 xbt_assert(saved_req);
415 /* because we got a copy of the executed request, we have to fetch the
416 real one, pointed by the request field of the issuer process */
418 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
419 smx_simcall_t req = &issuer->simcall;
421 /* TODO : handle test and testany simcalls */
422 e_mc_call_type_t call = MC_get_call_type(req);
423 mc_model_checker->handle_simcall(state->transition);
424 MC_handle_comm_pattern(call, req, req_num, 1);
425 mc_model_checker->wait_for_requests();
427 /* Update statistics */
428 mc_model_checker->visited_states++;
429 mc_model_checker->executed_transitions++;
433 void CommunicationDeterminismChecker::real_run()
435 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
436 smx_simcall_t req = nullptr;
438 while (not stack_.empty()) {
439 /* Get current state */
440 simgrid::mc::State* state = stack_.back().get();
442 XBT_DEBUG("**************************************************");
443 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), state->num,
444 state->interleaveSize());
446 /* Update statistics */
447 mc_model_checker->visited_states++;
449 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
450 req = MC_state_get_request(state);
454 if (req != nullptr && visited_state == nullptr) {
456 int req_num = state->transition.argument;
458 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
461 if (dot_output != nullptr)
462 req_str = simgrid::mc::request_get_dot_output(req, req_num);
464 mc_model_checker->executed_transitions++;
466 /* TODO : handle test and testany simcalls */
467 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
468 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
469 call = MC_get_call_type(req);
471 /* Answer the request */
472 mc_model_checker->handle_simcall(state->transition);
473 /* After this call req is no longer useful */
475 MC_handle_comm_pattern(call, req, req_num, 0);
477 /* Wait for requests (schedules processes) */
478 mc_model_checker->wait_for_requests();
480 /* Create the new expanded state */
481 std::unique_ptr<simgrid::mc::State> next_state =
482 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
484 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
485 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
486 * with the initial pattern. */
487 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
489 if (_sg_mc_max_visited_states != 0)
490 visited_state = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), compare_snapshots);
492 visited_state = nullptr;
494 if (visited_state == nullptr) {
496 /* Get enabled actors and insert them in the interleave set of the next state */
497 for (auto& actor : mc_model_checker->process().actors())
498 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
499 next_state->addInterleavingSet(actor.copy.getBuffer());
501 if (dot_output != nullptr)
502 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
503 state->num, next_state->num, req_str.c_str());
505 } else if (dot_output != nullptr)
506 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
507 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
509 stack_.push_back(std::move(next_state));
513 if (stack_.size() > (std::size_t) _sg_mc_max_depth)
514 XBT_WARN("/!\\ Max depth reached! /!\\ ");
515 else if (visited_state != nullptr)
516 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
517 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
519 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
521 if (not this->initial_communications_pattern_done)
522 this->initial_communications_pattern_done = 1;
524 /* Trash the current state, no longer needed */
525 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size());
528 visited_state = nullptr;
530 /* Check for deadlocks */
531 if (mc_model_checker->checkDeadlock()) {
533 throw simgrid::mc::DeadlockError();
536 while (not stack_.empty()) {
537 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
539 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
540 /* We found a back-tracking point, let's loop */
541 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num, stack_.size() + 1);
542 stack_.push_back(std::move(state));
544 this->restoreState();
546 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num, stack_.size());
550 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size() + 1);
556 simgrid::mc::session->logState();
559 void CommunicationDeterminismChecker::run()
561 XBT_INFO("Check communication determinism");
562 simgrid::mc::session->initialize();
569 Checker* createCommunicationDeterminismChecker(Session& session)
571 return new CommunicationDeterminismChecker(session);