Logo AND Algorithmique Numérique Distribuée

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