Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change dynar parameter for waitany to a raw array (mimic testany).
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-2019. 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_comm_pattern.hpp"
7 #include "src/mc/mc_request.hpp"
8 #include "src/mc/mc_smx.hpp"
9 #include "src/mc/mc_state.hpp"
10 #include "src/mc/mc_xbt.hpp"
11
12 #include <boost/range/algorithm.hpp>
13
14 using simgrid::mc::remote;
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC (state)");
17
18 namespace simgrid {
19 namespace mc {
20
21 State::State(unsigned long state_number) : num(state_number)
22 {
23   this->internal_comm.clear();
24   std::memset(&this->internal_req, 0, sizeof(this->internal_req));
25   std::memset(&this->executed_req, 0, sizeof(this->executed_req));
26
27   actorStates.resize(MC_smx_get_maxpid());
28   /* Stateful model checking */
29   if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
30     system_state = simgrid::mc::take_snapshot(num);
31     if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
32       MC_state_copy_incomplete_communications_pattern(this);
33       MC_state_copy_index_communications_pattern(this);
34     }
35   }
36 }
37
38 std::size_t State::interleaveSize() const
39 {
40   return boost::range::count_if(this->actorStates, [](simgrid::mc::ProcessState const& p) { return p.isTodo(); });
41 }
42
43 Transition State::getTransition() const
44 {
45   return this->transition;
46 }
47
48 }
49 }
50
51 /* Search an enabled transition for the given process.
52  *
53  * This can be seen as an iterator returning the next transition of the process.
54  *
55  * We only consider the processes that are both
56  *  - marked "to be interleaved" in their ProcessState (controlled by the checker algorithm).
57  *  - which simcall can currently be executed (like a comm where the other partner is already known)
58  * Once we returned the last enabled transition of a process, it is marked done.
59  *
60  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
61  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
62  */
63 static inline smx_simcall_t MC_state_get_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
64 {
65   /* reset the outgoing transition */
66   simgrid::mc::ProcessState* procstate = &state->actorStates[actor->get_pid()];
67   state->transition.pid                = -1;
68   state->transition.argument           = -1;
69   state->executed_req.call             = SIMCALL_NONE;
70
71   if (not simgrid::mc::actor_is_enabled(actor))
72     return nullptr; // Not executable in the application
73
74   smx_simcall_t req = nullptr;
75   switch (actor->simcall.call) {
76     case SIMCALL_COMM_WAITANY:
77       state->transition.argument = -1;
78       while (procstate->times_considered <
79              read_length(mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(&actor->simcall)))) {
80         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
81           state->transition.argument = procstate->times_considered - 1;
82           break;
83         }
84       }
85
86       if (procstate->times_considered >=
87           simgrid::mc::read_length(mc_model_checker->process(),
88                                    simgrid::mc::remote(simcall_comm_waitany__get__comms(&actor->simcall))))
89         procstate->setDone();
90       if (state->transition.argument != -1)
91         req = &actor->simcall;
92       break;
93
94     case SIMCALL_COMM_TESTANY: {
95       unsigned start_count       = procstate->times_considered;
96       state->transition.argument = -1;
97       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall))
98         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
99           state->transition.argument = procstate->times_considered - 1;
100           break;
101         }
102
103       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall))
104         procstate->setDone();
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(static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(&actor->simcall)));
115       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
116       mc_model_checker->process().read(temp_act, remote_act);
117       simgrid::kernel::activity::CommImpl* act = temp_act.getBuffer();
118       if (act->src_actor_.get() && act->dst_actor_.get())
119         state->transition.argument = 0;
120       else if (act->src_actor_.get() == nullptr && act->type == simgrid::kernel::activity::CommImpl::Type::READY &&
121                act->detached == 1)
122         state->transition.argument = 0;
123       else
124         state->transition.argument = -1;
125       procstate->setDone();
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->setDone();
136       req = &actor->simcall;
137       break;
138     }
139
140     default:
141       procstate->setDone();
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::ActivityImpl* remote_comm;
160     read_element(mc_model_checker->process(), &remote_comm, remote(simcall_comm_waitany__getraw__comms(req)),
161                  state->transition.argument, sizeof(remote_comm));
162     mc_model_checker->process().read(state->internal_comm,
163                                      remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_comm)));
164     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
165     simcall_comm_wait__set__timeout(&state->internal_req, 0);
166     break;
167   }
168
169   case SIMCALL_COMM_TESTANY:
170     state->internal_req.call = SIMCALL_COMM_TEST;
171
172     if (state->transition.argument > 0) {
173       simgrid::kernel::activity::ActivityImpl* remote_comm = mc_model_checker->process().read(
174           remote(simcall_comm_testany__getraw__comms(req) + state->transition.argument));
175       mc_model_checker->process().read(state->internal_comm,
176                                        remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_comm)));
177     }
178
179     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
180     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
181     break;
182
183   case SIMCALL_COMM_WAIT:
184     mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
185                                            remote(simcall_comm_wait__getraw__comm(req)));
186     simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
187     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
188     break;
189
190   case SIMCALL_COMM_TEST:
191     mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
192                                            remote(simcall_comm_test__getraw__comm(req)));
193     simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
194     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
195     break;
196
197   default:
198     /* No translation needed */
199     break;
200   }
201
202   return req;
203 }
204
205 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
206 {
207   for (auto& actor : mc_model_checker->process().actors()) {
208     /* Only consider the actors that were marked as interleaving by the checker algorithm */
209     if (not state->actorStates[actor.copy.getBuffer()->get_pid()].isTodo())
210       continue;
211
212     smx_simcall_t res = MC_state_get_request_for_process(state, actor.copy.getBuffer());
213     if (res)
214       return res;
215   }
216   return nullptr;
217 }