Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
861e3996985df5b301d048cb33d67838f6c6a4fe
[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/simix/smx_private.h"
14 #include "src/mc/mc_state.h"
15 #include "src/mc/mc_request.h"
16 #include "src/mc/mc_private.h"
17 #include "src/mc/mc_comm_pattern.h"
18 #include "src/mc/mc_smx.h"
19 #include "src/mc/mc_xbt.hpp"
20 #include "src/mc/Transition.hpp"
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   processStates.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->processStates,
50     [](simgrid::mc::ProcessState const& p) { return p.isToInterleave(); });
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 (controled by the checker algo).
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 process)
76 {
77   /* reset the outgoing transition */
78   simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
79   state->transition.pid                = -1;
80   state->transition.argument           = -1;
81   state->executed_req.call             = SIMCALL_NONE;
82
83   if (!procstate->isToInterleave())
84     return nullptr;
85   if (!simgrid::mc::actor_is_enabled(process))
86     return nullptr;
87
88   smx_simcall_t req = nullptr;
89   switch (process->simcall.call) {
90       case SIMCALL_COMM_WAITANY:
91         state->transition.argument = -1;
92         while (procstate->interleave_count <
93               read_length(mc_model_checker->process(),
94                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
95           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
96               procstate->interleave_count++)) {
97             state->transition.argument = procstate->interleave_count - 1;
98             break;
99           }
100         }
101
102         if (procstate->interleave_count >=
103             simgrid::mc::read_length(mc_model_checker->process(),
104               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
105           procstate->setDone();
106         if (state->transition.argument != -1)
107           req = &process->simcall;
108         break;
109
110       case SIMCALL_COMM_TESTANY: {
111         unsigned start_count = procstate->interleave_count;
112         state->transition.argument = -1;
113         while (procstate->interleave_count <
114                 simcall_comm_testany__get__count(&process->simcall))
115           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
116               procstate->interleave_count++)) {
117             state->transition.argument = procstate->interleave_count - 1;
118             break;
119           }
120
121         if (procstate->interleave_count >=
122             simcall_comm_testany__get__count(&process->simcall))
123           procstate->setDone();
124
125         if (state->transition.argument != -1 || start_count == 0)
126            req = &process->simcall;
127
128         break;
129       }
130
131       case SIMCALL_COMM_WAIT: {
132         simgrid::mc::RemotePtr<simgrid::kernel::activity::Comm> remote_act = remote(
133           static_cast<simgrid::kernel::activity::Comm*>(simcall_comm_wait__get__comm(&process->simcall)));
134         simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_act;
135         mc_model_checker->process().read(temp_act, remote_act);
136         simgrid::kernel::activity::Comm* act = temp_act.getBuffer();
137         if (act->src_proc && act->dst_proc)
138           state->transition.argument = 0;
139         else if (act->src_proc == nullptr && act->type == SIMIX_COMM_READY
140               && act->detached == 1)
141           state->transition.argument = 0;
142         else
143           state->transition.argument = -1;
144         procstate->setDone();
145         req = &process->simcall;
146         break;
147       }
148
149       case SIMCALL_MC_RANDOM: {
150         int min_value = simcall_mc_random__get__min(&process->simcall);
151         state->transition.argument = procstate->interleave_count + min_value;
152         procstate->interleave_count++;
153         if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
154           procstate->setDone();
155         req = &process->simcall;
156         break;
157       }
158
159       default:
160         procstate->setDone();
161         state->transition.argument = 0;
162         req = &process->simcall;
163         break;
164   }
165   if (!req)
166     return nullptr;
167
168   // Fetch the data of the request and translate it:
169
170   state->transition.pid = process->pid;
171
172   state->executed_req = *req;
173
174   /* The waitany and testany request are transformed into a wait or test request
175    * over the corresponding communication action so it can be treated later by
176    * the dependence function. */
177   switch (req->call) {
178   case SIMCALL_COMM_WAITANY: {
179     state->internal_req.call = SIMCALL_COMM_WAIT;
180     state->internal_req.issuer = req->issuer;
181     smx_activity_t remote_comm;
182     read_element(mc_model_checker->process(),
183       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
184       state->transition.argument, sizeof(remote_comm));
185     mc_model_checker->process().read(state->internal_comm, remote(
186       static_cast<simgrid::kernel::activity::Comm*>(remote_comm)));
187     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
188     simcall_comm_wait__set__timeout(&state->internal_req, 0);
189     break;
190   }
191
192   case SIMCALL_COMM_TESTANY:
193     state->internal_req.call = SIMCALL_COMM_TEST;
194     state->internal_req.issuer = req->issuer;
195
196     if (state->transition.argument > 0) {
197       smx_activity_t remote_comm = mc_model_checker->process().read(
198         remote(simcall_comm_testany__get__comms(req) + state->transition.argument));
199       mc_model_checker->process().read(state->internal_comm, remote(
200         static_cast<simgrid::kernel::activity::Comm*>(remote_comm)));
201     }
202
203     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
204     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
205     break;
206
207   case SIMCALL_COMM_WAIT:
208     state->internal_req = *req;
209     mc_model_checker->process().read_bytes(&state->internal_comm ,
210       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
211     simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
212     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
213     break;
214
215   case SIMCALL_COMM_TEST:
216     state->internal_req = *req;
217     mc_model_checker->process().read_bytes(&state->internal_comm,
218       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
219     simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
220     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
221     break;
222
223   default:
224     state->internal_req = *req;
225     break;
226   }
227
228   return req;
229 }
230
231 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
232 {
233   for (auto& actor : mc_model_checker->process().actors()) {
234     smx_simcall_t res = MC_state_get_request_for_process(state, actor.copy.getBuffer());
235     if (res)
236       return res;
237   }
238   return nullptr;
239 }