Logo AND Algorithmique Numérique Distribuée

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