Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pull from simgrid/master and a subsequent merge with origin/master
[simgrid.git] / src / mc / mc_api.cpp
1 #include "mc_api.hpp"
2
3 #include "src/kernel/activity/MailboxImpl.hpp"
4 #include "src/mc/Session.hpp"
5 #include "src/mc/mc_comm_pattern.hpp"
6 #include "src/mc/mc_private.hpp"
7 #include "src/mc/mc_record.hpp"
8 #include "src/mc/mc_smx.hpp"
9 #include "src/mc/remote/RemoteSimulation.hpp"
10 #include "src/mc/mc_pattern.hpp"
11 #include "src/mc/checker/SimcallInspector.hpp"
12
13 #include <xbt/asserts.h>
14 #include <xbt/log.h>
15
16 #if HAVE_SMPI
17 #include "src/smpi/include/smpi_request.hpp"
18 #endif
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_api, mc, "Logging specific to MC Fasade APIs ");
21
22 using Simcall = simgrid::simix::Simcall;
23
24 namespace simgrid {
25 namespace mc {
26
27 static inline const char* get_color(int id)
28 {
29   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
30                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
31                                                        "lightblue", "tan"}};
32   return colors[id % colors.size()];
33 }  
34
35 /* Search an enabled transition for the given process.
36  *
37  * This can be seen as an iterator returning the next transition of the process.
38  *
39  * We only consider the processes that are both
40  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
41  *  - which simcall can currently be executed (like a comm where the other partner is already known)
42  * Once we returned the last enabled transition of a process, it is marked done.
43  *
44  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
45  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
46  */
47 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
48 {
49   /* reset the outgoing transition */
50   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
51   state->transition_.pid_            = -1;
52   state->transition_.argument_       = -1;
53   state->executed_req_.call_         =  Simcall::NONE;
54
55   if (not simgrid::mc::actor_is_enabled(actor))
56     return nullptr; // Not executable in the application
57
58   smx_simcall_t req = nullptr;
59   switch (actor->simcall_.call_) {
60     case Simcall::COMM_WAITANY:
61       state->transition_.argument_ = -1;
62       while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
63         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
64           state->transition_.argument_ = procstate->times_considered;
65           ++procstate->times_considered;
66           break;
67         }
68         ++procstate->times_considered;
69       }
70
71       if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
72         procstate->set_done();
73       if (state->transition_.argument_ != -1)
74         req = &actor->simcall_;
75       break;
76
77     case Simcall::COMM_TESTANY: {
78       unsigned start_count         = procstate->times_considered;
79       state->transition_.argument_ = -1;
80       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
81         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
82           state->transition_.argument_ = procstate->times_considered;
83           ++procstate->times_considered;
84           break;
85         }
86         ++procstate->times_considered;
87       }
88
89       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
90         procstate->set_done();
91
92       if (state->transition_.argument_ != -1 || start_count == 0)
93         req = &actor->simcall_;
94
95       break;
96     }
97
98     case Simcall::COMM_WAIT: {
99       simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
100           remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
101       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
102       mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
103       const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
104       if (act->src_actor_.get() && act->dst_actor_.get())
105         state->transition_.argument_ = 0; // OK
106       else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
107                act->detached())
108         state->transition_.argument_ = 0; // OK
109       else
110         state->transition_.argument_ = -1; // timeout
111       procstate->set_done();
112       req = &actor->simcall_;
113       break;
114     }
115
116     case Simcall::MC_RANDOM: {
117       int min_value                = simcall_mc_random__get__min(&actor->simcall_);
118       state->transition_.argument_ = procstate->times_considered + min_value;
119       procstate->times_considered++;
120       if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
121         procstate->set_done();
122       req = &actor->simcall_;
123       break;
124     }
125
126     default:
127       procstate->set_done();
128       state->transition_.argument_ = 0;
129       req                          = &actor->simcall_;
130       break;
131   }
132   if (not req)
133     return nullptr;
134
135   state->transition_.pid_ = actor->get_pid();
136   state->executed_req_    = *req;
137   // Fetch the data of the request and translate it:
138   state->internal_req_ = *req;
139
140   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
141    * action so it can be treated later by the dependence function. */
142   switch (req->call_) {
143     case Simcall::COMM_WAITANY: {
144       state->internal_req_.call_ = Simcall::COMM_WAIT;
145       simgrid::kernel::activity::CommImpl* remote_comm;
146       remote_comm = mc_model_checker->get_remote_simulation().read(
147           remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
148       mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
149       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
150       simcall_comm_wait__set__timeout(&state->internal_req_, 0);
151       break;
152     }
153
154     case Simcall::COMM_TESTANY:
155       state->internal_req_.call_ = Simcall::COMM_TEST;
156
157       if (state->transition_.argument_ > 0) {
158         simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
159             remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
160         mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
161       }
162
163       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
164       simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
165       break;
166
167     case Simcall::COMM_WAIT:
168       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
169                                                            remote(simcall_comm_wait__getraw__comm(req)));
170       simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
171       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
172       break;
173
174     case Simcall::COMM_TEST:
175       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
176                                                            remote(simcall_comm_test__getraw__comm(req)));
177       simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
178       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
179       break;
180
181     default:
182       /* No translation needed */
183       break;
184   }
185
186   return req;
187 }
188
189 void mc_api::initialize(char** argv)
190 {
191   simgrid::mc::session = new simgrid::mc::Session([argv] {
192     int i = 1;
193     while (argv[i] != nullptr && argv[i][0] == '-')
194       i++;
195     xbt_assert(argv[i] != nullptr,
196                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
197     execvp(argv[i], argv + i);
198     xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
199   });
200 }
201
202 std::vector<simgrid::mc::ActorInformation>& mc_api::get_actors() const
203 {
204   return mc_model_checker->get_remote_simulation().actors();
205 }
206
207 bool mc_api::actor_is_enabled(aid_t pid) const
208 {
209   return session->actor_is_enabled(pid);
210 }
211
212 unsigned long mc_api::get_maxpid() const
213 {
214   return MC_smx_get_maxpid();
215 }
216
217 int mc_api::get_actors_size() const
218 {
219   return mc_model_checker->get_remote_simulation().actors().size();
220 }
221
222 bool mc_api::comm_addr_equal(const kernel::activity::CommImpl* comm_addr1, const kernel::activity::CommImpl* comm_addr2) const
223 {
224   return remote(comm_addr1) == remote(comm_addr2);
225 }
226
227 kernel::activity::CommImpl* mc_api::get_comm_isend_raw_addr(smx_simcall_t request) const
228 {
229   auto comm_addr = simcall_comm_isend__getraw__result(request);
230   return static_cast<kernel::activity::CommImpl*>(comm_addr);
231 }
232
233 kernel::activity::CommImpl* mc_api::get_comm_wait_raw_addr(smx_simcall_t request) const
234 {
235   return simcall_comm_wait__getraw__comm(request);
236 }
237
238 kernel::activity::CommImpl* mc_api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
239 {
240   auto addr = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__getraw__comms(request) + value));
241   return static_cast<simgrid::kernel::activity::CommImpl*>(addr);
242 }
243
244 std::string mc_api::get_pattern_comm_rdv(void* addr) const
245 {
246   Remote<kernel::activity::CommImpl> temp_synchro;
247   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
248   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
249
250   char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
251       (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
252   auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
253   return rdv;
254 }
255
256 unsigned long mc_api::get_pattern_comm_src_proc(void* addr) const
257 {
258   Remote<kernel::activity::CommImpl> temp_synchro;
259   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
260   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
261   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
262   return src_proc;
263 }
264
265 unsigned long mc_api::get_pattern_comm_dst_proc(void* addr) const
266 {
267   Remote<kernel::activity::CommImpl> temp_synchro;
268   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
269   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
270   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
271   return src_proc;
272 }
273
274 std::vector<char> mc_api::get_pattern_comm_data(void* addr) const
275 {
276   Remote<kernel::activity::CommImpl> temp_synchro;
277   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
278   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
279
280   std::vector<char> buffer {};
281   if (synchro->src_buff_ != nullptr) {
282     buffer.resize(synchro->src_buff_size_);
283     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
284                                                          remote(synchro->src_buff_));
285   }
286   return buffer;
287 }
288
289 std::vector<char> mc_api::get_pattern_comm_data(const kernel::activity::CommImpl* comm_addr) const
290 {
291   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
292   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
293   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
294   
295   std::vector<char> buffer {};
296   if (comm->src_buff_ != nullptr) {
297     buffer.resize(comm->src_buff_size_);
298     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
299                                                          remote(comm->src_buff_));
300   }
301   return buffer;
302 }
303
304 const char* mc_api::get_actor_host_name(smx_actor_t actor) const
305 {
306   const char* host_name = MC_smx_actor_get_host_name(actor);
307   return host_name;
308 }
309
310 #if HAVE_SMPI
311 bool mc_api::check_send_request_detached(smx_simcall_t const& simcall) const
312 {
313   simgrid::smpi::Request mpi_request;
314   mc_model_checker->get_remote_simulation().read(
315       &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
316   return mpi_request.detached();
317 }
318 #endif
319
320 smx_actor_t mc_api::get_src_actor(const kernel::activity::CommImpl* comm_addr) const
321 {
322   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
323   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
324   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
325
326   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
327   return src_proc;
328 }
329
330 smx_actor_t mc_api::get_dst_actor(const kernel::activity::CommImpl* comm_addr) const
331 {
332   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
333   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
334   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
335
336   auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
337   return dst_proc;
338 }
339
340 std::size_t mc_api::get_remote_heap_bytes() const
341 {
342   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
343   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
344   return heap_bytes_used;
345 }
346
347 void mc_api::session_initialize() const
348 {
349   session->initialize();
350 }
351
352 ModelChecker* mc_api::get_model_checker() const
353 {
354   return mc_model_checker;
355 }
356
357 void mc_api::mc_inc_visited_states() const
358 {
359   mc_model_checker->visited_states++;
360 }
361
362 void mc_api::mc_inc_executed_trans() const
363 {
364   mc_model_checker->executed_transitions++;
365 }
366
367 unsigned long mc_api::mc_get_visited_states() const
368 {
369   return mc_model_checker->visited_states;
370 }
371
372 unsigned long mc_api::mc_get_executed_trans() const
373 {
374   return mc_model_checker->executed_transitions;
375 }
376
377 bool mc_api::mc_check_deadlock() const
378 {
379   return mc_model_checker->checkDeadlock();
380 }
381
382 void mc_api::mc_show_deadlock() const
383 {
384   MC_show_deadlock();
385 }
386
387 smx_actor_t mc_api::simcall_get_issuer(s_smx_simcall const* req) const
388 {
389   return MC_smx_simcall_get_issuer(req);
390 }
391
392 bool mc_api::mc_is_null() const
393 {
394   auto is_null = (mc_model_checker == nullptr) ? true : false;
395   return is_null;
396 }
397
398 Checker* mc_api::mc_get_checker() const
399 {
400   return mc_model_checker->getChecker();
401 }
402
403 RemoteSimulation& mc_api::mc_get_remote_simulation() const
404 {
405   return mc_model_checker->get_remote_simulation();
406 }
407
408 void mc_api::handle_simcall(Transition const& transition) const
409 {
410   mc_model_checker->handle_simcall(transition);
411 }
412
413 void mc_api::mc_wait_for_requests() const
414 {
415   mc_model_checker->wait_for_requests();
416 }
417
418 void mc_api::mc_exit(int status) const
419 {
420   mc_model_checker->exit(status);
421 }
422
423 std::string const& mc_api::mc_get_host_name(std::string const& hostname) const
424 {
425   return mc_model_checker->get_host_name(hostname);
426 }
427
428 void mc_api::dump_record_path() const
429 {
430   simgrid::mc::dumpRecordPath();
431 }
432
433 smx_simcall_t mc_api::mc_state_choose_request(simgrid::mc::State* state) const
434 {
435   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
436     /* Only consider the actors that were marked as interleaving by the checker algorithm */
437     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
438       continue;
439
440     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
441     if (res)
442       return res;
443   }
444   return nullptr;
445 }
446
447 bool mc_api::request_depend(smx_simcall_t req1, smx_simcall_t req2) const
448 {
449   return simgrid::mc::request_depend(req1, req2);
450 }
451
452 std::string mc_api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
453 {
454   return simgrid::mc::request_to_string(req, value, request_type).c_str();
455 }
456
457 std::string mc_api::request_get_dot_output(smx_simcall_t req, int value) const
458 {
459   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
460   const char* color        = get_color(issuer->get_pid() - 1);
461
462   if (req->inspector_ != nullptr)
463     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
464                                        req->inspector_->dot_label().c_str(), color, color);
465
466   std::string label;
467
468   switch (req->call_) {
469     case Simcall::COMM_ISEND:
470       if (issuer->get_host())
471         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
472       else
473         label = bprintf("[(%ld)] iSend", issuer->get_pid());
474       break;
475
476     case Simcall::COMM_IRECV:
477       if (issuer->get_host())
478         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
479       else
480         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
481       break;
482
483     case Simcall::COMM_WAIT:
484       if (value == -1) {
485         if (issuer->get_host())
486           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
487         else
488           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
489       } else {
490         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
491         Remote<kernel::activity::CommImpl> temp_comm;
492         mc_model_checker->get_remote_simulation().read(temp_comm,
493                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
494         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
495
496         const kernel::actor::ActorImpl* src_proc =
497             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
498         const kernel::actor::ActorImpl* dst_proc =
499             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
500         if (issuer->get_host())
501           label =
502               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
503                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
504         else
505           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
506                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
507       }
508       break;
509
510     case Simcall::COMM_TEST: {
511       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
512       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
513       mc_model_checker->get_remote_simulation().read(temp_comm,
514                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
515       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
516       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
517         if (issuer->get_host())
518           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
519         else
520           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
521       } else {
522         if (issuer->get_host())
523           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
524         else
525           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
526       }
527       break;
528     }
529
530     case Simcall::COMM_WAITANY: {
531       size_t comms_size = simcall_comm_waitany__get__count(req);
532       if (issuer->get_host())
533         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
534                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
535       else
536         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
537       break;
538     }
539
540     case Simcall::COMM_TESTANY:
541       if (value == -1) {
542         if (issuer->get_host())
543           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
544         else
545           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
546       } else {
547         if (issuer->get_host())
548           label =
549               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
550                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
551         else
552           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
553                                      simcall_comm_testany__get__count(req));
554       }
555       break;
556
557     case Simcall::MUTEX_TRYLOCK:
558       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
559       break;
560
561     case Simcall::MUTEX_LOCK:
562       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
563       break;
564
565     case Simcall::MC_RANDOM:
566       if (issuer->get_host())
567         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
568                                    value);
569       else
570         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
571       break;
572
573     default:
574       THROW_UNIMPLEMENTED;
575   }
576
577   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);  
578 }
579
580 const char* mc_api::simcall_get_name(simgrid::simix::Simcall kind) const
581 {
582   return SIMIX_simcall_name(kind);
583 }
584
585 #if HAVE_SMPI
586 int mc_api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
587 {
588   simgrid::smpi::Request mpi_request;
589   void* simcall_data = nullptr;
590   if (type == Simcall::COMM_ISEND)
591     simcall_data = simcall_comm_isend__get__data(simcall);
592   else if (type == Simcall::COMM_IRECV)
593     simcall_data = simcall_comm_irecv__get__data(simcall);
594   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
595   return mpi_request.tag();
596 }
597 #endif
598
599 void mc_api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
600 {
601   system_state->restore(&mc_model_checker->get_remote_simulation());
602 }
603
604 void mc_api::log_state() const
605 {
606   session->log_state();
607 }
608
609 bool mc_api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
610 {
611   return simgrid::mc::snapshot_equal(s1, s2);
612 }
613
614 simgrid::mc::Snapshot* mc_api::take_snapshot(int num_state) const
615 {
616   auto snapshot = new simgrid::mc::Snapshot(num_state);
617   return snapshot;
618 }
619
620 void mc_api::s_close() const
621 {
622   session->close();
623 }
624
625 void mc_api::restore_initial_state() const
626 {
627   session->restore_initial_state();
628 }
629
630 void mc_api::execute(Transition const& transition) const
631 {
632   session->execute(transition);
633 }
634
635 #if SIMGRID_HAVE_MC
636 void mc_api::automaton_load(const char *file) const
637
638   MC_automaton_load(file); 
639 }
640 #endif
641
642 std::vector<int> mc_api::automaton_propositional_symbol_evaluate() const  
643 {
644   unsigned int cursor = 0;
645   std::vector<int> values;    
646   xbt_automaton_propositional_symbol_t ps = nullptr;
647   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
648     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
649   return values;
650 }
651
652 std::vector<xbt_automaton_state_t> mc_api::get_automaton_state() const
653 {
654   std::vector<xbt_automaton_state_t> automaton_stack;
655   unsigned int cursor = 0;
656   xbt_automaton_state_t automaton_state;
657   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
658     if (automaton_state->type == -1)
659       automaton_stack.push_back(automaton_state);
660   return automaton_stack;
661 }
662
663 int mc_api::compare_automaton_exp_lable(const xbt_automaton_exp_label* l, std::vector<int> const& values) const
664 {
665   unsigned int cursor = 0;
666   xbt_automaton_propositional_symbol_t p = nullptr;
667   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
668     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
669       return cursor;
670   }
671   return -1;
672 }
673
674 void mc_api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
675 {
676   mc::property_automaton->current_state = automaton_state;
677 }
678
679 } // namespace mc
680 } // namespace simgrid