Logo AND Algorithmique Numérique Distribuée

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