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 std::string pointer_to_string(void* pointer)
38 return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
41 static std::string buff_size_to_string(size_t buff_size)
43 return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
46 /* Search an enabled transition for the given process.
48 * This can be seen as an iterator returning the next transition of the process.
50 * We only consider the processes that are both
51 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
52 * - which simcall can currently be executed (like a comm where the other partner is already known)
53 * Once we returned the last enabled transition of a process, it is marked done.
55 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
56 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
58 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
60 /* reset the outgoing transition */
61 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
62 state->transition_.pid_ = -1;
63 state->transition_.times_considered_ = -1;
64 state->transition_.textual[0] = '\0';
65 state->executed_req_.call_ = Simcall::NONE;
67 if (not simgrid::mc::actor_is_enabled(actor))
68 return nullptr; // Not executable in the application
70 smx_simcall_t req = nullptr;
71 if (actor->simcall_.inspector_ != nullptr) {
72 state->transition_.times_considered_ = procstate->times_considered;
73 procstate->times_considered++;
74 if (actor->simcall_.mc_max_consider_ <= procstate->times_considered)
75 procstate->set_done();
76 req = &actor->simcall_;
78 switch (actor->simcall_.call_) {
79 case Simcall::COMM_WAITANY:
80 state->transition_.times_considered_ = -1;
81 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
82 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
83 state->transition_.times_considered_ = procstate->times_considered;
84 ++procstate->times_considered;
87 ++procstate->times_considered;
90 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
91 procstate->set_done();
92 if (state->transition_.times_considered_ != -1)
93 req = &actor->simcall_;
96 case Simcall::COMM_TESTANY: {
97 unsigned start_count = procstate->times_considered;
98 state->transition_.times_considered_ = -1;
99 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
100 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
101 state->transition_.times_considered_ = procstate->times_considered;
102 ++procstate->times_considered;
105 ++procstate->times_considered;
108 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
109 procstate->set_done();
111 if (state->transition_.times_considered_ != -1 || start_count == 0)
112 req = &actor->simcall_;
117 case Simcall::COMM_WAIT: {
118 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
119 remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
120 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
121 mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
122 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
123 if (act->src_actor_.get() && act->dst_actor_.get())
124 state->transition_.times_considered_ = 0; // OK
125 else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
127 state->transition_.times_considered_ = 0; // OK
129 state->transition_.times_considered_ = -1; // timeout
130 procstate->set_done();
131 req = &actor->simcall_;
136 procstate->set_done();
137 state->transition_.times_considered_ = 0;
138 req = &actor->simcall_;
144 state->transition_.pid_ = actor->get_pid();
145 state->executed_req_ = *req;
146 // Fetch the data of the request and translate it:
147 state->internal_req_ = *req;
149 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
150 * action so it can be treated later by the dependence function. */
151 switch (req->call_) {
152 case Simcall::COMM_WAITANY: {
153 state->internal_req_.call_ = Simcall::COMM_WAIT;
154 simgrid::kernel::activity::CommImpl* remote_comm;
155 remote_comm = mc_model_checker->get_remote_simulation().read(
156 remote(simcall_comm_waitany__get__comms(req) + state->transition_.times_considered_));
157 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
158 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
159 simcall_comm_wait__set__timeout(&state->internal_req_, 0);
163 case Simcall::COMM_TESTANY:
164 state->internal_req_.call_ = Simcall::COMM_TEST;
166 if (state->transition_.times_considered_ > 0) {
167 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
168 remote(simcall_comm_testany__get__comms(req) + state->transition_.times_considered_));
169 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
172 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
173 simcall_comm_test__set__result(&state->internal_req_, state->transition_.times_considered_);
176 case Simcall::COMM_WAIT:
177 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
178 remote(simcall_comm_wait__getraw__comm(req)));
179 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
180 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
183 case Simcall::COMM_TEST:
184 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
185 remote(simcall_comm_test__getraw__comm(req)));
186 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
187 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
191 /* No translation needed */
198 simgrid::kernel::activity::CommImpl* Api::get_comm(smx_simcall_t const r) const
201 case Simcall::COMM_WAIT:
202 return simcall_comm_wait__getraw__comm(r);
203 case Simcall::COMM_TEST:
204 return simcall_comm_test__getraw__comm(r);
210 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
212 * This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
213 * sense that we could achieve the same thing by having ActorInformation
214 * inherit from s_smx_actor_t but we don't really want to do that.
216 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
218 simgrid::mc::ActorInformation temp;
219 std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
221 auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
226 bool Api::request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2) const
228 if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
231 if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
234 // Those are internal requests, we do not need indirection because those objects are copies:
235 const kernel::activity::CommImpl* synchro1 = get_comm(r1);
236 const kernel::activity::CommImpl* synchro2 = get_comm(r2);
238 if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
239 auto mbox = get_mbox_remote_addr(r1);
240 RemotePtr<kernel::activity::MailboxImpl> synchro2_mbox_cpy = remote(synchro2->mbox_cpy);
242 if (mbox != synchro2_mbox_cpy && simcall_comm_wait__get__timeout(r2) <= 0)
245 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
246 simcall_comm_wait__get__timeout(r2) <= 0)
249 if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
250 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
253 if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
254 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
258 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
261 if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
262 && r2->call == Simcall::COMM_TEST)
266 if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
267 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
270 if (r1->call_ == Simcall::COMM_TEST &&
271 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
274 if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
275 synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
278 if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
279 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
280 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
281 synchro2->dst_buff_ != synchro1->src_buff_)
287 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
289 if (mc_model_checker == nullptr)
290 return actor->get_host()->get_name();
292 const simgrid::mc::RemoteSimulation* process = &mc_model_checker->get_remote_simulation();
294 // Read the simgrid::xbt::string in the MCed process:
295 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
296 auto remote_string_address =
297 remote(reinterpret_cast<const simgrid::xbt::string_data*>(&actor->get_host()->get_name()));
298 simgrid::xbt::string_data remote_string = process->read(remote_string_address);
299 std::vector<char> hostname(remote_string.len + 1);
300 // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
301 process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
302 info->hostname = &mc_model_checker->get_host_name(hostname.data());
303 return *info->hostname;
306 std::string Api::get_actor_name(smx_actor_t actor) const
308 if (mc_model_checker == nullptr)
309 return actor->get_cname();
311 const simgrid::mc::RemoteSimulation* process = &mc_model_checker->get_remote_simulation();
313 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
314 if (info->name.empty()) {
315 simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
316 info->name = process->read_string(remote(string_data.data), string_data.len);
321 std::string Api::get_actor_string(smx_actor_t actor) const
323 std::string res = "(" + std::to_string(actor->get_pid()) + ")";
324 if (actor->get_host())
325 res += get_actor_host_name(actor);
329 std::string Api::get_actor_string2(smx_actor_t actor) const
333 res = "(" + std::to_string(actor->get_pid()) + ")";
334 if (actor->get_host())
335 res += std::string(get_actor_host_name(actor)) + " (" + get_actor_name(actor) + ")";
337 res += get_actor_name(actor);
343 void Api::initialize(char** argv) const
345 simgrid::mc::session = new simgrid::mc::Session([argv] {
347 while (argv[i] != nullptr && argv[i][0] == '-')
349 xbt_assert(argv[i] != nullptr,
350 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
351 execvp(argv[i], argv + i);
352 xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
356 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
358 return mc_model_checker->get_remote_simulation().actors();
361 bool Api::actor_is_enabled(aid_t pid) const
363 return session->actor_is_enabled(pid);
366 unsigned long Api::get_maxpid() const
368 return MC_smx_get_maxpid();
371 int Api::get_actors_size() const
373 return mc_model_checker->get_remote_simulation().actors().size();
376 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
378 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
379 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
382 RemotePtr<kernel::activity::CommImpl> Api::get_comm_irecv_raw_addr(smx_simcall_t request) const
384 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
385 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
388 RemotePtr<kernel::activity::CommImpl> Api::get_comm_wait_raw_addr(smx_simcall_t request) const
390 auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl*>(request->args_[0]);
391 return RemotePtr<kernel::activity::CommImpl>(comm_addr);
394 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
396 auto addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl**>(request->args_[0]) + value;
397 auto comm_addr = mc_model_checker->get_remote_simulation().read(remote(addr));
398 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
401 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
403 Remote<kernel::activity::CommImpl> temp_synchro;
404 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
405 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
407 char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
408 (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
409 auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
413 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
415 Remote<kernel::activity::CommImpl> temp_synchro;
416 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
417 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
419 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
423 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
425 Remote<kernel::activity::CommImpl> temp_synchro;
426 mc_model_checker->get_remote_simulation().read(temp_synchro, addr);
427 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
429 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
433 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
435 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
436 mc_model_checker->get_remote_simulation().read(temp_comm, addr);
437 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
439 std::vector<char> buffer{};
440 if (comm->src_buff_ != nullptr) {
441 buffer.resize(comm->src_buff_size_);
442 mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
448 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
450 simgrid::smpi::Request mpi_request;
451 mc_model_checker->get_remote_simulation().read(
452 &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
453 return mpi_request.detached();
457 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
459 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
460 mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
461 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
463 auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
467 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
469 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
470 mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
471 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
473 auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
477 std::size_t Api::get_remote_heap_bytes() const
479 RemoteSimulation& process = mc_model_checker->get_remote_simulation();
480 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
481 return heap_bytes_used;
484 void Api::session_initialize() const
486 session->initialize();
489 void Api::mc_inc_visited_states() const
491 mc_model_checker->visited_states++;
494 void Api::mc_inc_executed_trans() const
496 mc_model_checker->executed_transitions++;
499 unsigned long Api::mc_get_visited_states() const
501 return mc_model_checker->visited_states;
504 unsigned long Api::mc_get_executed_trans() const
506 return mc_model_checker->executed_transitions;
509 bool Api::mc_check_deadlock() const
511 return mc_model_checker->checkDeadlock();
514 void Api::mc_show_deadlock() const
519 /** Get the issuer of a simcall (`req->issuer`)
521 * In split-process mode, it does the black magic necessary to get an address
522 * of a (shallow) copy of the data structure the issuer SIMIX actor in the local
525 * @param process the MCed process
526 * @param req the simcall (copied in the local process)
528 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
530 xbt_assert(mc_model_checker != nullptr);
532 // This is the address of the smx_actor in the MCed process:
533 auto address = simgrid::mc::remote(req->issuer_);
535 // Lookup by address:
536 for (auto& actor : mc_model_checker->get_remote_simulation().actors())
537 if (actor.address == address)
538 return actor.copy.get_buffer();
539 for (auto& actor : mc_model_checker->get_remote_simulation().dead_actors())
540 if (actor.address == address)
541 return actor.copy.get_buffer();
543 xbt_die("Issuer not found");
546 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
548 return simcall_get_issuer(req)->get_pid();
551 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
553 RemotePtr<kernel::activity::MailboxImpl> mbox_addr;
554 switch (req->call_) {
555 case Simcall::COMM_ISEND:
556 case Simcall::COMM_IRECV: {
557 auto mbox_addr_ptr = simix::unmarshal<smx_mailbox_t>(req->args_[1]);
558 mbox_addr = remote(mbox_addr_ptr);
562 mbox_addr = RemotePtr<kernel::activity::MailboxImpl>();
568 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
570 RemotePtr<kernel::activity::ActivityImpl> comm_addr;
571 switch (req->call_) {
572 case Simcall::COMM_ISEND:
573 case Simcall::COMM_IRECV: {
574 auto comm_addr_ptr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(req->result_);
575 comm_addr = remote(comm_addr_ptr);
579 comm_addr = RemotePtr<kernel::activity::ActivityImpl>();
585 bool Api::mc_is_null() const
587 auto is_null = (mc_model_checker == nullptr) ? true : false;
591 Checker* Api::mc_get_checker() const
593 return mc_model_checker->getChecker();
596 void Api::set_checker(Checker* const checker) const
598 xbt_assert(mc_model_checker);
599 xbt_assert(mc_model_checker->getChecker() == nullptr);
600 mc_model_checker->setChecker(checker);
603 void Api::handle_simcall(Transition const& transition) const
605 mc_model_checker->handle_simcall(transition);
608 void Api::mc_wait_for_requests() const
610 mc_model_checker->wait_for_requests();
613 void Api::mc_exit(int status) const
615 mc_model_checker->exit(status);
618 void Api::dump_record_path() const
620 simgrid::mc::dumpRecordPath();
623 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
625 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
626 /* Only consider the actors that were marked as interleaving by the checker algorithm */
627 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
630 smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
637 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state)
639 std::list<transition_detail_t> tr_list{};
641 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
642 auto actor_pid = actor.copy.get_buffer()->get_pid();
643 auto actor_impl = actor.copy.get_buffer();
645 // Only consider the actors that were marked as interleaving by the checker algorithm
646 if (not state->actor_states_[actor_pid].is_todo())
648 // Not executable in the application
649 if (not simgrid::mc::actor_is_enabled(actor_impl))
652 transition_detail_t transition = std::unique_ptr<s_transition_detail>(new s_transition_detail());
653 Simcall simcall_call = actor_impl->simcall_.call_;
654 smx_simcall_t simcall = &actor_impl->simcall_;
655 transition->call_ = simcall_call;
656 switch (simcall_call) {
657 case Simcall::COMM_ISEND:
658 case Simcall::COMM_IRECV: {
659 transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
660 transition->comm_remote_addr = get_comm_remote_addr(simcall);
667 tr_list.emplace_back(std::move(transition));
673 bool Api::simcall_check_dependency(smx_simcall_t const req1, smx_simcall_t const req2) const
675 if (req1->issuer_ == req2->issuer_)
678 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent
679 * with all other transitions */
680 if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
681 (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
684 if (req1->call_ != req2->call_)
685 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
687 // Those are internal requests, we do not need indirection because those objects are copies:
688 const kernel::activity::CommImpl* synchro1 = get_comm(req1);
689 const kernel::activity::CommImpl* synchro2 = get_comm(req2);
691 switch (req1->call_) {
692 case Simcall::COMM_ISEND:
693 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
694 case Simcall::COMM_IRECV:
695 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
696 case Simcall::COMM_WAIT:
697 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
699 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
700 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
701 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
709 std::string Api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
711 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
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;
729 smx_actor_t issuer = simcall_get_issuer(req);
731 if (issuer->simcall_.inspector_ != nullptr)
732 return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
734 switch (req->call_) {
735 case Simcall::COMM_ISEND:
737 args = "src=" + get_actor_string2(issuer);
738 args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
739 args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
742 case Simcall::COMM_IRECV: {
743 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
746 mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
749 args = "dst=" + get_actor_string2(issuer);
750 args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
751 args += ", size=" + buff_size_to_string(size);
755 case Simcall::COMM_WAIT: {
756 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
758 type = "WaitTimeout";
759 args = "comm=" + pointer_to_string(remote_act);
763 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
764 const simgrid::kernel::activity::CommImpl* act;
765 if (use_remote_comm) {
766 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
767 act = temp_synchro.get_buffer();
771 smx_actor_t src_proc =
772 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
773 smx_actor_t dst_proc =
774 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
775 args = "comm=" + pointer_to_string(remote_act);
776 args += " [" + get_actor_string2(src_proc) + "-> " + get_actor_string2(dst_proc) + "]";
781 case Simcall::COMM_TEST: {
782 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
783 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
784 const simgrid::kernel::activity::CommImpl* act;
785 if (use_remote_comm) {
786 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
787 act = temp_synchro.get_buffer();
791 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
793 args = "comm=" + pointer_to_string(remote_act);
797 smx_actor_t src_proc =
798 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
799 smx_actor_t dst_proc =
800 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
801 args = "comm=" + pointer_to_string(remote_act);
802 args += " [" + get_actor_string2(src_proc) + " -> " + get_actor_string2(dst_proc) + "]";
807 case Simcall::COMM_WAITANY: {
809 size_t count = simcall_comm_waitany__get__count(req);
811 simgrid::kernel::activity::CommImpl* remote_sync;
813 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
814 args = "comm=" + pointer_to_string(remote_sync) + xbt::string_printf("(%d of %zu)", value + 1, count);
816 args = "comm at idx " + std::to_string(value);
820 case Simcall::COMM_TESTANY:
822 type = "TestAny FALSE";
826 args = xbt::string_printf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
830 case Simcall::MUTEX_TRYLOCK:
831 case Simcall::MUTEX_LOCK: {
832 if (req->call_ == Simcall::MUTEX_LOCK)
835 type = "Mutex TRYLOCK";
837 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
838 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
839 remote(req->call_ == Simcall::MUTEX_LOCK
840 ? simcall_mutex_lock__get__mutex(req)
841 : simcall_mutex_trylock__get__mutex(req)));
842 args = "locked = " + std::to_string(mutex.get_buffer()->is_locked()) + ", owner = ";
843 if (mutex.get_buffer()->get_owner() != nullptr)
844 args += std::to_string(mc_model_checker->get_remote_simulation()
845 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
849 args += ", sleeping = n/a";
854 type = SIMIX_simcall_name(req->call_);
859 return "[" + get_actor_string2(issuer) + "] " + type + "(" + args + ")";
862 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
864 const smx_actor_t issuer = simcall_get_issuer(req);
865 const char* color = get_color(issuer->get_pid() - 1);
869 if (req->inspector_ != nullptr) {
870 label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
872 switch (req->call_) {
873 case Simcall::COMM_ISEND:
874 label = "[" + get_actor_string(issuer) + "] iSend";
877 case Simcall::COMM_IRECV:
878 label = "[" + get_actor_string(issuer) + "] iRecv";
881 case Simcall::COMM_WAIT:
883 label = "[" + get_actor_string(issuer) + "] WaitTimeout";
885 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
886 Remote<kernel::activity::CommImpl> temp_comm;
887 mc_model_checker->get_remote_simulation().read(temp_comm,
888 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
889 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
891 const kernel::actor::ActorImpl* src_proc =
892 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
893 const kernel::actor::ActorImpl* dst_proc =
894 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
895 label = "[" + get_actor_string(issuer) + "] Wait";
896 label += " [(" + std::to_string(src_proc ? src_proc->get_pid() : 0) + ")";
897 label += "->(" + std::to_string(dst_proc ? dst_proc->get_pid() : 0) + ")]";
901 case Simcall::COMM_TEST: {
902 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
903 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
904 mc_model_checker->get_remote_simulation().read(temp_comm,
905 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
906 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
907 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
908 label = "[" + get_actor_string(issuer) + "] Test FALSE";
910 label = "[" + get_actor_string(issuer) + "] Test TRUE";
915 case Simcall::COMM_WAITANY:
916 label = "[" + get_actor_string(issuer) + "] WaitAny";
917 label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_waitany__get__count(req));
920 case Simcall::COMM_TESTANY:
922 label = "[" + get_actor_string(issuer) + "] TestAny FALSE";
924 label = "[" + get_actor_string(issuer) + "] TestAny TRUE";
925 label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_testany__get__count(req));
929 case Simcall::MUTEX_TRYLOCK:
930 label = "[" + get_actor_string(issuer) + "] Mutex TRYLOCK";
933 case Simcall::MUTEX_LOCK:
934 label = "[" + get_actor_string(issuer) + "] Mutex LOCK";
941 return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
945 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
947 simgrid::smpi::Request mpi_request;
948 void* simcall_data = nullptr;
949 if (type == Simcall::COMM_ISEND)
950 simcall_data = simcall_comm_isend__get__data(simcall);
951 else if (type == Simcall::COMM_IRECV)
952 simcall_data = simcall_comm_irecv__get__data(simcall);
953 mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
954 return mpi_request.tag();
958 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
960 system_state->restore(&mc_model_checker->get_remote_simulation());
963 void Api::log_state() const
965 session->log_state();
968 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
970 return simgrid::mc::snapshot_equal(s1, s2);
973 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
975 auto snapshot = new simgrid::mc::Snapshot(num_state);
979 void Api::s_close() const
984 void Api::restore_initial_state() const
986 session->restore_initial_state();
989 void Api::execute(Transition& transition, smx_simcall_t simcall) const
991 /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
993 simgrid::mc::Api::get().request_to_string(simcall, transition.times_considered_, RequestType::executed);
994 strcpy((char*)transition.textual, textual.c_str());
996 session->execute(transition);
1000 void Api::automaton_load(const char* file) const
1002 MC_automaton_load(file);
1006 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
1008 unsigned int cursor = 0;
1009 std::vector<int> values;
1010 xbt_automaton_propositional_symbol_t ps = nullptr;
1011 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
1012 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
1016 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
1018 std::vector<xbt_automaton_state_t> automaton_stack;
1019 unsigned int cursor = 0;
1020 xbt_automaton_state_t automaton_state;
1021 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
1022 if (automaton_state->type == -1)
1023 automaton_stack.push_back(automaton_state);
1024 return automaton_stack;
1027 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
1029 unsigned int cursor = 0;
1030 xbt_automaton_propositional_symbol_t p = nullptr;
1031 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
1032 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
1038 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1040 mc::property_automaton->current_state = automaton_state;
1043 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1045 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1046 return transition->label;
1049 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1051 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1052 return transition->dst;
1056 } // namespace simgrid