Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill some use of the mc_model_checker global variable
[simgrid.git] / src / mc / api.cpp
1 #include "api.hpp"
2
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/Checker.hpp"
7 #include "src/mc/checker/SimcallObserver.hpp"
8 #include "src/mc/mc_comm_pattern.hpp"
9 #include "src/mc/mc_exit.hpp"
10 #include "src/mc/mc_pattern.hpp"
11 #include "src/mc/mc_private.hpp"
12 #include "src/mc/remote/RemoteProcess.hpp"
13
14 #include <xbt/asserts.h>
15 #include <xbt/log.h>
16 #include "simgrid/s4u/Host.hpp"
17 #include "xbt/string.hpp"
18 #if HAVE_SMPI
19 #include "src/smpi/include/smpi_request.hpp"
20 #endif
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
23
24 using Simcall = simgrid::simix::Simcall;
25
26 namespace simgrid {
27 namespace mc {
28
29 static inline const char* get_color(int id)
30 {
31   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
32                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
33                                                        "lightblue", "tan"}};
34   return colors[id % colors.size()];
35 }
36
37 static std::string pointer_to_string(void* pointer)
38 {
39   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
40 }
41
42 static std::string buff_size_to_string(size_t buff_size)
43 {
44   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
45 }
46
47 static void simcall_translate(smx_simcall_t req,
48                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm);
49
50 /* Search an enabled transition for the given process.
51  *
52  * This can be seen as an iterator returning the next transition of the process.
53  *
54  * We only consider the processes that are both
55  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
56  *  - which simcall can currently be executed (like a comm where the other partner is already known)
57  * Once we returned the last enabled transition of a process, it is marked done.
58  *
59  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
60  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
61  */
62 static inline smx_simcall_t MC_state_choose_request_for_process(const RemoteProcess& process, simgrid::mc::State* state,
63                                                                 smx_actor_t actor)
64 {
65   /* reset the outgoing transition */
66   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
67   state->transition_.pid_            = -1;
68   state->transition_.times_considered_ = -1;
69   state->transition_.textual[0]        = '\0';
70   state->executed_req_.call_         = Simcall::NONE;
71
72   if (not simgrid::mc::actor_is_enabled(actor))
73     return nullptr; // Not executable in the application
74
75   smx_simcall_t req = nullptr;
76   if (actor->simcall_.observer_ != nullptr) {
77     state->transition_.times_considered_ = procstate->times_considered;
78     procstate->times_considered++;
79     if (actor->simcall_.mc_max_consider_ <= procstate->times_considered)
80       procstate->set_done();
81     req = &actor->simcall_;
82   } else
83     switch (actor->simcall_.call_) {
84       case Simcall::COMM_WAITANY:
85         state->transition_.times_considered_ = -1;
86         while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
87           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->times_considered)) {
88             state->transition_.times_considered_ = procstate->times_considered;
89             ++procstate->times_considered;
90             break;
91           }
92           ++procstate->times_considered;
93         }
94
95         if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
96           procstate->set_done();
97         if (state->transition_.times_considered_ != -1)
98           req = &actor->simcall_;
99         break;
100
101       case Simcall::COMM_TESTANY:
102         state->transition_.times_considered_ = -1;
103         while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
104           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->times_considered)) {
105             state->transition_.times_considered_ = procstate->times_considered;
106             ++procstate->times_considered;
107             break;
108           }
109           ++procstate->times_considered;
110         }
111
112         if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
113           procstate->set_done();
114         if (state->transition_.times_considered_ != -1)
115           req = &actor->simcall_;
116         break;
117
118       case Simcall::COMM_WAIT: {
119         simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
120             remote(simcall_comm_wait__get__comm(&actor->simcall_));
121         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
122         process.read(temp_act, remote_act);
123         const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
124         if (act->src_actor_.get() && act->dst_actor_.get())
125           state->transition_.times_considered_ = 0; // OK
126         else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
127                  act->detached())
128           state->transition_.times_considered_ = 0; // OK
129         else
130           state->transition_.times_considered_ = -1; // timeout
131         procstate->set_done();
132         req = &actor->simcall_;
133         break;
134       }
135
136       default:
137         procstate->set_done();
138         state->transition_.times_considered_ = 0;
139         req                                  = &actor->simcall_;
140         break;
141     }
142   if (not req)
143     return nullptr;
144
145   state->transition_.pid_ = actor->get_pid();
146   state->executed_req_    = *req;
147
148   // Fetch the data of the request and translate it:
149   state->internal_req_ = *req;
150   state->internal_req_.mc_value_ = state->transition_.times_considered_;
151   simcall_translate(&state->internal_req_, state->internal_comm_);
152
153   return req;
154 }
155
156 static void simcall_translate(smx_simcall_t req,
157                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm)
158 {
159   simgrid::kernel::activity::CommImpl* chosen_comm;
160
161   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
162    * action so it can be treated later by the dependence function. */
163   switch (req->call_) {
164     case Simcall::COMM_WAITANY:
165       req->call_  = Simcall::COMM_WAIT;
166       chosen_comm =
167           mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + req->mc_value_));
168
169       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
170       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
171       simcall_comm_wait__set__timeout(req, 0);
172       break;
173
174     case Simcall::COMM_TESTANY:
175       req->call_  = Simcall::COMM_TEST;
176       chosen_comm =
177           mc_model_checker->get_remote_process().read(remote(simcall_comm_testany__get__comms(req) + req->mc_value_));
178
179       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
180       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
181       simcall_comm_test__set__result(req, req->mc_value_);
182       break;
183
184     case Simcall::COMM_WAIT:
185       chosen_comm = simcall_comm_wait__get__comm(req);
186       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
187       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
188       break;
189
190     case Simcall::COMM_TEST:
191       chosen_comm = simcall_comm_test__get__comm(req);
192       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
193       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
194       break;
195
196     default:
197       /* No translation needed */
198       break;
199   }
200 }
201
202 simgrid::kernel::activity::CommImpl* Api::get_comm_or_nullptr(smx_simcall_t const r) const
203 {
204   if (r->call_ == Simcall::COMM_WAIT)
205     return simcall_comm_wait__get__comm(r);
206   if (r->call_ == Simcall::COMM_TEST)
207     return simcall_comm_test__get__comm(r);
208   return nullptr;
209 }
210
211 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
212  *
213  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
214  *  sense that we could achieve the same thing by having ActorInformation
215  *  inherit from s_smx_actor_t but we don't really want to do that.
216  */
217 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
218 {
219   simgrid::mc::ActorInformation temp;
220   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
221
222   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
223   return process_info;
224 }
225
226 bool Api::simcall_check_dependency(smx_simcall_t req1, smx_simcall_t req2) const
227 {
228   const auto IRECV = Simcall::COMM_IRECV;
229   const auto ISEND = Simcall::COMM_ISEND;
230   const auto TEST  = Simcall::COMM_TEST;
231   const auto WAIT  = Simcall::COMM_WAIT;
232
233   if (req1->issuer_ == req2->issuer_)
234     return false;
235
236   /* The independence theorem only consider 4 simcalls. All others are dependent with anything. */
237   if (req1->call_ != ISEND && req1->call_ != IRECV && req1->call_ != TEST && req1->call_ != WAIT)
238     return true;
239   if (req2->call_ != ISEND && req2->call_ != IRECV && req2->call_ != TEST && req2->call_ != WAIT)
240     return true;
241
242   /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
243   if ((req1->call_ == WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
244       (req2->call_ == WAIT && simcall_comm_wait__get__timeout(req2) > 0))
245     return true;
246
247   /* Make sure that req1 and req2 are in alphabetic order */
248   if (req1->call_ > req2->call_) {
249     auto temp = req1;
250     req1      = req2;
251     req2      = temp;
252   }
253
254   auto comm1 = get_comm_or_nullptr(req1);
255   auto comm2 = get_comm_or_nullptr(req2);
256
257   /* First case: that's not the same kind of request (we also know that req1 < req2 alphabetically) */
258   if (req1->call_ != req2->call_) {
259     if (req1->call_ == IRECV && req2->call_ == ISEND)
260       return false;
261
262     if ((req1->call_ == IRECV || req1->call_ == ISEND) && req2->call_ == WAIT) {
263       auto mbox1 = get_mbox_remote_addr(req1);
264       auto mbox2 = remote(comm2->mbox_cpy);
265
266       if (mbox1 != mbox2 && simcall_comm_wait__get__timeout(req2) <= 0)
267         return false;
268
269       if ((req1->issuer_ != comm2->src_actor_.get()) && (req1->issuer_ != comm2->dst_actor_.get()) &&
270           simcall_comm_wait__get__timeout(req2) <= 0)
271         return false;
272
273       if ((req1->call_ == ISEND) && (comm2->type_ == kernel::activity::CommImpl::Type::SEND) &&
274           (comm2->src_buff_ != simcall_comm_isend__get__src_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
275         return false;
276
277       if ((req1->call_ == IRECV) && (comm2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
278           (comm2->dst_buff_ != simcall_comm_irecv__get__dst_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
279         return false;
280     }
281
282     /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
283      * test call. */
284 #if 0
285   if((req1->call == ISEND || req1->call == IRECV)
286       &&  req2->call == TEST)
287     return false;
288 #endif
289
290     if (req1->call_ == TEST && req2->call_ == WAIT &&
291         (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
292       return false;
293
294     if (req1->call_ == TEST &&
295         (simcall_comm_test__get__comm(req1) == nullptr || comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr))
296       return false;
297     if (req2->call_ == TEST &&
298         (simcall_comm_test__get__comm(req2) == nullptr || comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr))
299       return false;
300
301     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ == comm2->src_buff_ &&
302         comm1->dst_buff_ == comm2->dst_buff_)
303       return false;
304
305     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr &&
306         comm2->src_buff_ != nullptr && comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ &&
307         comm1->dst_buff_ != comm2->dst_buff_ && comm2->dst_buff_ != comm1->src_buff_)
308       return false;
309
310     return true;
311   }
312
313   /* Second case: req1 and req2 are of the same call type */
314   switch (req1->call_) {
315     case ISEND:
316       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
317     case IRECV:
318       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
319     case WAIT:
320       if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
321         return false;
322       if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
323           comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
324           comm2->dst_buff_ != comm1->src_buff_)
325         return false;
326       return true;
327     default:
328       return true;
329   }
330 }
331
332 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
333 {
334   if (mc_model_checker == nullptr)
335     return actor->get_host()->get_name();
336
337   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
338
339   // Read the simgrid::xbt::string in the MCed process:
340   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
341   auto remote_string_address =
342       remote(reinterpret_cast<const simgrid::xbt::string_data*>(&actor->get_host()->get_name()));
343   simgrid::xbt::string_data remote_string = process->read(remote_string_address);
344   std::vector<char> hostname(remote_string.len + 1);
345   // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
346   process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
347   info->hostname = &mc_model_checker->get_host_name(hostname.data());
348   return *info->hostname;
349 }
350
351 std::string Api::get_actor_name(smx_actor_t actor) const
352 {
353   if (mc_model_checker == nullptr)
354     return actor->get_cname();
355
356   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
357   if (info->name.empty()) {
358     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
359
360     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
361     info->name = process->read_string(remote(string_data.data), string_data.len);
362   }
363   return info->name;
364 }
365
366 std::string Api::get_actor_string(smx_actor_t actor) const
367 {
368   std::string res;
369   if (actor) {
370     res = "(" + std::to_string(actor->get_pid()) + ")";
371     if (actor->get_host())
372       res += std::string(get_actor_host_name(actor)) + " (" + get_actor_name(actor) + ")";
373     else
374       res += get_actor_name(actor);
375   } else
376     res = "(0) ()";
377   return res;
378 }
379
380 std::string Api::get_actor_dot_label(smx_actor_t actor) const
381 {
382   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
383   if (actor->get_host())
384     res += get_actor_host_name(actor);
385   return res;
386 }
387
388 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
389 {
390   simgrid::mc::session = new simgrid::mc::Session([argv] {
391     int i = 1;
392     while (argv[i] != nullptr && argv[i][0] == '-')
393       i++;
394     xbt_assert(argv[i] != nullptr,
395                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
396     execvp(argv[i], argv + i);
397     xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
398   });
399
400   simgrid::mc::Checker* checker;
401   switch (algo) {
402     case CheckerAlgorithm::CommDeterminism:
403       checker = simgrid::mc::createCommunicationDeterminismChecker(session);
404       break;
405
406     case CheckerAlgorithm::UDPOR:
407       checker = simgrid::mc::createUdporChecker(session);
408       break;
409
410     case CheckerAlgorithm::Safety:
411       checker = simgrid::mc::createSafetyChecker(session);
412       break;
413
414     case CheckerAlgorithm::Liveness:
415       checker = simgrid::mc::createLivenessChecker(session);
416       break;
417
418     default:
419       THROW_IMPOSSIBLE;
420   }
421
422   mc_model_checker->setChecker(checker);
423   return checker;
424 }
425
426 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
427 {
428   return mc_model_checker->get_remote_process().actors();
429 }
430
431 bool Api::actor_is_enabled(aid_t pid) const
432 {
433   return session->actor_is_enabled(pid);
434 }
435
436 unsigned long Api::get_maxpid() const
437 {
438   static const char* name = nullptr;
439   if (not name) {
440     name = "simgrid::kernel::actor::maxpid";
441     if (mc_model_checker->get_remote_process().find_variable(name) == nullptr)
442       name = "maxpid"; // We seem to miss the namespaces when compiling with GCC
443   }
444   unsigned long maxpid;
445   mc_model_checker->get_remote_process().read_variable(name, &maxpid, sizeof(maxpid));
446   return maxpid;
447 }
448
449 int Api::get_actors_size() const
450 {
451   return mc_model_checker->get_remote_process().actors().size();
452 }
453
454 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
455 {
456   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
457 }
458
459 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
460 {
461   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
462   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
463   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
464 }
465
466 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
467 {
468   Remote<kernel::activity::CommImpl> temp_activity;
469   mc_model_checker->get_remote_process().read(temp_activity, addr);
470   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
471
472   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
473       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
474   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
475   return rdv;
476 }
477
478 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
479 {
480   Remote<kernel::activity::CommImpl> temp_activity;
481   mc_model_checker->get_remote_process().read(temp_activity, addr);
482   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
483   auto src_proc =
484       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
485   return src_proc;
486 }
487
488 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
489 {
490   Remote<kernel::activity::CommImpl> temp_activity;
491   mc_model_checker->get_remote_process().read(temp_activity, addr);
492   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
493   auto src_proc =
494       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
495   return src_proc;
496 }
497
498 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
499 {
500   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
501   mc_model_checker->get_remote_process().read(temp_comm, addr);
502   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
503
504   std::vector<char> buffer{};
505   if (comm->src_buff_ != nullptr) {
506     buffer.resize(comm->src_buff_size_);
507     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
508   }
509   return buffer;
510 }
511
512 #if HAVE_SMPI
513 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
514 {
515   simgrid::smpi::Request mpi_request;
516   mc_model_checker->get_remote_process().read(
517       &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
518   return mpi_request.detached();
519 }
520 #endif
521
522 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
523 {
524   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
525   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
526   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
527
528   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
529   return src_proc;
530 }
531
532 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
533 {
534   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
535   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
536   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
537
538   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
539   return dst_proc;
540 }
541
542 std::size_t Api::get_remote_heap_bytes() const
543 {
544   RemoteProcess& process    = mc_model_checker->get_remote_process();
545   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
546   return heap_bytes_used;
547 }
548
549 void Api::session_initialize() const
550 {
551   session->initialize();
552 }
553
554 void Api::mc_inc_visited_states() const
555 {
556   mc_model_checker->visited_states++;
557 }
558
559 void Api::mc_inc_executed_trans() const
560 {
561   mc_model_checker->executed_transitions++;
562 }
563
564 unsigned long Api::mc_get_visited_states() const
565 {
566   return mc_model_checker->visited_states;
567 }
568
569 unsigned long Api::mc_get_executed_trans() const
570 {
571   return mc_model_checker->executed_transitions;
572 }
573
574 void Api::mc_check_deadlock() const
575 {
576   if (mc_model_checker->checkDeadlock()) {
577     MC_show_deadlock();
578     throw DeadlockError();
579   }
580 }
581
582 /** Get the issuer of a simcall (`req->issuer`)
583  *
584  *  In split-process mode, it does the black magic necessary to get an address
585  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
586  *  address space.
587  *
588  *  @param process the MCed process
589  *  @param req     the simcall (copied in the local process)
590  */
591 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
592 {
593   xbt_assert(mc_model_checker != nullptr);
594
595   // This is the address of the smx_actor in the MCed process:
596   auto address = simgrid::mc::remote(req->issuer_);
597
598   // Lookup by address:
599   for (auto& actor : mc_model_checker->get_remote_process().actors())
600     if (actor.address == address)
601       return actor.copy.get_buffer();
602   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
603     if (actor.address == address)
604       return actor.copy.get_buffer();
605
606   xbt_die("Issuer not found");
607 }
608
609 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
610 {
611   return simcall_get_issuer(req)->get_pid();
612 }
613
614 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
615 {
616   if (req->call_ == Simcall::COMM_ISEND)
617     return remote(simcall_comm_isend__get__mbox(req));
618   if (req->call_ == Simcall::COMM_IRECV)
619     return remote(simcall_comm_irecv__get__mbox(req));
620   THROW_IMPOSSIBLE;
621 }
622
623 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
624 {
625   if (req->call_ == Simcall::COMM_ISEND)
626     return remote(simcall_comm_isend__getraw__result(req));
627   if (req->call_ == Simcall::COMM_IRECV)
628     return remote(simcall_comm_irecv__getraw__result(req));
629   THROW_IMPOSSIBLE;
630 }
631
632 bool Api::mc_is_null() const
633 {
634   auto is_null = (mc_model_checker == nullptr) ? true : false;
635   return is_null;
636 }
637
638 Checker* Api::mc_get_checker() const
639 {
640   return mc_model_checker->getChecker();
641 }
642
643 void Api::handle_simcall(Transition const& transition) const
644 {
645   mc_model_checker->handle_simcall(transition);
646 }
647
648 void Api::mc_wait_for_requests() const
649 {
650   mc_model_checker->wait_for_requests();
651 }
652
653 void Api::mc_exit(int status) const
654 {
655   mc_model_checker->exit(status);
656 }
657
658 void Api::dump_record_path() const
659 {
660   simgrid::mc::dumpRecordPath();
661 }
662
663 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
664 {
665   RemoteProcess& process = mc_model_checker->get_remote_process();
666   for (auto& actor : process.actors()) {
667     /* Only consider the actors that were marked as interleaving by the checker algorithm */
668     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
669       continue;
670
671     smx_simcall_t res = MC_state_choose_request_for_process(process, state, actor.copy.get_buffer());
672     if (res)
673       return res;
674   }
675   return nullptr;
676 }
677
678 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state) const
679 {
680   std::list<transition_detail_t> tr_list{};
681
682   for (auto& actor : mc_model_checker->get_remote_process().actors()) {
683     auto actor_pid  = actor.copy.get_buffer()->get_pid();
684     auto actor_impl = actor.copy.get_buffer();
685
686     // Only consider the actors that were marked as interleaving by the checker algorithm
687     if (not state->actor_states_[actor_pid].is_todo())
688       continue;
689     // Not executable in the application
690     if (not simgrid::mc::actor_is_enabled(actor_impl))
691       continue;
692
693     auto transition       = std::make_unique<s_transition_detail>();
694     Simcall simcall_call  = actor_impl->simcall_.call_;
695     smx_simcall_t simcall = &actor_impl->simcall_;
696     transition->call_     = simcall_call;
697     switch (simcall_call) {
698       case Simcall::COMM_ISEND:
699       case Simcall::COMM_IRECV:
700         transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
701         transition->comm_remote_addr = get_comm_remote_addr(simcall);
702         break;
703
704       default:
705         break;
706     }
707     tr_list.emplace_back(std::move(transition));
708   }
709
710   return tr_list;
711 }
712
713 std::string Api::request_to_string(smx_simcall_t req, int value) const
714 {
715   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
716
717   std::string type;
718   std::string args;
719
720   smx_actor_t issuer = simcall_get_issuer(req);
721
722   if (issuer->simcall_.observer_ != nullptr)
723     return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
724
725   switch (req->call_) {
726     case Simcall::COMM_ISEND:
727       type = "iSend";
728       args = "src=" + get_actor_string(issuer);
729       args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
730       args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
731       break;
732
733     case Simcall::COMM_IRECV: {
734       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
735       size_t size         = 0;
736       if (remote_size)
737         mc_model_checker->get_remote_process().read_bytes(&size, sizeof(size), remote(remote_size));
738
739       type = "iRecv";
740       args = "dst=" + get_actor_string(issuer);
741       args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
742       args += ", size=" + buff_size_to_string(size);
743       break;
744     }
745
746     case Simcall::COMM_WAIT: {
747       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__get__comm(req);
748       if (value == -1) {
749         type = "WaitTimeout";
750         args = "comm=" + pointer_to_string(remote_act);
751       } else {
752         type = "Wait";
753
754         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
755         const simgrid::kernel::activity::CommImpl* act;
756         mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
757         act = temp_activity.get_buffer();
758
759         smx_actor_t src_proc =
760             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
761         smx_actor_t dst_proc =
762             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
763         args = "comm=" + pointer_to_string(remote_act);
764         args += " [" + get_actor_string(src_proc) + "-> " + get_actor_string(dst_proc) + "]";
765       }
766       break;
767     }
768
769     case Simcall::COMM_TEST: {
770       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__get__comm(req);
771       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
772       const simgrid::kernel::activity::CommImpl* act;
773       mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
774       act = temp_activity.get_buffer();
775
776       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
777         type = "Test FALSE";
778         args = "comm=" + pointer_to_string(remote_act);
779       } else {
780         type = "Test TRUE";
781
782         smx_actor_t src_proc =
783             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
784         smx_actor_t dst_proc =
785             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
786         args = "comm=" + pointer_to_string(remote_act);
787         args += " [" + get_actor_string(src_proc) + " -> " + get_actor_string(dst_proc) + "]";
788       }
789       break;
790     }
791
792     case Simcall::COMM_WAITANY: {
793       type         = "WaitAny";
794       size_t count = simcall_comm_waitany__get__count(req);
795       if (count > 0) {
796         simgrid::kernel::activity::CommImpl* remote_sync;
797         remote_sync =
798             mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + value));
799         args = "comm=" + pointer_to_string(remote_sync) + xbt::string_printf("(%d of %zu)", value + 1, count);
800       } else
801         args = "comm at idx " + std::to_string(value);
802       break;
803     }
804
805     case Simcall::COMM_TESTANY:
806       if (value == -1) {
807         type = "TestAny FALSE";
808         args = "-";
809       } else {
810         type = "TestAny";
811         args = xbt::string_printf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
812       }
813       break;
814
815     default:
816       type = SIMIX_simcall_name(req->call_);
817       args = "??";
818       break;
819   }
820
821   return "[" + get_actor_string(issuer) + "] " + type + "(" + args + ")";
822 }
823
824 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
825 {
826   const smx_actor_t issuer = simcall_get_issuer(req);
827   const char* color        = get_color(issuer->get_pid() - 1);
828
829   std::string label;
830
831   if (req->observer_ != nullptr) {
832     label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
833   } else
834     switch (req->call_) {
835       case Simcall::COMM_ISEND:
836         label = "[" + get_actor_dot_label(issuer) + "] iSend";
837         break;
838
839       case Simcall::COMM_IRECV:
840         label = "[" + get_actor_dot_label(issuer) + "] iRecv";
841         break;
842
843       case Simcall::COMM_WAIT:
844         if (value == -1) {
845           label = "[" + get_actor_dot_label(issuer) + "] WaitTimeout";
846         } else {
847           kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__get__comm(req);
848           Remote<kernel::activity::CommImpl> temp_comm;
849           mc_model_checker->get_remote_process().read(temp_comm,
850                                                       remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
851           const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
852
853           const kernel::actor::ActorImpl* src_proc =
854               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->src_actor_.get()));
855           const kernel::actor::ActorImpl* dst_proc =
856               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->dst_actor_.get()));
857           label = "[" + get_actor_dot_label(issuer) + "] Wait";
858           label += " [(" + std::to_string(src_proc ? src_proc->get_pid() : 0) + ")";
859           label += "->(" + std::to_string(dst_proc ? dst_proc->get_pid() : 0) + ")]";
860         }
861         break;
862
863       case Simcall::COMM_TEST: {
864         kernel::activity::ActivityImpl* remote_act = simcall_comm_test__get__comm(req);
865         Remote<simgrid::kernel::activity::CommImpl> temp_comm;
866         mc_model_checker->get_remote_process().read(temp_comm,
867                                                     remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
868         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
869         if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
870           label = "[" + get_actor_dot_label(issuer) + "] Test FALSE";
871         } else {
872           label = "[" + get_actor_dot_label(issuer) + "] Test TRUE";
873         }
874         break;
875       }
876
877       case Simcall::COMM_WAITANY:
878         label = "[" + get_actor_dot_label(issuer) + "] WaitAny";
879         label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_waitany__get__count(req));
880         break;
881
882       case Simcall::COMM_TESTANY:
883         if (value == -1) {
884           label = "[" + get_actor_dot_label(issuer) + "] TestAny FALSE";
885         } else {
886           label = "[" + get_actor_dot_label(issuer) + "] TestAny TRUE";
887           label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_testany__get__count(req));
888         }
889         break;
890
891       default:
892         THROW_UNIMPLEMENTED;
893     }
894
895   return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
896 }
897
898 #if HAVE_SMPI
899 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
900 {
901   simgrid::smpi::Request mpi_request;
902   void* simcall_data = nullptr;
903   if (type == Simcall::COMM_ISEND)
904     simcall_data = simcall_comm_isend__get__data(simcall);
905   else if (type == Simcall::COMM_IRECV)
906     simcall_data = simcall_comm_irecv__get__data(simcall);
907   mc_model_checker->get_remote_process().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
908   return mpi_request.tag();
909 }
910 #endif
911
912 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
913 {
914   system_state->restore(&mc_model_checker->get_remote_process());
915 }
916
917 void Api::log_state() const
918 {
919   session->log_state();
920 }
921
922 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
923 {
924   return simgrid::mc::snapshot_equal(s1, s2);
925 }
926
927 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
928 {
929   auto snapshot = new simgrid::mc::Snapshot(num_state);
930   return snapshot;
931 }
932
933 void Api::s_close() const
934 {
935   session->close();
936 }
937
938 void Api::restore_initial_state() const
939 {
940   session->restore_initial_state();
941 }
942
943 void Api::execute(Transition& transition, smx_simcall_t simcall) const
944 {
945   /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
946   transition.textual = request_to_string(simcall, transition.times_considered_);
947   session->execute(transition);
948 }
949
950 #if SIMGRID_HAVE_MC
951 void Api::automaton_load(const char* file) const
952 {
953   MC_automaton_load(file);
954 }
955 #endif
956
957 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
958 {
959   unsigned int cursor = 0;
960   std::vector<int> values;
961   xbt_automaton_propositional_symbol_t ps = nullptr;
962   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
963     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
964   return values;
965 }
966
967 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
968 {
969   std::vector<xbt_automaton_state_t> automaton_stack;
970   unsigned int cursor = 0;
971   xbt_automaton_state_t automaton_state;
972   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
973     if (automaton_state->type == -1)
974       automaton_stack.push_back(automaton_state);
975   return automaton_stack;
976 }
977
978 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
979 {
980   unsigned int cursor                    = 0;
981   xbt_automaton_propositional_symbol_t p = nullptr;
982   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
983     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
984       return cursor;
985   }
986   return -1;
987 }
988
989 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
990 {
991   mc::property_automaton->current_state = automaton_state;
992 }
993
994 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
995 {
996   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
997   return transition->label;
998 }
999
1000 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1001 {
1002   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1003   return transition->dst;
1004 }
1005
1006 } // namespace mc
1007 } // namespace simgrid