3 #include "src/kernel/activity/MailboxImpl.hpp"
4 #include "src/mc/Session.hpp"
5 #include "src/mc/mc_comm_pattern.hpp"
6 #include "src/mc/mc_private.hpp"
7 #include "src/mc/mc_record.hpp"
8 #include "src/mc/mc_smx.hpp"
9 #include "src/mc/remote/RemoteSimulation.hpp"
10 #include "src/mc/mc_pattern.hpp"
12 #include <xbt/asserts.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_api, mc, "Logging specific to MC Fasade APIs ");
20 /* Search an enabled transition for the given process.
22 * This can be seen as an iterator returning the next transition of the process.
24 * We only consider the processes that are both
25 * - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
26 * - which simcall can currently be executed (like a comm where the other partner is already known)
27 * Once we returned the last enabled transition of a process, it is marked done.
29 * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
30 * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
32 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
34 /* reset the outgoing transition */
35 simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
36 state->transition_.pid_ = -1;
37 state->transition_.argument_ = -1;
38 state->executed_req_.call_ = SIMCALL_NONE;
40 if (not simgrid::mc::actor_is_enabled(actor))
41 return nullptr; // Not executable in the application
43 smx_simcall_t req = nullptr;
44 switch (actor->simcall_.call_) {
45 case SIMCALL_COMM_WAITANY:
46 state->transition_.argument_ = -1;
47 while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
48 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
49 state->transition_.argument_ = procstate->times_considered;
50 ++procstate->times_considered;
53 ++procstate->times_considered;
56 if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
57 procstate->set_done();
58 if (state->transition_.argument_ != -1)
59 req = &actor->simcall_;
62 case SIMCALL_COMM_TESTANY: {
63 unsigned start_count = procstate->times_considered;
64 state->transition_.argument_ = -1;
65 while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
66 if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
67 state->transition_.argument_ = procstate->times_considered;
68 ++procstate->times_considered;
71 ++procstate->times_considered;
74 if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
75 procstate->set_done();
77 if (state->transition_.argument_ != -1 || start_count == 0)
78 req = &actor->simcall_;
83 case SIMCALL_COMM_WAIT: {
84 simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
85 remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
86 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
87 mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
88 const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
89 if (act->src_actor_.get() && act->dst_actor_.get())
90 state->transition_.argument_ = 0; // OK
91 else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
93 state->transition_.argument_ = 0; // OK
95 state->transition_.argument_ = -1; // timeout
96 procstate->set_done();
97 req = &actor->simcall_;
101 case SIMCALL_MC_RANDOM: {
102 int min_value = simcall_mc_random__get__min(&actor->simcall_);
103 state->transition_.argument_ = procstate->times_considered + min_value;
104 procstate->times_considered++;
105 if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
106 procstate->set_done();
107 req = &actor->simcall_;
112 procstate->set_done();
113 state->transition_.argument_ = 0;
114 req = &actor->simcall_;
120 state->transition_.pid_ = actor->get_pid();
121 state->executed_req_ = *req;
122 // Fetch the data of the request and translate it:
123 state->internal_req_ = *req;
125 /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
126 * action so it can be treated later by the dependence function. */
127 switch (req->call_) {
128 case SIMCALL_COMM_WAITANY: {
129 state->internal_req_.call_ = SIMCALL_COMM_WAIT;
130 simgrid::kernel::activity::CommImpl* remote_comm;
131 remote_comm = mc_model_checker->get_remote_simulation().read(
132 remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
133 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
134 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
135 simcall_comm_wait__set__timeout(&state->internal_req_, 0);
139 case SIMCALL_COMM_TESTANY:
140 state->internal_req_.call_ = SIMCALL_COMM_TEST;
142 if (state->transition_.argument_ > 0) {
143 simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
144 remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
145 mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
148 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
149 simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
152 case SIMCALL_COMM_WAIT:
153 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
154 remote(simcall_comm_wait__getraw__comm(req)));
155 simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
156 simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
159 case SIMCALL_COMM_TEST:
160 mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
161 remote(simcall_comm_test__getraw__comm(req)));
162 simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
163 simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
167 /* No translation needed */
174 void mc_api::initialize(char** argv)
176 simgrid::mc::session = new simgrid::mc::Session([argv] {
178 while (argv[i] != nullptr && argv[i][0] == '-')
180 xbt_assert(argv[i] != nullptr,
181 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
182 execvp(argv[i], argv + i);
183 xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
187 std::vector<simgrid::mc::ActorInformation>& mc_api::get_actors() const
189 return mc_model_checker->get_remote_simulation().actors();
192 bool mc_api::actor_is_enabled(aid_t pid) const
194 return session->actor_is_enabled(pid);
197 unsigned long mc_api::get_maxpid() const
199 return MC_smx_get_maxpid();
202 void mc_api::copy_incomplete_comm_pattern(const simgrid::mc::State* state) const
204 MC_state_copy_incomplete_communications_pattern((simgrid::mc::State*)state);
207 void mc_api::copy_index_comm_pattern(const simgrid::mc::State* state) const
209 MC_state_copy_index_communications_pattern((simgrid::mc::State*)state);
212 kernel::activity::CommImpl* mc_api::get_pattern_comm_addr(smx_simcall_t request) const
214 auto comm_addr = simcall_comm_isend__getraw__result(request);
215 return static_cast<kernel::activity::CommImpl*>(comm_addr);
217 std::string mc_api::get_pattern_comm_rdv(void* addr) const
219 Remote<kernel::activity::CommImpl> temp_synchro;
220 mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
221 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
223 char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
224 (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
225 auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
229 unsigned long mc_api::get_pattern_comm_src_proc(void* addr) const
231 Remote<kernel::activity::CommImpl> temp_synchro;
232 mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
233 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
234 auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
238 std::vector<char> mc_api::get_pattern_comm_data(void* addr) const
240 Remote<kernel::activity::CommImpl> temp_synchro;
241 mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
242 const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
244 std::vector<char> buffer {};
245 if (synchro->src_buff_ != nullptr) {
246 buffer.resize(synchro->src_buff_size_);
247 mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
248 remote(synchro->src_buff_));
253 const char* mc_api::get_actor_host_name(smx_actor_t actor) const
255 const char* host_name = MC_smx_actor_get_host_name(actor);
259 std::size_t mc_api::get_remote_heap_bytes() const
261 RemoteSimulation& process = mc_model_checker->get_remote_simulation();
262 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
263 return heap_bytes_used;
266 void mc_api::s_initialize() const
268 session->initialize();
271 ModelChecker* mc_api::get_model_checker() const
273 return mc_model_checker;
276 void mc_api::mc_inc_visited_states() const
278 mc_model_checker->visited_states++;
281 void mc_api::mc_inc_executed_trans() const
283 mc_model_checker->executed_transitions++;
286 unsigned long mc_api::mc_get_visited_states() const
288 return mc_model_checker->visited_states;
291 unsigned long mc_api::mc_get_executed_trans() const
293 return mc_model_checker->executed_transitions;
296 bool mc_api::mc_check_deadlock() const
298 return mc_model_checker->checkDeadlock();
301 void mc_api::mc_show_deadlock() const
306 smx_actor_t mc_api::mc_smx_simcall_get_issuer(s_smx_simcall const* req) const
308 return MC_smx_simcall_get_issuer(req);
311 bool mc_api::mc_is_null() const
313 auto is_null = (mc_model_checker == nullptr) ? true : false;
317 Checker* mc_api::mc_get_checker() const
319 return mc_model_checker->getChecker();
322 RemoteSimulation& mc_api::mc_get_remote_simulation() const
324 return mc_model_checker->get_remote_simulation();
327 void mc_api::handle_simcall(Transition const& transition) const
329 mc_model_checker->handle_simcall(transition);
332 void mc_api::mc_wait_for_requests() const
334 mc_model_checker->wait_for_requests();
337 void mc_api::mc_exit(int status) const
339 mc_model_checker->exit(status);
342 std::string const& mc_api::mc_get_host_name(std::string const& hostname) const
344 return mc_model_checker->get_host_name(hostname);
347 void mc_api::mc_dump_record_path() const
349 simgrid::mc::dumpRecordPath();
352 smx_simcall_t mc_api::mc_state_choose_request(simgrid::mc::State* state) const
354 for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
355 /* Only consider the actors that were marked as interleaving by the checker algorithm */
356 if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
359 smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
366 bool mc_api::request_depend(smx_simcall_t req1, smx_simcall_t req2) const
368 return simgrid::mc::request_depend(req1, req2);
371 std::string mc_api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
373 return simgrid::mc::request_to_string(req, value, request_type).c_str();
376 std::string mc_api::request_get_dot_output(smx_simcall_t req, int value) const
378 return simgrid::mc::request_get_dot_output(req, value);
381 const char* mc_api::simix_simcall_name(e_smx_simcall_t kind) const
383 return SIMIX_simcall_name(kind);
386 bool mc_api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
388 return simgrid::mc::snapshot_equal(s1, s2);
391 simgrid::mc::Snapshot* mc_api::take_snapshot(int num_state) const
393 auto snapshot = new simgrid::mc::Snapshot(num_state);
397 void mc_api::s_close() const
402 void mc_api::s_restore_initial_state() const
404 session->restore_initial_state();
407 void mc_api::execute(Transition const& transition)
409 session->execute(transition);
412 void mc_api::s_log_state() const
414 session->log_state();
418 } // namespace simgrid