Logo AND Algorithmique Numérique Distribuée

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