Logo AND Algorithmique Numérique Distribuée

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