Logo AND Algorithmique Numérique Distribuée

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