Logo AND Algorithmique Numérique Distribuée

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