Logo AND Algorithmique Numérique Distribuée

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