Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9cd6c7f6dbe2311b74dd53cda56cb3121fef3c4a
[simgrid.git] / src / mc / mc_global.c
1 #include "../surf/surf_private.h"
2 #include "../simix/private.h"
3 #include "xbt/fifo.h"
4 #include "private.h"
5
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
7                                 "Logging specific to MC (global)");
8
9 /* MC global data structures */
10 mc_snapshot_t initial_snapshot = NULL;
11 xbt_fifo_t mc_stack = NULL;
12 xbt_setset_t mc_setset = NULL;
13 mc_stats_t mc_stats = NULL;
14 mc_state_t mc_current_state = NULL;
15 char mc_replay_mode = FALSE;
16
17 /**
18  *  \brief Initialize the model-checker data structures
19  */
20 void MC_init(int method)
21 {
22   /* Check if MC is already initialized */
23   if (initial_snapshot)
24     return;
25
26   /* Initialize the data structures that must be persistent across every
27      iteration of the model-checker (in RAW memory) */
28   MC_SET_RAW_MEM;
29
30   /* Initialize statistics */
31   mc_stats = xbt_new0(s_mc_stats_t, 1);
32   mc_stats->state_size = 1;
33
34   /* Create exploration stack */
35   mc_stack = xbt_fifo_new();
36
37   /* Create the container for the sets */
38   mc_setset = xbt_setset_new(20);
39
40   switch (method) {
41   case 0:
42     MC_dfs_init();
43     break;
44   case 1:
45     MC_dpor_init();
46     break;
47   default:
48     break;
49   }
50
51   /* Save the initial state */
52   MC_SET_RAW_MEM;
53   initial_snapshot = xbt_new(s_mc_snapshot_t, 1);
54   MC_take_snapshot(initial_snapshot);
55   MC_UNSET_RAW_MEM;
56 }
57
58 void MC_modelcheck(int method)
59 {
60
61   MC_init(method);
62
63   switch (method) {
64   case 0:
65     MC_dfs();
66     break;
67   case 1:
68     MC_dpor();
69     break;
70   default:
71     break;
72   }
73
74   MC_exit(method);
75 }
76
77 void MC_exit(int method)
78 {
79   mc_state_t state;
80
81   switch (method) {
82   case 0:
83     //MC_dfs_exit();
84     break;
85   case 1:
86     //MC_dpor_exit();
87     break;
88   default:
89     break;
90   }
91
92   /* Destroy MC data structures (in RAW memory) */
93   MC_SET_RAW_MEM;
94   xbt_free(mc_stats);
95
96   while ((state = (mc_state_t) xbt_fifo_pop(mc_stack)) != NULL)
97     MC_state_delete(state);
98
99   xbt_fifo_free(mc_stack);
100   xbt_setset_destroy(mc_setset);
101   MC_UNSET_RAW_MEM;
102 }
103
104 int MC_random(int min, int max)
105 {
106   MC_trans_intercept_random(min, max);
107   return mc_current_state->executed_transition->random.value;
108 }
109
110 /**
111  * \brief Re-executes from the initial state all the transitions indicated by
112  *        a given model-checker stack.
113  * \param stack The stack with the transitions to execute.
114 */
115 void MC_replay(xbt_fifo_t stack)
116 {
117   xbt_fifo_item_t item;
118   mc_transition_t trans;
119
120   DEBUG0("**** Begin Replay ****");
121
122   /* Restore the initial state */
123   MC_restore_snapshot(initial_snapshot);
124
125   mc_replay_mode = TRUE;
126
127   MC_UNSET_RAW_MEM;
128
129   /* Traverse the stack from the initial state and re-execute the transitions */
130   for (item = xbt_fifo_get_last_item(stack);
131        item != xbt_fifo_get_first_item(stack);
132        item = xbt_fifo_get_prev_item(item)) {
133
134     mc_current_state = (mc_state_t) xbt_fifo_get_item_content(item);
135     trans = mc_current_state->executed_transition;
136
137     /* Update statistics */
138     mc_stats->visited_states++;
139     mc_stats->executed_transitions++;
140
141     DEBUG1("Executing transition %s", trans->name);
142     SIMIX_process_schedule(trans->process);
143
144     /* Do all surf's related black magic tricks to keep all working */
145     MC_execute_surf_actions();
146
147     /* Schedule every process that got enabled due to the executed transition */
148     MC_schedule_enabled_processes();
149   }
150   mc_replay_mode = FALSE;
151   DEBUG0("**** End Replay ****");
152 }
153
154 /**
155  * \brief Dumps the contents of a model-checker's stack and shows the actual
156  *        execution trace
157  * \param stack The stack to dump
158 */
159 void MC_dump_stack(xbt_fifo_t stack)
160 {
161   mc_state_t state;
162
163   MC_show_stack(stack);
164
165   MC_SET_RAW_MEM;
166   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
167     MC_state_delete(state);
168   MC_UNSET_RAW_MEM;
169 }
170
171 void MC_show_stack(xbt_fifo_t stack)
172 {
173   mc_state_t state;
174   mc_transition_t trans;
175   xbt_fifo_item_t item;
176
177   for (item = xbt_fifo_get_last_item(stack);
178        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
179         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
180     trans = state->executed_transition;
181     if (trans) {
182       INFO1("%s", trans->name);
183     }
184   }
185 }
186
187 /**
188  * \brief Schedules all the process that are ready to run
189  *        As a side effect it performs some clean-up required by SIMIX 
190  */
191 void MC_schedule_enabled_processes(void)
192 {
193   smx_process_t process;
194
195   //SIMIX_process_empty_trash();
196
197   /* Schedule every process that is ready to run due to an finished action */
198   while ((process = xbt_swag_extract(simix_global->process_to_run))) {
199     DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
200     SIMIX_process_schedule(process);
201   }
202 }
203
204 /******************************** States **************************************/
205
206 /**
207  * \brief Creates a state data structure used by the exploration algorithm
208  */
209 mc_state_t MC_state_new(void)
210 {
211   mc_state_t state = NULL;
212
213   state = xbt_new0(s_mc_state_t, 1);
214   state->created_transitions = xbt_setset_new_set(mc_setset);
215   state->transitions = xbt_setset_new_set(mc_setset);
216   state->enabled_transitions = xbt_setset_new_set(mc_setset);
217   state->interleave = xbt_setset_new_set(mc_setset);
218   state->done = xbt_setset_new_set(mc_setset);
219   state->executed_transition = NULL;
220
221   mc_stats->expanded_states++;
222
223   return state;
224 }
225
226 /**
227  * \brief Deletes a state data structure
228  * \param trans The state to be deleted
229  */
230 void MC_state_delete(mc_state_t state)
231 {
232   xbt_setset_cursor_t cursor;
233   mc_transition_t trans;
234
235   xbt_setset_foreach(state->created_transitions, cursor, trans) {
236     xbt_setset_elm_remove(mc_setset, trans);
237     MC_transition_delete(trans);
238   }
239
240   xbt_setset_destroy_set(state->created_transitions);
241   xbt_setset_destroy_set(state->transitions);
242   xbt_setset_destroy_set(state->enabled_transitions);
243   xbt_setset_destroy_set(state->interleave);
244   xbt_setset_destroy_set(state->done);
245
246   xbt_free(state);
247 }
248
249 /************************** SURF Emulation ************************************/
250
251 /* Dirty hack, we manipulate surf's clock to simplify the integration of the
252    model-checker */
253 extern double NOW;
254
255 /**
256  * \brief Executes all the actions at every model
257  */
258 void MC_execute_surf_actions(void)
259 {
260   unsigned int iter;
261   surf_action_t action = NULL;
262   surf_model_t model = NULL;
263   smx_action_t smx_action = NULL;
264
265   /* Execute all the actions in every model */
266   xbt_dynar_foreach(model_list, iter, model) {
267     while ((action = xbt_swag_extract(model->states.running_action_set))) {
268       /* FIXME: timeouts are not calculated correctly */
269       if (NOW >= action->max_duration) {
270         surf_action_state_set(action, SURF_ACTION_DONE);
271         smx_action = action->data;
272         DEBUG5
273             ("Resource [%s] (%d): Executing RUNNING action \"%s\" (%p) MaxDuration %lf",
274              model->name, xbt_swag_size(model->states.running_action_set),
275              smx_action->name, smx_action, action->max_duration);
276
277         if (smx_action)
278           SIMIX_action_signal_all(smx_action);
279       }
280     }
281     /*FIXME: check if this is always empty or not */
282     while ((action = xbt_swag_extract(model->states.failed_action_set))) {
283       smx_action = action->data;
284       DEBUG4("Resource [%s] (%d): Executing FAILED action \"%s\" (%p)",
285              model->name, xbt_swag_size(model->states.running_action_set),
286              smx_action->name, smx_action);
287       if (smx_action)
288         SIMIX_action_signal_all(smx_action);
289     }
290   }
291   /* That's it, now go one step deeper into the model-checking process! */
292   NOW += 0.5;                   /* FIXME: Check time increases */
293 }
294
295 /****************************** Statistics ************************************/
296 void MC_print_statistics(mc_stats_t stats)
297 {
298   INFO1("State space size ~= %lu", stats->state_size);
299   INFO1("Expanded states = %lu", stats->expanded_states);
300   INFO1("Visited states = %lu", stats->visited_states);
301   INFO1("Executed transitions = %lu", stats->executed_transitions);
302   INFO1("Expanded / Visited = %lf",
303         (double) stats->visited_states / stats->expanded_states);
304   /*INFO1("Exploration coverage = %lf", 
305      (double)stats->expanded_states / stats->state_size); */
306 }
307
308 /************************* Assertion Checking *********************************/
309 void MC_assert(int prop)
310 {
311   if (!prop) {
312     INFO0("**************************");
313     INFO0("*** PROPERTY NOT VALID ***");
314     INFO0("**************************");
315     INFO0("Counter-example execution trace:");
316     MC_dump_stack(mc_stack);
317     MC_print_statistics(mc_stats);
318     xbt_abort();
319   }
320 }