Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move visitedState as a field of SafetyChecker
[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 <xbt/log.h>
10 #include <xbt/sysdep.h>
11
12 #include "src/simix/smx_private.h"
13 #include "src/mc/mc_state.h"
14 #include "src/mc/mc_request.h"
15 #include "src/mc/mc_private.h"
16 #include "src/mc/mc_comm_pattern.h"
17 #include "src/mc/mc_smx.h"
18 #include "src/mc/mc_xbt.hpp"
19
20 using simgrid::mc::remote;
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
23                                 "Logging specific to MC (state)");
24
25 /**
26  * \brief Creates a state data structure used by the exploration algorithm
27  */
28 simgrid::mc::State* MC_state_new()
29 {
30   simgrid::mc::State* state = new simgrid::mc::State();
31   std::memset(&state->internal_comm, 0, sizeof(state->internal_comm));
32   std::memset(&state->internal_req, 0, sizeof(state->internal_req));
33   std::memset(&state->executed_req, 0, sizeof(state->executed_req));
34
35   state->max_pid = MC_smx_get_maxpid();
36   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
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->index_comm);
62   xbt_free(this->incomplete_comm_pattern);
63   xbt_free(this->proc_status);
64 }
65
66 }
67 }
68
69 /**
70  * \brief Deletes a state data structure
71  * \param trans The state to be deleted
72  */
73 void MC_state_delete(simgrid::mc::State* state, int free_snapshot)
74 {
75   delete state;
76 }
77
78 void MC_state_interleave_process(simgrid::mc::State* state, smx_process_t process)
79 {
80   assert(state);
81   state->proc_status[process->pid].state = MC_INTERLEAVE;
82   state->proc_status[process->pid].interleave_count = 0;
83 }
84
85 void MC_state_remove_interleave_process(simgrid::mc::State* state, smx_process_t process)
86 {
87   if (state->proc_status[process->pid].state == MC_INTERLEAVE)
88     state->proc_status[process->pid].state = MC_DONE;
89 }
90
91 unsigned int MC_state_interleave_size(simgrid::mc::State* state)
92 {
93   unsigned int i, size = 0;
94   for (i = 0; i < state->max_pid; i++)
95     if ((state->proc_status[i].state == MC_INTERLEAVE)
96         || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
97       size++;
98   return size;
99 }
100
101 int MC_state_process_is_done(simgrid::mc::State* state, smx_process_t process)
102 {
103   return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
104 }
105
106 void MC_state_set_executed_request(simgrid::mc::State* state, smx_simcall_t req,
107                                    int value)
108 {
109   state->executed_req = *req;
110   state->req_num = value;
111
112   /* The waitany and testany request are transformed into a wait or test request over the
113    * corresponding communication action so it can be treated later by the dependence
114    * function. */
115   switch (req->call) {
116   case SIMCALL_COMM_WAITANY: {
117     state->internal_req.call = SIMCALL_COMM_WAIT;
118     state->internal_req.issuer = req->issuer;
119     smx_synchro_t remote_comm;
120     read_element(mc_model_checker->process(),
121       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
122       value, sizeof(remote_comm));
123     mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
124     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
125     simcall_comm_wait__set__timeout(&state->internal_req, 0);
126     break;
127   }
128
129   case SIMCALL_COMM_TESTANY:
130     state->internal_req.call = SIMCALL_COMM_TEST;
131     state->internal_req.issuer = req->issuer;
132
133     if (value > 0) {
134       smx_synchro_t remote_comm;
135       read_element(mc_model_checker->process(),
136         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
137         value, sizeof(remote_comm));
138       mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
139     }
140
141     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
142     simcall_comm_test__set__result(&state->internal_req, value);
143     break;
144
145   case SIMCALL_COMM_WAIT:
146     state->internal_req = *req;
147     mc_model_checker->process().read_bytes(&state->internal_comm ,
148       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
149     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
150     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
151     break;
152
153   case SIMCALL_COMM_TEST:
154     state->internal_req = *req;
155     mc_model_checker->process().read_bytes(&state->internal_comm,
156       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
157     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
158     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
159     break;
160
161   case SIMCALL_MC_RANDOM: {
162     state->internal_req = *req;
163     int random_max = simcall_mc_random__get__max(req);
164     if (value != random_max)
165       for (auto& p : mc_model_checker->process().simix_processes()) {
166         mc_procstate_t procstate = &state->proc_status[p.copy.pid];
167         const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
168         if (p.copy.pid == issuer->pid) {
169           procstate->state = MC_MORE_INTERLEAVE;
170           break;
171         }
172       }
173     break;
174   }
175
176   default:
177     state->internal_req = *req;
178     break;
179   }
180 }
181
182 smx_simcall_t MC_state_get_executed_request(simgrid::mc::State* state, int *value)
183 {
184   *value = state->req_num;
185   return &state->executed_req;
186 }
187
188 smx_simcall_t MC_state_get_internal_request(simgrid::mc::State* state)
189 {
190   return &state->internal_req;
191 }
192
193 static inline smx_simcall_t MC_state_get_request_for_process(
194   simgrid::mc::State* state, int*value, smx_process_t process)
195 {
196   mc_procstate_t procstate = &state->proc_status[process->pid];
197
198   if (procstate->state != MC_INTERLEAVE
199       && procstate->state != MC_MORE_INTERLEAVE)
200       return nullptr;
201   if (!simgrid::mc::process_is_enabled(process))
202     return nullptr;
203
204   switch (process->simcall.call) {
205
206       case SIMCALL_COMM_WAITANY:
207         *value = -1;
208         while (procstate->interleave_count <
209               read_length(mc_model_checker->process(),
210                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
211           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
212               procstate->interleave_count++)) {
213             *value = procstate->interleave_count - 1;
214             break;
215           }
216         }
217
218         if (procstate->interleave_count >=
219             simgrid::mc::read_length(mc_model_checker->process(),
220               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
221           procstate->state = MC_DONE;
222
223         if (*value != -1)
224           return &process->simcall;
225
226         break;
227
228       case SIMCALL_COMM_TESTANY: {
229         unsigned start_count = procstate->interleave_count;
230         *value = -1;
231         while (procstate->interleave_count <
232                 read_length(mc_model_checker->process(),
233                   remote(simcall_comm_testany__get__comms(&process->simcall))))
234           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
235               procstate->interleave_count++)) {
236             *value = procstate->interleave_count - 1;
237             break;
238           }
239
240         if (procstate->interleave_count >=
241             read_length(mc_model_checker->process(),
242               remote(simcall_comm_testany__get__comms(&process->simcall))))
243           procstate->state = MC_DONE;
244
245         if (*value != -1 || start_count == 0)
246           return &process->simcall;
247
248         break;
249       }
250
251       case SIMCALL_COMM_WAIT: {
252         smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
253         s_smx_synchro_t act;
254         mc_model_checker->process().read_bytes(
255           &act, sizeof(act), remote(remote_act));
256         if (act.comm.src_proc && act.comm.dst_proc)
257           *value = 0;
258         else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
259               && act.comm.detached == 1)
260           *value = 0;
261         else
262           *value = -1;
263         procstate->state = MC_DONE;
264         return &process->simcall;
265       }
266
267       case SIMCALL_MC_RANDOM:
268         if (procstate->state == MC_INTERLEAVE)
269           *value = simcall_mc_random__get__min(&process->simcall);
270         else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
271           *value = state->req_num + 1;
272         procstate->state = MC_DONE;
273         return &process->simcall;
274
275       default:
276         procstate->state = MC_DONE;
277         *value = 0;
278         return &process->simcall;
279   }
280   return nullptr;
281 }
282
283 smx_simcall_t MC_state_get_request(simgrid::mc::State* state, int *value)
284 {
285   for (auto& p : mc_model_checker->process().simix_processes()) {
286     smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);
287     if (res)
288       return res;
289   }
290   return nullptr;
291 }