Logo AND Algorithmique Numérique Distribuée

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