Logo AND Algorithmique Numérique Distribuée

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