Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix comm determinism detection mechanisms
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2014. 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 <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/time.h>
11 #include <sys/mman.h>
12 #include <libgen.h>
13
14 #include "simgrid/sg_config.h"
15 #include "../surf/surf_private.h"
16 #include "../simix/smx_private.h"
17 #include "../xbt/mmalloc/mmprivate.h"
18 #include "xbt/fifo.h"
19 #include "mc_private.h"
20 #include "xbt/automaton.h"
21 #include "xbt/dict.h"
22
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
25                                 "Logging specific to MC (global)");
26
27 /* Configuration support */
28 e_mc_reduce_t mc_reduce_kind = e_mc_reduce_unset;
29
30 int _sg_do_model_check = 0;
31 int _sg_mc_checkpoint = 0;
32 char *_sg_mc_property_file = NULL;
33 int _sg_mc_timeout = 0;
34 int _sg_mc_hash = 0;
35 int _sg_mc_max_depth = 1000;
36 int _sg_mc_visited = 0;
37 char *_sg_mc_dot_output_file = NULL;
38 int _sg_mc_comms_determinism = 0;
39 int _sg_mc_send_determinism = 0;
40 int _sg_mc_safety = 0;
41 int _sg_mc_liveness = 0;
42
43 int user_max_depth_reached = 0;
44
45 void _mc_cfg_cb_reduce(const char *name, int pos)
46 {
47   if (_sg_cfg_init_status && !_sg_do_model_check) {
48     xbt_die
49         ("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
50   }
51   char *val = xbt_cfg_get_string(_sg_cfg_set, name);
52   if (!strcasecmp(val, "none")) {
53     mc_reduce_kind = e_mc_reduce_none;
54   } else if (!strcasecmp(val, "dpor")) {
55     mc_reduce_kind = e_mc_reduce_dpor;
56   } else {
57     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",
58             name);
59   }
60 }
61
62 void _mc_cfg_cb_checkpoint(const char *name, int pos)
63 {
64   if (_sg_cfg_init_status && !_sg_do_model_check) {
65     xbt_die
66         ("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
67   }
68   _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
69 }
70
71 void _mc_cfg_cb_property(const char *name, int pos)
72 {
73   if (_sg_cfg_init_status && !_sg_do_model_check) {
74     xbt_die
75         ("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
76   }
77   _sg_mc_property_file = xbt_cfg_get_string(_sg_cfg_set, name);
78 }
79
80 void _mc_cfg_cb_timeout(const char *name, int pos)
81 {
82   if (_sg_cfg_init_status && !_sg_do_model_check) {
83     xbt_die
84         ("You are specifying a value to enable/disable timeout for wait requests after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
85   }
86   _sg_mc_timeout = xbt_cfg_get_boolean(_sg_cfg_set, name);
87 }
88
89 void _mc_cfg_cb_hash(const char *name, int pos)
90 {
91   if (_sg_cfg_init_status && !_sg_do_model_check) {
92     xbt_die
93         ("You are specifying a value to enable/disable the use of global hash to speedup state comparaison, but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
94   }
95   _sg_mc_hash = xbt_cfg_get_boolean(_sg_cfg_set, name);
96 }
97
98 void _mc_cfg_cb_max_depth(const char *name, int pos)
99 {
100   if (_sg_cfg_init_status && !_sg_do_model_check) {
101     xbt_die
102         ("You are specifying a max depth value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
103   }
104   _sg_mc_max_depth = xbt_cfg_get_int(_sg_cfg_set, name);
105 }
106
107 void _mc_cfg_cb_visited(const char *name, int pos)
108 {
109   if (_sg_cfg_init_status && !_sg_do_model_check) {
110     xbt_die
111         ("You are specifying a number of stored visited states after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
112   }
113   _sg_mc_visited = xbt_cfg_get_int(_sg_cfg_set, name);
114 }
115
116 void _mc_cfg_cb_dot_output(const char *name, int pos)
117 {
118   if (_sg_cfg_init_status && !_sg_do_model_check) {
119     xbt_die
120         ("You are specifying a file name for a dot output of graph state after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
121   }
122   _sg_mc_dot_output_file = xbt_cfg_get_string(_sg_cfg_set, name);
123 }
124
125 void _mc_cfg_cb_comms_determinism(const char *name, int pos)
126 {
127   if (_sg_cfg_init_status && !_sg_do_model_check) {
128     xbt_die
129         ("You are specifying a value to enable/disable the detection of determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
130   }
131   _sg_mc_comms_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
132 }
133
134 void _mc_cfg_cb_send_determinism(const char *name, int pos)
135 {
136   if (_sg_cfg_init_status && !_sg_do_model_check) {
137     xbt_die
138         ("You are specifying a value to enable/disable the detection of send-determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
139   }
140   _sg_mc_send_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
141 }
142
143 /* MC global data structures */
144 mc_state_t mc_current_state = NULL;
145 char mc_replay_mode = FALSE;
146 double *mc_time = NULL;
147 __thread mc_comparison_times_t mc_comp_times = NULL;
148 __thread double mc_snapshot_comparison_time;
149 mc_stats_t mc_stats = NULL;
150 mc_global_t initial_global_state = NULL;
151 xbt_fifo_t mc_stack = NULL;
152
153 /* Liveness */
154 xbt_automaton_t _mc_property_automaton = NULL;
155
156 /* Variables */
157 mc_object_info_t mc_libsimgrid_info = NULL;
158 mc_object_info_t mc_binary_info = NULL;
159
160 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
161
162 size_t mc_object_infos_size = 2;
163
164 /* Dot output */
165 FILE *dot_output = NULL;
166 const char *colors[13];
167
168
169 /*******************************  Initialisation of MC *******************************/
170 /*********************************************************************************/
171
172 static void MC_init_dot_output()
173 {                               /* FIXME : more colors */
174
175   colors[0] = "blue";
176   colors[1] = "red";
177   colors[2] = "green3";
178   colors[3] = "goldenrod";
179   colors[4] = "brown";
180   colors[5] = "purple";
181   colors[6] = "magenta";
182   colors[7] = "turquoise4";
183   colors[8] = "gray25";
184   colors[9] = "forestgreen";
185   colors[10] = "hotpink";
186   colors[11] = "lightblue";
187   colors[12] = "tan";
188
189   dot_output = fopen(_sg_mc_dot_output_file, "w");
190
191   if (dot_output == NULL) {
192     perror("Error open dot output file");
193     xbt_abort();
194   }
195
196   fprintf(dot_output,
197           "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
198
199 }
200
201 static void MC_init_debug_info(void)
202 {
203   XBT_INFO("Get debug information ...");
204
205   memory_map_t maps = MC_get_memory_map();
206
207   /* Get local variables for state equality detection */
208   mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
209   mc_object_infos[0] = mc_binary_info;
210
211   mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
212   mc_object_infos[1] = mc_libsimgrid_info;
213
214   // Use information of the other objects:
215   MC_post_process_object_info(mc_binary_info);
216   MC_post_process_object_info(mc_libsimgrid_info);
217
218   MC_free_memory_map(maps);
219   XBT_INFO("Get debug information done !");
220 }
221
222
223 void MC_init()
224 {
225
226   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
227
228   mc_time = xbt_new0(double, simix_process_maxpid);
229
230   /* Initialize the data structures that must be persistent across every
231      iteration of the model-checker (in RAW memory) */
232
233   MC_SET_MC_HEAP;
234
235   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
236
237   /* Initialize statistics */
238   mc_stats = xbt_new0(s_mc_stats_t, 1);
239   mc_stats->state_size = 1;
240
241   MC_init_memory_map_info();
242   MC_init_debug_info();         /* FIXME : get debug information only if liveness verification or visited state reduction */
243
244   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
245     MC_init_dot_output();
246
247   /* Init parmap */
248   //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
249
250   MC_SET_STD_HEAP;
251
252   if (_sg_mc_visited > 0
253       || (_sg_mc_property_file && _sg_mc_property_file[0] != '\0')) {
254     /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
255     MC_ignore_local_variable("e", "*");
256     MC_ignore_local_variable("__ex_cleanup", "*");
257     MC_ignore_local_variable("__ex_mctx_en", "*");
258     MC_ignore_local_variable("__ex_mctx_me", "*");
259     MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
260     MC_ignore_local_variable("_log_ev", "*");
261     MC_ignore_local_variable("_throw_ctx", "*");
262     MC_ignore_local_variable("ctx", "*");
263
264     MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
265     MC_ignore_local_variable("next_context", "smx_ctx_sysv_suspend_serial");
266     MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
267
268     /* Ignore local variable about time used for tracing */
269     MC_ignore_local_variable("start_time", "*");
270
271     MC_ignore_global_variable("compared_pointers");
272     MC_ignore_global_variable("mc_comp_times");
273     MC_ignore_global_variable("mc_snapshot_comparison_time");
274     MC_ignore_global_variable("mc_time");
275     MC_ignore_global_variable("smpi_current_rank");
276     MC_ignore_global_variable("counter");       /* Static variable used for tracing */
277     MC_ignore_global_variable("maestro_stack_start");
278     MC_ignore_global_variable("maestro_stack_end");
279     MC_ignore_global_variable("smx_total_comms");
280
281     MC_ignore_heap(&(simix_global->process_to_run),
282                    sizeof(simix_global->process_to_run));
283     MC_ignore_heap(&(simix_global->process_that_ran),
284                    sizeof(simix_global->process_that_ran));
285     MC_ignore_heap(simix_global->process_to_run,
286                    sizeof(*(simix_global->process_to_run)));
287     MC_ignore_heap(simix_global->process_that_ran,
288                    sizeof(*(simix_global->process_that_ran)));
289     MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
290
291     smx_process_t process;
292     xbt_swag_foreach(process, simix_global->process_list) {
293       MC_ignore_heap(&(process->process_hookup),
294                      sizeof(process->process_hookup));
295     }
296   }
297
298   if (raw_mem_set)
299     MC_SET_MC_HEAP;
300
301 }
302
303 /*******************************  Core of MC *******************************/
304 /**************************************************************************/
305
306 static void MC_modelcheck_comm_determinism_init(void)
307 {
308
309   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
310
311   MC_init();
312
313   if (!mc_mem_set)
314     MC_SET_MC_HEAP;
315
316   /* Create exploration stack */
317   mc_stack = xbt_fifo_new();
318
319   MC_SET_STD_HEAP;
320
321   MC_pre_modelcheck_comm_determinism();
322
323   MC_SET_MC_HEAP;
324   initial_global_state = xbt_new0(s_mc_global_t, 1);
325   initial_global_state->snapshot = MC_take_snapshot(0);
326   initial_global_state->initial_communications_pattern_done = 0;
327   initial_global_state->comm_deterministic = 1;
328   initial_global_state->send_deterministic = 1;
329   MC_SET_STD_HEAP;
330
331   MC_modelcheck_comm_determinism();
332
333   if(mc_mem_set)
334     MC_SET_MC_HEAP;
335
336 }
337
338 static void MC_modelcheck_safety_init(void)
339 {
340   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
341
342   _sg_mc_safety = 1;
343
344   MC_init();
345
346   if (!mc_mem_set)
347     MC_SET_MC_HEAP;
348
349   /* Create exploration stack */
350   mc_stack = xbt_fifo_new();
351
352   MC_SET_STD_HEAP;
353
354   MC_pre_modelcheck_safety();
355
356   MC_SET_MC_HEAP;
357   /* Save the initial state */
358   initial_global_state = xbt_new0(s_mc_global_t, 1);
359   initial_global_state->snapshot = MC_take_snapshot(0);
360   MC_SET_STD_HEAP;
361
362   MC_modelcheck_safety();
363
364   if (mc_mem_set)
365     MC_SET_MC_HEAP;
366
367   xbt_abort();
368   //MC_exit();
369 }
370
371 static void MC_modelcheck_liveness_init()
372 {
373
374   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
375
376   _sg_mc_liveness = 1;
377
378   MC_init();
379
380   if (!mc_mem_set)
381     MC_SET_MC_HEAP;
382
383   /* Create exploration stack */
384   mc_stack = xbt_fifo_new();
385
386   /* Create the initial state */
387   initial_global_state = xbt_new0(s_mc_global_t, 1);
388
389   MC_SET_STD_HEAP;
390
391   MC_pre_modelcheck_liveness();
392
393   /* We're done */
394   MC_print_statistics(mc_stats);
395   xbt_free(mc_time);
396
397   if (mc_mem_set)
398     MC_SET_MC_HEAP;
399
400 }
401
402 void MC_do_the_modelcheck_for_real()
403 {
404
405   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
406     XBT_INFO("Check communication determinism");
407     MC_modelcheck_comm_determinism_init();
408   } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
409     if (mc_reduce_kind == e_mc_reduce_unset)
410       mc_reduce_kind = e_mc_reduce_dpor;
411     XBT_INFO("Check a safety property");
412     MC_modelcheck_safety_init();
413   } else {
414     if (mc_reduce_kind == e_mc_reduce_unset)
415       mc_reduce_kind = e_mc_reduce_none;
416     XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
417     MC_automaton_load(_sg_mc_property_file);
418     MC_modelcheck_liveness_init();
419   }
420 }
421
422
423 void MC_exit(void)
424 {
425   xbt_free(mc_time);
426
427   MC_memory_exit();
428   //xbt_abort();
429 }
430
431 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max)
432 {
433
434   return simcall->mc_value;
435 }
436
437
438 int MC_random(int min, int max)
439 {
440   /*FIXME: return mc_current_state->executed_transition->random.value; */
441   return simcall_mc_random(min, max);
442 }
443
444 /**
445  * \brief Schedules all the process that are ready to run
446  */
447 void MC_wait_for_requests(void)
448 {
449   smx_process_t process;
450   smx_simcall_t req;
451   unsigned int iter;
452
453   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
454     SIMIX_process_runall();
455     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
456       req = &process->simcall;
457       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
458         SIMIX_simcall_pre(req, 0);
459     }
460   }
461 }
462
463 int MC_deadlock_check()
464 {
465   int deadlock = FALSE;
466   smx_process_t process;
467   if (xbt_swag_size(simix_global->process_list)) {
468     deadlock = TRUE;
469     xbt_swag_foreach(process, simix_global->process_list) {
470       if (process->simcall.call != SIMCALL_NONE
471           && MC_request_is_enabled(&process->simcall)) {
472         deadlock = FALSE;
473         break;
474       }
475     }
476   }
477   return deadlock;
478 }
479
480 /**
481  * \brief Re-executes from the state at position start all the transitions indicated by
482  *        a given model-checker stack.
483  * \param stack The stack with the transitions to execute.
484  * \param start Start index to begin the re-execution.
485  */
486 void MC_replay(xbt_fifo_t stack, int start)
487 {
488   int raw_mem = (mmalloc_get_current_heap() == mc_heap);
489
490   int value, i = 1, count = 1;
491   char *req_str;
492   smx_simcall_t req = NULL, saved_req = NULL;
493   xbt_fifo_item_t item, start_item;
494   mc_state_t state;
495   smx_process_t process = NULL;
496   int comm_pattern = 0;
497   smx_action_t current_comm;
498
499   XBT_DEBUG("**** Begin Replay ****");
500
501   if (start == -1) {
502     /* Restore the initial state */
503     MC_restore_snapshot(initial_global_state->snapshot);
504     /* At the moment of taking the snapshot the raw heap was set, so restoring
505      * it will set it back again, we have to unset it to continue  */
506     MC_SET_STD_HEAP;
507   }
508
509   start_item = xbt_fifo_get_last_item(stack);
510   if (start != -1) {
511     while (i != start) {
512       start_item = xbt_fifo_get_prev_item(start_item);
513       i++;
514     }
515   }
516
517   MC_SET_MC_HEAP;
518
519   if (mc_reduce_kind == e_mc_reduce_dpor) {
520     xbt_dict_reset(first_enabled_state);
521     xbt_swag_foreach(process, simix_global->process_list) {
522       if (MC_process_is_enabled(process)) {
523         char *key = bprintf("%lu", process->pid);
524         char *data = bprintf("%d", count);
525         xbt_dict_set(first_enabled_state, key, data, NULL);
526         xbt_free(key);
527       }
528     }
529   }
530
531   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
532     xbt_dynar_reset(communications_pattern);
533     xbt_dynar_reset(incomplete_communications_pattern);
534   }
535
536   MC_SET_STD_HEAP;
537
538
539   /* Traverse the stack from the state at position start and re-execute the transitions */
540   for (item = start_item;
541        item != xbt_fifo_get_first_item(stack);
542        item = xbt_fifo_get_prev_item(item)) {
543
544     state = (mc_state_t) xbt_fifo_get_item_content(item);
545     saved_req = MC_state_get_executed_request(state, &value);
546
547     if (mc_reduce_kind == e_mc_reduce_dpor) {
548       MC_SET_MC_HEAP;
549       char *key = bprintf("%lu", saved_req->issuer->pid);
550       xbt_dict_remove(first_enabled_state, key);
551       xbt_free(key);
552       MC_SET_STD_HEAP;
553     }
554
555     if (saved_req) {
556       /* because we got a copy of the executed request, we have to fetch the  
557          real one, pointed by the request field of the issuer process */
558       req = &saved_req->issuer->simcall;
559
560       /* Debug information */
561       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
562         req_str = MC_request_to_string(req, value);
563         XBT_DEBUG("Replay: %s (%p)", req_str, state);
564         xbt_free(req_str);
565       }
566
567       /* TODO : handle test and testany simcalls */
568       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
569         if (req->call == SIMCALL_COMM_ISEND)
570           comm_pattern = 1;
571         else if (req->call == SIMCALL_COMM_IRECV)
572           comm_pattern = 2;
573         else if (req->call == SIMCALL_COMM_WAIT)
574           comm_pattern = 3;
575         else if (req->call == SIMCALL_COMM_WAITANY)
576           comm_pattern = 4;
577       }
578
579       SIMIX_simcall_pre(req, value);
580
581       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
582         MC_SET_MC_HEAP;
583         if (comm_pattern == 1 || comm_pattern == 2) {
584           get_comm_pattern(communications_pattern, req, comm_pattern);
585         } else if (comm_pattern == 3 /* || comm_pattern == 4 */ ) {
586           current_comm = simcall_comm_wait__get__comm(req);
587           if (current_comm->comm.refcount == 1) {       /* First wait only must be considered */
588             complete_comm_pattern(communications_pattern, current_comm);
589           }
590         } else if (comm_pattern == 4 /* || comm_pattern == 4 */ ) {
591           current_comm =
592               xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
593                                smx_action_t);
594           if (current_comm->comm.refcount == 1) {       /* First wait only must be considered */
595             complete_comm_pattern(communications_pattern, current_comm);
596           }
597         }
598         MC_SET_STD_HEAP;
599         comm_pattern = 0;
600       }
601
602
603       MC_wait_for_requests();
604
605       count++;
606
607       if (mc_reduce_kind == e_mc_reduce_dpor) {
608         MC_SET_MC_HEAP;
609         /* Insert in dict all enabled processes */
610         xbt_swag_foreach(process, simix_global->process_list) {
611           if (MC_process_is_enabled(process)
612               /*&& !MC_state_process_is_done(state, process) */ ) {
613             char *key = bprintf("%lu", process->pid);
614             if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
615               char *data = bprintf("%d", count);
616               xbt_dict_set(first_enabled_state, key, data, NULL);
617             }
618             xbt_free(key);
619           }
620         }
621         MC_SET_STD_HEAP;
622       }
623     }
624
625     /* Update statistics */
626     mc_stats->visited_states++;
627     mc_stats->executed_transitions++;
628
629   }
630
631   XBT_DEBUG("**** End Replay ****");
632
633   if (raw_mem)
634     MC_SET_MC_HEAP;
635   else
636     MC_SET_STD_HEAP;
637
638
639 }
640
641 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
642 {
643
644   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
645
646   int value;
647   char *req_str;
648   smx_simcall_t req = NULL, saved_req = NULL;
649   xbt_fifo_item_t item;
650   mc_state_t state;
651   mc_pair_t pair;
652   int depth = 1;
653
654   XBT_DEBUG("**** Begin Replay ****");
655
656   /* Restore the initial state */
657   MC_restore_snapshot(initial_global_state->snapshot);
658
659   /* At the moment of taking the snapshot the raw heap was set, so restoring
660    * it will set it back again, we have to unset it to continue  */
661   if (!initial_global_state->raw_mem_set)
662     MC_SET_STD_HEAP;
663
664   if (all_stack) {
665
666     item = xbt_fifo_get_last_item(stack);
667
668     while (depth <= xbt_fifo_size(stack)) {
669
670       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
671       state = (mc_state_t) pair->graph_state;
672
673       if (pair->requests > 0) {
674
675         saved_req = MC_state_get_executed_request(state, &value);
676         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
677
678         if (saved_req != NULL) {
679           /* because we got a copy of the executed request, we have to fetch the  
680              real one, pointed by the request field of the issuer process */
681           req = &saved_req->issuer->simcall;
682           //XBT_DEBUG("Req->call %u", req->call);
683
684           /* Debug information */
685           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
686             req_str = MC_request_to_string(req, value);
687             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
688             xbt_free(req_str);
689           }
690
691         }
692
693         SIMIX_simcall_pre(req, value);
694         MC_wait_for_requests();
695       }
696
697       depth++;
698
699       /* Update statistics */
700       mc_stats->visited_pairs++;
701       mc_stats->executed_transitions++;
702
703       item = xbt_fifo_get_prev_item(item);
704     }
705
706   } else {
707
708     /* Traverse the stack from the initial state and re-execute the transitions */
709     for (item = xbt_fifo_get_last_item(stack);
710          item != xbt_fifo_get_first_item(stack);
711          item = xbt_fifo_get_prev_item(item)) {
712
713       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
714       state = (mc_state_t) pair->graph_state;
715
716       if (pair->requests > 0) {
717
718         saved_req = MC_state_get_executed_request(state, &value);
719         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
720
721         if (saved_req != NULL) {
722           /* because we got a copy of the executed request, we have to fetch the  
723              real one, pointed by the request field of the issuer process */
724           req = &saved_req->issuer->simcall;
725           //XBT_DEBUG("Req->call %u", req->call);
726
727           /* Debug information */
728           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
729             req_str = MC_request_to_string(req, value);
730             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
731             xbt_free(req_str);
732           }
733
734         }
735
736         SIMIX_simcall_pre(req, value);
737         MC_wait_for_requests();
738       }
739
740       depth++;
741
742       /* Update statistics */
743       mc_stats->visited_pairs++;
744       mc_stats->executed_transitions++;
745     }
746   }
747
748   XBT_DEBUG("**** End Replay ****");
749
750   if (initial_global_state->raw_mem_set)
751     MC_SET_MC_HEAP;
752   else
753     MC_SET_STD_HEAP;
754
755 }
756
757 /**
758  * \brief Dumps the contents of a model-checker's stack and shows the actual
759  *        execution trace
760  * \param stack The stack to dump
761  */
762 void MC_dump_stack_safety(xbt_fifo_t stack)
763 {
764
765   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
766
767   MC_show_stack_safety(stack);
768
769   if (!_sg_mc_checkpoint) {
770
771     mc_state_t state;
772
773     MC_SET_MC_HEAP;
774     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
775       MC_state_delete(state);
776     MC_SET_STD_HEAP;
777
778   }
779
780   if (raw_mem_set)
781     MC_SET_MC_HEAP;
782   else
783     MC_SET_STD_HEAP;
784
785 }
786
787
788 void MC_show_stack_safety(xbt_fifo_t stack)
789 {
790
791   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
792
793   MC_SET_MC_HEAP;
794
795   int value;
796   mc_state_t state;
797   xbt_fifo_item_t item;
798   smx_simcall_t req;
799   char *req_str = NULL;
800
801   for (item = xbt_fifo_get_last_item(stack);
802        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
803         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
804     req = MC_state_get_executed_request(state, &value);
805     if (req) {
806       req_str = MC_request_to_string(req, value);
807       XBT_INFO("%s", req_str);
808       xbt_free(req_str);
809     }
810   }
811
812   if (!raw_mem_set)
813     MC_SET_STD_HEAP;
814 }
815
816 void MC_show_deadlock(smx_simcall_t req)
817 {
818   /*char *req_str = NULL; */
819   XBT_INFO("**************************");
820   XBT_INFO("*** DEAD-LOCK DETECTED ***");
821   XBT_INFO("**************************");
822   XBT_INFO("Locked request:");
823   /*req_str = MC_request_to_string(req);
824      XBT_INFO("%s", req_str);
825      xbt_free(req_str); */
826   XBT_INFO("Counter-example execution trace:");
827   MC_dump_stack_safety(mc_stack);
828   MC_print_statistics(mc_stats);
829 }
830
831
832 void MC_show_stack_liveness(xbt_fifo_t stack)
833 {
834   int value;
835   mc_pair_t pair;
836   xbt_fifo_item_t item;
837   smx_simcall_t req;
838   char *req_str = NULL;
839
840   for (item = xbt_fifo_get_last_item(stack);
841        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
842         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
843     req = MC_state_get_executed_request(pair->graph_state, &value);
844     if (req) {
845       if (pair->requests > 0) {
846         req_str = MC_request_to_string(req, value);
847         XBT_INFO("%s", req_str);
848         xbt_free(req_str);
849       } else {
850         XBT_INFO("End of system requests but evolution in Büchi automaton");
851       }
852     }
853   }
854 }
855
856 void MC_dump_stack_liveness(xbt_fifo_t stack)
857 {
858
859   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
860
861   mc_pair_t pair;
862
863   MC_SET_MC_HEAP;
864   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
865     MC_pair_delete(pair);
866   MC_SET_STD_HEAP;
867
868   if (raw_mem_set)
869     MC_SET_MC_HEAP;
870
871 }
872
873
874 void MC_print_statistics(mc_stats_t stats)
875 {
876   if (stats->expanded_pairs == 0) {
877     XBT_INFO("Expanded states = %lu", stats->expanded_states);
878     XBT_INFO("Visited states = %lu", stats->visited_states);
879   } else {
880     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
881     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
882   }
883   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
884   MC_SET_MC_HEAP;
885   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
886     fprintf(dot_output, "}\n");
887     fclose(dot_output);
888   }
889   if (initial_global_state != NULL) {
890     if (_sg_mc_comms_determinism)
891       XBT_INFO("Communication-deterministic : %s",
892                !initial_global_state->comm_deterministic ? "No" : "Yes");
893     if (_sg_mc_send_determinism)
894       XBT_INFO("Send-deterministic : %s",
895                !initial_global_state->send_deterministic ? "No" : "Yes");
896   }
897   MC_SET_STD_HEAP;
898 }
899
900 void MC_assert(int prop)
901 {
902   if (MC_is_active() && !prop) {
903     XBT_INFO("**************************");
904     XBT_INFO("*** PROPERTY NOT VALID ***");
905     XBT_INFO("**************************");
906     XBT_INFO("Counter-example execution trace:");
907     MC_dump_stack_safety(mc_stack);
908     MC_print_statistics(mc_stats);
909     xbt_abort();
910   }
911 }
912
913 void MC_cut(void)
914 {
915   user_max_depth_reached = 1;
916 }
917
918 void MC_process_clock_add(smx_process_t process, double amount)
919 {
920   mc_time[process->pid] += amount;
921 }
922
923 double MC_process_clock_get(smx_process_t process)
924 {
925   if (mc_time) {
926     if (process != NULL)
927       return mc_time[process->pid];
928     else
929       return -1;
930   } else {
931     return 0;
932   }
933 }
934
935 void MC_automaton_load(const char *file)
936 {
937
938   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
939
940   MC_SET_MC_HEAP;
941
942   if (_mc_property_automaton == NULL)
943     _mc_property_automaton = xbt_automaton_new();
944
945   xbt_automaton_load(_mc_property_automaton, file);
946
947   MC_SET_STD_HEAP;
948
949   if (raw_mem_set)
950     MC_SET_MC_HEAP;
951
952 }
953
954 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
955 {
956
957   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
958
959   MC_SET_MC_HEAP;
960
961   if (_mc_property_automaton == NULL)
962     _mc_property_automaton = xbt_automaton_new();
963
964   xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
965
966   MC_SET_STD_HEAP;
967
968   if (raw_mem_set)
969     MC_SET_MC_HEAP;
970
971 }