Logo AND Algorithmique Numérique Distribuée

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