Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
acbaabfe63eb5bd9ff9db7386f3c8445dac8f508
[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_random_create(min,max);
107   SIMIX_process_yield();
108
109   if(!mc_replay_mode)
110     return mc_current_state->executed_transition->current_value;
111   else
112     return mc_current_state->executed_transition->current_value - 1;
113 }
114
115 /**
116  * \brief Re-executes from the initial state all the transitions indicated by
117  *        a given model-checker stack.
118  * \param stack The stack with the transitions to execute.
119 */
120 void MC_replay(xbt_fifo_t stack)
121 {
122   xbt_fifo_item_t item;
123   mc_transition_t trans;
124
125   DEBUG0("**** Begin Replay ****");
126
127   /* Restore the initial state */
128   MC_restore_snapshot(initial_snapshot);
129
130   mc_replay_mode = TRUE;
131   
132   MC_UNSET_RAW_MEM;
133
134   /* Traverse the stack from the initial state and re-execute the transitions */
135   for(item = xbt_fifo_get_last_item(stack);
136       item != xbt_fifo_get_first_item(stack);
137       item = xbt_fifo_get_prev_item(item)){
138
139     mc_current_state = (mc_state_t) xbt_fifo_get_item_content(item);
140     trans = mc_current_state->executed_transition;
141
142     /* Update statistics */
143     mc_stats->visited_states++;
144     mc_stats->executed_transitions++;
145
146     DEBUG1("Executing transition %s", trans->name);
147     SIMIX_process_schedule(trans->process);
148
149     /* Do all surf's related black magic tricks to keep all working */
150     MC_execute_surf_actions();
151
152     /* Schedule every process that got enabled due to the executed transition */
153     MC_schedule_enabled_processes();
154   }
155   mc_replay_mode = FALSE;
156   DEBUG0("**** End Replay ****");
157 }
158
159 /**
160  * \brief Dumps the contents of a model-checker's stack and shows the actual
161  *        execution trace
162  * \param stack The stack to dump
163 */
164 void MC_dump_stack(xbt_fifo_t stack)
165 {
166   mc_state_t state;
167
168   MC_show_stack(stack);
169   
170   MC_SET_RAW_MEM;
171   while( (state = (mc_state_t)xbt_fifo_pop(stack)) != NULL )
172     MC_state_delete(state);
173   MC_UNSET_RAW_MEM;
174 }
175
176 void MC_show_stack(xbt_fifo_t stack)
177 {
178   mc_state_t state;
179   mc_transition_t trans;
180   xbt_fifo_item_t item;
181
182   for(item=xbt_fifo_get_last_item(stack);
183      (item?(state=(mc_state_t)(xbt_fifo_get_item_content(item))):(NULL));
184       item=xbt_fifo_get_prev_item(item)){
185     trans = state->executed_transition;
186     if(trans){      
187       INFO1("%s", trans->name);  
188     }
189   }
190 }
191
192 /**
193  * \brief Schedules all the process that are ready to run
194  *        As a side effect it performs some clean-up required by SIMIX 
195  */
196 void MC_schedule_enabled_processes(void)
197 {
198   smx_process_t process;
199
200   //SIMIX_process_empty_trash();
201
202   /* Schedule every process that is ready to run due to an finished action */
203   while ((process = xbt_swag_extract(simix_global->process_to_run))) {
204     DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
205     SIMIX_process_schedule(process);
206   }
207 }
208
209 /******************************** States **************************************/
210
211 /**
212  * \brief Creates a state data structure used by the exploration algorithm
213  */
214 mc_state_t MC_state_new(void)
215 {
216   mc_state_t state = NULL; 
217   
218   state = xbt_new0(s_mc_state_t, 1);
219   state->created_transitions = xbt_setset_new_set(mc_setset);
220   state->transitions = xbt_setset_new_set(mc_setset);
221   state->enabled_transitions = xbt_setset_new_set(mc_setset);
222   state->interleave = xbt_setset_new_set(mc_setset);
223   state->done = xbt_setset_new_set(mc_setset);
224   state->executed_transition = NULL;
225
226   mc_stats->expanded_states++;
227   
228   return state;
229 }
230 /**
231  * \brief Deletes a state data structure
232  * \param trans The state to be deleted
233  */
234 void MC_state_delete(mc_state_t state)
235 {
236   xbt_setset_cursor_t cursor;
237   mc_transition_t trans;  
238   
239   xbt_setset_foreach(state->created_transitions, cursor, trans){
240     MC_transition_delete(trans);
241   }
242
243   xbt_setset_destroy_set(state->created_transitions);
244   xbt_setset_destroy_set(state->transitions);
245   xbt_setset_destroy_set(state->enabled_transitions);
246   xbt_setset_destroy_set(state->interleave);
247   xbt_setset_destroy_set(state->done);
248
249   xbt_free(state);
250 }
251
252 /************************** SURF Emulation ************************************/
253
254 /* Dirty hack, we manipulate surf's clock to simplify the integration of the
255    model-checker */
256 extern double NOW;
257
258 /**
259  * \brief Executes all the actions at every model
260  */
261 void MC_execute_surf_actions(void)
262 {
263   unsigned int iter;
264   surf_action_t action = NULL;
265   surf_model_t model = NULL;
266   smx_action_t smx_action = NULL;
267
268   /* Execute all the actions in every model */
269   xbt_dynar_foreach(model_list, iter, model){
270     while ((action = xbt_swag_extract(model->states.running_action_set))){
271       /* FIXME: timeouts are not calculated correctly */
272       if(NOW >= action->max_duration){ 
273         surf_action_state_set(action, SURF_ACTION_DONE);
274         smx_action = action->data;
275         DEBUG5("Resource [%s] (%d): Executing RUNNING action \"%s\" (%p) MaxDuration %lf", 
276           model->name, xbt_swag_size(model->states.running_action_set),
277           smx_action->name, smx_action, action->max_duration);
278         
279         if(smx_action)
280           SIMIX_action_signal_all(smx_action);
281       }
282     }
283     /*FIXME: check if this is always empty or not */
284     while ((action = xbt_swag_extract(model->states.failed_action_set))) {
285       smx_action = action->data;
286       DEBUG4("Resource [%s] (%d): Executing FAILED action \"%s\" (%p)", 
287         model->name, xbt_swag_size(model->states.running_action_set),
288         smx_action->name, smx_action);
289       if (smx_action)
290        SIMIX_action_signal_all(smx_action);
291     }
292   }
293   /* That's it, now go one step deeper into the model-checking process! */
294   NOW += 0.5;  /* FIXME: Check time increases*/
295 }
296
297 /****************************** Statistics ************************************/
298 void MC_print_statistics(mc_stats_t stats)
299 {
300   INFO1("State space size ~= %lu", stats->state_size);
301   INFO1("Expanded states = %lu", stats->expanded_states);
302   INFO1("Visited states = %lu", stats->visited_states);
303   INFO1("Executed transitions = %lu", stats->executed_transitions);
304   INFO1("Expanded / Visited = %lf",
305     (double)stats->visited_states / stats->expanded_states);
306   /*INFO1("Exploration coverage = %lf", 
307     (double)stats->expanded_states / stats->state_size);*/
308 }
309
310 /************************* Assertion Checking *********************************/
311 void MC_assert(int prop)
312 {
313   if(!prop){
314     INFO0("**************************");
315     INFO0("*** PROPERTY NOT VALID ***");
316     INFO0("**************************");
317     INFO0("Counter-example execution trace:");
318     MC_dump_stack(mc_stack);
319     MC_print_statistics(mc_stats);
320     xbt_abort();
321   }
322 }
323