Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d6123a3e9f1bfc9edc300f61aa239a8f8138288b
[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
22 using simgrid::mc::remote;
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
25                                 "Logging specific to MC (state)");
26
27 /**
28  * \brief Creates a state data structure used by the exploration algorithm
29  */
30 simgrid::mc::State* MC_state_new()
31 {
32   simgrid::mc::State* state = new simgrid::mc::State();
33   state->processStates.resize(MC_smx_get_maxpid());
34   state->num = ++mc_stats->expanded_states;
35   /* Stateful model checking */
36   if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) ||  _sg_mc_termination){
37     state->system_state = simgrid::mc::take_snapshot(state->num);
38     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
39       MC_state_copy_incomplete_communications_pattern(state);
40       MC_state_copy_index_communications_pattern(state);
41     }
42   }
43   return state;
44 }
45
46 namespace simgrid {
47 namespace mc {
48
49 State::State()
50 {
51   std::memset(&this->internal_comm, 0, sizeof(this->internal_comm));
52   std::memset(&this->internal_req, 0, sizeof(this->internal_req));
53   std::memset(&this->executed_req, 0, sizeof(this->executed_req));
54 }
55
56 std::size_t State::interleaveSize() const
57 {
58   return std::count_if(this->processStates.begin(), this->processStates.end(),
59     [](simgrid::mc::ProcessState const& state) { return state.interleave(); });
60 }
61
62 }
63 }
64
65 void MC_state_interleave_process(simgrid::mc::State* state, smx_process_t process)
66 {
67   assert(state);
68   state->processStates[process->pid].state = simgrid::mc::ProcessInterleaveState::interleave;
69   state->processStates[process->pid].interleave_count = 0;
70 }
71
72 smx_simcall_t MC_state_get_executed_request(simgrid::mc::State* state, int *value)
73 {
74   *value = state->req_num;
75   return &state->executed_req;
76 }
77
78 smx_simcall_t MC_state_get_internal_request(simgrid::mc::State* state)
79 {
80   return &state->internal_req;
81 }
82
83 static inline smx_simcall_t MC_state_get_request_for_process(
84   simgrid::mc::State* state, int*value, smx_process_t process)
85 {
86   simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
87
88   if (!procstate->interleave())
89     return nullptr;
90   if (!simgrid::mc::process_is_enabled(process))
91     return nullptr;
92
93   smx_simcall_t req = nullptr;
94   switch (process->simcall.call) {
95       case SIMCALL_COMM_WAITANY:
96         *value = -1;
97         while (procstate->interleave_count <
98               read_length(mc_model_checker->process(),
99                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
100           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
101               procstate->interleave_count++)) {
102             *value = procstate->interleave_count - 1;
103             break;
104           }
105         }
106
107         if (procstate->interleave_count >=
108             simgrid::mc::read_length(mc_model_checker->process(),
109               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
110           procstate->state = simgrid::mc::ProcessInterleaveState::done;
111         if (*value != -1)
112           req = &process->simcall;
113         break;
114
115       case SIMCALL_COMM_TESTANY: {
116         unsigned start_count = procstate->interleave_count;
117         *value = -1;
118         while (procstate->interleave_count <
119                 read_length(mc_model_checker->process(),
120                   remote(simcall_comm_testany__get__comms(&process->simcall))))
121           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
122               procstate->interleave_count++)) {
123             *value = procstate->interleave_count - 1;
124             break;
125           }
126
127         if (procstate->interleave_count >=
128             read_length(mc_model_checker->process(),
129               remote(simcall_comm_testany__get__comms(&process->simcall))))
130           procstate->state = simgrid::mc::ProcessInterleaveState::done;
131
132         if (*value != -1 || start_count == 0)
133            req = &process->simcall;
134
135         break;
136       }
137
138       case SIMCALL_COMM_WAIT: {
139         smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
140         s_smx_synchro_t act;
141         mc_model_checker->process().read_bytes(
142           &act, sizeof(act), remote(remote_act));
143         if (act.comm.src_proc && act.comm.dst_proc)
144           *value = 0;
145         else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
146               && act.comm.detached == 1)
147           *value = 0;
148         else
149           *value = -1;
150         procstate->state = simgrid::mc::ProcessInterleaveState::done;
151         req = &process->simcall;
152         break;
153       }
154
155       case SIMCALL_MC_RANDOM:
156         if (procstate->state == simgrid::mc::ProcessInterleaveState::interleave)
157           *value = simcall_mc_random__get__min(&process->simcall);
158         else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
159           *value = state->req_num + 1;
160         procstate->state = simgrid::mc::ProcessInterleaveState::done;
161         req = &process->simcall;
162         break;
163
164       default:
165         procstate->state = simgrid::mc::ProcessInterleaveState::done;
166         *value = 0;
167         req = &process->simcall;
168         break;
169   }
170   if (!req)
171     return nullptr;
172
173   // Fetch the data of the request and translate it:
174
175   state->executed_req = *req;
176   state->req_num = *value;
177
178   /* The waitany and testany request are transformed into a wait or test request
179    * over the corresponding communication action so it can be treated later by
180    * the dependence function. */
181   switch (req->call) {
182   case SIMCALL_COMM_WAITANY: {
183     state->internal_req.call = SIMCALL_COMM_WAIT;
184     state->internal_req.issuer = req->issuer;
185     smx_synchro_t remote_comm;
186     read_element(mc_model_checker->process(),
187       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
188       *value, sizeof(remote_comm));
189     mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
190     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
191     simcall_comm_wait__set__timeout(&state->internal_req, 0);
192     break;
193   }
194
195   case SIMCALL_COMM_TESTANY:
196     state->internal_req.call = SIMCALL_COMM_TEST;
197     state->internal_req.issuer = req->issuer;
198
199     if (*value > 0) {
200       smx_synchro_t remote_comm;
201       read_element(mc_model_checker->process(),
202         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
203         *value, sizeof(remote_comm));
204       mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
205     }
206
207     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
208     simcall_comm_test__set__result(&state->internal_req, *value);
209     break;
210
211   case SIMCALL_COMM_WAIT:
212     state->internal_req = *req;
213     mc_model_checker->process().read_bytes(&state->internal_comm ,
214       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
215     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
216     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
217     break;
218
219   case SIMCALL_COMM_TEST:
220     state->internal_req = *req;
221     mc_model_checker->process().read_bytes(&state->internal_comm,
222       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
223     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
224     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
225     break;
226
227   case SIMCALL_MC_RANDOM: {
228     state->internal_req = *req;
229     int random_max = simcall_mc_random__get__max(req);
230     if (*value != random_max)
231       for (auto& p : mc_model_checker->process().simix_processes()) {
232         simgrid::mc::ProcessState* procstate = &state->processStates[p.copy.pid];
233         const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
234         if (p.copy.pid == issuer->pid) {
235           procstate->state = simgrid::mc::ProcessInterleaveState::more_interleave;
236           break;
237         }
238       }
239     break;
240   }
241
242   default:
243     state->internal_req = *req;
244     break;
245   }
246
247   return req;
248 }
249
250 smx_simcall_t MC_state_get_request(simgrid::mc::State* state, int *value)
251 {
252   for (auto& p : mc_model_checker->process().simix_processes()) {
253     smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);
254     if (res)
255       return res;
256   }
257   return nullptr;
258 }