Logo AND Algorithmique Numérique Distribuée

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