Logo AND Algorithmique Numérique Distribuée

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