Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f21715fbd35dc4148f14a4323448b14358a85f1e
[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 std::string mc_api::get_pattern_comm_rdv(void* addr) const
213 {
214   Remote<kernel::activity::CommImpl> temp_synchro;
215   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
216   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
217
218   char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
219       (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
220   auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
221   return rdv;
222 }
223
224 std::size_t mc_api::get_remote_heap_bytes() const
225 {
226   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
227   auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
228   return heap_bytes_used;
229 }
230
231 void mc_api::s_initialize() const
232 {
233   session->initialize();
234 }
235
236 ModelChecker* mc_api::get_model_checker() const
237 {
238   return mc_model_checker;
239 }
240
241 void mc_api::mc_inc_visited_states() const
242 {
243   mc_model_checker->visited_states++;
244 }
245
246 void mc_api::mc_inc_executed_trans() const
247 {
248   mc_model_checker->executed_transitions++;
249 }
250
251 unsigned long mc_api::mc_get_visited_states() const
252 {
253   return mc_model_checker->visited_states;
254 }
255
256 unsigned long mc_api::mc_get_executed_trans() const
257 {
258   return mc_model_checker->executed_transitions;
259 }
260
261 bool mc_api::mc_check_deadlock() const
262 {
263   return mc_model_checker->checkDeadlock();
264 }
265
266 void mc_api::mc_show_deadlock() const
267 {
268   MC_show_deadlock();
269 }
270
271 smx_actor_t mc_api::mc_smx_simcall_get_issuer(s_smx_simcall const* req) const
272 {
273   return MC_smx_simcall_get_issuer(req);
274 }
275
276 bool mc_api::mc_is_null() const
277 {
278   auto is_null = (mc_model_checker == nullptr) ? true : false;
279   return is_null;
280 }
281
282 Checker* mc_api::mc_get_checker() const
283 {
284   return mc_model_checker->getChecker();
285 }
286
287 RemoteSimulation& mc_api::mc_get_remote_simulation() const
288 {
289   return mc_model_checker->get_remote_simulation();
290 }
291
292 void mc_api::handle_simcall(Transition const& transition) const
293 {
294   mc_model_checker->handle_simcall(transition);
295 }
296
297 void mc_api::mc_wait_for_requests() const
298 {
299   mc_model_checker->wait_for_requests();
300 }
301
302 void mc_api::mc_exit(int status) const
303 {
304   mc_model_checker->exit(status);
305 }
306
307 std::string const& mc_api::mc_get_host_name(std::string const& hostname) const
308 {
309   return mc_model_checker->get_host_name(hostname);
310 }
311
312 void mc_api::mc_dump_record_path() const
313 {
314   simgrid::mc::dumpRecordPath();
315 }
316
317 smx_simcall_t mc_api::mc_state_choose_request(simgrid::mc::State* state) const
318 {
319   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
320     /* Only consider the actors that were marked as interleaving by the checker algorithm */
321     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
322       continue;
323
324     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
325     if (res)
326       return res;
327   }
328   return nullptr;
329 }
330
331 bool mc_api::request_depend(smx_simcall_t req1, smx_simcall_t req2) const
332 {
333   return simgrid::mc::request_depend(req1, req2);
334 }
335
336 std::string mc_api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
337 {
338   return simgrid::mc::request_to_string(req, value, request_type).c_str();
339 }
340
341 std::string mc_api::request_get_dot_output(smx_simcall_t req, int value) const
342 {
343   return simgrid::mc::request_get_dot_output(req, value);
344 }
345
346 const char* mc_api::simix_simcall_name(e_smx_simcall_t kind) const
347 {
348   return SIMIX_simcall_name(kind);
349 }
350
351 bool mc_api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
352 {
353   return simgrid::mc::snapshot_equal(s1, s2);
354 }
355
356 simgrid::mc::Snapshot* mc_api::take_snapshot(int num_state) const
357 {
358   auto snapshot = new simgrid::mc::Snapshot(num_state);
359   return snapshot;
360 }
361
362 void mc_api::s_close() const
363 {
364   session->close();
365 }
366
367 void mc_api::s_restore_initial_state() const
368 {
369   session->restore_initial_state();
370 }
371
372 void mc_api::execute(Transition const& transition)
373 {
374   session->execute(transition);
375 }
376
377 void mc_api::s_log_state() const
378 {
379   session->log_state();
380 }
381
382 } // namespace mc
383 } // namespace simgrid