Logo AND Algorithmique Numérique Distribuée

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