Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix MC with the class-hierarchification of simgrid::simix::Synchro
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <assert.h>
8
9 #include <algorithm>
10
11 #include <xbt/log.h>
12 #include <xbt/sysdep.h>
13
14 #include "src/simix/smx_private.h"
15 #include "src/mc/mc_state.h"
16 #include "src/mc/mc_request.h"
17 #include "src/mc/mc_private.h"
18 #include "src/mc/mc_comm_pattern.h"
19 #include "src/mc/mc_smx.h"
20 #include "src/mc/mc_xbt.hpp"
21 #include "src/mc/Transition.hpp"
22
23 using simgrid::mc::remote;
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
26                                 "Logging specific to MC (state)");
27
28 /**
29  * \brief Creates a state data structure used by the exploration algorithm
30  */
31 simgrid::mc::State* MC_state_new(unsigned long state_number)
32 {
33   simgrid::mc::State* state = new simgrid::mc::State();
34   state->processStates.resize(MC_smx_get_maxpid());
35   state->num = state_number;
36   /* Stateful model checking */
37   if((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) ||  _sg_mc_termination){
38     state->system_state = simgrid::mc::take_snapshot(state->num);
39     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
40       MC_state_copy_incomplete_communications_pattern(state);
41       MC_state_copy_index_communications_pattern(state);
42     }
43   }
44   return state;
45 }
46
47 namespace simgrid {
48 namespace mc {
49
50 State::State()
51 {
52   std::memset(this->internal_comm.data(), 0, this->internal_comm.size());
53   std::memset(&this->internal_req, 0, sizeof(this->internal_req));
54   std::memset(&this->executed_req, 0, sizeof(this->executed_req));
55 }
56
57 std::size_t State::interleaveSize() const
58 {
59   return std::count_if(this->processStates.begin(), this->processStates.end(),
60     [](simgrid::mc::ProcessState const& state) { return state.isToInterleave(); });
61 }
62
63 Transition State::getTransition() const
64 {
65   return this->transition;
66 }
67
68 }
69 }
70
71 static inline smx_simcall_t MC_state_get_request_for_process(
72   simgrid::mc::State* state, smx_process_t process)
73 {
74   simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
75   state->transition.pid = -1;
76   state->transition.argument = -1;
77
78   if (!procstate->isToInterleave())
79     return nullptr;
80   if (!simgrid::mc::process_is_enabled(process))
81     return nullptr;
82
83   smx_simcall_t req = nullptr;
84   switch (process->simcall.call) {
85       case SIMCALL_COMM_WAITANY:
86         state->transition.argument = -1;
87         while (procstate->interleave_count <
88               read_length(mc_model_checker->process(),
89                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
90           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
91               procstate->interleave_count++)) {
92             state->transition.argument = procstate->interleave_count - 1;
93             break;
94           }
95         }
96
97         if (procstate->interleave_count >=
98             simgrid::mc::read_length(mc_model_checker->process(),
99               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
100           procstate->setDone();
101         if (state->transition.argument != -1)
102           req = &process->simcall;
103         break;
104
105       case SIMCALL_COMM_TESTANY: {
106         unsigned start_count = procstate->interleave_count;
107         state->transition.argument = -1;
108         while (procstate->interleave_count <
109                 read_length(mc_model_checker->process(),
110                   remote(simcall_comm_testany__get__comms(&process->simcall))))
111           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
112               procstate->interleave_count++)) {
113             state->transition.argument = procstate->interleave_count - 1;
114             break;
115           }
116
117         if (procstate->interleave_count >=
118             read_length(mc_model_checker->process(),
119               remote(simcall_comm_testany__get__comms(&process->simcall))))
120           procstate->setDone();
121
122         if (state->transition.argument != -1 || start_count == 0)
123            req = &process->simcall;
124
125         break;
126       }
127
128       case SIMCALL_COMM_WAIT: {
129         simgrid::mc::RemotePtr<simgrid::simix::Comm> remote_act = remote(
130           static_cast<simgrid::simix::Comm*>(simcall_comm_wait__get__comm(&process->simcall)));
131         simgrid::mc::Remote<simgrid::simix::Comm> temp_act;
132         mc_model_checker->process().read(temp_act, remote_act);
133         simgrid::simix::Comm* act = static_cast<simgrid::simix::Comm*>(temp_act.data());
134         if (act->src_proc && act->dst_proc)
135           state->transition.argument = 0;
136         else if (act->src_proc == nullptr && act->type == SIMIX_COMM_READY
137               && act->detached == 1)
138           state->transition.argument = 0;
139         else
140           state->transition.argument = -1;
141         procstate->setDone();
142         req = &process->simcall;
143         break;
144       }
145
146       case SIMCALL_MC_RANDOM: {
147         int min_value = simcall_mc_random__get__min(&process->simcall);
148         state->transition.argument = procstate->interleave_count + min_value;
149         procstate->interleave_count++;
150         if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
151           procstate->setDone();
152         req = &process->simcall;
153         break;
154       }
155
156       default:
157         procstate->setDone();
158         state->transition.argument = 0;
159         req = &process->simcall;
160         break;
161   }
162   if (!req)
163     return nullptr;
164
165   // Fetch the data of the request and translate it:
166
167   state->transition.pid = process->pid;
168
169   state->executed_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     state->internal_req.issuer = req->issuer;
178     smx_synchro_t remote_comm;
179     read_element(mc_model_checker->process(),
180       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
181       state->transition.argument, sizeof(remote_comm));
182     mc_model_checker->process().read(state->internal_comm, remote(
183       static_cast<simgrid::simix::Comm*>(remote_comm)));
184     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
185     simcall_comm_wait__set__timeout(&state->internal_req, 0);
186     break;
187   }
188
189   case SIMCALL_COMM_TESTANY:
190     state->internal_req.call = SIMCALL_COMM_TEST;
191     state->internal_req.issuer = req->issuer;
192
193     if (state->transition.argument > 0) {
194       smx_synchro_t remote_comm;
195       read_element(mc_model_checker->process(),
196         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
197         state->transition.argument, sizeof(remote_comm));
198       mc_model_checker->process().read(state->internal_comm, remote(
199         static_cast<simgrid::simix::Comm*>(remote_comm)));
200     }
201
202     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
203     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
204     break;
205
206   case SIMCALL_COMM_WAIT:
207     state->internal_req = *req;
208     mc_model_checker->process().read_bytes(&state->internal_comm ,
209       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
210     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
211     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
212     break;
213
214   case SIMCALL_COMM_TEST:
215     state->internal_req = *req;
216     mc_model_checker->process().read_bytes(&state->internal_comm,
217       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
218     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
219     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
220     break;
221
222   default:
223     state->internal_req = *req;
224     break;
225   }
226
227   return req;
228 }
229
230 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
231 {
232   for (auto& p : mc_model_checker->process().simix_processes()) {
233     smx_simcall_t res = MC_state_get_request_for_process(state, &p.copy);
234     if (res)
235       return res;
236   }
237   return nullptr;
238 }