Logo AND Algorithmique Numérique Distribuée

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