Logo AND Algorithmique Numérique Distribuée

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