3 #include "src/kernel/activity/MailboxImpl.hpp"
4 #include "src/kernel/activity/MutexImpl.hpp"
5 #include "src/mc/Session.hpp"
6 #include "src/mc/checker/SimcallInspector.hpp"
7 #include "src/mc/mc_comm_pattern.hpp"
8 #include "src/mc/mc_pattern.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_smx.hpp"
12 #include "src/mc/remote/RemoteSimulation.hpp"
13 #include <xbt/asserts.h>
15 #include "simgrid/s4u/Host.hpp"
16 #include "xbt/string.hpp"
18 #include "src/smpi/include/smpi_request.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
23 using Simcall = simgrid::simix::Simcall;
28 static inline const char* get_color(int id)
30 static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
31 "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
33 return colors[id % colors.size()];
36 static char* pointer_to_string(void* pointer)
38 if (XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose))
39 return bprintf("%p", pointer);
41 return xbt_strdup("(verbose only)");
44 static char* buff_size_to_string(size_t buff_size)
46 if (XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose))
47 return bprintf("%zu", buff_size);
49 return xbt_strdup("(verbose only)");
52 /* Search an enabled transition for the given process.
54 * This can be seen as an iterator returning the next transition of the process.
56 * We only consider the processes that are both
57 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
58 * - which simcall can currently be executed (like a comm where the other partner is already known)
59 * Once we returned the last enabled transition of a process, it is marked done.
61 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
62 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
64 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
66 /* reset the outgoing transition */
67 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
68 state->transition_.pid_ = -1;
69 state->transition_.argument_ = -1;
70 state->executed_req_.call_ = Simcall::NONE;
72 if (not simgrid::mc::actor_is_enabled(actor))
73 return nullptr; // Not executable in the application
75 smx_simcall_t req = nullptr;
76 switch (actor->simcall_.call_) {
77 case Simcall::COMM_WAITANY:
78 state->transition_.argument_ = -1;
79 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
80 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
81 state->transition_.argument_ = procstate->times_considered;
82 ++procstate->times_considered;
85 ++procstate->times_considered;
88 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
89 procstate->set_done();
90 if (state->transition_.argument_ != -1)
91 req = &actor->simcall_;
94 case Simcall::COMM_TESTANY: {
95 unsigned start_count = procstate->times_considered;
96 state->transition_.argument_ = -1;
97 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
98 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
99 state->transition_.argument_ = procstate->times_considered;
100 ++procstate->times_considered;
103 ++procstate->times_considered;
106 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
107 procstate->set_done();
109 if (state->transition_.argument_ != -1 || start_count == 0)
110 req = &actor->simcall_;
115 case Simcall::COMM_WAIT: {
116 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
117 remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
118 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
119 mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
120 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
121 if (act->src_actor_.get() && act->dst_actor_.get())
122 state->transition_.argument_ = 0; // OK
123 else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
125 state->transition_.argument_ = 0; // OK
127 state->transition_.argument_ = -1; // timeout
128 procstate->set_done();
129 req = &actor->simcall_;
133 case Simcall::MC_RANDOM: {
134 int min_value = simcall_mc_random__get__min(&actor->simcall_);
135 state->transition_.argument_ = procstate->times_considered + min_value;
136 procstate->times_considered++;
137 if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
138 procstate->set_done();
139 req = &actor->simcall_;
144 procstate->set_done();
145 state->transition_.argument_ = 0;
146 req = &actor->simcall_;
152 state->transition_.pid_ = actor->get_pid();
153 state->executed_req_ = *req;
154 // Fetch the data of the request and translate it:
155 state->internal_req_ = *req;
157 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
158 * action so it can be treated later by the dependence function. */
159 switch (req->call_) {
160 case Simcall::COMM_WAITANY: {
161 state->internal_req_.call_ = Simcall::COMM_WAIT;
162 simgrid::kernel::activity::CommImpl* remote_comm;
163 remote_comm = mc_model_checker->get_remote_simulation().read(
164 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
165 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
166 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
167 simcall_comm_wait__set__timeout(&state->internal_req_, 0);
171 case Simcall::COMM_TESTANY:
172 state->internal_req_.call_ = Simcall::COMM_TEST;
174 if (state->transition_.argument_ > 0) {
175 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
176 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
177 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
180 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
181 simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
184 case Simcall::COMM_WAIT:
185 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
186 remote(simcall_comm_wait__getraw__comm(req)));
187 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
188 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
191 case Simcall::COMM_TEST:
192 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
193 remote(simcall_comm_test__getraw__comm(req)));
194 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
195 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
199 /* No translation needed */
206 simgrid::kernel::activity::CommImpl* Api::get_comm(smx_simcall_t const r) const
209 case Simcall::COMM_WAIT:
210 return simcall_comm_wait__getraw__comm(r);
211 case Simcall::COMM_TEST:
212 return simcall_comm_test__getraw__comm(r);
218 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
220 * This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
221 * sense that we could achieve the same thing by having ActorInformation
222 * inherit from s_smx_actor_t but we don't really want to do that.
224 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
226 simgrid::mc::ActorInformation temp;
227 std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
229 auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
234 bool Api::request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2) const
236 if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
239 if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
242 // Those are internal requests, we do not need indirection because those objects are copies:
243 const kernel::activity::CommImpl* synchro1 = get_comm(r1);
244 const kernel::activity::CommImpl* synchro2 = get_comm(r2);
246 if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
247 auto mbox = get_mbox_remote_addr(r1);
248 RemotePtr<kernel::activity::MailboxImpl> synchro2_mbox_cpy = remote(synchro2->mbox_cpy);
250 if (mbox != synchro2_mbox_cpy && simcall_comm_wait__get__timeout(r2) <= 0)
253 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
254 simcall_comm_wait__get__timeout(r2) <= 0)
257 if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
258 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
261 if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
262 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
266 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
269 if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
270 && r2->call == Simcall::COMM_TEST)
274 if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
275 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
278 if (r1->call_ == Simcall::COMM_TEST &&
279 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
282 if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
283 synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
286 if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
287 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
288 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
289 synchro2->dst_buff_ != synchro1->src_buff_)
295 const char* Api::actor_get_host_name(smx_actor_t actor) const
297 if (mc_model_checker == nullptr)
298 return actor->get_host()->get_cname();
300 const simgrid::mc::RemoteSimulation* process = &mc_model_checker->get_remote_simulation();
302 // Read the simgrid::xbt::string in the MCed process:
303 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
304 auto remote_string_address =
305 remote(reinterpret_cast<const simgrid::xbt::string_data*>(&actor->get_host()->get_name()));
306 simgrid::xbt::string_data remote_string = process->read(remote_string_address);
307 std::vector<char> hostname(remote_string.len + 1);
308 // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
309 process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
310 info->hostname = mc_model_checker->get_host_name(hostname.data()).c_str();
311 return info->hostname;
314 const char* Api::actor_get_name(smx_actor_t actor) const
316 if (mc_model_checker == nullptr)
317 return actor->get_cname();
319 const simgrid::mc::RemoteSimulation* process = &mc_model_checker->get_remote_simulation();
321 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
322 if (info->name.empty()) {
323 simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
324 info->name = process->read_string(remote(string_data.data), string_data.len);
326 return info->name.c_str();
329 void Api::initialize(char** argv) const
331 simgrid::mc::session = new simgrid::mc::Session([argv] {
333 while (argv[i] != nullptr && argv[i][0] == '-')
335 xbt_assert(argv[i] != nullptr,
336 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
337 execvp(argv[i], argv + i);
338 xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
342 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
344 return mc_model_checker->get_remote_simulation().actors();
347 bool Api::actor_is_enabled(aid_t pid) const
349 return session->actor_is_enabled(pid);
352 unsigned long Api::get_maxpid() const
354 return MC_smx_get_maxpid();
357 int Api::get_actors_size() const
359 return mc_model_checker->get_remote_simulation().actors().size();
362 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
364 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
365 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
368 RemotePtr<kernel::activity::CommImpl> Api::get_comm_irecv_raw_addr(smx_simcall_t request) const
370 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
371 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
374 RemotePtr<kernel::activity::CommImpl> Api::get_comm_wait_raw_addr(smx_simcall_t request) const
376 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl*>(request->args_[0]);
377 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
380 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
382 auto addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl**>(request->args_[0]) + value;
383 auto comm_addr = mc_model_checker->get_remote_simulation().read(remote(addr));
384 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
387 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
389 Remote<kernel::activity::CommImpl> temp_synchro;
390 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
391 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
393 char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
394 (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
395 auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
399 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
401 Remote<kernel::activity::CommImpl> temp_synchro;
402 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
403 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
405 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
409 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
411 Remote<kernel::activity::CommImpl> temp_synchro;
412 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
413 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
415 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
419 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
421 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
422 mc_model_checker->get_remote_simulation().read(temp_comm, addr);
423 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
425 std::vector<char> buffer{};
426 if (comm->src_buff_ != nullptr) {
427 buffer.resize(comm->src_buff_size_);
428 mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
433 const char* Api::get_actor_host_name(smx_actor_t actor) const
435 const char* host_name = actor_get_host_name(actor);
440 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
442 simgrid::smpi::Request mpi_request;
443 mc_model_checker->get_remote_simulation().read(
444 &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
445 return mpi_request.detached();
449 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
451 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
452 mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
453 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
455 auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
459 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
461 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
462 mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
463 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
465 auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
469 std::size_t Api::get_remote_heap_bytes() const
471 RemoteSimulation& process = mc_model_checker->get_remote_simulation();
472 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
473 return heap_bytes_used;
476 void Api::session_initialize() const
478 session->initialize();
481 void Api::mc_inc_visited_states() const
483 mc_model_checker->visited_states++;
486 void Api::mc_inc_executed_trans() const
488 mc_model_checker->executed_transitions++;
491 unsigned long Api::mc_get_visited_states() const
493 return mc_model_checker->visited_states;
496 unsigned long Api::mc_get_executed_trans() const
498 return mc_model_checker->executed_transitions;
501 bool Api::mc_check_deadlock() const
503 return mc_model_checker->checkDeadlock();
506 void Api::mc_show_deadlock() const
511 /** Get the issuer of a simcall (`req->issuer`)
513 * In split-process mode, it does the black magic necessary to get an address
514 * of a (shallow) copy of the data structure the issuer SIMIX actor in the local
517 * @param process the MCed process
518 * @param req the simcall (copied in the local process)
520 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
522 xbt_assert(mc_model_checker != nullptr);
524 // This is the address of the smx_actor in the MCed process:
525 auto address = simgrid::mc::remote(req->issuer_);
527 // Lookup by address:
528 for (auto& actor : mc_model_checker->get_remote_simulation().actors())
529 if (actor.address == address)
530 return actor.copy.get_buffer();
531 for (auto& actor : mc_model_checker->get_remote_simulation().dead_actors())
532 if (actor.address == address)
533 return actor.copy.get_buffer();
535 xbt_die("Issuer not found");
538 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
540 return simcall_get_issuer(req)->get_pid();
543 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
545 RemotePtr<kernel::activity::MailboxImpl> mbox_addr;
546 switch (req->call_) {
547 case Simcall::COMM_ISEND:
548 case Simcall::COMM_IRECV: {
549 auto mbox_addr_ptr = simix::unmarshal<smx_mailbox_t>(req->args_[1]);
550 mbox_addr = remote(mbox_addr_ptr);
554 mbox_addr = RemotePtr<kernel::activity::MailboxImpl>();
560 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
562 RemotePtr<kernel::activity::ActivityImpl> comm_addr;
563 switch (req->call_) {
564 case Simcall::COMM_ISEND:
565 case Simcall::COMM_IRECV: {
566 auto comm_addr_ptr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(req->result_);
567 comm_addr = remote(comm_addr_ptr);
571 comm_addr = RemotePtr<kernel::activity::ActivityImpl>();
577 bool Api::mc_is_null() const
579 auto is_null = (mc_model_checker == nullptr) ? true : false;
583 Checker* Api::mc_get_checker() const
585 return mc_model_checker->getChecker();
588 void Api::set_checker(Checker* const checker) const
590 xbt_assert(mc_model_checker);
591 xbt_assert(mc_model_checker->getChecker() == nullptr);
592 mc_model_checker->setChecker(checker);
595 void Api::handle_simcall(Transition const& transition) const
597 mc_model_checker->handle_simcall(transition);
600 void Api::mc_wait_for_requests() const
602 mc_model_checker->wait_for_requests();
605 void Api::mc_exit(int status) const
607 mc_model_checker->exit(status);
610 std::string const& Api::mc_get_host_name(std::string const& hostname) const
612 return mc_model_checker->get_host_name(hostname);
615 void Api::dump_record_path() const
617 simgrid::mc::dumpRecordPath();
620 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
622 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
623 /* Only consider the actors that were marked as interleaving by the checker algorithm */
624 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
627 smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
634 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state)
636 std::list<transition_detail_t> tr_list{};
638 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
639 auto actor_pid = actor.copy.get_buffer()->get_pid();
640 auto actor_impl = actor.copy.get_buffer();
642 // Only consider the actors that were marked as interleaving by the checker algorithm
643 if (not state->actor_states_[actor_pid].is_todo())
645 // Not executable in the application
646 if (not simgrid::mc::actor_is_enabled(actor_impl))
649 transition_detail_t transition = std::unique_ptr<s_transition_detail>(new s_transition_detail());
650 Simcall simcall_call = actor_impl->simcall_.call_;
651 smx_simcall_t simcall = &actor_impl->simcall_;
652 transition->call_ = simcall_call;
653 switch (simcall_call) {
654 case Simcall::COMM_ISEND:
655 case Simcall::COMM_IRECV: {
656 transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
657 transition->comm_remote_addr = get_comm_remote_addr(simcall);
664 tr_list.emplace_back(std::move(transition));
670 bool Api::simcall_check_dependency(smx_simcall_t const req1, smx_simcall_t const req2) const
672 if (req1->issuer_ == req2->issuer_)
675 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent
676 * with all other transitions */
677 if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
678 (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
681 if (req1->call_ != req2->call_)
682 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
684 // Those are internal requests, we do not need indirection because those objects are copies:
685 const kernel::activity::CommImpl* synchro1 = get_comm(req1);
686 const kernel::activity::CommImpl* synchro2 = get_comm(req2);
688 switch (req1->call_) {
689 case Simcall::COMM_ISEND:
690 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
691 case Simcall::COMM_IRECV:
692 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
693 case Simcall::COMM_WAIT:
694 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
696 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
697 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
698 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
706 std::string Api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
708 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
710 if (req->inspector_ != nullptr)
711 return req->inspector_->to_string();
713 bool use_remote_comm = true;
714 switch (request_type) {
715 case simgrid::mc::RequestType::simix:
716 use_remote_comm = true;
718 case simgrid::mc::RequestType::executed:
719 case simgrid::mc::RequestType::internal:
720 use_remote_comm = false;
726 const char* type = nullptr;
727 char* args = nullptr;
729 smx_actor_t issuer = simcall_get_issuer(req);
731 switch (req->call_) {
732 case Simcall::COMM_ISEND: {
734 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
735 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
736 if (issuer->get_host())
737 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), actor_get_host_name(issuer),
738 actor_get_name(issuer), p, bs);
740 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), actor_get_name(issuer), p, bs);
746 case Simcall::COMM_IRECV: {
747 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
750 mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
753 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
754 char* bs = buff_size_to_string(size);
755 if (issuer->get_host())
756 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), actor_get_host_name(issuer),
757 actor_get_name(issuer), p, bs);
759 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), actor_get_name(issuer), p, bs);
765 case Simcall::COMM_WAIT: {
766 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
769 type = "WaitTimeout";
770 p = pointer_to_string(remote_act);
771 args = bprintf("comm=%s", p);
774 p = pointer_to_string(remote_act);
776 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
777 const simgrid::kernel::activity::CommImpl* act;
778 if (use_remote_comm) {
779 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
780 act = temp_synchro.get_buffer();
784 smx_actor_t src_proc =
785 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
786 smx_actor_t dst_proc =
787 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
788 args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
789 src_proc ? actor_get_host_name(src_proc) : "",
790 src_proc ? actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
791 dst_proc ? actor_get_host_name(dst_proc) : "",
792 dst_proc ? actor_get_name(dst_proc) : "");
798 case Simcall::COMM_TEST: {
799 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
800 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
801 const simgrid::kernel::activity::CommImpl* act;
802 if (use_remote_comm) {
803 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
804 act = temp_synchro.get_buffer();
809 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
811 p = pointer_to_string(remote_act);
812 args = bprintf("comm=%s", p);
815 p = pointer_to_string(remote_act);
817 smx_actor_t src_proc =
818 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
819 smx_actor_t dst_proc =
820 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
821 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
822 actor_get_name(src_proc), actor_get_host_name(src_proc), dst_proc->get_pid(),
823 actor_get_name(dst_proc), actor_get_host_name(dst_proc));
829 case Simcall::COMM_WAITANY: {
831 size_t count = simcall_comm_waitany__get__count(req);
833 simgrid::kernel::activity::CommImpl* remote_sync;
835 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
836 char* p = pointer_to_string(remote_sync);
837 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
840 args = bprintf("comm at idx %d", value);
844 case Simcall::COMM_TESTANY:
846 type = "TestAny FALSE";
847 args = xbt_strdup("-");
850 args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
854 case Simcall::MUTEX_TRYLOCK:
855 case Simcall::MUTEX_LOCK: {
856 if (req->call_ == Simcall::MUTEX_LOCK)
859 type = "Mutex TRYLOCK";
861 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
862 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
863 remote(req->call_ == Simcall::MUTEX_LOCK
864 ? simcall_mutex_lock__get__mutex(req)
865 : simcall_mutex_trylock__get__mutex(req)));
866 args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
867 mutex.get_buffer()->get_owner() != nullptr
868 ? (int)mc_model_checker->get_remote_simulation()
869 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
875 case Simcall::MC_RANDOM:
877 args = bprintf("%d", value);
881 type = SIMIX_simcall_name(req->call_);
882 args = bprintf("??");
888 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), actor_get_host_name(issuer),
889 actor_get_name(issuer), type, args);
891 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), actor_get_host_name(issuer),
892 actor_get_name(issuer), type);
897 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
899 const smx_actor_t issuer = simcall_get_issuer(req);
900 const char* color = get_color(issuer->get_pid() - 1);
902 if (req->inspector_ != nullptr)
903 return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
904 req->inspector_->dot_label().c_str(), color, color);
908 switch (req->call_) {
909 case Simcall::COMM_ISEND:
910 if (issuer->get_host())
911 label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), actor_get_host_name(issuer));
913 label = bprintf("[(%ld)] iSend", issuer->get_pid());
916 case Simcall::COMM_IRECV:
917 if (issuer->get_host())
918 label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), actor_get_host_name(issuer));
920 label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
923 case Simcall::COMM_WAIT:
925 if (issuer->get_host())
926 label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), actor_get_host_name(issuer));
928 label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
930 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
931 Remote<kernel::activity::CommImpl> temp_comm;
932 mc_model_checker->get_remote_simulation().read(temp_comm,
933 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
934 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
936 const kernel::actor::ActorImpl* src_proc =
937 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
938 const kernel::actor::ActorImpl* dst_proc =
939 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
940 if (issuer->get_host())
942 xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), actor_get_host_name(issuer),
943 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
945 label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
946 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
950 case Simcall::COMM_TEST: {
951 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
952 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
953 mc_model_checker->get_remote_simulation().read(temp_comm,
954 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
955 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
956 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
957 if (issuer->get_host())
958 label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), actor_get_host_name(issuer));
960 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
962 if (issuer->get_host())
963 label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), actor_get_host_name(issuer));
965 label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
970 case Simcall::COMM_WAITANY: {
971 size_t comms_size = simcall_comm_waitany__get__count(req);
972 if (issuer->get_host())
973 label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
974 actor_get_host_name(issuer), value + 1, comms_size);
976 label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
980 case Simcall::COMM_TESTANY:
982 if (issuer->get_host())
983 label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), actor_get_host_name(issuer));
985 label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
987 if (issuer->get_host())
989 xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
990 actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
992 label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
993 simcall_comm_testany__get__count(req));
997 case Simcall::MUTEX_TRYLOCK:
998 label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
1001 case Simcall::MUTEX_LOCK:
1002 label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
1005 case Simcall::MC_RANDOM:
1006 if (issuer->get_host())
1007 label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), actor_get_host_name(issuer),
1010 label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
1014 THROW_UNIMPLEMENTED;
1017 return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
1020 const char* Api::simcall_get_name(simgrid::simix::Simcall kind) const
1022 return simcall_names[static_cast<int>(kind)];
1026 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
1028 simgrid::smpi::Request mpi_request;
1029 void* simcall_data = nullptr;
1030 if (type == Simcall::COMM_ISEND)
1031 simcall_data = simcall_comm_isend__get__data(simcall);
1032 else if (type == Simcall::COMM_IRECV)
1033 simcall_data = simcall_comm_irecv__get__data(simcall);
1034 mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
1035 return mpi_request.tag();
1039 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
1041 system_state->restore(&mc_model_checker->get_remote_simulation());
1044 void Api::log_state() const
1046 session->log_state();
1049 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
1051 return simgrid::mc::snapshot_equal(s1, s2);
1054 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
1056 auto snapshot = new simgrid::mc::Snapshot(num_state);
1060 void Api::s_close() const
1065 void Api::restore_initial_state() const
1067 session->restore_initial_state();
1070 void Api::execute(Transition const& transition) const
1072 session->execute(transition);
1076 void Api::automaton_load(const char* file) const
1078 MC_automaton_load(file);
1082 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
1084 unsigned int cursor = 0;
1085 std::vector<int> values;
1086 xbt_automaton_propositional_symbol_t ps = nullptr;
1087 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
1088 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
1092 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
1094 std::vector<xbt_automaton_state_t> automaton_stack;
1095 unsigned int cursor = 0;
1096 xbt_automaton_state_t automaton_state;
1097 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
1098 if (automaton_state->type == -1)
1099 automaton_stack.push_back(automaton_state);
1100 return automaton_stack;
1103 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
1105 unsigned int cursor = 0;
1106 xbt_automaton_propositional_symbol_t p = nullptr;
1107 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
1108 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
1114 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1116 mc::property_automaton->current_state = automaton_state;
1119 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1121 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1122 return transition->label;
1125 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1127 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1128 return transition->dst;
1132 } // namespace simgrid