Logo AND Algorithmique Numérique Distribuée

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