Logo AND Algorithmique Numérique Distribuée

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