Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc_api::get_pattern_comm_dst_proc() defined and used in comm_deter checker
[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
12 #include <xbt/asserts.h>
13 #include <xbt/log.h>
14
15 #if HAVE_SMPI
16 #include "src/smpi/include/smpi_request.hpp"
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_api, mc, "Logging specific to MC Fasade APIs ");
20
21 using Simcall = simgrid::simix::Simcall;
22
23 namespace simgrid {
24 namespace mc {
25
26 /* Search an enabled transition for the given process.
27  *
28  * This can be seen as an iterator returning the next transition of the process.
29  *
30  * We only consider the processes that are both
31  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
32  *  - which simcall can currently be executed (like a comm where the other partner is already known)
33  * Once we returned the last enabled transition of a process, it is marked done.
34  *
35  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
36  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
37  */
38 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
39 {
40   /* reset the outgoing transition */
41   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
42   state->transition_.pid_            = -1;
43   state->transition_.argument_       = -1;
44   state->executed_req_.call_         =  Simcall::NONE;
45
46   if (not simgrid::mc::actor_is_enabled(actor))
47     return nullptr; // Not executable in the application
48
49   smx_simcall_t req = nullptr;
50   switch (actor->simcall_.call_) {
51     case Simcall::COMM_WAITANY:
52       state->transition_.argument_ = -1;
53       while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
54         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
55           state->transition_.argument_ = procstate->times_considered;
56           ++procstate->times_considered;
57           break;
58         }
59         ++procstate->times_considered;
60       }
61
62       if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
63         procstate->set_done();
64       if (state->transition_.argument_ != -1)
65         req = &actor->simcall_;
66       break;
67
68     case Simcall::COMM_TESTANY: {
69       unsigned start_count         = procstate->times_considered;
70       state->transition_.argument_ = -1;
71       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
72         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
73           state->transition_.argument_ = procstate->times_considered;
74           ++procstate->times_considered;
75           break;
76         }
77         ++procstate->times_considered;
78       }
79
80       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
81         procstate->set_done();
82
83       if (state->transition_.argument_ != -1 || start_count == 0)
84         req = &actor->simcall_;
85
86       break;
87     }
88
89     case Simcall::COMM_WAIT: {
90       simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
91           remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
92       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
93       mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
94       const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
95       if (act->src_actor_.get() && act->dst_actor_.get())
96         state->transition_.argument_ = 0; // OK
97       else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
98                act->detached())
99         state->transition_.argument_ = 0; // OK
100       else
101         state->transition_.argument_ = -1; // timeout
102       procstate->set_done();
103       req = &actor->simcall_;
104       break;
105     }
106
107     case Simcall::MC_RANDOM: {
108       int min_value                = simcall_mc_random__get__min(&actor->simcall_);
109       state->transition_.argument_ = procstate->times_considered + min_value;
110       procstate->times_considered++;
111       if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
112         procstate->set_done();
113       req = &actor->simcall_;
114       break;
115     }
116
117     default:
118       procstate->set_done();
119       state->transition_.argument_ = 0;
120       req                          = &actor->simcall_;
121       break;
122   }
123   if (not req)
124     return nullptr;
125
126   state->transition_.pid_ = actor->get_pid();
127   state->executed_req_    = *req;
128   // Fetch the data of the request and translate it:
129   state->internal_req_ = *req;
130
131   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
132    * action so it can be treated later by the dependence function. */
133   switch (req->call_) {
134     case Simcall::COMM_WAITANY: {
135       state->internal_req_.call_ = Simcall::COMM_WAIT;
136       simgrid::kernel::activity::CommImpl* remote_comm;
137       remote_comm = mc_model_checker->get_remote_simulation().read(
138           remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
139       mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
140       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
141       simcall_comm_wait__set__timeout(&state->internal_req_, 0);
142       break;
143     }
144
145     case Simcall::COMM_TESTANY:
146       state->internal_req_.call_ = Simcall::COMM_TEST;
147
148       if (state->transition_.argument_ > 0) {
149         simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
150             remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
151         mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
152       }
153
154       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
155       simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
156       break;
157
158     case Simcall::COMM_WAIT:
159       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
160                                                            remote(simcall_comm_wait__getraw__comm(req)));
161       simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
162       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
163       break;
164
165     case Simcall::COMM_TEST:
166       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
167                                                            remote(simcall_comm_test__getraw__comm(req)));
168       simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
169       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
170       break;
171
172     default:
173       /* No translation needed */
174       break;
175   }
176
177   return req;
178 }
179
180 void mc_api::initialize(char** argv)
181 {
182   simgrid::mc::session = new simgrid::mc::Session([argv] {
183     int i = 1;
184     while (argv[i] != nullptr && argv[i][0] == '-')
185       i++;
186     xbt_assert(argv[i] != nullptr,
187                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
188     execvp(argv[i], argv + i);
189     xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
190   });
191 }
192
193 std::vector<simgrid::mc::ActorInformation>& mc_api::get_actors() const
194 {
195   return mc_model_checker->get_remote_simulation().actors();
196 }
197
198 bool mc_api::actor_is_enabled(aid_t pid) const
199 {
200   return session->actor_is_enabled(pid);
201 }
202
203 unsigned long mc_api::get_maxpid() const
204 {
205   return MC_smx_get_maxpid();
206 }
207
208 void mc_api::copy_incomplete_comm_pattern(const simgrid::mc::State* state) const
209 {
210   MC_state_copy_incomplete_communications_pattern((simgrid::mc::State*)state);
211 }
212
213 void mc_api::copy_index_comm_pattern(const simgrid::mc::State* state) const
214 {
215   MC_state_copy_index_communications_pattern((simgrid::mc::State*)state);
216 }
217
218 kernel::activity::CommImpl* mc_api::get_pattern_comm_addr(smx_simcall_t request) const
219 {
220   auto comm_addr = simcall_comm_isend__getraw__result(request);
221   return static_cast<kernel::activity::CommImpl*>(comm_addr);
222 }
223
224 std::string mc_api::get_pattern_comm_rdv(void* addr) const
225 {
226   Remote<kernel::activity::CommImpl> temp_synchro;
227   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
228   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
229
230   char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
231       (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
232   auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
233   return rdv;
234 }
235
236 unsigned long mc_api::get_pattern_comm_src_proc(void* addr) const
237 {
238   Remote<kernel::activity::CommImpl> temp_synchro;
239   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
240   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
241   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
242   return src_proc;
243 }
244
245 unsigned long mc_api::get_pattern_comm_dst_proc(void* addr) const
246 {
247   Remote<kernel::activity::CommImpl> temp_synchro;
248   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
249   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
250   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
251   return src_proc;
252 }
253
254 std::vector<char> mc_api::get_pattern_comm_data(void* addr) const
255 {
256   Remote<kernel::activity::CommImpl> temp_synchro;
257   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
258   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
259
260   std::vector<char> buffer {};
261   if (synchro->src_buff_ != nullptr) {
262     buffer.resize(synchro->src_buff_size_);
263     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
264                                                          remote(synchro->src_buff_));
265   }
266   return buffer;
267 }
268
269 const char* mc_api::get_actor_host_name(smx_actor_t actor) const
270 {
271   const char* host_name = MC_smx_actor_get_host_name(actor);
272   return host_name;
273 }
274
275 std::size_t mc_api::get_remote_heap_bytes() const
276 {
277   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
278   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
279   return heap_bytes_used;
280 }
281
282 void mc_api::s_initialize() const
283 {
284   session->initialize();
285 }
286
287 ModelChecker* mc_api::get_model_checker() const
288 {
289   return mc_model_checker;
290 }
291
292 void mc_api::mc_inc_visited_states() const
293 {
294   mc_model_checker->visited_states++;
295 }
296
297 void mc_api::mc_inc_executed_trans() const
298 {
299   mc_model_checker->executed_transitions++;
300 }
301
302 unsigned long mc_api::mc_get_visited_states() const
303 {
304   return mc_model_checker->visited_states;
305 }
306
307 unsigned long mc_api::mc_get_executed_trans() const
308 {
309   return mc_model_checker->executed_transitions;
310 }
311
312 bool mc_api::mc_check_deadlock() const
313 {
314   return mc_model_checker->checkDeadlock();
315 }
316
317 void mc_api::mc_show_deadlock() const
318 {
319   MC_show_deadlock();
320 }
321
322 smx_actor_t mc_api::mc_smx_simcall_get_issuer(s_smx_simcall const* req) const
323 {
324   return MC_smx_simcall_get_issuer(req);
325 }
326
327 bool mc_api::mc_is_null() const
328 {
329   auto is_null = (mc_model_checker == nullptr) ? true : false;
330   return is_null;
331 }
332
333 Checker* mc_api::mc_get_checker() const
334 {
335   return mc_model_checker->getChecker();
336 }
337
338 RemoteSimulation& mc_api::mc_get_remote_simulation() const
339 {
340   return mc_model_checker->get_remote_simulation();
341 }
342
343 void mc_api::handle_simcall(Transition const& transition) const
344 {
345   mc_model_checker->handle_simcall(transition);
346 }
347
348 void mc_api::mc_wait_for_requests() const
349 {
350   mc_model_checker->wait_for_requests();
351 }
352
353 void mc_api::mc_exit(int status) const
354 {
355   mc_model_checker->exit(status);
356 }
357
358 std::string const& mc_api::mc_get_host_name(std::string const& hostname) const
359 {
360   return mc_model_checker->get_host_name(hostname);
361 }
362
363 void mc_api::mc_dump_record_path() const
364 {
365   simgrid::mc::dumpRecordPath();
366 }
367
368 smx_simcall_t mc_api::mc_state_choose_request(simgrid::mc::State* state) const
369 {
370   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
371     /* Only consider the actors that were marked as interleaving by the checker algorithm */
372     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
373       continue;
374
375     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
376     if (res)
377       return res;
378   }
379   return nullptr;
380 }
381
382 bool mc_api::request_depend(smx_simcall_t req1, smx_simcall_t req2) const
383 {
384   return simgrid::mc::request_depend(req1, req2);
385 }
386
387 std::string mc_api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
388 {
389   return simgrid::mc::request_to_string(req, value, request_type).c_str();
390 }
391
392 std::string mc_api::request_get_dot_output(smx_simcall_t req, int value) const
393 {
394   return simgrid::mc::request_get_dot_output(req, value);
395 }
396
397 const char* mc_api::simix_simcall_name(simgrid::simix::Simcall kind) const
398 {
399   return SIMIX_simcall_name(kind);
400 }
401
402 #if HAVE_SMPI
403 int mc_api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
404 {
405   simgrid::smpi::Request mpi_request;
406   void* simcall_data = nullptr;
407   if (type == Simcall::COMM_ISEND)
408     simcall_data = simcall_comm_isend__get__data(simcall);
409   else if (type == Simcall::COMM_IRECV)
410     simcall_data = simcall_comm_irecv__get__data(simcall);
411   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
412   return mpi_request.tag();
413 }
414 #endif
415
416 bool mc_api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
417 {
418   return simgrid::mc::snapshot_equal(s1, s2);
419 }
420
421 simgrid::mc::Snapshot* mc_api::take_snapshot(int num_state) const
422 {
423   auto snapshot = new simgrid::mc::Snapshot(num_state);
424   return snapshot;
425 }
426
427 void mc_api::s_close() const
428 {
429   session->close();
430 }
431
432 void mc_api::s_restore_initial_state() const
433 {
434   session->restore_initial_state();
435 }
436
437 void mc_api::execute(Transition const& transition)
438 {
439   session->execute(transition);
440 }
441
442 void mc_api::s_log_state() const
443 {
444   session->log_state();
445 }
446
447 } // namespace mc
448 } // namespace simgrid