Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no need to comm_addr_equal()
[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 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
326 {
327   auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
328   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
329 }
330
331 RemotePtr<kernel::activity::CommImpl> Api::get_comm_wait_raw_addr(smx_simcall_t request) const
332 {
333   auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl*>(request->args_[0]);
334   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
335 }
336
337 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
338 {
339   auto addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl**>(request->args_[0]) + value;
340   auto comm_addr = mc_model_checker->get_remote_simulation().read(remote(addr));
341   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
342 }
343
344 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
345 {
346   Remote<kernel::activity::CommImpl> temp_synchro;
347   mc_model_checker->get_remote_simulation().read(temp_synchro, remote(addr.local()));
348   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
349
350   char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
351       (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
352   auto rdv          = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
353   return rdv;
354 }
355
356 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
357 {
358   Remote<kernel::activity::CommImpl> temp_synchro;
359   mc_model_checker->get_remote_simulation().read(temp_synchro, remote(addr.local()));
360   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
361   auto src_proc =
362       mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
363   return src_proc;
364 }
365
366 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
367 {
368   Remote<kernel::activity::CommImpl> temp_synchro;
369   mc_model_checker->get_remote_simulation().read(temp_synchro, remote(addr.local()));
370   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
371   auto src_proc =
372       mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
373   return src_proc;
374 }
375
376 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
377 {
378   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
379   mc_model_checker->get_remote_simulation().read(temp_comm, remote((addr.local())));
380   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
381
382   std::vector<char> buffer{};
383   if (comm->src_buff_ != nullptr) {
384     buffer.resize(comm->src_buff_size_);
385     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
386   }
387   return buffer;
388 }
389
390 const char* Api::get_actor_host_name(smx_actor_t actor) const
391 {
392   const char* host_name = MC_smx_actor_get_host_name(actor);
393   return host_name;
394 }
395
396 #if HAVE_SMPI
397 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
398 {
399   simgrid::smpi::Request mpi_request;
400   mc_model_checker->get_remote_simulation().read(
401       &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
402   return mpi_request.detached();
403 }
404 #endif
405
406 smx_actor_t Api::get_src_actor(const kernel::activity::CommImpl* comm_addr) const
407 {
408   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
409   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
410   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
411
412   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
413   return src_proc;
414 }
415
416 smx_actor_t Api::get_dst_actor(const kernel::activity::CommImpl* comm_addr) const
417 {
418   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
419   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
420   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
421
422   auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
423   return dst_proc;
424 }
425
426 std::size_t Api::get_remote_heap_bytes() const
427 {
428   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
429   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
430   return heap_bytes_used;
431 }
432
433 void Api::session_initialize() const
434 {
435   session->initialize();
436 }
437
438 void Api::mc_inc_visited_states() const
439 {
440   mc_model_checker->visited_states++;
441 }
442
443 void Api::mc_inc_executed_trans() const
444 {
445   mc_model_checker->executed_transitions++;
446 }
447
448 unsigned long Api::mc_get_visited_states() const
449 {
450   return mc_model_checker->visited_states;
451 }
452
453 unsigned long Api::mc_get_executed_trans() const
454 {
455   return mc_model_checker->executed_transitions;
456 }
457
458 bool Api::mc_check_deadlock() const
459 {
460   return mc_model_checker->checkDeadlock();
461 }
462
463 void Api::mc_show_deadlock() const
464 {
465   MC_show_deadlock();
466 }
467
468 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
469 {
470   return MC_smx_simcall_get_issuer(req);
471 }
472
473 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
474 {
475   return MC_smx_simcall_get_issuer(req)->get_pid();
476 }
477
478 smx_mailbox_t Api::simcall_get_mbox(smx_simcall_t const req) const
479 {
480   return get_mbox(req);
481 }
482
483 bool Api::mc_is_null() const
484 {
485   auto is_null = (mc_model_checker == nullptr) ? true : false;
486   return is_null;
487 }
488
489 Checker* Api::mc_get_checker() const
490 {
491   return mc_model_checker->getChecker();
492 }
493
494 void Api::set_checker(Checker* const checker) const
495 {
496   xbt_assert(mc_model_checker);
497   xbt_assert(mc_model_checker->getChecker() == nullptr);
498   mc_model_checker->setChecker(checker);
499 }
500
501 void Api::handle_simcall(Transition const& transition) const
502 {
503   mc_model_checker->handle_simcall(transition);
504 }
505
506 void Api::mc_wait_for_requests() const
507 {
508   mc_model_checker->wait_for_requests();
509 }
510
511 void Api::mc_exit(int status) const
512 {
513   mc_model_checker->exit(status);
514 }
515
516 std::string const& Api::mc_get_host_name(std::string const& hostname) const
517 {
518   return mc_model_checker->get_host_name(hostname);
519 }
520
521 void Api::dump_record_path() const
522 {
523   simgrid::mc::dumpRecordPath();
524 }
525
526 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
527 {
528   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
529     /* Only consider the actors that were marked as interleaving by the checker algorithm */
530     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
531       continue;
532
533     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
534     if (res)
535       return res;
536   }
537   return nullptr;
538 }
539
540 bool Api::simcall_check_dependency(smx_simcall_t const req1, smx_simcall_t const req2) const
541 {
542   if (req1->issuer_ == req2->issuer_)
543     return false;
544
545   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
546   if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
547       (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
548     return true;
549
550   if (req1->call_ != req2->call_)
551     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
552
553   // Those are internal requests, we do not need indirection because those objects are copies:
554   const kernel::activity::CommImpl* synchro1 = get_comm(req1);
555   const kernel::activity::CommImpl* synchro2 = get_comm(req2);
556
557   switch (req1->call_) {
558     case Simcall::COMM_ISEND:
559       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
560     case Simcall::COMM_IRECV:
561       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
562     case Simcall::COMM_WAIT:
563       if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
564         return false;
565       if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
566           synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
567           synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
568         return false;
569       return true;
570     default:
571       return true;
572   }
573 }
574
575 std::string Api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
576 {
577   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
578
579   if (req->inspector_ != nullptr)
580     return req->inspector_->to_string();
581
582   bool use_remote_comm = true;
583   switch (request_type) {
584     case simgrid::mc::RequestType::simix:
585       use_remote_comm = true;
586       break;
587     case simgrid::mc::RequestType::executed:
588     case simgrid::mc::RequestType::internal:
589       use_remote_comm = false;
590       break;
591     default:
592       THROW_IMPOSSIBLE;
593   }
594
595   const char* type = nullptr;
596   char* args       = nullptr;
597
598   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
599
600   switch (req->call_) {
601     case Simcall::COMM_ISEND: {
602       type     = "iSend";
603       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
604       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
605       if (issuer->get_host())
606         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
607                        MC_smx_actor_get_name(issuer), p, bs);
608       else
609         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
610       xbt_free(bs);
611       xbt_free(p);
612       break;
613     }
614
615     case Simcall::COMM_IRECV: {
616       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
617       size_t size         = 0;
618       if (remote_size)
619         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
620
621       type     = "iRecv";
622       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
623       char* bs = buff_size_to_string(size);
624       if (issuer->get_host())
625         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
626                        MC_smx_actor_get_name(issuer), p, bs);
627       else
628         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
629       xbt_free(bs);
630       xbt_free(p);
631       break;
632     }
633
634     case Simcall::COMM_WAIT: {
635       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
636       char* p;
637       if (value == -1) {
638         type = "WaitTimeout";
639         p    = pointer_to_string(remote_act);
640         args = bprintf("comm=%s", p);
641       } else {
642         type = "Wait";
643         p    = pointer_to_string(remote_act);
644
645         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
646         const simgrid::kernel::activity::CommImpl* act;
647         if (use_remote_comm) {
648           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
649           act = temp_synchro.get_buffer();
650         } else
651           act = remote_act;
652
653         smx_actor_t src_proc =
654             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
655         smx_actor_t dst_proc =
656             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
657         args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
658                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
659                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
660                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
661                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
662       }
663       xbt_free(p);
664       break;
665     }
666
667     case Simcall::COMM_TEST: {
668       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
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       char* p;
678       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
679         type = "Test FALSE";
680         p    = pointer_to_string(remote_act);
681         args = bprintf("comm=%s", p);
682       } else {
683         type = "Test TRUE";
684         p    = pointer_to_string(remote_act);
685
686         smx_actor_t src_proc =
687             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
688         smx_actor_t dst_proc =
689             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
690         args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
691                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
692                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
693       }
694       xbt_free(p);
695       break;
696     }
697
698     case Simcall::COMM_WAITANY: {
699       type         = "WaitAny";
700       size_t count = simcall_comm_waitany__get__count(req);
701       if (count > 0) {
702         simgrid::kernel::activity::CommImpl* remote_sync;
703         remote_sync =
704             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
705         char* p = pointer_to_string(remote_sync);
706         args    = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
707         xbt_free(p);
708       } else
709         args = bprintf("comm at idx %d", value);
710       break;
711     }
712
713     case Simcall::COMM_TESTANY:
714       if (value == -1) {
715         type = "TestAny FALSE";
716         args = xbt_strdup("-");
717       } else {
718         type = "TestAny";
719         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
720       }
721       break;
722
723     case Simcall::MUTEX_TRYLOCK:
724     case Simcall::MUTEX_LOCK: {
725       if (req->call_ == Simcall::MUTEX_LOCK)
726         type = "Mutex LOCK";
727       else
728         type = "Mutex TRYLOCK";
729
730       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
731       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
732                                                            remote(req->call_ == Simcall::MUTEX_LOCK
733                                                                       ? simcall_mutex_lock__get__mutex(req)
734                                                                       : simcall_mutex_trylock__get__mutex(req)));
735       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
736                      mutex.get_buffer()->get_owner() != nullptr
737                          ? (int)mc_model_checker->get_remote_simulation()
738                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
739                                ->get_pid()
740                          : -1);
741       break;
742     }
743
744     case Simcall::MC_RANDOM:
745       type = "MC_RANDOM";
746       args = bprintf("%d", value);
747       break;
748
749     default:
750       type = SIMIX_simcall_name(req->call_);
751       args = bprintf("??");
752       break;
753   }
754
755   std::string str;
756   if (args != nullptr)
757     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
758                                       MC_smx_actor_get_name(issuer), type, args);
759   else
760     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
761                                       MC_smx_actor_get_name(issuer), type);
762   xbt_free(args);
763   return str;
764 }
765
766 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
767 {
768   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
769   const char* color        = get_color(issuer->get_pid() - 1);
770
771   if (req->inspector_ != nullptr)
772     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
773                                        req->inspector_->dot_label().c_str(), color, color);
774
775   std::string label;
776
777   switch (req->call_) {
778     case Simcall::COMM_ISEND:
779       if (issuer->get_host())
780         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
781       else
782         label = bprintf("[(%ld)] iSend", issuer->get_pid());
783       break;
784
785     case Simcall::COMM_IRECV:
786       if (issuer->get_host())
787         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
788       else
789         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
790       break;
791
792     case Simcall::COMM_WAIT:
793       if (value == -1) {
794         if (issuer->get_host())
795           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
796         else
797           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
798       } else {
799         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
800         Remote<kernel::activity::CommImpl> temp_comm;
801         mc_model_checker->get_remote_simulation().read(temp_comm,
802                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
803         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
804
805         const kernel::actor::ActorImpl* src_proc =
806             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
807         const kernel::actor::ActorImpl* dst_proc =
808             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
809         if (issuer->get_host())
810           label =
811               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
812                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
813         else
814           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
815                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
816       }
817       break;
818
819     case Simcall::COMM_TEST: {
820       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
821       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
822       mc_model_checker->get_remote_simulation().read(temp_comm,
823                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
824       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
825       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
826         if (issuer->get_host())
827           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
828         else
829           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
830       } else {
831         if (issuer->get_host())
832           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
833         else
834           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
835       }
836       break;
837     }
838
839     case Simcall::COMM_WAITANY: {
840       size_t comms_size = simcall_comm_waitany__get__count(req);
841       if (issuer->get_host())
842         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
843                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
844       else
845         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
846       break;
847     }
848
849     case Simcall::COMM_TESTANY:
850       if (value == -1) {
851         if (issuer->get_host())
852           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
853         else
854           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
855       } else {
856         if (issuer->get_host())
857           label =
858               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
859                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
860         else
861           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
862                                      simcall_comm_testany__get__count(req));
863       }
864       break;
865
866     case Simcall::MUTEX_TRYLOCK:
867       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
868       break;
869
870     case Simcall::MUTEX_LOCK:
871       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
872       break;
873
874     case Simcall::MC_RANDOM:
875       if (issuer->get_host())
876         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
877                                    value);
878       else
879         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
880       break;
881
882     default:
883       THROW_UNIMPLEMENTED;
884   }
885
886   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
887 }
888
889 const char* Api::simcall_get_name(simgrid::simix::Simcall kind) const
890 {
891   return SIMIX_simcall_name(kind);
892 }
893
894 #if HAVE_SMPI
895 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
896 {
897   simgrid::smpi::Request mpi_request;
898   void* simcall_data = nullptr;
899   if (type == Simcall::COMM_ISEND)
900     simcall_data = simcall_comm_isend__get__data(simcall);
901   else if (type == Simcall::COMM_IRECV)
902     simcall_data = simcall_comm_irecv__get__data(simcall);
903   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
904   return mpi_request.tag();
905 }
906 #endif
907
908 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
909 {
910   system_state->restore(&mc_model_checker->get_remote_simulation());
911 }
912
913 void Api::log_state() const
914 {
915   session->log_state();
916 }
917
918 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
919 {
920   return simgrid::mc::snapshot_equal(s1, s2);
921 }
922
923 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
924 {
925   auto snapshot = new simgrid::mc::Snapshot(num_state);
926   return snapshot;
927 }
928
929 void Api::s_close() const
930 {
931   session->close();
932 }
933
934 void Api::restore_initial_state() const
935 {
936   session->restore_initial_state();
937 }
938
939 void Api::execute(Transition const& transition) const
940 {
941   session->execute(transition);
942 }
943
944 #if SIMGRID_HAVE_MC
945 void Api::automaton_load(const char* file) const
946 {
947   MC_automaton_load(file);
948 }
949 #endif
950
951 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
952 {
953   unsigned int cursor = 0;
954   std::vector<int> values;
955   xbt_automaton_propositional_symbol_t ps = nullptr;
956   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
957     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
958   return values;
959 }
960
961 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
962 {
963   std::vector<xbt_automaton_state_t> automaton_stack;
964   unsigned int cursor = 0;
965   xbt_automaton_state_t automaton_state;
966   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
967     if (automaton_state->type == -1)
968       automaton_stack.push_back(automaton_state);
969   return automaton_stack;
970 }
971
972 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
973 {
974   unsigned int cursor                    = 0;
975   xbt_automaton_propositional_symbol_t p = nullptr;
976   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
977     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
978       return cursor;
979   }
980   return -1;
981 }
982
983 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
984 {
985   mc::property_automaton->current_state = automaton_state;
986 }
987
988 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
989 {
990   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
991   return transition->label;
992 }
993
994 xbt_automaton_state_t Api::get_automaton_transition_dst(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->dst;
998 }
999
1000 } // namespace mc
1001 } // namespace simgrid