Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
68d6453533c5b4048c7474fb15e6454bd72a5533
[simgrid.git] / src / mc / mc_global.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <sys/time.h>
5
6 #include "../surf/surf_private.h"
7 #include "../simix/private.h"
8 #include "xbt/fifo.h"
9 #include "private.h"
10
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
13                                 "Logging specific to MC (global)");
14
15 /* MC global data structures */
16
17 mc_state_t mc_current_state = NULL;
18 char mc_replay_mode = FALSE;
19 double *mc_time = NULL;
20 mc_snapshot_t initial_snapshot = NULL;
21
22 /* Safety */
23
24 xbt_fifo_t mc_stack_safety_stateful = NULL;
25 xbt_fifo_t mc_stack_safety_stateless = NULL;
26 mc_stats_t mc_stats = NULL;
27
28 /* Liveness */
29
30 xbt_fifo_t mc_stack_liveness_stateful = NULL;
31 mc_stats_pair_t mc_stats_pair = NULL;
32 xbt_fifo_t mc_stack_liveness_stateless = NULL;
33
34
35 /**
36  *  \brief Initialize the model-checker data structures
37  */
38 void MC_init_safety_stateless(void)
39 {
40
41   /* Check if MC is already initialized */
42   if (initial_snapshot)
43     return;
44
45   mc_time = xbt_new0(double, simix_process_maxpid);
46
47   /* Initialize the data structures that must be persistent across every
48      iteration of the model-checker (in RAW memory) */
49   MC_SET_RAW_MEM;
50
51   /* Initialize statistics */
52   mc_stats = xbt_new0(s_mc_stats_t, 1);
53   mc_stats->state_size = 1;
54
55   /* Create exploration stack */
56   mc_stack_safety_stateless = xbt_fifo_new();
57
58   MC_UNSET_RAW_MEM;
59
60   MC_dpor_init();
61
62   MC_SET_RAW_MEM;
63   /* Save the initial state */
64   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
65   MC_take_snapshot(initial_snapshot);
66   MC_UNSET_RAW_MEM;
67 }
68
69 void MC_init_safety_stateful(void){
70
71   
72    /* Check if MC is already initialized */
73   if (initial_snapshot)
74     return;
75
76   mc_time = xbt_new0(double, simix_process_maxpid);
77
78   /* Initialize the data structures that must be persistent across every
79      iteration of the model-checker (in RAW memory) */
80   MC_SET_RAW_MEM;
81
82   /* Initialize statistics */
83   mc_stats = xbt_new0(s_mc_stats_t, 1);
84   mc_stats->state_size = 1;
85
86   /* Create exploration stack */
87   mc_stack_safety_stateful = xbt_fifo_new();
88
89   MC_UNSET_RAW_MEM;
90
91   MC_dpor_stateful_init();
92
93
94 }
95
96 void MC_init_liveness_stateful(xbt_automaton_t a){
97
98   XBT_DEBUG("Start init mc");
99   
100   mc_time = xbt_new0(double, simix_process_maxpid);
101
102   /* Initialize the data structures that must be persistent across every
103      iteration of the model-checker (in RAW memory) */
104
105   MC_SET_RAW_MEM;
106
107   /* Initialize statistics */
108   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
109   mc_stats = xbt_new0(s_mc_stats_t, 1);
110
111   XBT_DEBUG("Creating snapshot_stack");
112
113  /* Create exploration stack */
114   mc_stack_liveness_stateful = xbt_fifo_new();
115
116
117   MC_UNSET_RAW_MEM;
118
119   //MC_vddfs_stateful_init(a);
120   MC_ddfs_stateful_init(a);
121   //MC_dpor2_init(a);
122   //MC_dpor3_init(a);
123 }
124
125 void MC_init_liveness_stateless(xbt_automaton_t a){
126
127   XBT_DEBUG("Start init mc");
128   
129   mc_time = xbt_new0(double, simix_process_maxpid);
130
131   /* Initialize the data structures that must be persistent across every
132      iteration of the model-checker (in RAW memory) */
133
134   MC_SET_RAW_MEM;
135
136   /* Initialize statistics */
137   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
138
139   XBT_DEBUG("Creating snapshot_stack");
140
141  /* Create exploration stack */
142   mc_stack_liveness_stateless = xbt_fifo_new();
143
144   MC_UNSET_RAW_MEM;
145
146   MC_ddfs_stateless_init(a);
147 }
148
149
150 void MC_modelcheck(void)
151 {
152   MC_init_safety_stateless();
153   MC_dpor();
154   MC_exit();
155 }
156
157 void MC_modelcheck_stateful(void)
158 {
159   MC_init_safety_stateful();
160   MC_dpor_stateful();
161   MC_exit();
162 }
163
164 void MC_modelcheck_liveness_stateful(xbt_automaton_t a){
165   MC_init_liveness_stateful(a);
166   MC_exit_liveness();
167 }
168
169 void MC_modelcheck_liveness_stateless(xbt_automaton_t a){
170   MC_init_liveness_stateless(a);
171   MC_exit_liveness();
172 }
173
174 void MC_exit_liveness(void)
175 {
176   MC_print_statistics_pairs(mc_stats_pair);
177   xbt_free(mc_time);
178   MC_memory_exit();
179 }
180
181
182 void MC_exit(void)
183 {
184   MC_print_statistics(mc_stats);
185   xbt_free(mc_time);
186   MC_memory_exit();
187 }
188
189
190 int MC_random(int min, int max)
191 {
192   /*FIXME: return mc_current_state->executed_transition->random.value;*/
193   return 0;
194 }
195
196 /**
197  * \brief Schedules all the process that are ready to run
198  */
199 void MC_wait_for_requests(void)
200 {
201   smx_process_t process;
202   smx_req_t req;
203   unsigned int iter;
204
205   while (xbt_dynar_length(simix_global->process_to_run)) {
206     SIMIX_process_runall();
207     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
208       req = &process->request;
209       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
210           SIMIX_request_pre(req, 0);
211     }
212   }
213 }
214
215 int MC_deadlock_check()
216 {
217   int deadlock = FALSE;
218   smx_process_t process;
219   if(xbt_swag_size(simix_global->process_list)){
220     deadlock = TRUE;
221     xbt_swag_foreach(process, simix_global->process_list){
222       if(process->request.call != REQ_NO_REQ
223          && MC_request_is_enabled(&process->request)){
224         deadlock = FALSE;
225         break;
226       }
227     }
228   }
229   return deadlock;
230 }
231
232 /**
233  * \brief Re-executes from the initial state all the transitions indicated by
234  *        a given model-checker stack.
235  * \param stack The stack with the transitions to execute.
236 */
237 void MC_replay(xbt_fifo_t stack)
238 {
239   int value;
240   char *req_str;
241   smx_req_t req = NULL, saved_req = NULL;
242   xbt_fifo_item_t item;
243   mc_state_t state;
244
245   XBT_DEBUG("**** Begin Replay ****");
246
247   /* Restore the initial state */
248   MC_restore_snapshot(initial_snapshot);
249   /* At the moment of taking the snapshot the raw heap was set, so restoring
250    * it will set it back again, we have to unset it to continue  */
251   MC_UNSET_RAW_MEM;
252
253   /* Traverse the stack from the initial state and re-execute the transitions */
254   for (item = xbt_fifo_get_last_item(stack);
255        item != xbt_fifo_get_first_item(stack);
256        item = xbt_fifo_get_prev_item(item)) {
257
258     state = (mc_state_t) xbt_fifo_get_item_content(item);
259     saved_req = MC_state_get_executed_request(state, &value);
260    
261     if(saved_req){
262       /* because we got a copy of the executed request, we have to fetch the  
263          real one, pointed by the request field of the issuer process */
264       req = &saved_req->issuer->request;
265
266       /* Debug information */
267       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
268         req_str = MC_request_to_string(req, value);
269         XBT_DEBUG("Replay: %s (%p)", req_str, state);
270         xbt_free(req_str);
271       }
272     }
273          
274     SIMIX_request_pre(req, value);
275     MC_wait_for_requests();
276          
277     /* Update statistics */
278     mc_stats->visited_states++;
279     mc_stats->executed_transitions++;
280   }
281   XBT_DEBUG("**** End Replay ****");
282 }
283
284 /**
285  * \brief Dumps the contents of a model-checker's stack and shows the actual
286  *        execution trace
287  * \param stack The stack to dump
288 */
289 void MC_dump_stack_safety_stateless(xbt_fifo_t stack)
290 {
291   mc_state_t state;
292
293   MC_show_stack_safety_stateless(stack);
294
295   MC_SET_RAW_MEM;
296   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
297     MC_state_delete(state);
298   MC_UNSET_RAW_MEM;
299 }
300
301
302 void MC_show_stack_safety_stateless(xbt_fifo_t stack)
303 {
304   int value;
305   mc_state_t state;
306   xbt_fifo_item_t item;
307   smx_req_t req;
308   char *req_str = NULL;
309   
310   for (item = xbt_fifo_get_last_item(stack);
311        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
312         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
313     req = MC_state_get_executed_request(state, &value);
314     if(req){
315       req_str = MC_request_to_string(req, value);
316       XBT_INFO("%s", req_str);
317       xbt_free(req_str);
318     }
319   }
320 }
321
322 void MC_show_deadlock(smx_req_t req)
323 {
324   /*char *req_str = NULL;*/
325   XBT_INFO("**************************");
326   XBT_INFO("*** DEAD-LOCK DETECTED ***");
327   XBT_INFO("**************************");
328   XBT_INFO("Locked request:");
329   /*req_str = MC_request_to_string(req);
330   XBT_INFO("%s", req_str);
331   xbt_free(req_str);*/
332   XBT_INFO("Counter-example execution trace:");
333   MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
334 }
335
336 void MC_show_deadlock_stateful(smx_req_t req)
337 {
338   /*char *req_str = NULL;*/
339   XBT_INFO("**************************");
340   XBT_INFO("*** DEAD-LOCK DETECTED ***");
341   XBT_INFO("**************************");
342   XBT_INFO("Locked request:");
343   /*req_str = MC_request_to_string(req);
344   XBT_INFO("%s", req_str);
345   xbt_free(req_str);*/
346   XBT_INFO("Counter-example execution trace:");
347   MC_show_stack_safety_stateful(mc_stack_safety_stateful);
348 }
349
350 void MC_dump_stack_safety_stateful(xbt_fifo_t stack)
351 {
352   //mc_state_ws_t state;
353
354   MC_show_stack_safety_stateful(stack);
355
356   /*MC_SET_RAW_MEM;
357   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
358     MC_state_delete(state);
359     MC_UNSET_RAW_MEM;*/
360 }
361
362
363 void MC_show_stack_safety_stateful(xbt_fifo_t stack)
364 {
365   int value;
366   mc_state_ws_t state;
367   xbt_fifo_item_t item;
368   smx_req_t req;
369   char *req_str = NULL;
370   
371   for (item = xbt_fifo_get_last_item(stack);
372        (item ? (state = (mc_state_ws_t) (xbt_fifo_get_item_content(item)))
373         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
374     req = MC_state_get_executed_request(state->graph_state, &value);
375     if(req){
376       req_str = MC_request_to_string(req, value);
377       XBT_INFO("%s", req_str);
378       xbt_free(req_str);
379     }
380   }
381 }
382
383 void MC_show_stack_liveness_stateful(xbt_fifo_t stack){
384   int value;
385   mc_pair_t pair;
386   xbt_fifo_item_t item;
387   smx_req_t req;
388   char *req_str = NULL;
389   
390   for (item = xbt_fifo_get_last_item(stack);
391        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
392         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
393     req = MC_state_get_executed_request(pair->graph_state, &value);
394     if(req){
395       req_str = MC_request_to_string(req, value);
396       XBT_INFO("%s", req_str);
397       xbt_free(req_str);
398     }
399   }
400 }
401
402 void MC_dump_stack_liveness_stateful(xbt_fifo_t stack){
403   mc_pair_t pair;
404
405   MC_SET_RAW_MEM;
406   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
407     MC_pair_delete(pair);
408   MC_UNSET_RAW_MEM;
409 }
410
411 void MC_show_stack_liveness_stateless(xbt_fifo_t stack){
412   int value;
413   mc_pair_stateless_t pair;
414   xbt_fifo_item_t item;
415   smx_req_t req;
416   char *req_str = NULL;
417   
418   for (item = xbt_fifo_get_last_item(stack);
419        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
420         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
421     req = MC_state_get_executed_request(pair->graph_state, &value);
422     if(req){
423       req_str = MC_request_to_string(req, value);
424       XBT_INFO("%s", req_str);
425       xbt_free(req_str);
426     }
427   }
428 }
429
430 void MC_dump_stack_liveness_stateless(xbt_fifo_t stack){
431   mc_pair_stateless_t pair;
432
433   MC_SET_RAW_MEM;
434   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
435     MC_pair_stateless_delete(pair);
436   MC_UNSET_RAW_MEM;
437 }
438
439
440 void MC_print_statistics(mc_stats_t stats)
441 {
442   XBT_INFO("State space size ~= %lu", stats->state_size);
443   XBT_INFO("Expanded states = %lu", stats->expanded_states);
444   XBT_INFO("Visited states = %lu", stats->visited_states);
445   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
446   XBT_INFO("Expanded / Visited = %lf",
447         (double) stats->visited_states / stats->expanded_states);
448   /*XBT_INFO("Exploration coverage = %lf",
449      (double)stats->expanded_states / stats->state_size); */
450 }
451
452 void MC_print_statistics_pairs(mc_stats_pair_t stats)
453 {
454   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
455   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
456   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
457   XBT_INFO("Expanded / Visited = %lf",
458         (double) stats->visited_pairs / stats->expanded_pairs);
459   /*XBT_INFO("Exploration coverage = %lf",
460      (double)stats->expanded_states / stats->state_size); */
461 }
462
463 void MC_assert(int prop)
464 {
465   if (MC_IS_ENABLED && !prop) {
466     XBT_INFO("**************************");
467     XBT_INFO("*** PROPERTY NOT VALID ***");
468     XBT_INFO("**************************");
469     XBT_INFO("Counter-example execution trace:");
470     MC_dump_stack_safety_stateless(mc_stack_safety_stateless);
471     MC_print_statistics(mc_stats);
472     xbt_abort();
473   }
474 }
475
476 void MC_assert_stateful(int prop)
477 {
478   if (MC_IS_ENABLED && !prop) {
479     XBT_INFO("**************************");
480     XBT_INFO("*** PROPERTY NOT VALID ***");
481     XBT_INFO("**************************");
482     XBT_INFO("Counter-example execution trace:");
483     MC_dump_stack_safety_stateful(mc_stack_safety_stateful);
484     MC_print_statistics(mc_stats);
485     xbt_abort();
486   }
487 }
488
489 void MC_assert_pair_stateful(int prop){
490   if (MC_IS_ENABLED && !prop) {
491     XBT_INFO("**************************");
492     XBT_INFO("*** PROPERTY NOT VALID ***");
493     XBT_INFO("**************************");
494     //XBT_INFO("Counter-example execution trace:");
495     MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
496     //MC_dump_snapshot_stack(mc_snapshot_stack);
497     MC_print_statistics_pairs(mc_stats_pair);
498     xbt_abort();
499   }
500 }
501
502 void MC_assert_pair_stateless(int prop){
503   if (MC_IS_ENABLED && !prop) {
504     XBT_INFO("**************************");
505     XBT_INFO("*** PROPERTY NOT VALID ***");
506     XBT_INFO("**************************");
507     //XBT_INFO("Counter-example execution trace:");
508     MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
509     //MC_dump_snapshot_stack(mc_snapshot_stack);
510     MC_print_statistics_pairs(mc_stats_pair);
511     xbt_abort();
512   }
513 }
514
515 void MC_process_clock_add(smx_process_t process, double amount)
516 {
517   mc_time[process->pid] += amount;
518 }
519
520 double MC_process_clock_get(smx_process_t process)
521 {
522   if(mc_time)
523     return mc_time[process->pid];
524   else
525     return 0;
526 }