Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
copy_incomplete_comm_pattern() and copy_index_comm_pattern() in mc_api
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/mc_state.hpp"
7 // #include "src/mc/mc_comm_pattern.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_request.hpp"
10 #include "src/mc/mc_api.hpp"
11
12 #include <boost/range/algorithm.hpp>
13
14 using simgrid::mc::remote;
15 using mcapi = simgrid::mc::mc_api;
16
17 namespace simgrid {
18 namespace mc {
19
20 State::State(unsigned long state_number) : num_(state_number)
21 {
22   this->internal_comm_.clear();
23   auto maxpid = mcapi::get().get_maxpid();
24   actor_states_.resize(maxpid);
25   /* Stateful model checking */
26   if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
27     auto snapshot_ptr = mcapi::get().take_snapshot(num_);
28     system_state_ = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
29     if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
30       mcapi::get().copy_incomplete_comm_pattern(this);
31       mcapi::get().copy_index_comm_pattern(this);
32     }
33   }
34 }
35
36 std::size_t State::interleave_size() const
37 {
38   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
39 }
40
41 Transition State::get_transition() const
42 {
43   return this->transition_;
44 }
45
46 }
47 }
48
49 /* Search an enabled transition for the given process.
50  *
51  * This can be seen as an iterator returning the next transition of the process.
52  *
53  * We only consider the processes that are both
54  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
55  *  - which simcall can currently be executed (like a comm where the other partner is already known)
56  * Once we returned the last enabled transition of a process, it is marked done.
57  *
58  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
59  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
60  */
61 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
62 {
63   /* reset the outgoing transition */
64   simgrid::mc::ActorState* procstate   = &state->actor_states_[actor->get_pid()];
65   state->transition_.pid_              = -1;
66   state->transition_.argument_         = -1;
67   state->executed_req_.call_           = SIMCALL_NONE;
68
69   if (not simgrid::mc::actor_is_enabled(actor))
70     return nullptr; // Not executable in the application
71
72   smx_simcall_t req = nullptr;
73   switch (actor->simcall_.call_) {
74     case SIMCALL_COMM_WAITANY:
75       state->transition_.argument_ = -1;
76       while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
77         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
78           state->transition_.argument_ = procstate->times_considered;
79           ++procstate->times_considered;
80           break;
81         }
82         ++procstate->times_considered;
83       }
84
85       if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
86         procstate->set_done();
87       if (state->transition_.argument_ != -1)
88         req = &actor->simcall_;
89       break;
90
91     case SIMCALL_COMM_TESTANY: {
92       unsigned start_count       = procstate->times_considered;
93       state->transition_.argument_ = -1;
94       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
95         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
96           state->transition_.argument_ = procstate->times_considered;
97           ++procstate->times_considered;
98           break;
99         }
100         ++procstate->times_considered;
101       }
102
103       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
104         procstate->set_done();
105
106       if (state->transition_.argument_ != -1 || start_count == 0)
107         req = &actor->simcall_;
108
109       break;
110     }
111
112     case SIMCALL_COMM_WAIT: {
113       simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
114           remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
115       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
116       mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
117       const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
118       if (act->src_actor_.get() && act->dst_actor_.get())
119         state->transition_.argument_ = 0; // OK
120       else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
121                act->detached())
122         state->transition_.argument_ = 0; // OK
123       else
124         state->transition_.argument_ = -1; // timeout
125       procstate->set_done();
126       req = &actor->simcall_;
127       break;
128     }
129
130     case SIMCALL_MC_RANDOM: {
131       int min_value                = simcall_mc_random__get__min(&actor->simcall_);
132       state->transition_.argument_ = procstate->times_considered + min_value;
133       procstate->times_considered++;
134       if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
135         procstate->set_done();
136       req = &actor->simcall_;
137       break;
138     }
139
140     default:
141       procstate->set_done();
142       state->transition_.argument_ = 0;
143       req                          = &actor->simcall_;
144       break;
145   }
146   if (not req)
147     return nullptr;
148
149   state->transition_.pid_ = actor->get_pid();
150   state->executed_req_    = *req;
151   // Fetch the data of the request and translate it:
152   state->internal_req_ = *req;
153
154   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
155    * action so it can be treated later by the dependence function. */
156   switch (req->call_) {
157     case SIMCALL_COMM_WAITANY: {
158       state->internal_req_.call_ = SIMCALL_COMM_WAIT;
159       simgrid::kernel::activity::CommImpl* remote_comm;
160       remote_comm = mc_model_checker->get_remote_simulation().read(
161           remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
162       mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
163       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
164       simcall_comm_wait__set__timeout(&state->internal_req_, 0);
165       break;
166     }
167
168     case SIMCALL_COMM_TESTANY:
169       state->internal_req_.call_ = SIMCALL_COMM_TEST;
170
171       if (state->transition_.argument_ > 0) {
172         simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
173             remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
174         mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
175       }
176
177       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
178       simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
179       break;
180
181     case SIMCALL_COMM_WAIT:
182       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
183                                                            remote(simcall_comm_wait__getraw__comm(req)));
184       simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
185       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
186       break;
187
188     case SIMCALL_COMM_TEST:
189       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
190                                                            remote(simcall_comm_test__getraw__comm(req)));
191       simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
192       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
193       break;
194
195     default:
196       /* No translation needed */
197       break;
198   }
199
200   return req;
201 }
202
203 smx_simcall_t MC_state_choose_request(simgrid::mc::State* state)
204 {
205   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
206     /* Only consider the actors that were marked as interleaving by the checker algorithm */
207     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
208       continue;
209
210     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
211     if (res)
212       return res;
213   }
214   return nullptr;
215 }