Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50df4a3f11950c002db40e9183e9aa4c0e7f668c
[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, 0, sizeof(this->internal_comm));
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         smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
130         s_smx_synchro_t act;
131         mc_model_checker->process().read_bytes(
132           &act, sizeof(act), remote(remote_act));
133         if (act.comm.src_proc && act.comm.dst_proc)
134           state->transition.argument = 0;
135         else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
136               && act.comm.detached == 1)
137           state->transition.argument = 0;
138         else
139           state->transition.argument = -1;
140         procstate->setDone();
141         req = &process->simcall;
142         break;
143       }
144
145       case SIMCALL_MC_RANDOM: {
146         int min_value = simcall_mc_random__get__min(&process->simcall);
147         state->transition.argument = procstate->interleave_count + min_value;
148         procstate->interleave_count++;
149         if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
150           procstate->setDone();
151         req = &process->simcall;
152         break;
153       }
154
155       default:
156         procstate->setDone();
157         state->transition.argument = 0;
158         req = &process->simcall;
159         break;
160   }
161   if (!req)
162     return nullptr;
163
164   // Fetch the data of the request and translate it:
165
166   state->transition.pid = process->pid;
167
168   state->executed_req = *req;
169
170   /* The waitany and testany request are transformed into a wait or test request
171    * over the corresponding communication action so it can be treated later by
172    * the dependence function. */
173   switch (req->call) {
174   case SIMCALL_COMM_WAITANY: {
175     state->internal_req.call = SIMCALL_COMM_WAIT;
176     state->internal_req.issuer = req->issuer;
177     smx_synchro_t remote_comm;
178     read_element(mc_model_checker->process(),
179       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
180       state->transition.argument, sizeof(remote_comm));
181     mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
182     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
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     state->internal_req.issuer = req->issuer;
190
191     if (state->transition.argument > 0) {
192       smx_synchro_t remote_comm;
193       read_element(mc_model_checker->process(),
194         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
195         state->transition.argument, sizeof(remote_comm));
196       mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
197     }
198
199     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
200     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
201     break;
202
203   case SIMCALL_COMM_WAIT:
204     state->internal_req = *req;
205     mc_model_checker->process().read_bytes(&state->internal_comm ,
206       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
207     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
208     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
209     break;
210
211   case SIMCALL_COMM_TEST:
212     state->internal_req = *req;
213     mc_model_checker->process().read_bytes(&state->internal_comm,
214       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
215     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
216     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
217     break;
218
219   default:
220     state->internal_req = *req;
221     break;
222   }
223
224   return req;
225 }
226
227 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
228 {
229   for (auto& p : mc_model_checker->process().simix_processes()) {
230     smx_simcall_t res = MC_state_get_request_for_process(state, &p.copy);
231     if (res)
232       return res;
233   }
234   return nullptr;
235 }