Logo AND Algorithmique Numérique Distribuée

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