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_proc.get()));
104 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc.get()));
105 comm_pattern->src_proc = src_proc->pid_;
106 comm_pattern->dst_proc = dst_proc->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->pid_, simgrid::mc::PatternCommunicationList*);
176 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(incomplete_communications_pattern, issuer->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));
196 pattern->src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(synchro->src_proc.get()))->pid_;
197 pattern->src_host = MC_smx_actor_get_host_name(issuer);
200 simgrid::smpi::Request mpi_request = mc_model_checker->process().read<simgrid::smpi::Request>(
201 RemotePtr<simgrid::smpi::Request>((std::uint64_t)simcall_comm_isend__get__data(request)));
202 pattern->tag = mpi_request.tag();
205 if (synchro->src_buff != nullptr) {
206 pattern->data.resize(synchro->src_buff_size);
207 mc_model_checker->process().read_bytes(pattern->data.data(), pattern->data.size(), remote(synchro->src_buff));
210 if(mpi_request.detached()){
211 if (not this->initial_communications_pattern_done) {
212 /* Store comm pattern */
213 simgrid::mc::PatternCommunicationList* list =
214 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*);
215 list->list.push_back(std::move(pattern));
217 /* Evaluate comm determinism */
218 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
219 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*)
225 } else if (call_type == MC_CALL_TYPE_RECV) {
226 pattern->type = simgrid::mc::PatternCommunicationType::receive;
227 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_irecv__getraw__result(request));
230 simgrid::smpi::Request mpi_request;
231 mc_model_checker->process().read(&mpi_request,
232 remote((simgrid::smpi::Request*)simcall_comm_irecv__get__data(request)));
233 pattern->tag = mpi_request.tag();
236 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
237 mc_model_checker->process().read(temp_comm,
238 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
239 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
242 mc_model_checker->process().read(
243 &remote_name, remote(comm->mbox ? &simgrid::xbt::string::to_string_data(comm->mbox->name_).data
244 : &simgrid::xbt::string::to_string_data(comm->mbox_cpy->name_).data));
245 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
246 pattern->dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc.get()))->pid_;
247 pattern->dst_host = MC_smx_actor_get_host_name(issuer);
249 xbt_die("Unexpected call_type %i", (int) call_type);
251 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->pid_);
252 xbt_dynar_t dynar = xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid_, xbt_dynar_t);
253 simgrid::mc::PatternCommunication* pattern2 = pattern.release();
254 xbt_dynar_push(dynar, &pattern2);
257 void CommunicationDeterminismChecker::complete_comm_pattern(
258 xbt_dynar_t list, simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer,
261 simgrid::mc::PatternCommunication* current_comm_pattern;
262 unsigned int cursor = 0;
263 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern;
266 /* Complete comm pattern */
267 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern)
268 if (remote(current_comm_pattern->comm_addr) == comm_addr) {
269 update_comm_pattern(current_comm_pattern, comm_addr);
271 simgrid::mc::PatternCommunication* temp;
272 xbt_dynar_remove_at(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &temp);
273 comm_pattern = std::unique_ptr<simgrid::mc::PatternCommunication>(temp);
274 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
279 xbt_die("Corresponding communication not found!");
281 simgrid::mc::PatternCommunicationList* pattern =
282 xbt_dynar_get_as(initial_communications_pattern, issuer, simgrid::mc::PatternCommunicationList*);
284 if (not this->initial_communications_pattern_done)
285 /* Store comm pattern */
286 pattern->list.push_back(std::move(comm_pattern));
288 /* Evaluate comm determinism */
289 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
290 pattern->index_comm++;
294 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& session) : Checker(session)
298 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
300 RecordTrace CommunicationDeterminismChecker::getRecordTrace() // override
303 for (auto const& state : stack_)
304 res.push_back(state->getTransition());
308 std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // override
310 std::vector<std::string> trace;
311 for (auto const& state : stack_) {
312 smx_simcall_t req = &state->executed_req;
315 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
320 void CommunicationDeterminismChecker::logState() // override
322 if (_sg_mc_comms_determinism && not this->recv_deterministic && this->send_deterministic) {
323 XBT_INFO("******************************************************");
324 XBT_INFO("**** Only-send-deterministic communication pattern ****");
325 XBT_INFO("******************************************************");
326 XBT_INFO("%s", this->recv_diff);
327 } else if (_sg_mc_comms_determinism && not this->send_deterministic && this->recv_deterministic) {
328 XBT_INFO("******************************************************");
329 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
330 XBT_INFO("******************************************************");
331 XBT_INFO("%s", this->send_diff);
333 XBT_INFO("Expanded states = %lu", expandedStatesCount_);
334 XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
335 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
336 XBT_INFO("Send-deterministic : %s", not this->send_deterministic ? "No" : "Yes");
337 if (_sg_mc_comms_determinism)
338 XBT_INFO("Recv-deterministic : %s", not this->recv_deterministic ? "No" : "Yes");
341 void CommunicationDeterminismChecker::prepare()
343 const int maxpid = MC_smx_get_maxpid();
345 // Create initial_communications_pattern elements:
346 initial_communications_pattern = simgrid::xbt::newDeleteDynar<simgrid::mc::PatternCommunicationList*>();
347 for (int i = 0; i < maxpid; i++) {
348 simgrid::mc::PatternCommunicationList* process_list_pattern = new simgrid::mc::PatternCommunicationList();
349 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
352 // Create incomplete_communications_pattern elements:
353 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
354 for (int i = 0; i < maxpid; i++) {
355 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(simgrid::mc::PatternCommunication*), nullptr);
356 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
359 std::unique_ptr<simgrid::mc::State> initial_state =
360 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
362 XBT_DEBUG("********* Start communication determinism verification *********");
364 /* Get an enabled actor and insert it in the interleave set of the initial state */
365 for (auto& actor : mc_model_checker->process().actors())
366 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
367 initial_state->addInterleavingSet(actor.copy.getBuffer());
369 stack_.push_back(std::move(initial_state));
372 static inline bool all_communications_are_finished()
374 for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
375 xbt_dynar_t pattern = xbt_dynar_get_as(incomplete_communications_pattern, current_actor, xbt_dynar_t);
376 if (not xbt_dynar_is_empty(pattern)) {
377 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
384 void CommunicationDeterminismChecker::restoreState()
386 /* Intermediate backtracking */
387 simgrid::mc::State* state = stack_.back().get();
388 if (state->system_state) {
389 simgrid::mc::restore_snapshot(state->system_state);
390 MC_restore_communications_pattern(state);
394 /* Restore the initial state */
395 simgrid::mc::session->restoreInitialState();
397 unsigned n = MC_smx_get_maxpid();
398 assert(n == xbt_dynar_length(incomplete_communications_pattern));
399 assert(n == xbt_dynar_length(initial_communications_pattern));
400 for (unsigned j=0; j < n ; j++) {
401 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
402 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
405 /* Traverse the stack from the state at position start and re-execute the transitions */
406 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
407 if (state == stack_.back())
410 int req_num = state->transition.argument;
411 smx_simcall_t saved_req = &state->executed_req;
412 xbt_assert(saved_req);
414 /* because we got a copy of the executed request, we have to fetch the
415 real one, pointed by the request field of the issuer process */
417 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
418 smx_simcall_t req = &issuer->simcall;
420 /* TODO : handle test and testany simcalls */
421 e_mc_call_type_t call = MC_get_call_type(req);
422 mc_model_checker->handle_simcall(state->transition);
423 MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
424 mc_model_checker->wait_for_requests();
426 /* Update statistics */
427 mc_model_checker->visited_states++;
428 mc_model_checker->executed_transitions++;
432 void CommunicationDeterminismChecker::real_run()
434 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
435 smx_simcall_t req = nullptr;
437 while (not stack_.empty()) {
438 /* Get current state */
439 simgrid::mc::State* state = stack_.back().get();
441 XBT_DEBUG("**************************************************");
442 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), state->num,
443 state->interleaveSize());
445 /* Update statistics */
446 mc_model_checker->visited_states++;
448 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
449 req = MC_state_get_request(state);
453 if (req != nullptr && visited_state == nullptr) {
455 int req_num = state->transition.argument;
457 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
460 if (dot_output != nullptr)
461 req_str = simgrid::mc::request_get_dot_output(req, req_num);
463 mc_model_checker->executed_transitions++;
465 /* TODO : handle test and testany simcalls */
466 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
467 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
468 call = MC_get_call_type(req);
470 /* Answer the request */
471 mc_model_checker->handle_simcall(state->transition);
472 /* After this call req is no longer useful */
474 if (not this->initial_communications_pattern_done)
475 MC_handle_comm_pattern(call, req, req_num, initial_communications_pattern, 0);
477 MC_handle_comm_pattern(call, req, req_num, nullptr, 0);
479 /* Wait for requests (schedules processes) */
480 mc_model_checker->wait_for_requests();
482 /* Create the new expanded state */
483 std::unique_ptr<simgrid::mc::State> next_state =
484 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
486 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
487 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
488 * with the initial pattern. */
489 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
491 if (_sg_mc_max_visited_states != 0)
492 visited_state = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), compare_snapshots);
494 visited_state = nullptr;
496 if (visited_state == nullptr) {
498 /* Get enabled actors and insert them in the interleave set of the next state */
499 for (auto& actor : mc_model_checker->process().actors())
500 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
501 next_state->addInterleavingSet(actor.copy.getBuffer());
503 if (dot_output != nullptr)
504 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
505 state->num, next_state->num, req_str.c_str());
507 } else if (dot_output != nullptr)
508 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
509 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
511 stack_.push_back(std::move(next_state));
515 if (stack_.size() > (std::size_t) _sg_mc_max_depth)
516 XBT_WARN("/!\\ Max depth reached! /!\\ ");
517 else if (visited_state != nullptr)
518 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
519 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
521 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
523 if (not this->initial_communications_pattern_done)
524 this->initial_communications_pattern_done = 1;
526 /* Trash the current state, no longer needed */
527 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size());
530 visited_state = nullptr;
532 /* Check for deadlocks */
533 if (mc_model_checker->checkDeadlock()) {
535 throw simgrid::mc::DeadlockError();
538 while (not stack_.empty()) {
539 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
541 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
542 /* We found a back-tracking point, let's loop */
543 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num, stack_.size() + 1);
544 stack_.push_back(std::move(state));
546 this->restoreState();
548 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num, stack_.size());
552 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size() + 1);
558 simgrid::mc::session->logState();
561 void CommunicationDeterminismChecker::run()
563 XBT_INFO("Check communication determinism");
564 simgrid::mc::session->initialize();
571 Checker* createCommunicationDeterminismChecker(Session& session)
573 return new CommunicationDeterminismChecker(session);