1 /* Copyright (c) 2008-2018. 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. */
10 #include <xbt/sysdep.h>
12 #include "src/mc/VisitedState.hpp"
13 #include "src/mc/checker/CommunicationDeterminismChecker.hpp"
14 #include "src/mc/mc_exit.hpp"
15 #include "src/mc/mc_private.hpp"
16 #include "src/mc/mc_record.hpp"
17 #include "src/mc/mc_request.hpp"
18 #include "src/mc/mc_smx.hpp"
19 #include "src/mc/mc_state.hpp"
20 #include "src/mc/remote/Client.hpp"
22 #include "smpi_request.hpp"
24 using simgrid::mc::remote;
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
28 /********** Global variables **********/
30 xbt_dynar_t initial_communications_pattern;
31 xbt_dynar_t incomplete_communications_pattern;
33 /********** Static functions ***********/
35 static e_mc_comm_pattern_difference_t compare_comm_pattern(simgrid::mc::PatternCommunication* comm1,
36 simgrid::mc::PatternCommunication* comm2)
38 if(comm1->type != comm2->type)
40 if (comm1->rdv != comm2->rdv)
42 if (comm1->src_proc != comm2->src_proc)
44 if (comm1->dst_proc != comm2->dst_proc)
46 if (comm1->tag != comm2->tag)
48 if (comm1->data.size() != comm2->data.size())
49 return DATA_SIZE_DIFF;
50 if (comm1->data != comm2->data)
55 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process,
56 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);
68 res = bprintf("%s Different type for communication #%u", type, cursor);
71 res = bprintf("%s Different rdv for communication #%u", type, cursor);
74 res = bprintf("%s Different tag for communication #%u", type, cursor);
77 res = bprintf("%s Different source for communication #%u", type, cursor);
80 res = bprintf("%s Different destination for communication #%u", type, cursor);
83 res = bprintf("%s\n Different data size for communication #%u", type, cursor);
86 res = bprintf("%s\n Different data for communication #%u", type, cursor);
96 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
97 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr)
100 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
101 mc_model_checker->process().read(temp_comm, comm_addr);
102 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
104 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_proc));
105 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc));
106 comm_pattern->src_proc = src_proc->pid;
107 comm_pattern->dst_proc = dst_proc->pid;
108 comm_pattern->src_host = MC_smx_actor_get_host_name(src_proc);
109 comm_pattern->dst_host = MC_smx_actor_get_host_name(dst_proc);
110 if (comm_pattern->data.size() == 0 && comm->src_buff != nullptr) {
112 mc_model_checker->process().read(&buff_size, remote(comm->dst_buff_size));
113 comm_pattern->data.resize(buff_size);
114 mc_model_checker->process().read_bytes(comm_pattern->data.data(), comm_pattern->data.size(),
115 remote(comm->src_buff));
122 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, simgrid::mc::PatternCommunication* comm,
125 simgrid::mc::PatternCommunicationList* list =
126 xbt_dynar_get_as(initial_communications_pattern, process, simgrid::mc::PatternCommunicationList*);
128 if (not backtracking) {
129 e_mc_comm_pattern_difference_t diff = compare_comm_pattern(list->list[list->index_comm].get(), comm);
131 if (diff != NONE_DIFF) {
132 if (comm->type == simgrid::mc::PatternCommunicationType::send) {
133 this->send_deterministic = 0;
134 if (this->send_diff != nullptr)
135 xbt_free(this->send_diff);
136 this->send_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
138 this->recv_deterministic = 0;
139 if (this->recv_diff != nullptr)
140 xbt_free(this->recv_diff);
141 this->recv_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
143 if (_sg_mc_send_determinism && not this->send_deterministic) {
144 XBT_INFO("*********************************************************");
145 XBT_INFO("***** Non-send-deterministic communications pattern *****");
146 XBT_INFO("*********************************************************");
147 XBT_INFO("%s", this->send_diff);
148 xbt_free(this->send_diff);
149 this->send_diff = nullptr;
150 simgrid::mc::session->logState();
151 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
152 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
153 XBT_INFO("****************************************************");
154 XBT_INFO("***** Non-deterministic communications pattern *****");
155 XBT_INFO("****************************************************");
156 XBT_INFO("%s", this->send_diff);
157 XBT_INFO("%s", this->recv_diff);
158 xbt_free(this->send_diff);
159 this->send_diff = nullptr;
160 xbt_free(this->recv_diff);
161 this->recv_diff = nullptr;
162 simgrid::mc::session->logState();
163 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
169 /********** Non Static functions ***********/
171 void CommunicationDeterminismChecker::get_comm_pattern(xbt_dynar_t list, smx_simcall_t request,
172 e_mc_call_type_t call_type, int backtracking)
174 const smx_actor_t issuer = MC_smx_simcall_get_issuer(request);
175 simgrid::mc::PatternCommunicationList* initial_pattern =
176 xbt_dynar_get_as(initial_communications_pattern, issuer->pid, simgrid::mc::PatternCommunicationList*);
177 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
179 std::unique_ptr<simgrid::mc::PatternCommunication> pattern =
180 std::unique_ptr<simgrid::mc::PatternCommunication>(new simgrid::mc::PatternCommunication());
181 pattern->index = initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
183 if (call_type == MC_CALL_TYPE_SEND) {
184 /* Create comm pattern */
185 pattern->type = simgrid::mc::PatternCommunicationType::send;
186 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request));
188 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
189 mc_model_checker->process().read(temp_synchro,
190 remote(static_cast<simgrid::kernel::activity::CommImpl*>(pattern->comm_addr)));
191 simgrid::kernel::activity::CommImpl* synchro =
192 static_cast<simgrid::kernel::activity::CommImpl*>(temp_synchro.getBuffer());
194 char* remote_name = mc_model_checker->process().read<char*>(
195 RemotePtr<char*>((uint64_t)(synchro->mbox ? &synchro->mbox->name_ : &synchro->mbox_cpy->name_)));
196 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
197 pattern->src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(synchro->src_proc))->pid;
198 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();
204 if (synchro->src_buff != nullptr) {
205 pattern->data.resize(synchro->src_buff_size);
206 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 simgrid::mc::PatternCommunicationList* list =
212 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*);
213 list->list.push_back(std::move(pattern));
215 /* Evaluate comm determinism */
216 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
217 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*)
222 } else if (call_type == MC_CALL_TYPE_RECV) {
223 pattern->type = simgrid::mc::PatternCommunicationType::receive;
224 pattern->comm_addr = static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_irecv__getraw__result(request));
226 simgrid::smpi::Request mpi_request;
227 mc_model_checker->process().read(&mpi_request,
228 remote((simgrid::smpi::Request*)simcall_comm_irecv__get__data(request)));
229 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(
238 &remote_name, remote(comm->mbox ? &simgrid::xbt::string::to_string_data(comm->mbox->name_).data
239 : &simgrid::xbt::string::to_string_data(comm->mbox_cpy->name_).data));
240 pattern->rdv = mc_model_checker->process().read_string(RemotePtr<char>(remote_name));
241 pattern->dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc))->pid;
242 pattern->dst_host = MC_smx_actor_get_host_name(issuer);
244 xbt_die("Unexpected call_type %i", (int) call_type);
246 XBT_DEBUG("Insert incomplete comm pattern %p for process %ld", pattern.get(), issuer->pid);
247 xbt_dynar_t dynar = xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
248 simgrid::mc::PatternCommunication* pattern2 = pattern.release();
249 xbt_dynar_push(dynar, &pattern2);
252 void CommunicationDeterminismChecker::complete_comm_pattern(
253 xbt_dynar_t list, simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> comm_addr, unsigned int issuer,
256 simgrid::mc::PatternCommunication* current_comm_pattern;
257 unsigned int cursor = 0;
258 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern;
261 /* Complete comm pattern */
262 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern)
263 if (remote(current_comm_pattern->comm_addr) == comm_addr) {
264 update_comm_pattern(current_comm_pattern, comm_addr);
266 simgrid::mc::PatternCommunication* temp;
267 xbt_dynar_remove_at(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &temp);
268 comm_pattern = std::unique_ptr<simgrid::mc::PatternCommunication>(temp);
269 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
274 xbt_die("Corresponding communication not found!");
276 simgrid::mc::PatternCommunicationList* pattern =
277 xbt_dynar_get_as(initial_communications_pattern, issuer, simgrid::mc::PatternCommunicationList*);
279 if (not this->initial_communications_pattern_done)
280 /* Store comm pattern */
281 pattern->list.push_back(std::move(comm_pattern));
283 /* Evaluate comm determinism */
284 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
285 pattern->index_comm++;
289 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& session) : Checker(session)
293 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
295 RecordTrace CommunicationDeterminismChecker::getRecordTrace() // override
298 for (auto const& state : stack_)
299 res.push_back(state->getTransition());
303 std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // override
305 std::vector<std::string> trace;
306 for (auto const& state : stack_) {
307 smx_simcall_t req = &state->executed_req;
310 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
315 void CommunicationDeterminismChecker::logState() // override
317 if (_sg_mc_comms_determinism && not this->recv_deterministic && this->send_deterministic) {
318 XBT_INFO("******************************************************");
319 XBT_INFO("**** Only-send-deterministic communication pattern ****");
320 XBT_INFO("******************************************************");
321 XBT_INFO("%s", this->recv_diff);
322 } else if (_sg_mc_comms_determinism && not this->send_deterministic && this->recv_deterministic) {
323 XBT_INFO("******************************************************");
324 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
325 XBT_INFO("******************************************************");
326 XBT_INFO("%s", this->send_diff);
328 XBT_INFO("Expanded states = %lu", expandedStatesCount_);
329 XBT_INFO("Visited states = %lu", mc_model_checker->visited_states);
330 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
331 XBT_INFO("Send-deterministic : %s", not this->send_deterministic ? "No" : "Yes");
332 if (_sg_mc_comms_determinism)
333 XBT_INFO("Recv-deterministic : %s", not this->recv_deterministic ? "No" : "Yes");
336 void CommunicationDeterminismChecker::prepare()
338 const int maxpid = MC_smx_get_maxpid();
340 // Create initial_communications_pattern elements:
341 initial_communications_pattern = simgrid::xbt::newDeleteDynar<simgrid::mc::PatternCommunicationList*>();
342 for (int i = 0; i < maxpid; i++) {
343 simgrid::mc::PatternCommunicationList* process_list_pattern = new simgrid::mc::PatternCommunicationList();
344 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
347 // Create incomplete_communications_pattern elements:
348 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
349 for (int i = 0; i < maxpid; i++) {
350 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(simgrid::mc::PatternCommunication*), nullptr);
351 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
354 std::unique_ptr<simgrid::mc::State> initial_state =
355 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
357 XBT_DEBUG("********* Start communication determinism verification *********");
359 /* Get an enabled actor and insert it in the interleave set of the initial state */
360 for (auto& actor : mc_model_checker->process().actors())
361 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
362 initial_state->addInterleavingSet(actor.copy.getBuffer());
364 stack_.push_back(std::move(initial_state));
367 static inline bool all_communications_are_finished()
369 for (size_t current_actor = 1; current_actor < MC_smx_get_maxpid(); current_actor++) {
370 xbt_dynar_t pattern = xbt_dynar_get_as(incomplete_communications_pattern, current_actor, xbt_dynar_t);
371 if (not xbt_dynar_is_empty(pattern)) {
372 XBT_DEBUG("Some communications are not finished, cannot stop the exploration! State not visited.");
379 void CommunicationDeterminismChecker::restoreState()
381 /* Intermediate backtracking */
382 simgrid::mc::State* state = stack_.back().get();
383 if (state->system_state) {
384 simgrid::mc::restore_snapshot(state->system_state);
385 MC_restore_communications_pattern(state);
389 /* Restore the initial state */
390 simgrid::mc::session->restoreInitialState();
392 unsigned n = MC_smx_get_maxpid();
393 assert(n == xbt_dynar_length(incomplete_communications_pattern));
394 assert(n == xbt_dynar_length(initial_communications_pattern));
395 for (unsigned j=0; j < n ; j++) {
396 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
397 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
400 /* Traverse the stack from the state at position start and re-execute the transitions */
401 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
402 if (state == stack_.back())
405 int req_num = state->transition.argument;
406 smx_simcall_t saved_req = &state->executed_req;
407 xbt_assert(saved_req);
409 /* because we got a copy of the executed request, we have to fetch the
410 real one, pointed by the request field of the issuer process */
412 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
413 smx_simcall_t req = &issuer->simcall;
415 /* TODO : handle test and testany simcalls */
416 e_mc_call_type_t call = MC_get_call_type(req);
417 mc_model_checker->handle_simcall(state->transition);
418 MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
419 mc_model_checker->wait_for_requests();
421 /* Update statistics */
422 mc_model_checker->visited_states++;
423 mc_model_checker->executed_transitions++;
427 void CommunicationDeterminismChecker::main()
429 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
430 smx_simcall_t req = nullptr;
432 while (not stack_.empty()) {
433 /* Get current state */
434 simgrid::mc::State* state = stack_.back().get();
436 XBT_DEBUG("**************************************************");
437 XBT_DEBUG("Exploration depth = %zu (state = %d, interleaved processes = %zu)", stack_.size(), state->num,
438 state->interleaveSize());
440 /* Update statistics */
441 mc_model_checker->visited_states++;
443 if (stack_.size() <= (std::size_t)_sg_mc_max_depth)
444 req = MC_state_get_request(state);
448 if (req != nullptr && visited_state == nullptr) {
450 int req_num = state->transition.argument;
452 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
455 if (dot_output != nullptr)
456 req_str = simgrid::mc::request_get_dot_output(req, req_num);
458 mc_model_checker->executed_transitions++;
460 /* TODO : handle test and testany simcalls */
461 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
462 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
463 call = MC_get_call_type(req);
465 /* Answer the request */
466 mc_model_checker->handle_simcall(state->transition);
467 /* After this call req is no longer useful */
469 if (not this->initial_communications_pattern_done)
470 MC_handle_comm_pattern(call, req, req_num, initial_communications_pattern, 0);
472 MC_handle_comm_pattern(call, req, req_num, nullptr, 0);
474 /* Wait for requests (schedules processes) */
475 mc_model_checker->wait_for_requests();
477 /* Create the new expanded state */
478 std::unique_ptr<simgrid::mc::State> next_state =
479 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
481 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
482 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
483 * with the initial pattern. */
484 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
486 if (_sg_mc_max_visited_states != 0)
487 visited_state = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), compare_snapshots);
489 visited_state = nullptr;
491 if (visited_state == nullptr) {
493 /* Get enabled actors and insert them in the interleave set of the next state */
494 for (auto& actor : mc_model_checker->process().actors())
495 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
496 next_state->addInterleavingSet(actor.copy.getBuffer());
498 if (dot_output != nullptr)
499 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
500 state->num, next_state->num, req_str.c_str());
502 } else if (dot_output != nullptr)
503 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
504 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
506 stack_.push_back(std::move(next_state));
510 if (stack_.size() > (std::size_t) _sg_mc_max_depth)
511 XBT_WARN("/!\\ Max depth reached! /!\\ ");
512 else if (visited_state != nullptr)
513 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
514 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
516 XBT_DEBUG("There are no more processes to interleave. (depth %zu)", stack_.size());
518 if (not this->initial_communications_pattern_done)
519 this->initial_communications_pattern_done = 1;
521 /* Trash the current state, no longer needed */
522 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size());
525 visited_state = nullptr;
527 /* Check for deadlocks */
528 if (mc_model_checker->checkDeadlock()) {
530 throw simgrid::mc::DeadlockError();
533 while (not stack_.empty()) {
534 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
536 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
537 /* We found a back-tracking point, let's loop */
538 XBT_DEBUG("Back-tracking to state %d at depth %zu", state->num, stack_.size() + 1);
539 stack_.push_back(std::move(state));
541 this->restoreState();
543 XBT_DEBUG("Back-tracking to state %d at depth %zu done", stack_.back()->num, stack_.size());
547 XBT_DEBUG("Delete state %d at depth %zu", state->num, stack_.size() + 1);
553 simgrid::mc::session->logState();
556 void CommunicationDeterminismChecker::run()
558 XBT_INFO("Check communication determinism");
559 simgrid::mc::session->initialize();
566 Checker* createCommunicationDeterminismChecker(Session& session)
568 return new CommunicationDeterminismChecker(session);