3 #include "src/kernel/activity/MailboxImpl.hpp"
4 #include "src/kernel/activity/MutexImpl.hpp"
5 #include "src/kernel/actor/SimcallObserver.hpp"
6 #include "src/mc/Session.hpp"
7 #include "src/mc/checker/Checker.hpp"
8 #include "src/mc/mc_base.hpp"
9 #include "src/mc/mc_comm_pattern.hpp"
10 #include "src/mc/mc_exit.hpp"
11 #include "src/mc/mc_pattern.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/remote/RemoteProcess.hpp"
14 #include "src/surf/HostImpl.hpp"
16 #include <xbt/asserts.h>
18 #include "simgrid/s4u/Host.hpp"
19 #include "xbt/string.hpp"
21 #include "src/smpi/include/smpi_request.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
26 using Simcall = simgrid::simix::Simcall;
31 static inline const char* get_color(int id)
33 static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
34 "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
36 return colors[id % colors.size()];
39 static std::string pointer_to_string(void* pointer)
41 return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
44 static std::string buff_size_to_string(size_t buff_size)
46 return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
49 static void simcall_translate(smx_simcall_t req,
50 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm);
52 static bool request_is_enabled_by_idx(const RemoteProcess& process, smx_simcall_t req, unsigned int idx)
54 kernel::activity::CommImpl* remote_act = nullptr;
56 case Simcall::COMM_WAIT:
57 /* FIXME: check also that src and dst processes are not suspended */
58 remote_act = simcall_comm_wait__getraw__comm(req);
61 case Simcall::COMM_WAITANY:
62 remote_act = process.read(remote(simcall_comm_waitany__get__comms(req) + idx));
65 case Simcall::COMM_TESTANY:
66 remote_act = process.read(remote(simcall_comm_testany__get__comms(req) + idx));
73 Remote<kernel::activity::CommImpl> temp_comm;
74 process.read(temp_comm, remote(remote_act));
75 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
76 return comm->src_actor_.get() && comm->dst_actor_.get();
79 /* Search an enabled transition for the given process.
81 * This can be seen as an iterator returning the next transition of the process.
83 * We only consider the processes that are both
84 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
85 * - which simcall can currently be executed (like a comm where the other partner is already known)
86 * Once we returned the last enabled transition of a process, it is marked done.
88 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
89 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
91 static inline smx_simcall_t MC_state_choose_request_for_process(const RemoteProcess& process, simgrid::mc::State* state,
94 /* reset the outgoing transition */
95 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
96 state->transition_.aid_ = -1;
97 state->transition_.times_considered_ = -1;
98 state->transition_.textual[0] = '\0';
99 state->executed_req_.call_ = Simcall::NONE;
101 if (not simgrid::mc::actor_is_enabled(actor))
102 return nullptr; // Not executable in the application
104 smx_simcall_t req = nullptr;
105 if (actor->simcall_.observer_ != nullptr) {
106 state->transition_.times_considered_ = procstate->times_considered;
107 procstate->times_considered++;
108 if (actor->simcall_.mc_max_consider_ <= procstate->times_considered)
109 procstate->set_done();
110 req = &actor->simcall_;
112 switch (actor->simcall_.call_) {
113 case Simcall::COMM_WAITANY:
114 state->transition_.times_considered_ = -1;
115 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
116 if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->times_considered)) {
117 state->transition_.times_considered_ = procstate->times_considered;
118 ++procstate->times_considered;
121 ++procstate->times_considered;
124 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
125 procstate->set_done();
126 if (state->transition_.times_considered_ != -1)
127 req = &actor->simcall_;
130 case Simcall::COMM_TESTANY:
131 state->transition_.times_considered_ = -1;
132 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
133 if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->times_considered)) {
134 state->transition_.times_considered_ = procstate->times_considered;
135 ++procstate->times_considered;
138 ++procstate->times_considered;
141 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
142 procstate->set_done();
143 if (state->transition_.times_considered_ != -1)
144 req = &actor->simcall_;
147 case Simcall::COMM_WAIT: {
148 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
149 remote(simcall_comm_wait__get__comm(&actor->simcall_));
150 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
151 process.read(temp_act, remote_act);
152 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
153 if (act->src_actor_.get() && act->dst_actor_.get())
154 state->transition_.times_considered_ = 0; // OK
155 else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
157 state->transition_.times_considered_ = 0; // OK
159 state->transition_.times_considered_ = -1; // timeout
160 procstate->set_done();
161 req = &actor->simcall_;
166 procstate->set_done();
167 state->transition_.times_considered_ = 0;
168 req = &actor->simcall_;
174 state->transition_.aid_ = actor->get_pid();
175 state->executed_req_ = *req;
177 // Fetch the data of the request and translate it:
178 state->internal_req_ = *req;
179 state->internal_req_.mc_value_ = state->transition_.times_considered_;
180 simcall_translate(&state->internal_req_, state->internal_comm_);
185 static void simcall_translate(smx_simcall_t req,
186 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm)
188 simgrid::kernel::activity::CommImpl* chosen_comm;
190 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
191 * action so it can be treated later by the dependence function. */
192 switch (req->call_) {
193 case Simcall::COMM_WAITANY:
194 req->call_ = Simcall::COMM_WAIT;
196 mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + req->mc_value_));
198 mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
199 simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
200 simcall_comm_wait__set__timeout(req, 0);
203 case Simcall::COMM_TESTANY:
204 req->call_ = Simcall::COMM_TEST;
206 mc_model_checker->get_remote_process().read(remote(simcall_comm_testany__get__comms(req) + req->mc_value_));
208 mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
209 simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
210 simcall_comm_test__set__result(req, req->mc_value_);
213 case Simcall::COMM_WAIT:
214 chosen_comm = simcall_comm_wait__get__comm(req);
215 mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
216 simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
219 case Simcall::COMM_TEST:
220 chosen_comm = simcall_comm_test__get__comm(req);
221 mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
222 simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
226 /* No translation needed */
231 simgrid::kernel::activity::CommImpl* Api::get_comm_or_nullptr(smx_simcall_t const r) const
233 if (r->call_ == Simcall::COMM_WAIT)
234 return simcall_comm_wait__get__comm(r);
235 if (r->call_ == Simcall::COMM_TEST)
236 return simcall_comm_test__get__comm(r);
240 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
242 * This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
243 * sense that we could achieve the same thing by having ActorInformation
244 * inherit from s_smx_actor_t but we don't really want to do that.
246 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
248 simgrid::mc::ActorInformation temp;
249 std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
251 auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
255 bool Api::simcall_check_dependency(smx_simcall_t req1, smx_simcall_t req2) const
257 const auto IRECV = Simcall::COMM_IRECV;
258 const auto ISEND = Simcall::COMM_ISEND;
259 const auto TEST = Simcall::COMM_TEST;
260 const auto WAIT = Simcall::COMM_WAIT;
262 if (req1->issuer_ == req2->issuer_)
265 /* The independence theorem only consider 4 simcalls. All others are dependent with anything. */
266 if (req1->call_ != ISEND && req1->call_ != IRECV && req1->call_ != TEST && req1->call_ != WAIT)
268 if (req2->call_ != ISEND && req2->call_ != IRECV && req2->call_ != TEST && req2->call_ != WAIT)
271 /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
272 if ((req1->call_ == WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
273 (req2->call_ == WAIT && simcall_comm_wait__get__timeout(req2) > 0))
276 /* Make sure that req1 and req2 are in alphabetic order */
277 if (req1->call_ > req2->call_) {
283 auto comm1 = get_comm_or_nullptr(req1);
284 auto comm2 = get_comm_or_nullptr(req2);
286 /* First case: that's not the same kind of request (we also know that req1 < req2 alphabetically) */
287 if (req1->call_ != req2->call_) {
288 if (req1->call_ == IRECV && req2->call_ == ISEND)
291 if ((req1->call_ == IRECV || req1->call_ == ISEND) && req2->call_ == WAIT) {
292 auto mbox1 = get_mbox_remote_addr(req1);
293 auto mbox2 = remote(comm2->mbox_cpy);
295 if (mbox1 != mbox2 && simcall_comm_wait__get__timeout(req2) <= 0)
298 if ((req1->issuer_ != comm2->src_actor_.get()) && (req1->issuer_ != comm2->dst_actor_.get()) &&
299 simcall_comm_wait__get__timeout(req2) <= 0)
302 if ((req1->call_ == ISEND) && (comm2->type_ == kernel::activity::CommImpl::Type::SEND) &&
303 (comm2->src_buff_ != simcall_comm_isend__get__src_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
306 if ((req1->call_ == IRECV) && (comm2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
307 (comm2->dst_buff_ != simcall_comm_irecv__get__dst_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
311 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
314 if((req1->call == ISEND || req1->call == IRECV)
315 && req2->call == TEST)
319 if (req1->call_ == TEST && req2->call_ == WAIT &&
320 (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
323 if (req1->call_ == TEST &&
324 (simcall_comm_test__get__comm(req1) == nullptr || comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr))
326 if (req2->call_ == TEST &&
327 (simcall_comm_test__get__comm(req2) == nullptr || comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr))
330 if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ == comm2->src_buff_ &&
331 comm1->dst_buff_ == comm2->dst_buff_)
334 if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr &&
335 comm2->src_buff_ != nullptr && comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ &&
336 comm1->dst_buff_ != comm2->dst_buff_ && comm2->dst_buff_ != comm1->src_buff_)
342 /* Second case: req1 and req2 are of the same call type */
343 switch (req1->call_) {
345 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
347 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
349 if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
351 if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
352 comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
353 comm2->dst_buff_ != comm1->src_buff_)
361 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
363 if (mc_model_checker == nullptr)
364 return actor->get_host()->get_name();
366 const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
368 // Read the simgrid::xbt::string in the MCed process:
369 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
371 if (not info->hostname) {
372 Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
373 auto remote_string_address = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
374 simgrid::xbt::string_data remote_string = process->read(remote_string_address);
375 std::vector<char> hostname(remote_string.len + 1);
376 // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
377 process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
378 info->hostname = &mc_model_checker->get_host_name(hostname.data());
380 return *info->hostname;
383 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
385 if (mc_model_checker == nullptr)
386 return actor->get_name();
388 simgrid::mc::ActorInformation* info = actor_info_cast(actor);
389 if (info->name.empty()) {
390 const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
392 simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
393 info->name = process->read_string(remote(string_data.data), string_data.len);
398 std::string Api::get_actor_string(smx_actor_t actor) const
402 res = "(" + std::to_string(actor->get_pid()) + ")";
403 if (actor->get_host())
404 res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
406 res += get_actor_name(actor);
412 std::string Api::get_actor_dot_label(smx_actor_t actor) const
414 std::string res = "(" + std::to_string(actor->get_pid()) + ")";
415 if (actor->get_host())
416 res += get_actor_host_name(actor);
420 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
422 auto session = new simgrid::mc::Session([argv] {
424 while (argv[i] != nullptr && argv[i][0] == '-')
426 xbt_assert(argv[i] != nullptr,
427 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
428 execvp(argv[i], argv + i);
429 xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
432 simgrid::mc::Checker* checker;
434 case CheckerAlgorithm::CommDeterminism:
435 checker = simgrid::mc::create_communication_determinism_checker(session);
438 case CheckerAlgorithm::UDPOR:
439 checker = simgrid::mc::create_udpor_checker(session);
442 case CheckerAlgorithm::Safety:
443 checker = simgrid::mc::create_safety_checker(session);
446 case CheckerAlgorithm::Liveness:
447 checker = simgrid::mc::create_liveness_checker(session);
454 // FIXME: session and checker are never deleted
455 simgrid::mc::session_singleton = session;
456 mc_model_checker->setChecker(checker);
460 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
462 return mc_model_checker->get_remote_process().actors();
465 unsigned long Api::get_maxpid() const
467 return mc_model_checker->get_remote_process().get_maxpid();
470 int Api::get_actors_size() const
472 return mc_model_checker->get_remote_process().actors().size();
475 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
477 return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
480 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
482 auto addr = simcall_comm_waitany__getraw__comms(request) + value;
483 auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
484 return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
487 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
489 Remote<kernel::activity::CommImpl> temp_activity;
490 mc_model_checker->get_remote_process().read(temp_activity, addr);
491 const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
493 char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
494 (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
495 auto rdv = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
499 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
501 Remote<kernel::activity::CommImpl> temp_activity;
502 mc_model_checker->get_remote_process().read(temp_activity, addr);
503 const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
505 mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
509 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
511 Remote<kernel::activity::CommImpl> temp_activity;
512 mc_model_checker->get_remote_process().read(temp_activity, addr);
513 const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
515 mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
519 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
521 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
522 mc_model_checker->get_remote_process().read(temp_comm, addr);
523 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
525 std::vector<char> buffer{};
526 if (comm->src_buff_ != nullptr) {
527 buffer.resize(comm->src_buff_size_);
528 mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
534 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
536 Remote<simgrid::smpi::Request> mpi_request;
537 mc_model_checker->get_remote_process().read(
538 mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
539 return mpi_request.get_buffer()->detached();
543 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
545 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
546 mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
547 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
549 auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
553 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
555 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
556 mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
557 const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
559 auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
563 std::size_t Api::get_remote_heap_bytes() const
565 RemoteProcess& process = mc_model_checker->get_remote_process();
566 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
567 return heap_bytes_used;
570 void Api::mc_inc_visited_states() const
572 mc_model_checker->visited_states++;
575 void Api::mc_inc_executed_trans() const
577 mc_model_checker->executed_transitions++;
580 unsigned long Api::mc_get_visited_states() const
582 return mc_model_checker->visited_states;
585 unsigned long Api::mc_get_executed_trans() const
587 return mc_model_checker->executed_transitions;
590 void Api::mc_check_deadlock() const
592 if (mc_model_checker->checkDeadlock()) {
594 throw DeadlockError();
598 /** Get the issuer of a simcall (`req->issuer`)
600 * In split-process mode, it does the black magic necessary to get an address
601 * of a (shallow) copy of the data structure the issuer SIMIX actor in the local
604 * @param process the MCed process
605 * @param req the simcall (copied in the local process)
607 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
609 xbt_assert(mc_model_checker != nullptr);
611 // This is the address of the smx_actor in the MCed process:
612 auto address = simgrid::mc::remote(req->issuer_);
614 // Lookup by address:
615 for (auto& actor : mc_model_checker->get_remote_process().actors())
616 if (actor.address == address)
617 return actor.copy.get_buffer();
618 for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
619 if (actor.address == address)
620 return actor.copy.get_buffer();
622 xbt_die("Issuer not found");
625 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
627 return simcall_get_issuer(req)->get_pid();
630 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
632 if (req->call_ == Simcall::COMM_ISEND)
633 return remote(simcall_comm_isend__get__mbox(req));
634 if (req->call_ == Simcall::COMM_IRECV)
635 return remote(simcall_comm_irecv__get__mbox(req));
639 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
641 if (req->call_ == Simcall::COMM_ISEND)
642 return remote(simcall_comm_isend__getraw__result(req));
643 if (req->call_ == Simcall::COMM_IRECV)
644 return remote(simcall_comm_irecv__getraw__result(req));
648 bool Api::mc_is_null() const
650 auto is_null = (mc_model_checker == nullptr) ? true : false;
654 Checker* Api::mc_get_checker() const
656 return mc_model_checker->getChecker();
659 void Api::handle_simcall(Transition const& transition) const
661 mc_model_checker->handle_simcall(transition);
664 void Api::mc_wait_for_requests() const
666 mc_model_checker->wait_for_requests();
669 void Api::mc_exit(int status) const
671 mc_model_checker->exit(status);
674 void Api::dump_record_path() const
676 simgrid::mc::dumpRecordPath();
679 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
681 RemoteProcess& process = mc_model_checker->get_remote_process();
682 for (auto& actor : process.actors()) {
683 /* Only consider the actors that were marked as interleaving by the checker algorithm */
684 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
687 smx_simcall_t res = MC_state_choose_request_for_process(process, state, actor.copy.get_buffer());
694 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state) const
696 std::list<transition_detail_t> tr_list{};
698 for (auto& actor : mc_model_checker->get_remote_process().actors()) {
699 auto actor_pid = actor.copy.get_buffer()->get_pid();
700 auto actor_impl = actor.copy.get_buffer();
702 // Only consider the actors that were marked as interleaving by the checker algorithm
703 if (not state->actor_states_[actor_pid].is_todo())
705 // Not executable in the application
706 if (not simgrid::mc::actor_is_enabled(actor_impl))
709 auto transition = std::make_unique<s_transition_detail>();
710 Simcall simcall_call = actor_impl->simcall_.call_;
711 smx_simcall_t simcall = &actor_impl->simcall_;
712 transition->call_ = simcall_call;
713 switch (simcall_call) {
714 case Simcall::COMM_ISEND:
715 case Simcall::COMM_IRECV:
716 transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
717 transition->comm_remote_addr = get_comm_remote_addr(simcall);
723 tr_list.emplace_back(std::move(transition));
729 std::string Api::request_to_string(smx_simcall_t req, int value) const
731 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
736 smx_actor_t issuer = simcall_get_issuer(req);
738 if (issuer->simcall_.observer_ != nullptr)
739 return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
741 switch (req->call_) {
742 case Simcall::COMM_ISEND:
744 args = "src=" + get_actor_string(issuer);
745 args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
746 args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
749 case Simcall::COMM_IRECV: {
750 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
753 mc_model_checker->get_remote_process().read_bytes(&size, sizeof(size), remote(remote_size));
756 args = "dst=" + get_actor_string(issuer);
757 args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
758 args += ", size=" + buff_size_to_string(size);
762 case Simcall::COMM_WAIT: {
763 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__get__comm(req);
765 type = "WaitTimeout";
766 args = "comm=" + pointer_to_string(remote_act);
770 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
771 const simgrid::kernel::activity::CommImpl* act;
772 mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
773 act = temp_activity.get_buffer();
775 smx_actor_t src_proc =
776 mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
777 smx_actor_t dst_proc =
778 mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
779 args = "comm=" + pointer_to_string(remote_act);
780 args += " [" + get_actor_string(src_proc) + "-> " + get_actor_string(dst_proc) + "]";
785 case Simcall::COMM_TEST: {
786 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__get__comm(req);
787 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
788 const simgrid::kernel::activity::CommImpl* act;
789 mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
790 act = temp_activity.get_buffer();
792 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
794 args = "comm=" + pointer_to_string(remote_act);
798 smx_actor_t src_proc =
799 mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
800 smx_actor_t dst_proc =
801 mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
802 args = "comm=" + pointer_to_string(remote_act);
803 args += " [" + get_actor_string(src_proc) + " -> " + get_actor_string(dst_proc) + "]";
808 case Simcall::COMM_WAITANY: {
810 size_t count = simcall_comm_waitany__get__count(req);
812 simgrid::kernel::activity::CommImpl* remote_sync;
814 mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + value));
815 args = "comm=" + pointer_to_string(remote_sync) + xbt::string_printf("(%d of %zu)", value + 1, count);
817 args = "comm at idx " + std::to_string(value);
821 case Simcall::COMM_TESTANY:
823 type = "TestAny FALSE";
827 args = xbt::string_printf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
832 type = SIMIX_simcall_name(*req);
837 return "[" + get_actor_string(issuer) + "] " + type + "(" + args + ")";
840 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
842 const smx_actor_t issuer = simcall_get_issuer(req);
843 const char* color = get_color(issuer->get_pid() - 1);
847 if (req->observer_ != nullptr) {
848 label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
850 switch (req->call_) {
851 case Simcall::COMM_ISEND:
852 label = "[" + get_actor_dot_label(issuer) + "] iSend";
855 case Simcall::COMM_IRECV:
856 label = "[" + get_actor_dot_label(issuer) + "] iRecv";
859 case Simcall::COMM_WAIT:
861 label = "[" + get_actor_dot_label(issuer) + "] WaitTimeout";
863 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__get__comm(req);
864 Remote<kernel::activity::CommImpl> temp_comm;
865 mc_model_checker->get_remote_process().read(temp_comm,
866 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
867 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
869 const kernel::actor::ActorImpl* src_proc =
870 mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->src_actor_.get()));
871 const kernel::actor::ActorImpl* dst_proc =
872 mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->dst_actor_.get()));
873 label = "[" + get_actor_dot_label(issuer) + "] Wait";
874 label += " [(" + std::to_string(src_proc ? src_proc->get_pid() : 0) + ")";
875 label += "->(" + std::to_string(dst_proc ? dst_proc->get_pid() : 0) + ")]";
879 case Simcall::COMM_TEST: {
880 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__get__comm(req);
881 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
882 mc_model_checker->get_remote_process().read(temp_comm,
883 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
884 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
885 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
886 label = "[" + get_actor_dot_label(issuer) + "] Test FALSE";
888 label = "[" + get_actor_dot_label(issuer) + "] Test TRUE";
893 case Simcall::COMM_WAITANY:
894 label = "[" + get_actor_dot_label(issuer) + "] WaitAny";
895 label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_waitany__get__count(req));
898 case Simcall::COMM_TESTANY:
900 label = "[" + get_actor_dot_label(issuer) + "] TestAny FALSE";
902 label = "[" + get_actor_dot_label(issuer) + "] TestAny TRUE";
903 label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_testany__get__count(req));
911 return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
915 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
917 void* simcall_data = nullptr;
918 if (type == Simcall::COMM_ISEND)
919 simcall_data = simcall_comm_isend__get__data(simcall);
920 else if (type == Simcall::COMM_IRECV)
921 simcall_data = simcall_comm_irecv__get__data(simcall);
922 Remote<simgrid::smpi::Request> mpi_request;
923 mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
924 return mpi_request.get_buffer()->tag();
928 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
930 system_state->restore(&mc_model_checker->get_remote_process());
933 void Api::log_state() const
935 session_singleton->log_state();
938 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
940 return simgrid::mc::snapshot_equal(s1, s2);
943 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
945 auto snapshot = new simgrid::mc::Snapshot(num_state);
949 void Api::s_close() const
951 session_singleton->close();
954 void Api::execute(Transition& transition, smx_simcall_t simcall) const
956 /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
957 transition.textual = request_to_string(simcall, transition.times_considered_);
958 session_singleton->execute(transition);
961 void Api::automaton_load(const char* file) const
963 MC_automaton_load(file);
966 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
968 unsigned int cursor = 0;
969 std::vector<int> values;
970 xbt_automaton_propositional_symbol_t ps = nullptr;
971 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
972 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
976 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
978 std::vector<xbt_automaton_state_t> automaton_stack;
979 unsigned int cursor = 0;
980 xbt_automaton_state_t automaton_state;
981 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
982 if (automaton_state->type == -1)
983 automaton_stack.push_back(automaton_state);
984 return automaton_stack;
987 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
989 unsigned int cursor = 0;
990 xbt_automaton_propositional_symbol_t p = nullptr;
991 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
992 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
998 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1000 mc::property_automaton->current_state = automaton_state;
1003 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1005 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1006 return transition->label;
1009 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1011 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1012 return transition->dst;
1016 } // namespace simgrid