1 /* Copyright (c) 2008-2017. 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. */
9 #include <xbt/dynar.hpp>
11 #include <xbt/sysdep.h>
13 #include "src/mc/Transition.hpp"
14 #include "src/mc/VisitedState.hpp"
15 #include "src/mc/checker/CommunicationDeterminismChecker.hpp"
16 #include "src/mc/mc_exit.h"
17 #include "src/mc/mc_private.h"
18 #include "src/mc/mc_record.h"
19 #include "src/mc/mc_request.h"
20 #include "src/mc/mc_smx.h"
21 #include "src/mc/mc_state.h"
22 #include "src/mc/remote/Client.hpp"
24 #include "src/smpi/smpi_request.hpp"
26 using simgrid::mc::remote;
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc, "Logging specific to MC communication determinism detection");
30 /********** Global variables **********/
32 xbt_dynar_t initial_communications_pattern;
33 xbt_dynar_t incomplete_communications_pattern;
35 /********** Static functions ***********/
37 static e_mc_comm_pattern_difference_t compare_comm_pattern(simgrid::mc::PatternCommunication* comm1,
38 simgrid::mc::PatternCommunication* comm2)
40 if(comm1->type != comm2->type)
42 if (comm1->rdv != comm2->rdv)
44 if (comm1->src_proc != comm2->src_proc)
46 if (comm1->dst_proc != comm2->dst_proc)
48 if (comm1->tag != comm2->tag)
50 if (comm1->data.size() != comm2->data.size())
51 return DATA_SIZE_DIFF;
52 if (comm1->data != comm2->data)
57 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process,
58 simgrid::mc::PatternCommunication* comm, unsigned int cursor)
63 if (comm->type == simgrid::mc::PatternCommunicationType::send)
64 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
66 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
70 res = bprintf("%s Different type for communication #%d", type, cursor);
73 res = bprintf("%s Different rdv for communication #%d", type, cursor);
76 res = bprintf("%s Different tag for communication #%d", type, cursor);
79 res = bprintf("%s Different source for communication #%d", type, cursor);
82 res = bprintf("%s Different destination for communication #%d", type, cursor);
85 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
88 res = bprintf("%s\n Different data for communication #%d", type, cursor);
98 static void update_comm_pattern(simgrid::mc::PatternCommunication* comm_pattern,
99 simgrid::mc::RemotePtr<simgrid::kernel::activity::Comm> comm_addr)
101 // HACK, type punning
102 simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_comm;
103 mc_model_checker->process().read(temp_comm, comm_addr);
104 simgrid::kernel::activity::Comm* comm = temp_comm.getBuffer();
106 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_proc));
107 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc));
108 comm_pattern->src_proc = src_proc->pid;
109 comm_pattern->dst_proc = dst_proc->pid;
110 comm_pattern->src_host = MC_smx_actor_get_host_name(src_proc);
111 comm_pattern->dst_host = MC_smx_actor_get_host_name(dst_proc);
112 if (comm_pattern->data.size() == 0 && comm->src_buff != nullptr) {
114 mc_model_checker->process().read(&buff_size, remote(comm->dst_buff_size));
115 comm_pattern->data.resize(buff_size);
116 mc_model_checker->process().read_bytes(comm_pattern->data.data(), comm_pattern->data.size(),
117 remote(comm->src_buff));
124 void CommunicationDeterminismChecker::deterministic_comm_pattern(int process, simgrid::mc::PatternCommunication* comm,
127 simgrid::mc::PatternCommunicationList* list =
128 xbt_dynar_get_as(initial_communications_pattern, process, simgrid::mc::PatternCommunicationList*);
130 if (not backtracking) {
131 e_mc_comm_pattern_difference_t diff = compare_comm_pattern(list->list[list->index_comm].get(), comm);
133 if (diff != NONE_DIFF) {
134 if (comm->type == simgrid::mc::PatternCommunicationType::send) {
135 this->send_deterministic = 0;
136 if (this->send_diff != nullptr)
137 xbt_free(this->send_diff);
138 this->send_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
140 this->recv_deterministic = 0;
141 if (this->recv_diff != nullptr)
142 xbt_free(this->recv_diff);
143 this->recv_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
145 if (_sg_mc_send_determinism && not this->send_deterministic) {
146 XBT_INFO("*********************************************************");
147 XBT_INFO("***** Non-send-deterministic communications pattern *****");
148 XBT_INFO("*********************************************************");
149 XBT_INFO("%s", this->send_diff);
150 xbt_free(this->send_diff);
151 this->send_diff = nullptr;
152 simgrid::mc::session->logState();
153 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
154 } else if (_sg_mc_comms_determinism && (not this->send_deterministic && not this->recv_deterministic)) {
155 XBT_INFO("****************************************************");
156 XBT_INFO("***** Non-deterministic communications pattern *****");
157 XBT_INFO("****************************************************");
158 XBT_INFO("%s", this->send_diff);
159 XBT_INFO("%s", this->recv_diff);
160 xbt_free(this->send_diff);
161 this->send_diff = nullptr;
162 xbt_free(this->recv_diff);
163 this->recv_diff = nullptr;
164 simgrid::mc::session->logState();
165 mc_model_checker->exit(SIMGRID_MC_EXIT_NON_DETERMINISM);
171 /********** Non Static functions ***********/
173 void CommunicationDeterminismChecker::get_comm_pattern(xbt_dynar_t list, smx_simcall_t request,
174 e_mc_call_type_t call_type, int backtracking)
176 const smx_actor_t issuer = MC_smx_simcall_get_issuer(request);
177 simgrid::mc::PatternCommunicationList* initial_pattern =
178 xbt_dynar_get_as(initial_communications_pattern, issuer->pid, simgrid::mc::PatternCommunicationList*);
179 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
181 std::unique_ptr<simgrid::mc::PatternCommunication> pattern =
182 std::unique_ptr<simgrid::mc::PatternCommunication>(new simgrid::mc::PatternCommunication());
183 pattern->index = initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
185 if (call_type == MC_CALL_TYPE_SEND) {
186 /* Create comm pattern */
187 pattern->type = simgrid::mc::PatternCommunicationType::send;
188 pattern->comm_addr = simcall_comm_isend__get__result(request);
190 simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_synchro;
191 mc_model_checker->process().read(temp_synchro,
192 remote(static_cast<simgrid::kernel::activity::Comm*>(pattern->comm_addr)));
193 simgrid::kernel::activity::Comm* synchro = static_cast<simgrid::kernel::activity::Comm*>(temp_synchro.getBuffer());
195 char* remote_name = mc_model_checker->process().read<char*>(
196 (std::uint64_t)(synchro->mbox ? &synchro->mbox->name_ : &synchro->mbox_cpy->name_));
197 pattern->rdv = mc_model_checker->process().read_string(remote_name);
198 pattern->src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(synchro->src_proc))->pid;
199 pattern->src_host = MC_smx_actor_get_host_name(issuer);
201 simgrid::smpi::Request mpi_request =
202 mc_model_checker->process().read<simgrid::smpi::Request>((std::uint64_t)simcall_comm_isend__get__data(request));
203 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));
209 if(mpi_request.detached()){
210 if (not this->initial_communications_pattern_done) {
211 /* Store comm pattern */
212 simgrid::mc::PatternCommunicationList* list =
213 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*);
214 list->list.push_back(std::move(pattern));
216 /* Evaluate comm determinism */
217 this->deterministic_comm_pattern(pattern->src_proc, pattern.get(), backtracking);
218 xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, simgrid::mc::PatternCommunicationList*)
223 } else if (call_type == MC_CALL_TYPE_RECV) {
224 pattern->type = simgrid::mc::PatternCommunicationType::receive;
225 pattern->comm_addr = simcall_comm_irecv__get__result(request);
227 simgrid::smpi::Request mpi_request;
228 mc_model_checker->process().read(&mpi_request,
229 remote((simgrid::smpi::Request*)simcall_comm_irecv__get__data(request)));
230 pattern->tag = mpi_request.tag();
232 simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_comm;
233 mc_model_checker->process().read(temp_comm,
234 remote(static_cast<simgrid::kernel::activity::Comm*>(pattern->comm_addr)));
235 simgrid::kernel::activity::Comm* comm = temp_comm.getBuffer();
238 mc_model_checker->process().read(&remote_name, remote(comm->mbox ? &comm->mbox->name_ : &comm->mbox_cpy->name_));
239 pattern->rdv = mc_model_checker->process().read_string(remote_name);
240 pattern->dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc))->pid;
241 pattern->dst_host = MC_smx_actor_get_host_name(issuer);
243 xbt_die("Unexpected call_type %i", (int) call_type);
245 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern.get(), issuer->pid);
246 xbt_dynar_t dynar = xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
247 simgrid::mc::PatternCommunication* pattern2 = pattern.release();
248 xbt_dynar_push(dynar, &pattern2);
251 void CommunicationDeterminismChecker::complete_comm_pattern(
252 xbt_dynar_t list, simgrid::mc::RemotePtr<simgrid::kernel::activity::Comm> comm_addr, unsigned int issuer,
255 simgrid::mc::PatternCommunication* current_comm_pattern;
256 unsigned int cursor = 0;
257 std::unique_ptr<simgrid::mc::PatternCommunication> comm_pattern;
260 /* Complete comm pattern */
261 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern)
262 if (remote(current_comm_pattern->comm_addr) == comm_addr) {
263 update_comm_pattern(current_comm_pattern, comm_addr);
265 simgrid::mc::PatternCommunication* temp;
266 xbt_dynar_remove_at(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &temp);
267 comm_pattern = std::unique_ptr<simgrid::mc::PatternCommunication>(temp);
268 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
273 xbt_die("Corresponding communication not found!");
275 simgrid::mc::PatternCommunicationList* pattern =
276 xbt_dynar_get_as(initial_communications_pattern, issuer, simgrid::mc::PatternCommunicationList*);
278 if (not this->initial_communications_pattern_done)
279 /* Store comm pattern */
280 pattern->list.push_back(std::move(comm_pattern));
282 /* Evaluate comm determinism */
283 this->deterministic_comm_pattern(issuer, comm_pattern.get(), backtracking);
284 pattern->index_comm++;
288 CommunicationDeterminismChecker::CommunicationDeterminismChecker(Session& session) : Checker(session)
292 CommunicationDeterminismChecker::~CommunicationDeterminismChecker() = default;
294 RecordTrace CommunicationDeterminismChecker::getRecordTrace() // override
297 for (auto const& state : stack_)
298 res.push_back(state->getTransition());
302 std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // override
304 std::vector<std::string> trace;
305 for (auto const& state : stack_) {
306 smx_simcall_t req = &state->executed_req;
309 simgrid::mc::request_to_string(req, state->transition.argument, simgrid::mc::RequestType::executed));
314 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->interleave(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 // int n = xbt_dynar_length(incomplete_communications_pattern);
393 unsigned n = MC_smx_get_maxpid();
394 assert(n == xbt_dynar_length(incomplete_communications_pattern));
395 assert(n == xbt_dynar_length(initial_communications_pattern));
396 for (unsigned j=0; j < n ; j++) {
397 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
398 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
401 /* Traverse the stack from the state at position start and re-execute the transitions */
402 for (std::unique_ptr<simgrid::mc::State> const& state : stack_) {
403 if (state == stack_.back())
406 int req_num = state->transition.argument;
407 smx_simcall_t saved_req = &state->executed_req;
408 xbt_assert(saved_req);
410 /* because we got a copy of the executed request, we have to fetch the
411 real one, pointed by the request field of the issuer process */
413 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
414 smx_simcall_t req = &issuer->simcall;
416 /* TODO : handle test and testany simcalls */
417 e_mc_call_type_t call = MC_get_call_type(req);
418 mc_model_checker->handle_simcall(state->transition);
419 MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
420 mc_model_checker->wait_for_requests();
422 /* Update statistics */
423 mc_model_checker->visited_states++;
424 mc_model_checker->executed_transitions++;
428 void CommunicationDeterminismChecker::main()
430 std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
431 smx_simcall_t req = nullptr;
433 while (not stack_.empty()) {
434 /* Get current state */
435 simgrid::mc::State* state = stack_.back().get();
437 XBT_DEBUG("**************************************************");
438 XBT_DEBUG("Exploration depth = %zi (state = %d, interleaved processes = %zd)", stack_.size(), state->num,
439 state->interleaveSize());
441 /* Update statistics */
442 mc_model_checker->visited_states++;
444 if (stack_.size() <= (std::size_t)_sg_mc_max_depth && (req = MC_state_get_request(state)) != nullptr &&
445 (visited_state == nullptr)) {
447 int req_num = state->transition.argument;
449 XBT_DEBUG("Execute: %s", simgrid::mc::request_to_string(req, req_num, simgrid::mc::RequestType::simix).c_str());
452 if (dot_output != nullptr)
453 req_str = simgrid::mc::request_get_dot_output(req, req_num);
455 mc_model_checker->executed_transitions++;
457 /* TODO : handle test and testany simcalls */
458 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
459 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
460 call = MC_get_call_type(req);
462 /* Answer the request */
463 mc_model_checker->handle_simcall(state->transition);
464 /* After this call req is no longer useful */
466 if (not this->initial_communications_pattern_done)
467 MC_handle_comm_pattern(call, req, req_num, initial_communications_pattern, 0);
469 MC_handle_comm_pattern(call, req, req_num, nullptr, 0);
471 /* Wait for requests (schedules processes) */
472 mc_model_checker->wait_for_requests();
474 /* Create the new expanded state */
475 std::unique_ptr<simgrid::mc::State> next_state =
476 std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
478 /* If comm determinism verification, we cannot stop the exploration if some communications are not finished (at
479 * least, data are transferred). These communications are incomplete and they cannot be analyzed and compared
480 * with the initial pattern. */
481 bool compare_snapshots = all_communications_are_finished() && this->initial_communications_pattern_done;
483 if (_sg_mc_max_visited_states == 0 ||
484 (visited_state = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), compare_snapshots)) ==
487 /* Get enabled actors and insert them in the interleave set of the next state */
488 for (auto& actor : mc_model_checker->process().actors())
489 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
490 next_state->interleave(actor.copy.getBuffer());
492 if (dot_output != nullptr)
493 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
494 state->num, next_state->num, req_str.c_str());
496 } else if (dot_output != nullptr)
497 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
498 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num, req_str.c_str());
500 stack_.push_back(std::move(next_state));
504 if (stack_.size() > (std::size_t) _sg_mc_max_depth)
505 XBT_WARN("/!\\ Max depth reached! /!\\ ");
506 else if (visited_state != nullptr)
507 XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.",
508 visited_state->original_num == -1 ? visited_state->num : visited_state->original_num);
510 XBT_DEBUG("There are no more processes to interleave. (depth %zi)", stack_.size());
512 if (not this->initial_communications_pattern_done)
513 this->initial_communications_pattern_done = 1;
515 /* Trash the current state, no longer needed */
516 XBT_DEBUG("Delete state %d at depth %zi", state->num, stack_.size());
519 visited_state = nullptr;
521 /* Check for deadlocks */
522 if (mc_model_checker->checkDeadlock()) {
524 throw new simgrid::mc::DeadlockError();
527 while (not stack_.empty()) {
528 std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
530 if (state->interleaveSize() && stack_.size() < (std::size_t)_sg_mc_max_depth) {
531 /* We found a back-tracking point, let's loop */
532 XBT_DEBUG("Back-tracking to state %d at depth %zi", state->num, stack_.size() + 1);
533 stack_.push_back(std::move(state));
535 this->restoreState();
537 XBT_DEBUG("Back-tracking to state %d at depth %zi done", stack_.back()->num, stack_.size());
541 XBT_DEBUG("Delete state %d at depth %zi", state->num, stack_.size() + 1);
547 simgrid::mc::session->logState();
550 void CommunicationDeterminismChecker::run()
552 XBT_INFO("Check communication determinism");
553 simgrid::mc::session->initialize();
560 Checker* createCommunicationDeterminismChecker(Session& session)
562 return new CommunicationDeterminismChecker(session);