Logo AND Algorithmique Numérique Distribuée

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